const { getToken, removeToken } = require('../util/token') export const BASE_URL = "https://jzxx.hncsga.cn/api"; export function get(url) { return request(url, { method: 'GET', headers: { "Content-Type": 'application/json', "Authorization": getToken() } }) } export function post(url, data) { return request(url, { method: 'POST', body: data, headers: { "Content-Type": 'application/json', "Authorization": getToken() } }) } export function put(url, data) { return request(url, { method: 'PUT', body: data, headers: { "Content-Type": 'application/json', "Authorization": getToken() } }) } export function del(url) { return request(url, { method: 'DELETE', headers: { "Content-Type": 'application/json', "Authorization": getToken() } }) } export function upload(tempFilePaths) { return new Promise((resolve, reject) => { wx.uploadFile({ url: `${BASE_URL}/file/upload`, header: { "Authorization": getToken() }, filePath: tempFilePaths, name: 'file', success(response) { const res = JSON.parse(response.data); if (res.code === 200) { resolve(res.data) } else { reject(res) } } }); }) } function request(url, options) { return new Promise((resolve, reject) => { wx.request({ url: `${BASE_URL}${url}`, method: options.method, header: options.headers, data: options.body, success(response) { const res = response.data; if (res.code === 200) { resolve(res.data) } else { if (res.code === 401) { removeToken(); wx.showToast({ title: '登录失效', icon: 'none', duration: 3000 }); wx.redirectTo({ url: '/pages/home/index', }) return } wx.showToast({ title: res.msg, icon: 'none', duration: 3000 }) reject(res) } }, fail(err) { reject(err) } }); }) }