Showing
2 changed files
with
200 additions
and
146 deletions
| @@ -3,13 +3,46 @@ | @@ -3,13 +3,46 @@ | ||
| 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; | ||
| 12 | - | 11 | +const server_host = uri_dc.HOST__LINE; |
| 12 | +/** | ||
| 13 | + * @param data | ||
| 14 | + * @param status started, succeeded (default), failed, or timed_out | ||
| 15 | + */ | ||
| 16 | +function rollbar(data) { | ||
| 17 | + // let raw=JSON.stringify(data) | ||
| 18 | + // let trace={ | ||
| 19 | + // frames:[data], | ||
| 20 | + // exception:{class:raw}, | ||
| 21 | + // } | ||
| 22 | + let param={ | ||
| 23 | + access_token: '83bd2afd09b8402b943cb1894a84ec51', | ||
| 24 | + data:{ | ||
| 25 | + body:{ | ||
| 26 | + //trace:trace, | ||
| 27 | + // trace_chain:[trace], | ||
| 28 | + message:{ | ||
| 29 | + body:data, | ||
| 30 | + }, | ||
| 31 | + // crash_report:{ | ||
| 32 | + // raw:raw | ||
| 33 | + // }, | ||
| 34 | + }, | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + wx.request({ | ||
| 38 | + url: 'https://api.rollbar.com/api/1/item/', | ||
| 39 | + method: 'POST', | ||
| 40 | + data: param, | ||
| 41 | + header: { | ||
| 42 | + //'content-type': 'application/x-www-form-urlencoded' | ||
| 43 | + }, | ||
| 44 | + }) | ||
| 45 | +} | ||
| 13 | /** | 46 | /** |
| 14 | * | 47 | * |
| 15 | * @param act | 48 | * @param act |
| @@ -21,161 +54,182 @@ const server_host=uri_dc.HOST__LINE; | @@ -21,161 +54,182 @@ const server_host=uri_dc.HOST__LINE; | ||
| 21 | * @param timeout 接口超时 | 54 | * @param timeout 接口超时 |
| 22 | * @returns {Promise} | 55 | * @returns {Promise} |
| 23 | */ | 56 | */ |
| 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 | - } | 57 | +function rpc(host = '', act, data = {}, fouce = false, header = {}, silence = true, cachetime = 0, timeout = api_timeout) { |
| 58 | + host = host ? host : server_host; | ||
| 59 | + let param = ""; | ||
| 60 | + for (let key in data) { | ||
| 61 | + let val = data[key]; | ||
| 62 | + if (val != "") { | ||
| 63 | + param += '&' + key + '=' + val | ||
| 32 | } | 64 | } |
| 33 | - let cache_key=act; | ||
| 34 | - if(param){ | ||
| 35 | - cache_key+='?'+param.substr((param.length-1)*-1); | 65 | + } |
| 66 | + let cache_key = act; | ||
| 67 | + if (param) { | ||
| 68 | + cache_key += '?' + param.substr((param.length - 1) * -1); | ||
| 69 | + } | ||
| 70 | + if (fouce == 'clear' || fouce == 'clean') { | ||
| 71 | + return wx.removeStorageSync(cache_key); | ||
| 72 | + } | ||
| 73 | + if (!fouce && cachetime != 0) {//优先使用缓存 | ||
| 74 | + let getCache = cache(cache_key); | ||
| 75 | + //console.log('getCache.'+getCache,cache_key); | ||
| 76 | + if (getCache != -99999) { | ||
| 77 | + return new Promise((success, fail) => { | ||
| 78 | + getCache.isCache = true; | ||
| 79 | + console.log(cache_key + '\nuse cache', getCache); | ||
| 80 | + success(getCache); | ||
| 81 | + }) | ||
| 36 | } | 82 | } |
| 37 | - if(fouce=='clear' || fouce=='clean'){ | ||
| 38 | - return wx.removeStorageSync(cache_key); | ||
| 39 | - } | ||
| 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 | - } | 83 | + } |
| 84 | + if (!silence) { | ||
| 85 | + wx.showLoading({ | ||
| 86 | + title: '处理中', | ||
| 87 | + mask: true | ||
| 88 | + }) | ||
| 89 | + } | ||
| 90 | + return new Promise((success, fail) => { | ||
| 91 | + let openid = wx.getStorageSync('customer_openid'); | ||
| 92 | + let formids = wx.getStorageSync('formids'); | ||
| 93 | + if (formids) { | ||
| 94 | + wx.removeStorage({key: 'formids'}); | ||
| 50 | } | 95 | } |
| 51 | - if(!silence){ | ||
| 52 | - wx.showLoading({ | ||
| 53 | - title: '处理中', | ||
| 54 | - mask:true | ||
| 55 | - }) | 96 | + let enter_params = wx.getStorageSync('enter_params'); |
| 97 | + let operator = '' | ||
| 98 | + if (enter_params) { | ||
| 99 | + operator = JSON.stringify({ | ||
| 100 | + uid: enter_params.manager_id, | ||
| 101 | + openid: enter_params.seller_openid, | ||
| 102 | + sid: enter_params.seller_id, | ||
| 103 | + }) | ||
| 56 | } | 104 | } |
| 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, | 105 | + wx.request({ |
| 106 | + url: host + act, | ||
| 107 | + method: 'POST', | ||
| 108 | + data: data, | ||
| 109 | + header: { | ||
| 110 | + ...header, | ||
| 111 | + openid: openid, | ||
| 112 | + formids: formids.toString(), | ||
| 113 | + operator: operator, | ||
| 114 | + 'content-type': 'application/x-www-form-urlencoded' | ||
| 115 | + }, | ||
| 116 | + success: function (res) { | ||
| 117 | + console.log('request.api==>', host + cache_key + '\nrequest.success', res.data); | ||
| 118 | + // return; | ||
| 119 | + try{ | ||
| 120 | + if (res.data) { | ||
| 121 | + if (cachetime != 0 && res.data.ret == 200) { | ||
| 122 | + cache(cache_key, res.data, cachetime);//.then().catch(); | ||
| 123 | + } | ||
| 124 | + if (res.data.ret !== 200) { | ||
| 125 | + try { | ||
| 126 | + res.data.msg = res.data.msg.replace('非法请求:', ''); | ||
| 127 | + } catch (e) { | ||
| 128 | + console.log('res-fail', res, e); | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + } | ||
| 132 | + success(res.data); | ||
| 133 | + }catch (e){ | ||
| 134 | + rollbar({ | ||
| 135 | + post_host:host, | ||
| 136 | + openid: openid, | ||
| 137 | + formids: formids.toString(), | ||
| 138 | + operator: operator, | ||
| 139 | + post_data:cache_key, | ||
| 140 | + response:res | ||
| 70 | }) | 141 | }) |
| 71 | } | 142 | } |
| 72 | - wx.request({ | ||
| 73 | - url: host+act, | ||
| 74 | - method: 'POST', | ||
| 75 | - data: data, | ||
| 76 | - header: { | ||
| 77 | - ...header, | ||
| 78 | - openid:openid, | ||
| 79 | - formids:formids.toString(), | ||
| 80 | - operator:operator, | ||
| 81 | - 'content-type': 'application/x-www-form-urlencoded' | ||
| 82 | - }, | ||
| 83 | - success: function (res) { | ||
| 84 | - console.log('request.api==>',host+cache_key+'\nrequest.success',res.data); | ||
| 85 | - // return; | ||
| 86 | - if(res.data){ | ||
| 87 | - if(cachetime!=0 && res.data.ret==200){ | ||
| 88 | - cache(cache_key,res.data,cachetime);//.then().catch(); | ||
| 89 | - } | ||
| 90 | - if(res.data.ret!==200){ | ||
| 91 | - try{ | ||
| 92 | - res.data.msg=res.data.msg.replace('非法请求:',''); | ||
| 93 | - }catch(e){ | ||
| 94 | - console.log('res-fail',res,e); | ||
| 95 | - } | ||
| 96 | - } | ||
| 97 | - } | ||
| 98 | - success(res.data); | ||
| 99 | - }, | ||
| 100 | - fail: function (res) { | ||
| 101 | - console.log('request.api==>',host+cache_key+'\nrequest.fail',res); | ||
| 102 | - fail(res); | ||
| 103 | - // let query = wx.createSelectorQuery().selectComponent("#network"); | ||
| 104 | - // let net=query.exec(function(){ | ||
| 105 | - // console.log('back=>',arguments); | ||
| 106 | - // }); | ||
| 107 | - //query.show(); | ||
| 108 | - //console.log('net',query); | ||
| 109 | - }, | ||
| 110 | - complete:function(){ | ||
| 111 | - wx.hideLoading(); | ||
| 112 | - } | 143 | + |
| 144 | + }, | ||
| 145 | + fail: function (res) { | ||
| 146 | + console.log('request.api==>', host + cache_key + '\nrequest.fail', res); | ||
| 147 | + rollbar({ | ||
| 148 | + post_host:host, | ||
| 149 | + openid: openid, | ||
| 150 | + formids: formids.toString(), | ||
| 151 | + operator: operator, | ||
| 152 | + post_data:cache_key, | ||
| 153 | + response:res | ||
| 113 | }) | 154 | }) |
| 155 | + fail(res); | ||
| 156 | + // let query = wx.createSelectorQuery().selectComponent("#network"); | ||
| 157 | + // let net=query.exec(function(){ | ||
| 158 | + // console.log('back=>',arguments); | ||
| 159 | + // }); | ||
| 160 | + //query.show(); | ||
| 161 | + //console.log('net',query); | ||
| 162 | + }, | ||
| 163 | + complete: function (res) { | ||
| 164 | + wx.hideLoading(); | ||
| 165 | + } | ||
| 114 | }) | 166 | }) |
| 167 | + }) | ||
| 115 | } | 168 | } |
| 116 | 169 | ||
| 117 | -function __api(){ | ||
| 118 | - let tmpApi={} | ||
| 119 | - let keys=[]; | ||
| 120 | - for(let key in api){ | ||
| 121 | - api[key]=api[key]||{}; | ||
| 122 | - keys=key.split('.'); | ||
| 123 | - tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{}; | ||
| 124 | - tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{}; | ||
| 125 | - tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){ | ||
| 126 | - //console.log('api[key].host',api[key].host); | ||
| 127 | - return rpc(api[key].host,api[key].alias||key,data,fouce,header, | ||
| 128 | - api[key].silence,api[key].cachetime,api[key].timeout); | ||
| 129 | - } | 170 | +function __api() { |
| 171 | + let tmpApi = {} | ||
| 172 | + let keys = []; | ||
| 173 | + for (let key in api) { | ||
| 174 | + api[key] = api[key] || {}; | ||
| 175 | + keys = key.split('.'); | ||
| 176 | + tmpApi[keys[0]] = tmpApi[keys[0]] ? tmpApi[keys[0]] : {}; | ||
| 177 | + tmpApi[keys[0]][keys[1]] = tmpApi[keys[0]][keys[1]] ? tmpApi[keys[0]][keys[1]] : {}; | ||
| 178 | + tmpApi[keys[0]][keys[1]][keys[2]] = function (data, fouce = false, header) { | ||
| 179 | + //console.log('api[key].host',api[key].host); | ||
| 180 | + return rpc(api[key].host, api[key].alias || key, data, fouce, header, | ||
| 181 | + api[key].silence, api[key].cachetime, api[key].timeout); | ||
| 130 | } | 182 | } |
| 131 | - for(let xkey in uri_dc){ | ||
| 132 | - let key=uri_dc[xkey].toString(); | ||
| 133 | - if(key.indexOf('App.')>-1){ | ||
| 134 | - keys=key.split('.'); | ||
| 135 | - try{ | ||
| 136 | - if(typeof tmpApi[keys[0]][keys[1]][keys[2]]=='undefined'){ | ||
| 137 | - throw new Error('undefined'); | ||
| 138 | - } | ||
| 139 | - }catch(e){ | ||
| 140 | - api[key]=api[key]||{}; | ||
| 141 | - tmpApi[keys[0]]=tmpApi[keys[0]]?tmpApi[keys[0]]:{}; | ||
| 142 | - tmpApi[keys[0]][keys[1]]=tmpApi[keys[0]][keys[1]]?tmpApi[keys[0]][keys[1]]:{}; | ||
| 143 | - tmpApi[keys[0]][keys[1]][keys[2]]=function(data,fouce=false,header){ | ||
| 144 | - //console.log('api[key].host',api[key]); | ||
| 145 | - return rpc(api[key].host,api[key].alias||key,data,fouce,header, | ||
| 146 | - api[key].silence,api[key].cachetime,api[key].timeout); | ||
| 147 | - } | ||
| 148 | - } | 183 | + } |
| 184 | + for (let xkey in uri_dc) { | ||
| 185 | + let key = uri_dc[xkey].toString(); | ||
| 186 | + if (key.indexOf('App.') > -1) { | ||
| 187 | + keys = key.split('.'); | ||
| 188 | + try { | ||
| 189 | + if (typeof tmpApi[keys[0]][keys[1]][keys[2]] == 'undefined') { | ||
| 190 | + throw new Error('undefined'); | ||
| 191 | + } | ||
| 192 | + } catch (e) { | ||
| 193 | + api[key] = api[key] || {}; | ||
| 194 | + tmpApi[keys[0]] = tmpApi[keys[0]] ? tmpApi[keys[0]] : {}; | ||
| 195 | + tmpApi[keys[0]][keys[1]] = tmpApi[keys[0]][keys[1]] ? tmpApi[keys[0]][keys[1]] : {}; | ||
| 196 | + tmpApi[keys[0]][keys[1]][keys[2]] = function (data, fouce = false, header) { | ||
| 197 | + //console.log('api[key].host',api[key]); | ||
| 198 | + return rpc(api[key].host, api[key].alias || key, data, fouce, header, | ||
| 199 | + api[key].silence, api[key].cachetime, api[key].timeout); | ||
| 149 | } | 200 | } |
| 201 | + } | ||
| 150 | } | 202 | } |
| 151 | - //console.log(tmpApi[keys[0]]); | ||
| 152 | - return tmpApi[keys[0]]; | 203 | + } |
| 204 | + //console.log(tmpApi[keys[0]]); | ||
| 205 | + tmpApi[keys[0]].rollbar=rollbar; | ||
| 206 | + return tmpApi[keys[0]]; | ||
| 153 | } | 207 | } |
| 154 | -function cache(key,val=-99999,timeout=-1){ | ||
| 155 | - if(val!=-99999){//写入 | ||
| 156 | - return new Promise((success,fail)=>{ | ||
| 157 | - let data={ | ||
| 158 | - data:val, | ||
| 159 | - timeout:timeout==-1?-1:(Date.parse(new Date())/1000+timeout) | ||
| 160 | - } | ||
| 161 | - wx.setStorage({ | ||
| 162 | - key:key, | ||
| 163 | - data:data, | ||
| 164 | - success:()=>{ | ||
| 165 | - success() | ||
| 166 | - }, | ||
| 167 | - fail:()=>{ | ||
| 168 | - fail() | ||
| 169 | - } | ||
| 170 | - }); | ||
| 171 | - }) | ||
| 172 | - } | ||
| 173 | - let res=wx.getStorageSync(key); | ||
| 174 | - let time=Date.parse(new Date())/1000; | ||
| 175 | - if(res && (res.timeout==-1 || res.timeout>time) && res.data){ | ||
| 176 | - return res.data; | ||
| 177 | - }else{ | ||
| 178 | - return -99999 | ||
| 179 | - } | 208 | +function cache(key, val = -99999, timeout = -1) { |
| 209 | + if (val != -99999) {//写入 | ||
| 210 | + return new Promise((success, fail) => { | ||
| 211 | + let data = { | ||
| 212 | + data: val, | ||
| 213 | + timeout: timeout == -1 ? -1 : (Date.parse(new Date()) / 1000 + timeout) | ||
| 214 | + } | ||
| 215 | + wx.setStorage({ | ||
| 216 | + key: key, | ||
| 217 | + data: data, | ||
| 218 | + success: () => { | ||
| 219 | + success() | ||
| 220 | + }, | ||
| 221 | + fail: () => { | ||
| 222 | + fail() | ||
| 223 | + } | ||
| 224 | + }); | ||
| 225 | + }) | ||
| 226 | + } | ||
| 227 | + let res = wx.getStorageSync(key); | ||
| 228 | + let time = Date.parse(new Date()) / 1000; | ||
| 229 | + if (res && (res.timeout == -1 || res.timeout > time) && res.data) { | ||
| 230 | + return res.data; | ||
| 231 | + } else { | ||
| 232 | + return -99999 | ||
| 233 | + } | ||
| 180 | } | 234 | } |
| 181 | export default __api(); | 235 | export default __api(); |
-
Please register or login to post a comment