Commit 16951c28854d074e63c0c06e0176ca3ac5aeb116

Authored by wangfan
1 parent ce2ba538

customer

@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 "navigationBarTextStyle": "light" 14 "navigationBarTextStyle": "light"
15 }, 15 },
16 "networkTimeout": { 16 "networkTimeout": {
17 - "request": 10000, 17 + "request": 3000,
18 "downloadFile": 10000 18 "downloadFile": 10000
19 }, 19 },
20 "debug": true 20 "debug": true
1 // pages/customer/index.js 1 // pages/customer/index.js
2 -const { regeneratorRuntime } = global 2 +import App from '../../utils/api';
  3 +const {regeneratorRuntime} = global
3 Page({ 4 Page({
4 5
5 - /**  
6 - * 页面的初始数据  
7 - */  
8 - data: {  
9 -  
10 - },  
11 -  
12 - /**  
13 - * 生命周期函数--监听页面加载  
14 - */  
15 - async onLoad(opt) {  
16 - console.log('onLoad',opt);  
17 - //转化场景值到opt  
18 - this.selectComponent("#network").debug(opt,'opt');  
19 - if(opt.scene){//扫码进入  
20 - let query=decodeURIComponent(opt.scene);  
21 - query.split('&').forEach(q=>{  
22 - let _q=q.split('=');  
23 - opt[_q[0]]=_q[1];  
24 - })  
25 - this.selectComponent("#network").debug(opt,'scene');  
26 - }  
27 -  
28 - opt.openid=await this.getOpenid();  
29 - if(!opt.openid){  
30 - console.log('App.Orders.CheckOrder no openid');  
31 - this.selectComponent("#network").refresh();  
32 - return;  
33 - }  
34 - },  
35 -  
36 - /**  
37 - * 生命周期函数--监听页面初次渲染完成  
38 - */  
39 - onReady: function () {  
40 -  
41 - },  
42 -  
43 - /**  
44 - * 生命周期函数--监听页面显示  
45 - */  
46 - onShow: function () {  
47 -  
48 - },  
49 -  
50 - /**  
51 - * 生命周期函数--监听页面隐藏  
52 - */  
53 - onHide: function () {  
54 -  
55 - },  
56 -  
57 - /**  
58 - * 生命周期函数--监听页面卸载  
59 - */  
60 - onUnload: function () {  
61 -  
62 - },  
63 -  
64 - /**  
65 - * 页面相关事件处理函数--监听用户下拉动作  
66 - */  
67 - onPullDownRefresh: function () {  
68 -  
69 - },  
70 -  
71 - /**  
72 - * 页面上拉触底事件的处理函数  
73 - */  
74 - onReachBottom: function () {  
75 -  
76 - },  
77 -  
78 - /**  
79 - * 用户点击右上角分享  
80 - */  
81 - onShareAppMessage: function () {  
82 -  
83 - }, 6 + /**
  7 + * 页面的初始数据
  8 + */
  9 + data: {},
  10 +
  11 + /**
  12 + * 生命周期函数--监听页面加载
  13 + */
  14 + async onLoad(opt) {
  15 + console.log('onLoad', opt);
  16 + //转化场景值到opt
  17 + this.selectComponent("#network").debug(opt, 'opt');
  18 + if (opt.scene) {//扫码进入
  19 + let query = decodeURIComponent(opt.scene);
  20 + query.split('&').forEach(q => {
  21 + let _q = q.split('=');
  22 + opt[_q[0]] = _q[1];
  23 + })
  24 + this.selectComponent("#network").debug(opt, 'scene');
  25 + }
  26 +
  27 + opt.openid = await this.getOpenid();
  28 + if (!opt.openid) {
  29 + console.log('App.Orders.CheckOrder no openid');
  30 + this.selectComponent("#network").refresh();
  31 + return;
  32 + }
  33 + let url = 'list';
  34 + if(opt.fromid){
  35 + App.Reserve.SetRelaOpenid({
  36 + id:opt.id,
  37 + openid:opt.openid,
  38 + fromid:opt.fromid?opt.fromid:''
  39 + })
  40 + }
  41 + if (opt.id > 0) {
  42 + App.Reserve.Detail({
  43 + openid: opt.openid,
  44 + id: opt.id
  45 + }).then(res => {
  46 + if (!res.owner_openid) {//无预订人
  47 + url = 'detail_owner';
  48 + } else {
  49 + url = 'detail_customer';
  50 + if (res.owner_openid == opt.openid) {
  51 + url = 'detail_owner';
  52 + }
  53 + }
  54 + url+='?id='+opt.id
  55 + wx.redirectTo({url: url});
  56 + }).catch()
  57 + } else {
  58 + wx.redirectTo({url: url});
  59 + }
  60 +
  61 + },
  62 +
  63 + /**
  64 + * 生命周期函数--监听页面初次渲染完成
  65 + */
  66 + onReady: function () {
  67 +
  68 + },
  69 +
  70 + /**
  71 + * 生命周期函数--监听页面显示
  72 + */
  73 + onShow: function () {
  74 +
  75 + },
  76 +
  77 + /**
  78 + * 生命周期函数--监听页面隐藏
  79 + */
  80 + onHide: function () {
  81 +
  82 + },
  83 +
  84 + /**
  85 + * 生命周期函数--监听页面卸载
  86 + */
  87 + onUnload: function () {
  88 +
  89 + },
  90 +
  91 + /**
  92 + * 页面相关事件处理函数--监听用户下拉动作
  93 + */
  94 + onPullDownRefresh: function () {
  95 +
  96 + },
  97 +
  98 + /**
  99 + * 页面上拉触底事件的处理函数
  100 + */
  101 + onReachBottom: function () {
  102 +
  103 + },
  104 +
  105 + /**
  106 + * 用户点击右上角分享
  107 + */
  108 + onShareAppMessage: function () {
  109 +
  110 + },
84 async getOpenid(){ 111 async getOpenid(){
85 - let openid=getApp().openid; 112 + let openid = getApp().openid;
86 return openid; 113 return openid;
87 }, 114 },
88 }) 115 })
  1 +/**
  2 + * Created by goto9 on 2018/7/19.
  3 + */
  4 +//接口地址,测试环境请修改以下地址
  5 +//const server_host='https://xt04.colorcun.com/?s=';
  6 +const api_timeout=2000; //接口超时时间,默认2s
  7 +
  8 +//引入接口映射关系表
  9 +import api from './api.url'
  10 +import uri_dc from './uri'
  11 +const server_host=uri_dc.HOST__LINE;
  12 +
  13 +/**
  14 + *
  15 + * @param act
  16 + * @param data
  17 + * @param fouce false|true 强制请求服务器,获取最新数据;默认优先使用缓存
  18 + * @param header 追加header
  19 + * @param silence loading
  20 + * @param cachetime 缓存时间
  21 + * @param timeout 接口超时
  22 + * @returns {Promise}
  23 + */
  24 +function rpc(host='',act,data={},fouce=false,header={},silence=true,cachetime=0,timeout=api_timeout){
  25 + host=host?host:server_host;
  26 + let param="";
  27 + for(let key in data){
  28 + let val=data[key];
  29 + if(val!=""){
  30 + param+='&'+key+'='+val
  31 + }
  32 + }
  33 + let cache_key=act;
  34 + if(param){
  35 + cache_key+='?'+param.substr((param.length-1)*-1);
  36 + }
  37 + if(!fouce && cachetime!=0){//优先使用缓存
  38 + let getCache= cache(cache_key);
  39 + if(getCache!=-99999){
  40 + return new Promise((success,fail)=>{
  41 + if(true){
  42 + getCache.isCache=true;
  43 + console.log(cache_key+'->use cache',getCache);
  44 + success(getCache);
  45 + }else{
  46 + fail();
  47 + }
  48 + })
  49 + }
  50 + }
  51 + if(!silence){
  52 + wx.showLoading({
  53 + title: '数据加载中',
  54 + })
  55 + }
  56 + return new Promise((success,fail)=>{
  57 + let openid = wx.getStorageSync('openid');
  58 + let formids = wx.getStorageSync('formids');
  59 + if(formids){
  60 + wx.removeStorage({key:'formids'});
  61 + }
  62 +
  63 + wx.request({
  64 + url: host+act,
  65 + method: 'POST',
  66 + data: data,
  67 + header: {
  68 + ...header,
  69 + openid:openid,
  70 + formids:formids.toString(),
  71 + 'content-type': 'application/x-www-form-urlencoded'
  72 + },
  73 + success: function (res) {
  74 + console.log('request.api==>',host+cache_key+'\nrequest.success',res.data);
  75 + // return;
  76 + if(cachetime!=0 && res.data.ret==200 && res.data.data.length>0){
  77 + cache(cache_key,res.data,cachetime);//.then().catch();
  78 + }
  79 + if(res.data.ret!==200){
  80 + try{
  81 + res.data.msg=res.data.msg.replace('非法请求:','');
  82 + }catch(e){
  83 + console.log('res-fail',res,e);
  84 + }
  85 + }
  86 + success(res.data);
  87 + },
  88 + fail: function (res) {
  89 + console.log('request.api==>',host+cache_key+'\nrequest.fail',res);
  90 + fail(res);
  91 + // let query = wx.createSelectorQuery().selectComponent("#network");
  92 + // let net=query.exec(function(){
  93 + // console.log('back=>',arguments);
  94 + // });
  95 + //query.show();
  96 + //console.log('net',query);
  97 + },
  98 + complete:function(){
  99 + wx.hideLoading();
  100 + }
  101 + })
  102 + })
  103 +}
  104 +
  105 +function __api(){
  106 + let tmpApi={}
  107 + let keys=[];
  108 + for(let key in api){
  109 + api[key]=api[key]||{};
  110 + keys=key.split('.');
  111 + tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};
  112 + tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};
  113 + tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){
  114 + //console.log('api[key].host',api[key].host);
  115 + return rpc(api[key].host,key,data,fouce,header,
  116 + api[key].silence,api[key].cachetime,api[key].timeout);
  117 + }
  118 + }
  119 + for(let xkey in uri_dc){
  120 + let key=uri_dc[xkey].toString();
  121 + if(key.indexOf('App.')>-1){
  122 + keys=key.split('.');
  123 + try{
  124 + if(typeof tmpApi[keys[0]][keys[1]][keys[2]]=='undefined'){
  125 + throw new Error('undefined');
  126 + }
  127 + }catch(e){
  128 + api[key]=api[key]||{};
  129 + tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};
  130 + tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};
  131 + tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){
  132 + //console.log('api[key].host',api[key].host);
  133 + return rpc(api[key].host,key,data,fouce,header,
  134 + api[key].silence,api[key].cachetime,api[key].timeout);
  135 + }
  136 + }
  137 + }
  138 + }
  139 + //console.log(tmpApi[keys[0]]);
  140 + return tmpApi[keys[0]];
  141 +}
  142 +function cache(key,val=-99999,timeout=-1){
  143 + if(val!=-99999){//写入
  144 + return new Promise((success,fail)=>{
  145 + let data={
  146 + data:val,
  147 + timeout:timeout==-1?-1:(Date.parse(new Date())/1000+timeout)
  148 + }
  149 + wx.setStorage({
  150 + key:key,
  151 + data:data,
  152 + success:()=>{
  153 + success()
  154 + },
  155 + fail:()=>{
  156 + fail()
  157 + }
  158 + });
  159 + })
  160 + }
  161 + let res=wx.getStorageSync(key);
  162 + let time=Date.parse(new Date())/1000;
  163 + if(res && (res.timeout==-1 || res.timeout>time) && res.data.data){
  164 + return res.data;
  165 + }else{
  166 + return -99999
  167 + }
  168 +}
  169 +export default __api();
  1 +export default {
  2 + //示例,JS接口名,和服务端接口名称保持一致
  3 + 'App.TestManagement.Test':{
  4 + silence:true, //静默,不带loading; 默认 true 不带loading
  5 + cachetime:0, //缓存 0无,-1永久,86400秒; 默认 0 不缓存
  6 + timeout:2000, //接口超时时间,默认2s
  7 + host:'' //默认空使用04服务器,测试填写 'https://xt05.colorcun.com/?s='
  8 + },
  9 +
  10 + //预定信息详情
  11 + 'App.Reserve.Detail':{
  12 + cachetime:30,
  13 + },
  14 +
  15 + //建立预定关系
  16 + 'App.Reserve.SetRelaOpenid':{
  17 + cachetime:-1,
  18 + }
  19 +
  20 +}