Commit 16951c28854d074e63c0c06e0176ca3ac5aeb116

Authored by wangfan
1 parent ce2ba538

customer

... ... @@ -14,7 +14,7 @@
"navigationBarTextStyle": "light"
},
"networkTimeout": {
"request": 10000,
"request": 3000,
"downloadFile": 10000
},
"debug": true
... ...
// pages/customer/index.js
const { regeneratorRuntime } = global
import App from '../../utils/api';
const {regeneratorRuntime} = global
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(opt) {
console.log('onLoad',opt);
//转化场景值到opt
this.selectComponent("#network").debug(opt,'opt');
if(opt.scene){//扫码进入
let query=decodeURIComponent(opt.scene);
query.split('&').forEach(q=>{
let _q=q.split('=');
opt[_q[0]]=_q[1];
})
this.selectComponent("#network").debug(opt,'scene');
}
opt.openid=await this.getOpenid();
if(!opt.openid){
console.log('App.Orders.CheckOrder no openid');
this.selectComponent("#network").refresh();
return;
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
/**
* 页面的初始数据
*/
data: {},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(opt) {
console.log('onLoad', opt);
//转化场景值到opt
this.selectComponent("#network").debug(opt, 'opt');
if (opt.scene) {//扫码进入
let query = decodeURIComponent(opt.scene);
query.split('&').forEach(q => {
let _q = q.split('=');
opt[_q[0]] = _q[1];
})
this.selectComponent("#network").debug(opt, 'scene');
}
opt.openid = await this.getOpenid();
if (!opt.openid) {
console.log('App.Orders.CheckOrder no openid');
this.selectComponent("#network").refresh();
return;
}
let url = 'list';
if(opt.fromid){
App.Reserve.SetRelaOpenid({
id:opt.id,
openid:opt.openid,
fromid:opt.fromid?opt.fromid:''
})
}
if (opt.id > 0) {
App.Reserve.Detail({
openid: opt.openid,
id: opt.id
}).then(res => {
if (!res.owner_openid) {//无预订人
url = 'detail_owner';
} else {
url = 'detail_customer';
if (res.owner_openid == opt.openid) {
url = 'detail_owner';
}
}
url+='?id='+opt.id
wx.redirectTo({url: url});
}).catch()
} else {
wx.redirectTo({url: url});
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
},
async getOpenid(){
let openid=getApp().openid;
let openid = getApp().openid;
return openid;
},
})
\ No newline at end of file
... ...
/**
* Created by goto9 on 2018/7/19.
*/
//接口地址,测试环境请修改以下地址
//const server_host='https://xt04.colorcun.com/?s=';
const api_timeout=2000; //接口超时时间,默认2s
//引入接口映射关系表
import api from './api.url'
import uri_dc from './uri'
const server_host=uri_dc.HOST__LINE;
/**
*
* @param act
* @param data
* @param fouce false|true 强制请求服务器,获取最新数据;默认优先使用缓存
* @param header 追加header
* @param silence loading
* @param cachetime 缓存时间
* @param timeout 接口超时
* @returns {Promise}
*/
function rpc(host='',act,data={},fouce=false,header={},silence=true,cachetime=0,timeout=api_timeout){
host=host?host:server_host;
let param="";
for(let key in data){
let val=data[key];
if(val!=""){
param+='&'+key+'='+val
}
}
let cache_key=act;
if(param){
cache_key+='?'+param.substr((param.length-1)*-1);
}
if(!fouce && cachetime!=0){//优先使用缓存
let getCache= cache(cache_key);
if(getCache!=-99999){
return new Promise((success,fail)=>{
if(true){
getCache.isCache=true;
console.log(cache_key+'->use cache',getCache);
success(getCache);
}else{
fail();
}
})
}
}
if(!silence){
wx.showLoading({
title: '数据加载中',
})
}
return new Promise((success,fail)=>{
let openid = wx.getStorageSync('openid');
let formids = wx.getStorageSync('formids');
if(formids){
wx.removeStorage({key:'formids'});
}
wx.request({
url: host+act,
method: 'POST',
data: data,
header: {
...header,
openid:openid,
formids:formids.toString(),
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log('request.api==>',host+cache_key+'\nrequest.success',res.data);
// return;
if(cachetime!=0 && res.data.ret==200 && res.data.data.length>0){
cache(cache_key,res.data,cachetime);//.then().catch();
}
if(res.data.ret!==200){
try{
res.data.msg=res.data.msg.replace('非法请求:','');
}catch(e){
console.log('res-fail',res,e);
}
}
success(res.data);
},
fail: function (res) {
console.log('request.api==>',host+cache_key+'\nrequest.fail',res);
fail(res);
// let query = wx.createSelectorQuery().selectComponent("#network");
// let net=query.exec(function(){
// console.log('back=>',arguments);
// });
//query.show();
//console.log('net',query);
},
complete:function(){
wx.hideLoading();
}
})
})
}
function __api(){
let tmpApi={}
let keys=[];
for(let key in api){
api[key]=api[key]||{};
keys=key.split('.');
tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};
tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};
tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){
//console.log('api[key].host',api[key].host);
return rpc(api[key].host,key,data,fouce,header,
api[key].silence,api[key].cachetime,api[key].timeout);
}
}
for(let xkey in uri_dc){
let key=uri_dc[xkey].toString();
if(key.indexOf('App.')>-1){
keys=key.split('.');
try{
if(typeof tmpApi[keys[0]][keys[1]][keys[2]]=='undefined'){
throw new Error('undefined');
}
}catch(e){
api[key]=api[key]||{};
tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};
tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};
tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){
//console.log('api[key].host',api[key].host);
return rpc(api[key].host,key,data,fouce,header,
api[key].silence,api[key].cachetime,api[key].timeout);
}
}
}
}
//console.log(tmpApi[keys[0]]);
return tmpApi[keys[0]];
}
function cache(key,val=-99999,timeout=-1){
if(val!=-99999){//写入
return new Promise((success,fail)=>{
let data={
data:val,
timeout:timeout==-1?-1:(Date.parse(new Date())/1000+timeout)
}
wx.setStorage({
key:key,
data:data,
success:()=>{
success()
},
fail:()=>{
fail()
}
});
})
}
let res=wx.getStorageSync(key);
let time=Date.parse(new Date())/1000;
if(res && (res.timeout==-1 || res.timeout>time) && res.data.data){
return res.data;
}else{
return -99999
}
}
export default __api();
\ No newline at end of file
... ...
export default {
//示例,JS接口名,和服务端接口名称保持一致
'App.TestManagement.Test':{
silence:true, //静默,不带loading; 默认 true 不带loading
cachetime:0, //缓存 0无,-1永久,86400秒; 默认 0 不缓存
timeout:2000, //接口超时时间,默认2s
host:'' //默认空使用04服务器,测试填写 'https://xt05.colorcun.com/?s='
},
//预定信息详情
'App.Reserve.Detail':{
cachetime:30,
},
//建立预定关系
'App.Reserve.SetRelaOpenid':{
cachetime:-1,
}
}
\ No newline at end of file
... ...