1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
|
{"version":3,"file":"index.js","sources":["../../../../../src/sheep/request/index.js"],"sourcesContent":["/**\r\n * Shopro-request\r\n * @description api模块管理,loading配置,请求拦截,错误处理\r\n */\r\n\r\nimport Request from 'luch-request'\r\nimport { apiPath, baseUrl, tenantId } from '@/sheep/config'\r\nimport $store from '@/sheep/store'\r\nimport $platform from '@/sheep/platform'\r\nimport { showAuthModal } from '@/sheep/hooks/useModal'\r\nimport AuthUtil from '@/sheep/api/member/auth'\r\nimport { getTerminal } from '@/sheep/util/const'\r\n\r\nconst options = {\r\n // 显示操作成功消息 默认不显示\r\n showSuccess: false,\r\n // 成功提醒 默认使用后端返回值\r\n successMsg: '',\r\n // 显示失败消息 默认显示\r\n showError: true,\r\n // 失败提醒 默认使用后端返回信息\r\n errorMsg: '',\r\n // 显示请求时loading模态框 默认显示\r\n showLoading: true,\r\n // loading提醒文字\r\n loadingMsg: '加载中',\r\n // 需要授权才能请求 默认放开\r\n auth: false,\r\n // ...\r\n}\r\n\r\n// Loading全局实例\r\nconst LoadingInstance = {\r\n target: null,\r\n count: 0,\r\n}\r\n\r\n/**\r\n * 关闭loading\r\n */\r\nfunction closeLoading() {\r\n if (LoadingInstance.count > 0) LoadingInstance.count--\r\n if (LoadingInstance.count === 0) uni.hideLoading()\r\n}\r\n\r\n/**\r\n * @description 请求基础配置 可直接使用访问自定义请求\r\n */\r\nconst http = new Request({\r\n baseURL: baseUrl + apiPath,\r\n timeout: 8000,\r\n method: 'GET',\r\n header: {\r\n Accept: 'text/json',\r\n 'Content-Type': 'application/json;charset=UTF-8',\r\n platform: $platform.name,\r\n },\r\n // #ifdef APP-PLUS\r\n sslVerify: false,\r\n // #endif\r\n // #ifdef H5\r\n // 跨域请求时是否携带凭证(cookies)仅H5支持(HBuilderX 2.6.15+)\r\n withCredentials: false,\r\n // #endif\r\n custom: options,\r\n})\r\n\r\n/**\r\n * @description 请求拦截器\r\n */\r\nhttp.interceptors.request.use(\r\n (config) => {\r\n // 自定义处理【auth 授权】:必须登录的接口,则跳出 AuthModal 登录弹窗\r\n if (config.custom.auth && !$store('user').isLogin) {\r\n showAuthModal()\r\n return Promise.reject()\r\n }\r\n\r\n // 自定义处理【loading 加载中】:如果需要显示 loading,则显示 loading\r\n if (config.custom.showLoading) {\r\n LoadingInstance.count++\r\n LoadingInstance.count === 1 &&\r\n uni.showLoading({\r\n title: config.custom.loadingMsg,\r\n mask: true,\r\n fail: () => {\r\n uni.hideLoading()\r\n },\r\n })\r\n }\r\n\r\n // 增加 token 令牌、terminal 终端、tenant 租户的请求头\r\n const token = getAccessToken()\r\n if (token) {\r\n config.header.Authorization = token\r\n }\r\n config.header.terminal = getTerminal()\r\n\r\n config.header.Accept = '*/*'\r\n config.header['tenant-id'] = tenantId\r\n return config\r\n },\r\n (error) => {\r\n return Promise.reject(error)\r\n },\r\n)\r\n\r\n/**\r\n * @description 响应拦截器\r\n */\r\nhttp.interceptors.response.use(\r\n (response) => {\r\n // 约定:如果是 /auth/ 下的 URL 地址,并且返回了 accessToken 说明是登录相关的接口,则自动设置登陆令牌\r\n if (response.config.url.indexOf('/member/auth/') >= 0 && response.data?.data?.accessToken) {\r\n $store('user').setToken(response.data.data.accessToken, response.data.data.refreshToken)\r\n }\r\n\r\n // 自定处理【loading 加载中】:如果需要显示 loading,则关闭 loading\r\n response.config.custom.showLoading && closeLoading()\r\n\r\n // 自定义处理【error 错误提示】:如果需要显示错误提示,则显示错误提示\r\n if (response.data.code !== 0) {\r\n // 特殊:如果 401 错误码,则跳转到登录页 or 刷新令牌\r\n if (response.data.code === 401) {\r\n return refreshToken(response.config)\r\n }\r\n\r\n // 错误提
|