公寓小程序端前端代码
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.

84 lines
2.8 KiB

/*
* @Author: mk 2403457699@qq.com
* @Date: 2024-08-19 14:55:23
* @LastEditors: mk 2403457699@qq.com
* @LastEditTime: 2024-10-09 09:34:49
* @FilePath: \epmet-voluntary-mp\utils\request.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
var global = require('./config.js')
const request = function (url, options) {
let token = wx.getStorageSync('token')
// 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX3BhcGVydHlwZSI6MSwibG9naW5fdXNlcl90ZWxlcGhvbmUiOiIxNzY4NTkwODE1NiIsImxvZ2luX3VzZXJfY2xpZW50IjoiYnlzbXAiLCJsb2dpbl91c2VyX25hbWUiOiLmnY7puY_nqIsiLCJpZGNhcmQiOiIzNzAyODIyMDAwMDkxNjMyMTYiLCJsb2dpbl91c2VyX2tleSI6OCwibG9naW5fdXNlcl9nZW5kZXIiOjF9.Ho2cqEM7sbI6k-IODA9Ffse5LTsLskmCg7u1TtAVHbCaQ5legdAhqtURN5J_v9u3GfvXr36Qc8f2xoAJCs0-9Q'
return new Promise((resolve, reject) => {
let header = {
'Content-Type': 'application/json; charset=UTF-8',
}
if(token){
header.Authorization = token
}
wx.request({
url: `${global.BASEURL()}${url}`,
method: options.method,
data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
header: header,
success (response) {
if (response.statusCode === 200) {
if(response.data.code===200){
resolve(response.data)
}else if(response.data.code === 10007 ||response.data.code === 10006 || response.data.code == 1024 || response.data.code == 2003||response.data.code === 10005){
wx.removeStorageSync('token')
wx.switchTab({
url: '/pages/index/index',
})
}else if(response.data.code){
let errmsg = response.data.msg
if(errmsg==undefined || errmsg=='undefined' ){
errmsg = '未返回错误信息'
}
wx.showToast({
title: errmsg,
icon: 'none',
duration: 3000
})
reject(response.data)
}
} else if(response.statusCode !== 404){
wx.showToast({
title: '网络问题,请稍后再试。',
icon: 'none',
duration: 2000
})
reject(false)
}
},
fail (error) {
wx.showToast({
title: '网络问题,请稍后再试。',
icon: 'none',
duration: 2000
})
reject(error.data)
}
})
})
}
function get(url, options = {}) {
return request(url, { method: 'GET', data: options })
}
function post(url, options = {}) {
return request(url, { method: 'POST', data: options })
}
function put(url, options = {}) {
return request(url, { method: 'PUT', data: options })
}
module.exports = {
get: get,
post: post,
put: put
}