1.请求配置
// config.js
const isDev = 0 // 1:开发环境 2:测试环境 0: 生产环境
const version = `v1.0.0 `
/**
*
* @param url {string} 请求地址
* @param data {object} 请求数据
* @param method {string} 默认 get 请求
* @returns {Promise<any>}
*/
function request(url, data = {}, method = "GET", header) {
return new Promise(function (resolve, reject) {
if (/[a-z]/g.test(method)) {
method = method.toUpperCase()
}
wx.showNavigationBarLoading()
/**新版接口post 请求设置请求
* @Date 2019/07/08
*/
let head = {
'Content-Type': 'application/json',
'version': version
}
if (!/applet/g.test(url) && method != "GET") {
Object.assign(head, {
"Content-Type": "application/x-www-form-urlencoded"
})
}
const memberId = wx.getStorageSync('memberId') || 0
if (memberId) {
Object.assign(head, {
memToken: memberId
})
}
wx.request({
url: url,
data: data,
method: method,
header: header || head,
success: function (res) {
if (res.statusCode == 200) {
if (res.data) {
if (res.data.code == 0 || res.data.code == 1000 || res.data.code == 200) {
resolve(res.data);
} else if (res.data.code == 1013) {
resolve(res.data);
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false
})
} else {
throwError(res.data, reject)
}
} else {
throwError(res, reject)
}
} else {
reject({
message: res.message || res.msg || "网络出现异常",
code: res.statusCode
})
}
},
fail: function (err) {
if (err.errMsg && !err.message) {
err.message = err.errMsg
}
reject(err)
console.log('request failed')
},
complete() {
wx.hideNavigationBarLoading()
}
})
})
}
/*使用示例
* 1、 引入 const req = require('../../config').request
*
* 2、 使用 get 请求 req(url, {busId, memberId, addressId})
* .then(res => {...})
* .catch(err => {...})
* post 请求 req(url, {busId, memberId, addressId}, 'POST')
* .then(res => {...})
* .catch(err => {...})
* 3、错误捕获统一使用 err.message 捕获错误信息
* */
function throwError(res, reject) {
if (res.msg) {
reject({
message: res.msg,
code: res.code
})
} else if (res.message) {
reject({
message: res.message,
code: res.code
})
} else if (res.data && res.data.errorMsg) {
reject({
message: res.data.errorMsg,
code: res.code
})
} else {
reject({
message: '网络出现异常',
code: res.code
})
}
}
module.exports = {
isDev,
request,
}
2. 公用配置
// app.js
// 开发环境
const isDev = require('./config.js').isDev
// 接口
const api = require('./utils/constant.js')
// 公用封装ajax请求方法
const ajax = require("./config.js")
App({
onLaunch: function (options) {
var self = this
// 获取ext.json的配置信息
let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
if (extConfig.subDomain) {
console.log('获取Ext文件')
self.globalData.subDomain = extConfig.subDomain;
self.globalData.http = extConfig.subDomain + '/mallApplet/79B4DE7C/' //接口域名配置
self.globalData.busId = extConfig.busId //商家id
self.globalData.picUrl = extConfig.upload //图片域名
self.globalData.appid = extConfig.appid
self.globalData.style = extConfig.style
self.globalData.socketdomain = extConfig.socketdomain
self.globalData.domain = extConfig.domain //主域名
} else if (wx.getExtConfig) {
wx.getExtConfig({
success: function (res) {
self.globalData.subDomain = res.extConfig.subDomain;
self.globalData.http = res.extConfig.subDomain + '/mallApplet/79B4DE7C/' //接口域名配置
self.globalData.busId = res.extConfig.busId //商家id
self.globalData.picUrl = res.extConfig.upload //图片域名
self.globalData.appid = res.extConfig.appid
self.globalData.style = res.extConfig.style
self.globalData.socketdomain = res.extConfig.socketdomain
self.globalData.domain = res.extConfig.domain //主域名
},
complete: () => {
this.setEnv() // 替换开发环境变量
}
})
}
},
onShow() {
wx.getNetworkType({
success(res) {
if (res.networkType === 'none') {
wx.showToast({
title: '网络链接不可用',
icon: 'none',
duration: 2000
})
}
}
})
// 版本更新
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
})
},
// 页面不存在重定向
onPageNotFound(res) {
wx.redirectTo({
url: 'pages/not_found404/not_found404'
})
},
globalData: {
userInfo: null,
http: "",
newHttp: "",
picUrl: "",
busId: "",
appid: "",
style: "",
domain: "",
subDomain: "",
socketdomain: "",
}
})