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.
60 lines
1.5 KiB
60 lines
1.5 KiB
import { config } from '../config.js'
|
|
const Method = {
|
|
GET: 'GET',
|
|
POST: 'POST'
|
|
}
|
|
// 定义一些通用常量
|
|
const HTTPConst = {
|
|
TokenStoreKey: 'ETokenKey'
|
|
}
|
|
class HTTP {
|
|
constructor() {
|
|
this.baseUrl = config.api_url
|
|
}
|
|
request = (params) => {
|
|
// 从缓存中读取 token
|
|
let token = wx.getStorageSync(HTTPConst.TokenStoreKey) || ''
|
|
let url = this.baseUrl + params.url
|
|
console.log(params.url)
|
|
if (!params.method) {
|
|
params.method = Method.GET
|
|
}
|
|
// token 拼接到 请求体中
|
|
let data = params.data
|
|
console.log(data)
|
|
wx.request({
|
|
url: url,
|
|
data: data,
|
|
header: {
|
|
'token': token,
|
|
'content-type': 'application/json',
|
|
},
|
|
method: params.method,
|
|
dataType: 'json',
|
|
success: function (res) {
|
|
console.log(res)
|
|
let code = res.statusCode.toString()
|
|
let startCode = code.charAt(0)
|
|
if (startCode == '2') {
|
|
if (res.data.code == 200) {
|
|
params.success && params.success(res.data)
|
|
} else {
|
|
console.log('数据请求失败')
|
|
wx.showToast({
|
|
title: res.data.message,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
} else {
|
|
console.log('请求错误')
|
|
params.error && params.error(res)
|
|
}
|
|
},
|
|
fail: function (res) {
|
|
console.log('服务器错误')
|
|
params.fail && params.fail(res)
|
|
},
|
|
})
|
|
}
|
|
}
|
|
export { HTTP, Method, HTTPConst }
|
|
|