Commit 58fddd481e9e9d660255bf80ebdd20d7154656fd

Authored by vking
1 parent 460a6e44

fix map & api

@@ -13,7 +13,6 @@ @@ -13,7 +13,6 @@
13 enable-zoom 13 enable-zoom
14 enable-scroll 14 enable-scroll
15 enable-rotate 15 enable-rotate
16 - show-compass  
17 subkey="SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ" 16 subkey="SDEBZ-I74W4-QLGUX-XXYXU-HIR23-3QFBJ"
18 style="width: 100%; height: {{map.height}}px;"></map> 17 style="width: 100%; height: {{map.height}}px;"></map>
19 <formids url="/pages/reserve/search" style="position: fixed;"> 18 <formids url="/pages/reserve/search" style="position: fixed;">
@@ -3,12 +3,59 @@ @@ -3,12 +3,59 @@
3 */ 3 */
4 //接口地址,测试环境请修改以下地址 4 //接口地址,测试环境请修改以下地址
5 //const server_host='https://xt04.colorcun.com/?s='; 5 //const server_host='https://xt04.colorcun.com/?s=';
6 -const api_timeout=2000; //接口超时时间,默认2s 6 +const api_timeout = 2000; //接口超时时间,默认2s
7 7
8 //引入接口映射关系表 8 //引入接口映射关系表
9 import api from './api.url' 9 import api from './api.url'
10 import uri_dc from './uri' 10 import uri_dc from './uri'
11 -const server_host=uri_dc.HOST__LINE; 11 +
  12 +const server_host = uri_dc.HOST__LINE;
  13 +
  14 +class rpcm {
  15 + tasks = {}
  16 +
  17 + check(key) {
  18 + let nt = parseInt((new Date() / 1000))
  19 + if (this.tasks[key]) {
  20 + if (nt <= this.tasks[key].timeout) {
  21 + return true
  22 + }
  23 + }
  24 + return false
  25 + }
  26 +
  27 + get(key) {
  28 + return new Promise((success, fail) => {
  29 + let resSetInterval=setInterval(()=>{
  30 + //console.log('resSetInterval',this.tasks[key])
  31 + if(this.tasks[key].type=='success'){
  32 + wx.hideLoading();
  33 + success(this.tasks[key].res)
  34 + clearInterval(resSetInterval)
  35 + }
  36 + if(this.tasks[key].type=='fail'){
  37 + wx.hideLoading();
  38 + fail(this.tasks[key].res)
  39 + clearInterval(resSetInterval)
  40 + }
  41 + },500)
  42 + })
  43 +
  44 + }
  45 +
  46 + set(key,type='',res=null) {
  47 + let nt = parseInt((new Date() / 1000))
  48 + this.tasks[key] = {
  49 + timeout: nt + 1000
  50 + }
  51 + if(type){
  52 + this.tasks[key]['type']=type
  53 + this.tasks[key]['res']=res
  54 + }
  55 + }
  56 +}
  57 +
  58 +const RMT = new rpcm()
