first commit
This commit is contained in:
120
衣莎项目/源码/YiShaXiYi/http/ajax.js
Normal file
120
衣莎项目/源码/YiShaXiYi/http/ajax.js
Normal file
@@ -0,0 +1,120 @@
|
||||
const app = getApp();
|
||||
const config = app.loadConfig();
|
||||
const regeneratorRuntime = app.loadAsync()
|
||||
|
||||
const { GetSetting } = require("../model/getSetting")
|
||||
const getSetting = new GetSetting()
|
||||
|
||||
const { Getauth } = app.loadModel("user")
|
||||
const getauth = new Getauth()
|
||||
|
||||
const { Msg } = require("../model/message")
|
||||
const msg = new Msg()
|
||||
|
||||
const util = app.loadUtils("util")
|
||||
/**
|
||||
* 对request二次封装
|
||||
*/
|
||||
export class Http {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
/**
|
||||
* 普通请求处理
|
||||
* @param {Object} option
|
||||
* header 自定义请求头 url传入的请求地址 data接口入参 method接口请求方式默认GET
|
||||
*/
|
||||
async request1(option) {
|
||||
// 拼接请求头内容 此处拿出来方便后期对请求头做二次处理
|
||||
let header = { ...option.header || "" }
|
||||
return new Promise(function (resolve, reject) {
|
||||
wx.request({
|
||||
url: `${config.baseUrl}${option.url}`,
|
||||
data: option.data || {},
|
||||
method: option.method || "GET",
|
||||
header: header,
|
||||
success(res) {
|
||||
// 数据成功时返回数据
|
||||
if (res.data.status == 1) {
|
||||
resolve(res)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 带token请求处理
|
||||
* @param {Object} option
|
||||
* header 自定义请求头 url传入的请求地址 data接口入参 method 接口请求方式默认GET
|
||||
*/
|
||||
async request2(option) {
|
||||
let that = this
|
||||
// 获取token放入请求头中
|
||||
let token = await util.token();
|
||||
// 拼接请求头内容 此处拿出来方便后期对请求头做二次处理
|
||||
let header = { token: token || "", ...option.header || "" }
|
||||
return new Promise(function (resolve, reject) {
|
||||
// 判断本地是否已经授权和登录,如果没授权和登录跳转
|
||||
if (!getSetting.isAuthorization() || !token) {
|
||||
reject('用户未授权登录')
|
||||
wx.navigateTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
return
|
||||
}
|
||||
wx.request({
|
||||
url: `${config.baseUrl}${option.url}`,
|
||||
data: option.data || "",
|
||||
method: option.method,
|
||||
header: header,
|
||||
success(res) {
|
||||
// token过期
|
||||
if (res.data.error_code == 30002) {
|
||||
getauth.login()
|
||||
.then(async login => {
|
||||
// token过期重新请求过期接口
|
||||
// 然后resolve把结果跑出去
|
||||
let newRes = await that.request2(option)
|
||||
resolve(newRes)
|
||||
})
|
||||
return
|
||||
}
|
||||
// 数据成功时返回数据
|
||||
if (res.data.status == 1) {
|
||||
resolve(res)
|
||||
} else {
|
||||
if (res.data.error_code == 30002) return;
|
||||
wx.showToast({
|
||||
title: res.data.msg,
|
||||
icon: 'none',
|
||||
duration: 2500
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 上传图片
|
||||
* @param {*} data 需要上传的图片路径
|
||||
*/
|
||||
uploadFile(data) {
|
||||
wx.showLoading({
|
||||
title: '上传中',
|
||||
})
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.uploadFile({
|
||||
url: `${config.baseUrl}/addImg`,
|
||||
filePath: data,
|
||||
name: 'file',
|
||||
header: {
|
||||
'Content-Type': 'multipart/form-data;'
|
||||
},
|
||||
success(res) {
|
||||
wx.hideLoading()
|
||||
resolve(res)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user