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.
xzjl-ui/utils/request.js

165 lines
4.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class request {
constructor() {
this._baseUrl = 'https://xzjl-api.windymuse.cn';
this._token = wx.getStorageSync('token');
this._header = {'Authorization': this._token}
}
/**
* 文件上传封装请求
*/
uploadRequest(url, data, header = this._header) {
return this.uploadAll(url, data, header)
}
/**
* GET类型的网络请求
*/
getRequest(url, data, header = this._header) {
return this.requestAll(url, data, header, 'GET')
}
/**
* DELETE类型的网络请求
*/
deleteRequest(url, data, header = this._header) {
return this.requestAll(url, data, header, 'DELETE')
}
/**
* PUT类型的网络请求
*/
putRequest(url, data, header = this._header) {
return this.requestAll(url, data, header, 'PUT')
}
/**
* POST类型的网络请求
*/
postRequest(url, data, header = this._header) {
return this.requestAll(url, data, header, 'POST')
}
/**
* patch类型的网络请求
*/
patchRequest(url, data, header = this._header) {
return this.requestAll(url, data, header, 'PATCH')
}
uploadAll(url, data, header){
console.log('到这个位置了。。。。。')
console.log(data)
return new Promise((resolve, reject) => {
console.log('输出以下。。。。。')
console.log(data.file)
wx.uploadFile({
method:'POST',
url: this._baseUrl + url,
name:'',
filePath:'',
header: {'Authorization': wx.getStorageSync('token')},
formData: {
type:data.type,
file:data.file
},
success: (res => {
if(res.data.code==401){
wx.setStorageSync('token', null)
wx.showToast({
title:'Token无效或过期请重新登录',
icon: 'none',
duration: 5000
})
wx.navigateTo({
url: '/pages/index/index'
})
resolve(res)
}
if (res.statusCode === 200) {
//200: 服务端业务处理正常结束
if(res.data.code==401){
wx.showToast({
title:'Token无效或过期请重新登录',
icon: 'none',
duration: 5000
})
wx.navigateTo({
url: '/pages/index/index'
})
}
resolve(res)
} else {
//其它错误,提示用户错误信息
reject(res)
}
}),
fail: (res => {
reject(res)
})
})
})
}
/**
* 网络请求
*/
requestAll(url, data, header, method) {
return new Promise((resolve, reject) => {
wx.request({
url: this._baseUrl + url,
data: data,
header: {'Authorization': wx.getStorageSync('token')},
method: method,
success: (res => {
if(res.data.code==401){
wx.setStorageSync('token', null)
wx.showToast({
title:'Token无效或过期请重新登录',
icon: 'none',
duration: 5000
})
wx.navigateTo({
url: '/pages/index/index'
})
resolve(res)
}
if (res.statusCode === 200) {
//200: 服务端业务处理正常结束
if(res.data.code==401){
wx.showToast({
title:'Token无效或过期请重新登录',
icon: 'none',
duration: 5000
})
wx.navigateTo({
url: '/pages/index/index'
})
}
resolve(res)
} else {
//其它错误,提示用户错误信息
reject(res)
}
}),
fail: (res => {
reject(res)
})
})
})
}
}
export default request