import config from '../config.js'; const getDomain = (uri, method, type) => { let serverUrl = config.serverUrl; let fileUrl = config.serverFile; if (type && type == 'file') return fileUrl + `/files` + uri; else return serverUrl + `/cxyy/api` + uri; }; // 获取token const getToken = () => { try { const value = uni.getStorageSync('token'); if (value) return value; } catch {} }; export const requestBase = async (uri, method = 'GET', data, type) => { let baseUrl = getDomain(uri, method, type); let header = {}; const token = uni.getStorageSync('token'); if (token) header.token = token; return new Promise((resolve, reject) => { uni.request({ url: baseUrl, method: method || 'GET', data: data || {}, header, success: (res) => { resolve(res.data); }, fail: (err) => { uni.showToast({ title: '请求接口失败', icon: 'fail', }); reject(err.data); }, }); }); }; export const requestApp = async (uri, method = 'GET', data, type) => { let baseUrl = getDomain(uri, method, type); let header = {}; const token = uni.getStorageSync('token'); if (token) header.token = token; return new Promise((resolve, reject) => { uni.request({ url: baseUrl, method: method || 'GET', data: data || {}, header, success: (res) => { resolve(res.data); }, fail: (err) => { uni.showToast({ title: '请求接口失败', icon: 'fail', }); reject(err.data); }, }); }); }; export const requestFile = (uri, method, data, type) => { let baseUrl = getDomain(uri, method, type); return new Promise((resolve, reject) => { uni.uploadFile({ url: baseUrl, filePath: data, name: method || 'file', formData: {}, success: (res) => { let data = JSON.parse(res.data); if (data.errcode != '0') { return uni.showToast({ title: data.errmsg, icon: 'error', }); } resolve(data); }, fail: (err) => { let data = JSON.parse(err.data); uni.showToast({ title: '请求接口失败', icon: 'fail', }); reject(data); }, }); }); };