const devBaseUrl = 'uav.djiplant.com:3100' const proBaseUrl = 'fly.djiplant.com:3300' const env = wx.getAccountInfoSync(); const { envVersion } = env.miniProgram export const baseUrl = envVersion === 'release' ? proBaseUrl : devBaseUrl let is401 = false const defaultOptions = { loading: true, timeout: 5000, successMessage: true, errorMessage: true, header: { "content-type": "application/json" }, } export function upLoadRequest( url, file = "", options = {} ) { return new Promise((resolve, reject) => { const { token } = wx.getStorageSync('userInfo') || {} options = Object.assign(defaultOptions, options) if (is401) reject() if (options.loading) wx.showToast({ title: 'loading...', icon: 'none' }) if (token) { options.header['X-Access-Token'] = token options.header['Authorization'] = token } wx.uploadFile({ url: `https://${baseUrl}/hisun-boot${url}`, // 仅为示例,非真实的接口地址 filePath: file.url, name: 'file', header: options.header, success(res) { if (typeof(res.data)=='string') { let _res =JSON.parse(res.data) if (_res.code==200) { resolve(_res.result) } }else{ if (res.data==200) { resolve(res.data) } } console.log(res); }, }); }) } export function request( url, method = 'get', data = {}, options = {} ) { return new Promise((resolve, reject) => { const { token } = wx.getStorageSync('userInfo') || {} options = Object.assign(defaultOptions, options) if (is401) reject() if (options.loading) wx.showToast({ title: 'loading...', icon: 'none' }) if (token) { options.header['X-Access-Token'] = token options.header['Authorization'] = token } wx.request({ url: `https://${baseUrl}/hisun-boot${url}`, method: method.toLocaleUpperCase(), data: data, header: options.header, timeout: options.timeout, // 成功回调 success: res => { console.log('request::success', res); wx.hideToast() if (res?.data?.code === 401) { is401 = true wx.showToast({ title: '登录过期, 请重新登录!', icon: 'error', mask: true, duration: 2000, complete() { is401 = false wx.clearStorageSync() wx.reLaunch({ url: '/pages/may/index', }) } }) } else if ([200, 0].includes(res?.data?.code)) { options.success && wx.showToast({ title: '提交成功!', icon: 'success', duration: 1000 }) resolve(res.data.result) } else { options.errorMessage && wx.showToast({ title: res?.data?.message || '网络错误!', icon: 'error', duration: 1000 }) reject() } }, // 错误回调 fail: err => { console.log('request::error', err); wx.hideToast() options.errorMessage && wx.showToast({ title: err.errMsg || '网络错误!', icon: 'error', duration: 1000 }) reject() }, }); }) }