12 59
13 /** 60 /**
14 * 61 *
@@ -21,178 +68,184 @@ const server_host=uri_dc.HOST__LINE; @@ -21,178 +68,184 @@ const server_host=uri_dc.HOST__LINE;
21 * @param timeout 接口超时 68 * @param timeout 接口超时
22 * @returns {Promise} 69 * @returns {Promise}
23 */ 70 */
24 -function rpc(host='',act,data={},fouce=false,header={},silence=true,cachetime=0,timeout=api_timeout,api={}){  
25 - host=host?host:server_host;  
26 - let param="";  
27 - for(let key in data){  
28 - let val=data[key];  
29 - if(val!=="" && val!==null){  
30 - param+='&'+key+'='+val  
31 - } 71 +function rpc(host = '', act, data = {}, fouce = false, header = {}, silence = true, cachetime = 0, timeout = api_timeout, api = {}) {
  72 + host = host ? host : server_host;
  73 + let param = "";
  74 + for (let key in data) {
  75 + let val = data[key];
  76 + if (val !== "" && val !== null) {
  77 + param += '&' + key + '=' + val
32 } 78 }
33 - let cache_key=act;  
34 - if(param){  
35 - cache_key+='?'+param.substr((param.length-1)*-1); 79 + }
  80 + let cache_key = act;
  81 + if (param) {
  82 + cache_key += '?' + param.substr((param.length - 1) * -1);
  83 + }
  84 + if (fouce == 'clear' || fouce == 'clean') {
  85 + return wx.removeStorageSync(cache_key);
  86 + }
  87 + if (!fouce && cachetime != 0) {//优先使用缓存
  88 + let getCache = cache(cache_key);
  89 + //console.log('getCache.'+getCache,cache_key);
  90 + if (getCache != -99999) {
  91 + return new Promise((success, fail) => {
  92 + getCache.isCache = true;
  93 + console.log(cache_key + '\nuse cache', getCache);
  94 + success(getCache);
  95 + })
36 } 96 }
37 - if(fouce=='clear' || fouce=='clean'){  
38 - return wx.removeStorageSync(cache_key); 97 + }
  98 + if (!silence) {
  99 + wx.showLoading({
  100 + title: '处理中',
  101 + mask: true
  102 + })
  103 + }
  104 + if (RMT.check(cache_key)) return RMT.get(cache_key)
  105 + RMT.set(cache_key)
  106 + return new Promise((success, fail) => {
  107 + let openid = wx.getStorageSync('customer_openid');
  108 + let formids = wx.getStorageSync('formids');
  109 + if (formids) {
  110 + wx.removeStorage({key: 'formids'});
39 } 111 }
40 - if(!fouce && cachetime!=0){//优先使用缓存  
41 - let getCache= cache(cache_key);  
42 - //console.log('getCache.'+getCache,cache_key);  
43 - if(getCache!=-99999){  
44 - return new Promise((success,fail)=>{  
45 - getCache.isCache=true;  
46 - console.log(cache_key+'\nuse cache',getCache);  
47 - success(getCache);  
48 - })  
49 - } 112 + let enter_params = wx.getStorageSync('enter_params');
  113 + let operator = ''
  114 + if (enter_params) {
  115 + operator = JSON.stringify({
  116 + uid: enter_params.manager_id,
  117 + openid: enter_params.seller_openid,
  118 + sid: enter_params.seller_id,
  119 + })
50 } 120 }
51 - if(!silence){  
52 - wx.showLoading({  
53 - title: '处理中',  
54 - mask:true  
55 - }) 121 + if (api.openid) {
  122 + data[api.openid] = data[api.openid] || openid;
56 } 123 }
57 - return new Promise((success,fail)=>{  
58 - let openid = wx.getStorageSync('customer_openid');  
59 - let formids = wx.getStorageSync('formids');  
60 - if(formids){  
61 - wx.removeStorage({key:'formids'});  
62 - }  
63 - let enter_params = wx.getStorageSync('enter_params');  
64 - let operator=''  
65 - if(enter_params){  
66 - operator=JSON.stringify({  
67 - uid:enter_params.manager_id,  
68 - openid: enter_params.seller_openid,  
69 - sid:enter_params.seller_id,  
70 - })  
71 - }  
72 - if(api.openid){  
73 - data[api.openid]=data[api.openid]||openid;  
74 - }  
75 - let stime=new Date();  
76 - wx.request({  
77 - url: host+act,  
78 - method: 'POST',  
79 - data: data,  
80 - header: {  
81 - ...header,  
82 - openid:openid,  
83 - formids:formids.toString(),  
84 - operator:operator,  
85 - 'content-type': 'application/x-www-form-urlencoded'  
86 - },  
87 - success: function (res) {  
88 - console.log('request.api['+(new Date()-stime)+'ms]==>',host+cache_key+('&formids='+formids.toString())+'\nrequest.success',res.data);  
89 - // return;  
90 - if(res.data){  
91 - if(cachetime!=0 && res.data.ret==200){  
92 - cache(cache_key,res.data,cachetime);//.then().catch();  
93 - }  
94 - if(res.data.ret!==200){  
95 - try{  
96 - res.data.msg=res.data.msg.replace('非法请求:','');  
97 - }catch(e){  
98 - console.log('res-fail',res,e);  
99 - if (wx.getRealtimeLogManager) {  
100 - const logger = wx.getRealtimeLogManager()  
101 - logger.error('接口错误',{url:host+cache_key,res:res.data})  
102 - }  
103 - }  
104 - }  
105 - }else{  
106 - if (wx.getRealtimeLogManager) {  
107 - const logger = wx.getRealtimeLogManager()  
108 - logger.error('接口错误',{url:host+cache_key,res:res})  
109 - }  
110 - }  
111 - success(res.data);  
112 - },  
113 - fail: function (res) {  
114 - console.log('request.api==>',host+cache_key+'\nrequest.fail',res);  
115 - fail(res);  
116 - // let query = wx.createSelectorQuery().selectComponent("#network");  
117 - // let net=query.exec(function(){  
118 - // console.log('back=>',arguments);  
119 - // });  
120 - //query.show();  
121 - //console.log('net',query); 124 + let stime = new Date();
  125 + wx.request({
  126 + url: host + act,
  127 + method: 'POST',
  128 + data: data,
  129 + header: {
  130 + ...header,
  131 + openid: openid,
  132 + formids: formids.toString(),
  133 + operator: operator,
  134 + 'content-type': 'application/x-www-form-urlencoded'
  135 + },
  136 + success: function (res) {
  137 + console.log('request.api[' + (new Date() - stime) + 'ms]==>', host + cache_key + ('&formids=' + formids.toString()) + '\nrequest.success', res.data);
  138 + // return;
  139 + if (res.data) {
  140 + if (cachetime != 0 && res.data.ret == 200) {
  141 + cache(cache_key, res.data, cachetime);//.then().catch();
  142 + }
  143 + if (res.data.ret !== 200) {
  144 + try {
  145 + res.data.msg = res.data.msg.replace('非法请求:', '');
  146 + } catch (e) {
  147 + console.log('res-fail', res, e);
122 if (wx.getRealtimeLogManager) { 148 if (wx.getRealtimeLogManager) {
123 const logger = wx.getRealtimeLogManager() 149 const logger = wx.getRealtimeLogManager()
124 - logger.error('调用失败',{url:host+cache_key,res:res}) 150 + logger.error('接口错误', {url: host + cache_key, res: res.data})
125 } 151 }
126 - },  
127 - complete:function(){  
128 - wx.hideLoading();  
129 } 152 }
130 - }) 153 + }
  154 + } else {
  155 + if (wx.getRealtimeLogManager) {
  156 + const logger = wx.getRealtimeLogManager()
  157 + logger.error('接口错误', {url: host + cache_key, res: res})
  158 + }
  159 + }
  160 + success(res.data);
  161 + RMT.set(cache_key,'success',res.data)
  162 + },
  163 + fail: function (res) {
  164 + console.log('request.api==>', host + cache_key + '\nrequest.fail', res);
  165 + fail(res);
  166 + RMT.set(cache_key,'fail',res)
  167 + // let query = wx.createSelectorQuery().selectComponent("#network");
  168 + // let net=query.exec(function(){
  169 + // console.log('back=>',arguments);
  170 + // });
  171 + //query.show();
  172 + //console.log('net',query);
  173 + if (wx.getRealtimeLogManager) {
  174 + const logger = wx.getRealtimeLogManager()
  175 + logger.error('调用失败', {url: host + cache_key, res: res})
  176 + }
  177 + },
  178 + complete: function () {
  179 + wx.hideLoading();
  180 + }
131 }) 181 })
  182 + })
132 } 183 }
133 184
134 -function __api(){  
135 - let tmpApi={}  
136 - let keys=[];  
137 - for(let key in api){  
138 - api[key]=api[key]||{};  
139 - keys=key.split('.');  
140 - tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};  
141 - tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};  
142 - tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){  
143 - //console.log('api[key].host',api[key].host);  
144 - return rpc(api[key].host,api[key].alias||key,data,fouce,header,  
145 - api[key].silence,api[key].cachetime,api[key].timeout,api[key]);  
146 - } 185 +function __api() {
  186 + let tmpApi = {}
  187 + let keys = [];
  188 + for (let key in api) {
  189 + api[key] = api[key] || {};
  190 + keys = key.split('.');
  191 + tmpApi[keys[0]] = tmpApi[keys[0]] ? tmpApi[keys[0]] : {};
  192 + tmpApi[keys[0]][keys[1]] = tmpApi[keys[0]][keys[1]] ? tmpApi[keys[0]][keys[1]] : {};
  193 + tmpApi[keys[0]][keys[1]][keys[2]] = function (data, fouce = false, header) {
  194 + //console.log('api[key].host',api[key].host);
  195 + return rpc(api[key].host, api[key].alias || key, data, fouce, header,
  196 + api[key].silence, api[key].cachetime, api[key].timeout, api[key]);
147 } 197 }
148 - for(let xkey in uri_dc){  
149 - let key=uri_dc[xkey].toString();  
150 - if(key.indexOf('App.')>-1){  
151 - keys=key.split('.');  
152 - try{  
153 - if(typeof tmpApi[keys[0]][keys[1]][keys[2]]=='undefined'){  
154 - throw new Error('undefined');  
155 - }  
156 - }catch(e){  
157 - api[key]=api[key]||{};  
158 - tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{};  
159 - tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{};  
160 - tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){  
161 - //console.log('api[key].host',api[key]);  
162 - return rpc(api[key].host,api[key].alias||key,data,fouce,header,  
163 - api[key].silence,api[key].cachetime,api[key].timeout,api[key]);  
164 - }  
165 - } 198 + }
  199 + for (let xkey in uri_dc) {
  200 + let key = uri_dc[xkey].toString();
  201 + if (key.indexOf('App.') > -1) {
  202 + keys = key.split('.');
  203 + try {
  204 + if (typeof tmpApi[keys[0]][keys[1]][keys[2]] == 'undefined') {
  205 + throw new Error('undefined');
  206 + }
  207 + } catch (e) {
  208 + api[key] = api[key] || {};
  209 + tmpApi[keys[0]] = tmpApi[keys[0]] ? tmpApi[keys[0]] : {};
  210 + tmpApi[keys[0]][keys[1]] = tmpApi[keys[0]][keys[1]] ? tmpApi[keys[0]][keys[1]] : {};
  211 + tmpApi[keys[0]][keys[1]][keys[2]] = function (data, fouce = false, header) {
  212 + //console.log('api[key].host',api[key]);
  213 + return rpc(api[key].host, api[key].alias || key, data, fouce, header,
  214 + api[key].silence, api[key].cachetime, api[key].timeout, api[key]);
166 } 215 }
  216 + }
167 } 217 }
168 - //console.log(tmpApi[keys[0]]);  
169 - return tmpApi[keys[0]]; 218 + }
  219 + //console.log(tmpApi[keys[0]]);
  220 + return tmpApi[keys[0]];
