2023-02-21 22:40:56 +08:00

87 lines
1.6 KiB
JavaScript

import {
baseURL
} from '../../config/api'
import {
getToken,
isLogin
} from '../permission/login'
const unsaveInterface = [
'/wx/login',
'/system/article/list'
]
function unsaveCheck(path) {
for (let index = 0; index < unsaveInterface.length; index++) {
const element = unsaveInterface[index];
if (element == path) {
return false
}
}
return true
}
export function request({
path,
params,
method
}) {
return new Promise((resolve, reject) => {
if (getToken() == '' && unsaveCheck(path)) {
wx.showToast({
title: '请登录后再次尝试。',
icon: 'error',
duration: 3000
})
}
wx.request({
url: baseURL + path, //仅为示例,并非真实的接口地址
data: params,
header: {
'content-type': 'application/json', // 默认值
'X-API-TOKEN': getToken()
},
method: method,
success(res) {
console.log(res.data)
resolve(res.data);
},
fail(res) {
console.log(res);
reject(res)
}
})
})
}
export function uploadFile({
path,
url,
data
}) {
return new Promise((resolve, reject) => {
let token = getToken()
if (token == '') {
isLogin()
}
console.log(url);
wx.uploadFile({
url: baseURL + path, //接口地址
filePath: url,
name: 'file',
header: {
"X-API-TOKEN": getToken()
},
name: 'upfile',
formData: data,
success: function (res) {
console.log(res)
resolve(res.data)
},
fail: function (res) {
console.log(res);
reject(res.data)
}
})
})
}