|
@@ -0,0 +1,47 @@
|
|
|
|
+import WdRequest from './request'
|
|
|
|
+import { platform } from '@/utils/platform'
|
|
|
|
+import { BASE_URL, TIME_OUT } from './config/index'
|
|
|
|
+// 使用封装的请求方法
|
|
|
|
+const request = new WdRequest({
|
|
|
|
+ baseUrl: BASE_URL,
|
|
|
|
+ timeout: TIME_OUT,
|
|
|
|
+ interceptor: {
|
|
|
|
+ requestSuccessFn: (config) => {
|
|
|
|
+ // console.log('全局请求成功拦截')
|
|
|
|
+ config.header = {
|
|
|
|
+ platform,
|
|
|
|
+ ...config.header,
|
|
|
|
+ }
|
|
|
|
+ // 添加token
|
|
|
|
+ return config
|
|
|
|
+ },
|
|
|
|
+ // 响应拦截
|
|
|
|
+ responseSuccessFn: (res) => {
|
|
|
|
+ // 状态码 2xx,参考 axios 的设计
|
|
|
|
+ if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
|
|
+ // 2.1 提取核心数据 res.data
|
|
|
|
+ return res.data
|
|
|
|
+ } else if (res.statusCode === 401) {
|
|
|
|
+ // 401错误 -> 清理用户信息,跳转到登录页
|
|
|
|
+ // userStore.clearUserInfo()
|
|
|
|
+ // uni.navigateTo({ url: '/pages/login/login' })
|
|
|
|
+ return Promise.reject(res)
|
|
|
|
+ } else {
|
|
|
|
+ // 其他错误 -> 根据后端错误信息轻提示
|
|
|
|
+ uni.showToast({
|
|
|
|
+ icon: 'none',
|
|
|
|
+ title: res?.data?.msg || '请求错误',
|
|
|
|
+ })
|
|
|
|
+ return Promise.reject(res)
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ responseErrorFn: (err) => {
|
|
|
|
+ uni.showToast({
|
|
|
|
+ icon: 'none',
|
|
|
|
+ title: '网络错误,换个网络试试',
|
|
|
|
+ })
|
|
|
|
+ return err
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+})
|
|
|
|
+export default request
|