170 } 221 }
171 -function cache(key,val=-99999,timeout=-1){  
172 - if(val!=-99999){//写入  
173 - return new Promise((success,fail)=>{  
174 - let data={  
175 - data:val,  
176 - timeout:timeout==-1?-1:(Date.parse(new Date())/1000+timeout)  
177 - }  
178 - wx.setStorage({  
179 - key:key,  
180 - data:data,  
181 - success:()=>{  
182 - success()  
183 - },  
184 - fail:()=>{  
185 - fail()  
186 - }  
187 - });  
188 - })  
189 - }  
190 - let res=wx.getStorageSync(key);  
191 - let time=Date.parse(new Date())/1000;  
192 - if(res && (res.timeout==-1 || res.timeout>time) && res.data){  
193 - return res.data;  
194 - }else{  
195 - return -99999  
196 - } 222 +
  223 +function cache(key, val = -99999, timeout = -1) {
  224 + if (val != -99999) {//写入
  225 + return new Promise((success, fail) => {
  226 + let data = {
  227 + data: val,
  228 + timeout: timeout == -1 ? -1 : (Date.parse(new Date()) / 1000 + timeout)
  229 + }
  230 + wx.setStorage({
  231 + key: key,
  232 + data: data,
  233 + success: () => {
  234 + success()
  235 + },
  236 + fail: () => {
  237 + fail()
  238 + }
  239 + });
  240 + })
  241 + }
  242 + let res = wx.getStorageSync(key);
  243 + let time = Date.parse(new Date()) / 1000;
  244 + if (res && (res.timeout == -1 || res.timeout > time) && res.data) {
  245 + return res.data;
  246 + } else {
  247 + return -99999
  248 + }
197 } 249 }
  250 +
198 export default __api(); 251 export default __api();