From c9434b9ec9750afcd2de321d1b7fd33d7a89be13 Mon Sep 17 00:00:00 2001 From: Ordinary Date: Sat, 3 Aug 2024 10:33:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(request):=20=E5=AE=9E=E7=8E=B0=E5=8A=A8?= =?UTF-8?q?=E6=80=81Terminal=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sheep/request/index.js | 18 +++++++++--------- sheep/util/const.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/sheep/request/index.js b/sheep/request/index.js index 700c7dda..50ac2dea 100644 --- a/sheep/request/index.js +++ b/sheep/request/index.js @@ -4,13 +4,12 @@ */ import Request from 'luch-request'; -import { baseUrl, apiPath, tenantId } from '@/sheep/config'; +import { apiPath, baseUrl, tenantId } from '@/sheep/config'; import $store from '@/sheep/store'; import $platform from '@/sheep/platform'; -import { - showAuthModal -} from '@/sheep/hooks/useModal'; +import { showAuthModal } from '@/sheep/hooks/useModal'; import AuthUtil from '@/sheep/api/member/auth'; +import { getTerminalEnumByUniPlatform } from '@/sheep/util/const'; const options = { // 显示操作成功消息 默认不显示 @@ -93,13 +92,14 @@ http.interceptors.request.use( // 增加 token 令牌、terminal 终端、tenant 租户的请求头 const token = getAccessToken(); if (token) { - config.header['Authorization'] = token; - } - // TODO 芋艿:特殊处理 + config.header['Authorization'] = token; + } + + const terminalType = uni.getSystemInfoSync().uniPlatform + config.header['terminal'] = getTerminalEnumByUniPlatform(terminalType); + config.header['Accept'] = '*/*'; config.header['tenant-id'] = tenantId; - config.header['terminal'] = '20'; - // config.header['Authorization'] = 'Bearer test247'; return config; }, (error) => { diff --git a/sheep/util/const.js b/sheep/util/const.js index e1d3ee08..4339ef90 100644 --- a/sheep/util/const.js +++ b/sheep/util/const.js @@ -1,3 +1,39 @@ +// ========== COMMON - 公共模块 ========== + +/** + * 与后端Terminal枚举一一对应 + */ +export const TerminalEnum = { + UNKNOWN: 0, // 未知, 目的:在无法解析到 terminal 时,使用它 + WECHAT_MINI_PROGRAM: 10, //微信小程序 + WECHAT_WAP: 11, // 微信公众号 + H5: 20, // H5 网页 + APP: 31, // 手机 App +}; + +/** + * 将Uniapp提供的平台转换为后端所需的Terminal值 + * @param platformType Uniapp提供的平台类型 + */ +export const getTerminalEnumByUniPlatform = (platformType) => { + let terminal; + // 与后端terminal枚举一一对应 + switch (platformType) { + case 'app': + terminal = TerminalEnum.APP; + break; + case 'web': + terminal = TerminalEnum.H5; + break; + case 'mp-weixin': + terminal = TerminalEnum.WECHAT_MINI_PROGRAM; + break; + default: + terminal = TerminalEnum.UNKNOWN; + } + return terminal; +}; + // ========== MALL - 营销模块 ========== import dayjs from "dayjs";