Commit 71f63ad068ac724d46864cd31f70f17e8cc6a588

Authored by wangfan
1 parent 4570476b

qnlog

Showing 2 changed files with 200 additions and 146 deletions
{
"description": "项目配置文件。",
"setting": {
"urlCheck": true,
"urlCheck": false,
"es6": true,
"postcss": true,
"minified": true,
... ...
... ... @@ -3,13 +3,46 @@
*/
//接口地址,测试环境请修改以下地址
//const server_host='https://xt04.colorcun.com/?s=';
const api_timeout=2000; //接口超时时间,默认2s
const api_timeout = 2000; //接口超时时间,默认2s
//引入接口映射关系表
import api from './api.url'
import uri_dc from './uri'
const server_host=uri_dc.HOST__LINE;
const server_host = uri_dc.HOST__LINE;
/**
* @param data
* @param status started, succeeded (default), failed, or timed_out
*/
function rollbar(data) {
// let raw=JSON.stringify(data)
// let trace={
// frames:[data],
// exception:{class:raw},
// }
let param={
access_token: '83bd2afd09b8402b943cb1894a84ec51',
data:{
body:{
//trace:trace,
// trace_chain:[trace],
message:{
body:data,
},
// crash_report:{
// raw:raw
// },
},
}
}
wx.request({
url: 'https://api.rollbar.com/api/1/item/',
method: 'POST',
data: param,
header: {
//'content-type': 'application/x-www-form-urlencoded'
},
})
}
/**
*
* @param act
... ... @@ -21,161 +54,182 @@ const server_host=uri_dc.HOST__LINE;
* @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
}
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);
}
let cache_key = act;
if (param) {
cache_key += '?' + param.substr((param.length - 1) * -1);
}
if (fouce == 'clear' || fouce == 'clean') {
return wx.removeStorageSync(cache_key);
}
if (!fouce && cachetime != 0) {//优先使用缓存
let getCache = cache(cache_key);
//console.log('getCache.'+getCache,cache_key);
if (getCache != -99999) {
return new Promise((success, fail) => {
getCache.isCache = true;
console.log(cache_key + '\nuse cache', getCache);
success(getCache);
})
}
if(fouce=='clear' || fouce=='clean'){
return wx.removeStorageSync(cache_key);
}
if(!fouce && cachetime!=0){//优先使用缓存
let getCache= cache(cache_key);
//console.log('getCache.'+getCache,cache_key);
if(getCache!=-99999){
return new Promise((success,fail)=>{
getCache.isCache=true;
console.log(cache_key+'\nuse cache',getCache);
success(getCache);
})
}
}
if (!silence) {
wx.showLoading({
title: '处理中',
mask: true
})
}
return new Promise((success, fail) => {
let openid = wx.getStorageSync('customer_openid');
let formids = wx.getStorageSync('formids');
if (formids) {
wx.removeStorage({key: 'formids'});
}
if(!silence){
wx.showLoading({
title: '处理中',
mask:true
})
let enter_params = wx.getStorageSync('enter_params');
let operator = ''
if (enter_params) {
operator = JSON.stringify({
uid: enter_params.manager_id,
openid: enter_params.seller_openid,
sid: enter_params.seller_id,
})
}
return new Promise((success,fail)=>{
let openid = wx.getStorageSync('customer_openid');
let formids = wx.getStorageSync('formids');
if(formids){
wx.removeStorage({key:'formids'});
}
let enter_params = wx.getStorageSync('enter_params');
let operator=''
if(enter_params){
operator=JSON.stringify({
uid:enter_params.manager_id,
openid: enter_params.seller_openid,
sid:enter_params.seller_id,
wx.request({
url: host + act,
method: 'POST',
data: data,
header: {
...header,
openid: openid,
formids: formids.toString(),
operator: operator,
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log('request.api==>', host + cache_key + '\nrequest.success', res.data);
// return;
try{
if (res.data) {
if (cachetime != 0 && res.data.ret == 200) {
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);
}catch (e){
rollbar({
post_host:host,
openid: openid,
formids: formids.toString(),
operator: operator,
post_data:cache_key,
response:res
})
}
wx.request({
url: host+act,
method: 'POST',
data: data,
header: {
...header,
openid:openid,
formids:formids.toString(),
operator:operator,
'content-type': 'application/x-www-form-urlencoded'
},
success: function (res) {
console.log('request.api==>',host+cache_key+'\nrequest.success',res.data);
// return;
if(res.data){
if(cachetime!=0 && res.data.ret==200){
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();
}
},
fail: function (res) {
console.log('request.api==>', host + cache_key + '\nrequest.fail', res);
rollbar({
post_host:host,
openid: openid,
formids: formids.toString(),
operator: operator,
post_data:cache_key,
response: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 (res) {
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,api[key].alias||key,data,fouce,header,
api[key].silence,api[key].cachetime,api[key].timeout);
}
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, api[key].alias || 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]);
return rpc(api[key].host,api[key].alias||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]);
return rpc(api[key].host, api[key].alias || key, data, fouce, header,
api[key].silence, api[key].cachetime, api[key].timeout);
}
}
}
//console.log(tmpApi[keys[0]]);
return tmpApi[keys[0]];
}
//console.log(tmpApi[keys[0]]);
tmpApi[keys[0]].rollbar=rollbar;
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){
return res.data;
}else{
return -99999
}
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) {
return res.data;
} else {
return -99999
}
}
export default __api();
\ No newline at end of file
... ...