refactor(utils): 提取请求默认参数到config文件
将分散在多个文件中的请求默认参数提取到config.js中统一管理 修改BASE_URL为测试服务器地址 修复授权页面跳转逻辑
Showing
3 changed files
with
21 additions
and
12 deletions
| 1 | import Taro from '@tarojs/taro' | 1 | import Taro from '@tarojs/taro' |
| 2 | import { routerStore } from '@/stores/router' | 2 | import { routerStore } from '@/stores/router' |
| 3 | -import BASE_URL from './config' | 3 | +import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config' |
| 4 | 4 | ||
| 5 | /** | 5 | /** |
| 6 | * 授权与回跳相关工具 | 6 | * 授权与回跳相关工具 |
| ... | @@ -112,7 +112,7 @@ export const refreshSession = async (options) => { | ... | @@ -112,7 +112,7 @@ export const refreshSession = async (options) => { |
| 112 | 112 | ||
| 113 | // 换取后端会话(服务端通过 Set-Cookie 返回会话信息) | 113 | // 换取后端会话(服务端通过 Set-Cookie 返回会话信息) |
| 114 | const response = await Taro.request({ | 114 | const response = await Taro.request({ |
| 115 | - url: `${BASE_URL}/srv/?a=openid&f=reserve&client_name=${encodeURIComponent('智慧西园寺')}`, | 115 | + url: `${BASE_URL}/srv/?a=openid&f=${encodeURIComponent(REQUEST_DEFAULT_PARAMS.f)}&client_name=${encodeURIComponent(REQUEST_DEFAULT_PARAMS.client_name)}`, |
| 116 | method: 'POST', | 116 | method: 'POST', |
| 117 | data: request_data, | 117 | data: request_data, |
| 118 | }) | 118 | }) |
| ... | @@ -211,7 +211,7 @@ export const navigateToAuth = (return_path) => { | ... | @@ -211,7 +211,7 @@ export const navigateToAuth = (return_path) => { |
| 211 | return | 211 | return |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | - // navigateTo 失败时(例如页面栈满),降级为 redirectTo | 214 | + // TAG: navigateTo 失败时(例如页面栈满),降级为 redirectTo |
| 215 | Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => { | 215 | Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => { |
| 216 | return Taro.redirectTo({ url: '/pages/auth/index' }) | 216 | return Taro.redirectTo({ url: '/pages/auth/index' }) |
| 217 | }) | 217 | }) |
| ... | @@ -294,12 +294,12 @@ export const handleSharePageAuth = async (options, callback) => { | ... | @@ -294,12 +294,12 @@ export const handleSharePageAuth = async (options, callback) => { |
| 294 | if (typeof callback === 'function') callback() | 294 | if (typeof callback === 'function') callback() |
| 295 | }, | 295 | }, |
| 296 | () => { | 296 | () => { |
| 297 | - // Taro.navigateTo({ url: '/pages/auth/index' }) | 297 | + Taro.navigateTo({ url: '/pages/auth/index' }) |
| 298 | } | 298 | } |
| 299 | ) | 299 | ) |
| 300 | return true | 300 | return true |
| 301 | } catch (error) { | 301 | } catch (error) { |
| 302 | - // Taro.navigateTo({ url: '/pages/auth/index' }) | 302 | + Taro.navigateTo({ url: '/pages/auth/index' }) |
| 303 | return false | 303 | return false |
| 304 | } | 304 | } |
| 305 | } | 305 | } | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2024-01-15 17:07:14 | 4 | + * @LastEditTime: 2026-01-08 16:42:38 |
| 5 | - * @FilePath: /meihuaApp/src/utils/config.js | 5 | + * @FilePath: /xyxBooking-weapp/src/utils/config.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| 8 | // TAG:服务器环境配置 | 8 | // TAG:服务器环境配置 |
| 9 | -// const BASE_URL = "https://oa-dev.onwall.cn"; // 测试服务器 | 9 | +const BASE_URL = "https://oa-dev.onwall.cn"; // 测试服务器 |
| 10 | -const BASE_URL = "https://oa.onwall.cn"; // 正式服务器 | 10 | +// const BASE_URL = "https://oa.onwall.cn"; // 正式服务器 |
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 接口默认公共参数(避免在多个文件里硬编码) | ||
| 14 | + * - f:业务模块标识 | ||
| 15 | + * - client_name:客户端标识(后端用于识别来源) | ||
| 16 | + */ | ||
| 17 | +export const REQUEST_DEFAULT_PARAMS = { | ||
| 18 | + f: 'reserve', | ||
| 19 | + client_name: '智慧西园寺', | ||
| 20 | +} | ||
| 11 | 21 | ||
| 12 | export default BASE_URL | 22 | export default BASE_URL | ... | ... |
| ... | @@ -15,7 +15,7 @@ import { refreshSession, saveCurrentPagePath, navigateToAuth } from './authRedir | ... | @@ -15,7 +15,7 @@ import { refreshSession, saveCurrentPagePath, navigateToAuth } from './authRedir |
| 15 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; | 15 | // import { ProgressStart, ProgressEnd } from '@/components/axios-progress/progress'; |
| 16 | // import store from '@/store' | 16 | // import store from '@/store' |
| 17 | // import { getToken } from '@/utils/auth' | 17 | // import { getToken } from '@/utils/auth' |
| 18 | -import BASE_URL from './config'; | 18 | +import BASE_URL, { REQUEST_DEFAULT_PARAMS } from './config'; |
| 19 | 19 | ||
| 20 | /** | 20 | /** |
| 21 | * 获取sessionid的工具函数 | 21 | * 获取sessionid的工具函数 |
| ... | @@ -43,8 +43,7 @@ const service = axios.create({ | ... | @@ -43,8 +43,7 @@ const service = axios.create({ |
| 43 | }) | 43 | }) |
| 44 | 44 | ||
| 45 | service.defaults.params = { | 45 | service.defaults.params = { |
| 46 | - f: 'reserve', | 46 | + ...REQUEST_DEFAULT_PARAMS, |
| 47 | - client_name: '智慧西园寺', | ||
| 48 | }; | 47 | }; |
| 49 | 48 | ||
| 50 | // request interceptor | 49 | // request interceptor | ... | ... |
-
Please register or login to post a comment