import { getToken, clearToken } from './auth' import store from '@/store' const BASE_PATH = 'http://192.168.31.146:8082/api/app/forward' // const BASE_PATH = 'https://mailbox.biutag.com/lan-api/api' function get(options) { options.method = 'GET'; return ajax(options.url, options) } function post(options) { options.method = 'POST'; return ajax(options.url, options) } function put(options) { options.method = 'PUT'; return ajax(options.url, options) } function del(options) { options.method = 'DELETE'; return ajax(options.url, options) } function ajax(url, options) { let body; if (options?.params && Object.keys(options.params).length > 0) { if (options.method === 'GET') { options.query = options.params; } else { body = JSON.stringify(options.params); } } if (options?.query) { const queryParams = [] for (const key in options.query) { queryParams.push(key + '=' + options.query[key]) } url += (url.indexOf('?') > -1 ? '' : '?') + queryParams.join('&') } if (options?.body) { if (options.body === 'string') { body = options.body; } else { if (Object.keys(options.body).length > 0) { body = JSON.stringify(options.body); } } } return new Promise((resolve, reject) => { if (!store.state.requestUrl) { uni.showToast({ title: '未找到资源', icon: 'none', duration: 3000 }) reject('未找到资源') return } uni.request({ url: store.state.requestUrl, method: "POST", header: { "Content-Type": "application/json", messageId: generateUUID(), appCredential: store.state.appCredential, userCredential: store.state.userCredential }, data: { url, method: options.method, admin: getToken(), body }, success: function(response) { if (response.statusCode !== 200) { uni.showToast({ title: 'response信息:' + JSON.stringify(response), icon: 'none', duration: 3000 }); return } const res = response.data; if (res.code === 200) { resolve(res.data) } else { let message = res.msg; if (res.code === 401) { message = '未授权登录' clearToken() } uni.showToast({ title: message, icon: 'none', duration: 3000 }); reject(res) } }, fail: function(err) { uni.showToast({ title: '网络异常,请检查网络', icon: 'none', duration: 3000 }) resolve() } }) }) } function generateUUID() { var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; } const request = { get, post, put, del, BASE_PATH } export default request;