You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

92 lines
3.3 KiB

import util from '../utils/util';
// import { app as vm } from '../main';
const islog = true;
export class Server {
request(url,data,method,header,isDefaultGet,failMsg){
return new Promise(async (rs,rj)=>{
// const _store = vm.$store;
// let storeInfo = _store.state.storeInfo || null;
// if(storeInfo)data['brand_id'] = storeInfo.brand_id;
const _token = uni.getStorageSync('token') || '';
if(_token)data['token'] = _token;
if(method === 'POST'&&_token)url = url + `?token=${_token}`
uni.request({
url,
data,
method, // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: {...header, Pcassistant: 2}, // 设置请求的 header // {'custom-header': 'application/json'}
success: res=>{
if(islog)console.log('req success---->',{
link: url,
query: data,
method: method,
data: res,
})
if(isDefaultGet){
if(failMsg == '')throw Error('默认回调,失败提示不能为空 key -> failMsg');
defaultGet({
url,
data,
res,
failMsg,
resolve: rs,
reject: rj
});
return
}
rs(res);
},
fail: err=>{
if(islog)console.log('req fail---->',{
link: url,
query: data,
method: method,
data: err,
})
util.hideLoad();
if(typeof(err.errMsg) == 'string'&&(err.errMsg.indexOf('timeout')!=-1 || err.errMsg.indexOf('interrupted')!=-1 || err.errMsg.indexOf('请求超时')!=-1)){
util.showNone('网络超时!');
}else if(typeof(err.errMsg) == 'string'){
util.showNone(err.errMsg);
}else{
util.showNone('请求数据失败!请检查当前网络状态。');
}
rj(err);
},
})
})
function defaultGet({res,failMsg,resolve,reject,url,data}){
if(res.data.code == 0){
resolve(res.data.data);
}else{
util.hideLoad();
util.showNone(res.data.message || failMsg || '');
reject({url,res,data});
}
}
}
get({url,data={},header={},isDefaultGet=true,failMsg=''}){
return this.request(url,data,'GET',header,isDefaultGet,failMsg);
}
post({url,data={},header={'custom-header': 'application/json'},isDefaultGet=true,failMsg=''}){
return this.request(url,data,'POST',header,isDefaultGet,failMsg);
}
uploadFile({url,filePath,onProgressCallBack,formData={}}){
return new Promise((rs,rj)=>{
let uploadTask = wx.uploadFile({
url,filePath,formData,name:'file',success:rs,fail:rj
})
uploadTask.onProgressUpdate(res=>onProgressCallBack&&onProgressCallBack(res))
})
}
}
export const servers = new Server();
export default { servers, Server };