promise 封装
var app = getApp()
import {
ENV
} from '../env' // 引入本地项目的代理地址 BaseUrl 或 host
/**
* @description:
* @param {*} params:
* url:'',
* method:'GET',//非必须,默认GET
* data:{},
* responseType:'text', //非必须,默认为text
* loading:'loading提示文字', //非必须,默认为空字符串,表示不显示loading
* @return {*}
*/
// let status = false
// 记录并发请求数量
let count = 0
function request(params) {
// console.log(params,'params')
// 每次请求开始加1
count += 1
return new Promise((resolve, rejects) => {
// 确保本次登录的token是有效的
app.loginMethod().then(() => {
// 是否显示loading
if (params.loading) {
wx.showLoading({
title: params.loading,
mask:true
})
}
wx.request({
url: ENV.host + params.url,
method: params.method || 'GET',
data: params.data || {},
responseType: params.responseType || 'text',
header: {
'content-type': params.contentType || 'application/json', // 默认值
'miniprogram-satoken': wx.getStorageSync('token'),
'api-version': 'v1.0'
},
success(res) {
if (res.statusCode >= 200 && res.statusCode < 300 || res.statusCode === 304) {
if (res.data.code === 0) {
resolve(res)
} else if (res.data.code === 10000) { // token失效
// if (!status) {
// status = true
wx.clearStorage()
app.fetchStatus = null
// }
let pages = getCurrentPages(); //获取加载的页面
let currentPage = pages[pages.length - 1]; //获取当前页面的对象
let url = '/' + currentPage.route; //当前页面ur
wx.reLaunch({
url
})
} else {
rejects(res)
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
confirmColor: '#DF222E'
})
}
} else {
wx.showModal({
title: '提示',
content: '错误码:' + res.statusCode,
showCancel: false,
confirmColor: '#DF222E'
})
}
},
fail(err) {
wx.showModal({
title: '提示',
content: '网络错误,请稍后重试!',
showCancel: false,
confirmColor: '#DF222E'
})
rejects(err)
},
complete(res) {
count = count - 1
if (params.loading && count === 0) {
// setTimeout(() => {
wx.hideLoading()
// }, 500)
}
}
})
})
})
}
export default request
引入配置的服务器代理地址
*/
const CURRENT_ENV = 'test' //当前环境 dev开发环境 test测试环境 prod生产环境
const ENV_LIST = {
'dev':{
host: 'http://192.168.14.126:11000/', //测试环境
baseUrl:'http://192.168.14.126:11000/assistor', // 图片地址前缀
//....
},
'test':{
host: 'https://scrmtest.qiweioa.cn:48080/api/', //测试环境
baseUrl: 'https://scrmtest.qiweioa.cn:48080/api/assistor', // 图片地址前缀
// corpId:'wwba49910602626af3',
// agentId: 1000005
//....
//....
},
'prod':{
host: '', //测试环境
baseUrl:'' // 图片地址前缀
//....
//....
}
}
const ENV = ENV_LIST[CURRENT_ENV]
export {ENV , CURRENT_ENV}