import comstemutil from "./comstemutil"; //es6 // const baseUrl = 'http://192.168.0.229:9999'; const baseUrl = 'https://www.bjkeyware.com/test-api'; // const baseUrl = 'https://www.bjkeyware.com/prod-api'; // const baseUrl = 'http://172.16.36.180:9999'; // const baseUrl = 'https://www.bjkeyware.com/prod-api'; const devStatus = false; const commonhttp = (params, header) => { return new Promise((resolve, reject) => { uni.showLoading({ title: "加载中" }) uni.request({ ...params, header, success(res) { resolve(res) }, fail(err) { reject(err) }, complete(res) { // console.log('--请求-',res) let statusCode = res.data.code; // console.log('---',statusCode) if (statusCode==401){ // comstemutil.systemLogOut(); // reject({code:1,msg:'未授权'}) uni.setStorageSync('accessToken', null) uni.setStorageSync('wxUserInfo', null) if(res.data.msg.includes('/my/shoppingCart')||res.data.msg.includes('message/my')){ uni.setStorageSync('accessToken', null) uni.setStorageSync('wxUserInfo', null) }else{ uni.showToast({ title:'登录失效,请重新登录', icon: 'none', duration:1000 }); setTimeout(()=>{ uni.navigateTo({ url: '/pages/personal/login' }) },1000) } } // 不管请求成没成功都会走这一步 然后隐藏 加载中的效果 uni.hideLoading() } }) }) } // fial:(err)=>{} // { // // fail(err) { // // reject(err) // // }, // complete(hide){ // console.log('--报错-',hide); //可以看到错误信息被打印出来了 // if(statusCode ==401){ // reject({code:1,msg:'未授权'}) // } else { // uni.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }); // } // } // } const request = (params) => { return new Promise((resolve, reject) => { let header = {'TENANT-ID': 3}; // console.log('params',params,) // header['Authorization'] = 'Basic cGlnOnBpZw=='; // 身份token信息 // let {notToken} = {...params} // console.log('====',notToken) // if (notToken) { // header['isToken'] = false; // } // const isToken = (header || {}).isToken === false // if(!isToken){ // header={isToken:false}; // } // console.log('==header==',header) let accessToken = uni.getStorageSync('accessToken'); // console.log('tpken--',accessToken) if (accessToken && params.requireToken) { // console.log('11111',res) header['Authorization'] = 'Bearer ' + accessToken; // let expiresTimeStamp = uni.getStorageSync('expiresTimeStamp'); // let stamp = expiresTimeStamp - new Date().getTime(); // if (stamp < 5000) { // // 刷新 token // reject({code: 1, msg: '需要刷新token重新登录'}); /*comstemutil.refreshToken().then(()=>{ accessToken = uni.getStorageSync('accessToken'); header['Authorization'] = 'Bearer ' + accessToken; commonhttp(params, header).then((res)=>{ resolve(res) }).catch((err)=>{ reject(err) }) }).catch((err)=>{ reject(err) })*/ // } } commonhttp(params, header).then((res)=>{ resolve(res) }).catch((err)=>{ reject(err) }) }) } const buildQueryStr = (params) => { if(typeof params === 'string') { return params; } if(typeof params === 'function') { return ''; } if(Object.keys(params).length === 0) { return ''; } let paramArr = []; Object.keys(params).forEach(kk=>{ paramArr.push(kk + '=' + params[kk]); }) return paramArr.join('&'); } const buildUrl = (baseUrl, url) => { if(url.indexOf("http") > -1) { return url; } return baseUrl + url; } export default { quickPost(url, data,requireToken){ console.log('url/',url, data,requireToken) return request({url: buildUrl(baseUrl , url), data, method:'POST',requireToken}) }, post(url, params, data,requireToken){ let queryStr = params ? '?'+buildQueryStr(params) : ''; return request({url: buildUrl(baseUrl , url) + queryStr, data, method:'POST',requireToken}) }, quickGet(url,requireToken) { return request({url: baseUrl + url, method:'GET',requireToken}) }, get(url,params,requireToken) { let queryStr = params ? '?'+buildQueryStr(params) : ''; return request({url: buildUrl(baseUrl , url) + queryStr, method:'GET',requireToken}) }, quickDelete(url,requireToken) { return request({url: buildUrl(baseUrl , url), method:'DELETE',requireToken}) }, quickPut(url, data,requireToken) { return request({url: buildUrl(baseUrl , url), data, method:'PUT',requireToken}) }, devStatus, baseUrl // devUrl }