// 基础配置 const config = { // #ifdef APP-PLUS || H5 baseURL: 'https://app.mrrweb.com.cn', // APP和H5的基础URL // #endif // #ifdef MP-WEIXIN baseURL: 'https://app.mrrweb.com.cn', // 小程序的基础URL // #endif header: { 'content-type': 'application/json' }, timeout: 60000 // 超时时间 } let noTokenUrls = [''] // 请求拦截器 const requestInterceptor = (options) => { // 获取token const token = uni.getStorageSync('accessToken') if (token) { options.header = { ...options.header, 'Authorization': `Bearer ${token}` } } // #ifdef H5 // H5端跨域处理 options.header = { ...options.header, 'Access-Control-Allow-Origin': '*' } // #endif return options } // 响应拦截器 const responseInterceptor = (response,options,type) => { const { statusCode, data } = response // 请求成功 if (statusCode >= 200 && statusCode < 300) { return data } // 处理错误 if (statusCode === 401) { // token过期,跳转登录 if (uni.getStorageSync('refreshToken')) { return updateToken(options,type) }else { uni.removeStorageSync('accessToken') uni.removeStorageSync('refreshToken') // #ifdef H5 // H5端使用replace而不是navigateTo // uni.redirectTo({ // url: '/pages/login/login' // }) uni.reLaunch({ url: '/pages/login/login' }) // #endif // #ifdef APP-PLUS || MP-WEIXIN uni.reLaunch({ url: '/pages/login/login' }) // #endif return Promise.reject(new Error('未授权,请重新登录')) } } // 其他错误 uni.showToast({ title: data.message || '请求失败', icon: 'none' }) return Promise.reject(data) } // 请求方法 const request = (options,isUpdate) => { // 合并配置 options = { ...config, ...options, url: !isUpdate ? `${config.baseURL}${options.url}` : options.url } // 请求拦截 options = requestInterceptor(options) // 发起请求 return new Promise((resolve, reject) => { // #ifdef H5 // H5端添加loading if (!options.hideLoading) { uni.showLoading({ title: '加载中...' }) } // #endif uni.request({ ...options, success: (res) => { // #ifdef H5 if (!options.hideLoading) { uni.hideLoading() } // #endif resolve(responseInterceptor(res,options)) }, fail: (err) => { // #ifdef H5 if (!options.hideLoading) { uni.hideLoading() } // #endif // 网络错误处理 let errorMsg = '网络错误,请稍后重试' // #ifdef H5 if (err.errMsg.includes('timeout')) { errorMsg = '请求超时,请检查网络' } // #endif uni.showToast({ title: errorMsg, icon: 'none' }) reject(err) } }) }) } const uploadPhoto = (options,isUpdate) => { options = { ...options, url: !isUpdate ? `${config.baseURL}${options.url}` : options.url } // 请求拦截 options = requestInterceptor(options) // 发起上传 return new Promise((resolve, reject) => { // #ifdef H5 // H5端显示上传进度 if (!options.hideLoading) { uni.showLoading({ title: '上传中...' }) } // #endif uni.uploadFile({ ...options, success: (res) => { const data = JSON.parse(res.data) resolve(responseInterceptor({ statusCode: res.statusCode, data },options,'uploadPhoto')) }, fail: (err) => { reject(err) } }) }) } // 上传文件 const uploadFile = (options,isUpdate) => { // 合并配置 options = { ...options, url: !isUpdate ? `${config.baseURL}${options.url}` : options.url } // 请求拦截 options = requestInterceptor(options) // 发起上传 return new Promise((resolve, reject) => { // #ifdef H5 // H5端显示上传进度 if (!options.hideLoading) { uni.showLoading({ title: '上传中...' }) } // #endif uni.uploadFile({ ...options, success: (res) => { // #ifdef H5 if (!options.hideLoading) { uni.hideLoading() } // #endif try { const data = JSON.parse(res.data) resolve(responseInterceptor({ statusCode: res.statusCode, data },options,'upload')) } catch (err) { reject('err',err) } }, fail: (err) => { // #ifdef H5 if (!options.hideLoading) { uni.hideLoading() } // #endif uni.showToast({ title: '上传失败,请稍后重试', icon: 'none' }) reject(err) } }) }) } // 更新token const updateToken = async (options,type) => { // 判断距离刷新token过期还剩 4个小时 try { const res = await request({ url: '/user/refreshtoken', data: { refresh_token: uni.getStorageSync('refreshToken'), deviceid: getApp().globalData.deviceid }, method: 'POST' }) if(res.state == 1) { // 2. 保存到本地 uni.setStorageSync('accessToken',res.access_token) uni.setStorageSync('refreshToken',res.refresh_token) // 3. 重发请求 if(type == 'upload') { return uploadFile(options,true) }else if(type == 'uploadPhoto') { return uploadPhoto(options,true) }else { return request(options,true) } }else if(res.state == 2) { // 清除token uni.removeStorageSync('accessToken') uni.removeStorageSync('refreshToken') uni.reLaunch({ url: '/pages/login/login' }) } } catch (error) { // 清除token uni.removeStorageSync('accessToken') uni.removeStorageSync('refreshToken') uni.reLaunch({ url: '/pages/login/login' }) return Promise.reject(error) } } // 导出请求方法 export default { // GET请求 get(url, data = {}, options = {}) { return request({ url, data, method: 'GET', ...options }) }, // POST请求 post(url, data = {}, options = {}) { return request({ url, data, method: 'POST', ...options }) }, // PUT请求 put(url, data = {}, options = {}) { return request({ url, data, method: 'PUT', ...options }) }, // DELETE请求 delete(url, data = {}, options = {}) { return request({ url, data, method: 'DELETE', ...options }) }, // 上传图片 uploadImage(url, filePath, formData = {}, options = {}) { return uploadPhoto({ url, filePath, name: 'file', ...formData, ...options }) }, // 上传视频 uploadVideo(url, filePath, name, formData, options = {}) { return uploadFile({ url: url, filePath, name: name, formData, ...options }) }, // 上传分片视频 uploadTask(url, filePath, name, formData, options = {}) { return uploadFile({ url: url, // 微信/App使用文件路径 // #ifdef MP-WEIXIN || APP-PLUS filePath: filePath, // #endif // H5使用File对象 // #ifdef H5 file: filePath, // #endif name: name, formData, ...options }) } }