li.shaoyi 2 år sedan
förälder
incheckning
7720c15cc8

+ 6 - 6
src/business/login/index.ts

@@ -25,7 +25,7 @@ export function useLogin() {
         DeviceID: ''
     })
 
-    const queryBaseData = async () => {
+    const loadBaseData = async () => {
         await service.onReady() // 等待服务初始化
         await Promise.all([
             errorInfoStore.actions.getErrorInfoList(),
@@ -33,7 +33,7 @@ export function useLogin() {
         ])
     }
 
-    const queryLoginData = async () => {
+    const loadUserData = async () => {
         await checkToken() // 令牌校验
         await userStore.actions.getUserData()
         futuresStore.actions.getGoodsList()
@@ -54,7 +54,7 @@ export function useLogin() {
                 localStorage.setItem('thj_loginId', formData.LoginID) // 记住登录ID
             }
         })
-        await queryLoginData()
+        await loadUserData()
         eventBus.$emit('LoginNotify') // 登录成功通知
     }
 
@@ -63,7 +63,7 @@ export function useLogin() {
         logining.value = true
         try {
             // 等待加载业务数据
-            await queryBaseData()
+            await loadBaseData()
             // 自动登录
             if (autoLogin) {
                 const encryptedData = localData.getValue('autoLoginEncryptedData')
@@ -77,7 +77,7 @@ export function useLogin() {
                     }
                 }
             } else if (token.value) {
-                await queryLoginData()
+                await loadUserData()
             }
         } finally {
             logining.value = false
@@ -89,7 +89,7 @@ export function useLogin() {
         logining.value = true
         try {
             const params = { ...formData }
-            await queryBaseData()
+            await loadBaseData()
             await queryLoginId({
                 data: {
                     username: formData.LoginID

+ 2 - 0
src/packages/mobile/router/index.ts

@@ -1,5 +1,6 @@
 import { createWebHashHistory, RouteRecordRaw } from "vue-router";
 import { loginStore } from "@/stores";
+import { clearPending } from "@/services/http/pending";
 import service from "@/services";
 import Page from "@mobile/components/layouts/page/index.vue";
 import animateRouter from "./animateRouter";
@@ -493,6 +494,7 @@ const router = animateRouter.create({
 
 // 路由跳转拦截
 router.beforeEach((to, from, next) => {
+  clearPending()
   // 判断服务是否加载完成
   if (service.isReady) {
     if (to.meta.ignoreAuth || token.value) {

+ 4 - 4
src/services/api/account/index.ts

@@ -35,28 +35,28 @@ export function queryAccountMenu(params: HttpParams<{ rsp: Model.UserRoutes[] }>
  * 查询登录ID
  */
 export function queryLoginId(params: HttpParams<{ req: { username: string }, rsp: string }>) {
-    return httpRequest('/User/GetLoginID', 'get', params);
+    return httpRequest('/User/GetLoginID', 'get', params, 'GetLoginID');
 }
 
 /**
  * 查询登录账户配置信息
  */
 export function queryLoginData(params: HttpParams<{ req: Model.LoginQueryReq, rsp: Model.LoginQueryRsp }>) {
-    return httpRequest('/User/LoginQuery', 'get', params);
+    return httpRequest('/User/LoginQuery', 'get', params, 'LoginQuery');
 }
 
 /**
  * 获取用户账号信息
  */
 export function queryUserAccount(params: HttpParams<{ req: { userID: number }, rsp: Model.UserAccount }>) {
-    return httpRequest('/User/GetUserAccount', 'get', params);
+    return httpRequest('/User/GetUserAccount', 'get', params, 'GetUserAccount');
 }
 
 /**
  * 查询资金账户信息
  */
 export function queryTaAccounts(params: HttpParams<{ req: Model.TaAccountsReq, rsp: Model.TaAccountsRsp[] }>) {
-    return httpRequest('/TaAccount/GetTaAccounts', 'get', params);
+    return httpRequest('/TaAccount/GetTaAccounts', 'get', params, 'GetTaAccounts');
 }
 
 /**

+ 2 - 2
src/services/api/common/index.ts

@@ -61,14 +61,14 @@ export function queryImageConfigs(params: HttpParams<{ req: Model.ImageConfigsRe
  * 查询所有枚举信息
  */
 export function queryAllEnums(params: HttpParams<{ req: Model.EnumReq, rsp: Model.EnumRsp[] }>) {
-    return httpRequest('/Common/GetAllEnums', 'get', params);
+    return httpRequest('/Common/GetAllEnums', 'get', params, 'GetAllEnums');
 }
 
 /**
  * 获取数据库错误信息
  */
 export function queryErrorInfos(params: HttpParams<{ req: Model.ErrorInfosReq, rsp: Model.ErrorInfosRsp[] }>) {
-    return httpRequest('/Common/QueryErrorInfos', 'get', params);
+    return httpRequest('/Common/QueryErrorInfos', 'get', params, 'QueryErrorInfos');
 }
 
 /**

+ 2 - 2
src/services/api/goods/index.ts

@@ -12,14 +12,14 @@ export function queryGoodsList(params: HttpParams<{ rsp: Model.GoodsRsp[] }>) {
  * 查询企业风管期货商品信息
  */
 export function queryErmcpGoods(params: HttpParams<{ req: Model.GoodsReq, rsp: Model.GoodsRsp[] }>) {
-    return httpRequest('/Ermcp/GetErmcpGoods', 'get', params);
+    return httpRequest('/Ermcp/GetErmcpGoods', 'get', params, 'GetErmcpGoods');
 }
 
 /**
  * 获取商品盘面信息
  */
 export function queryQuoteDay(params: HttpParams<{ req: Model.QuoteDayReq, rsp: Model.QuoteDayRsp[] }>) {
-    return httpRequest('/Quote/QueryQuoteDay', 'get', params);
+    return httpRequest('/Quote/QueryQuoteDay', 'get', params, 'QueryQuoteDay');
 }
 
 /**

+ 14 - 12
src/services/http/index.ts

@@ -58,45 +58,46 @@ const httpService = new (class {
                         console.error(err)
                     }
                     // 异常提示待优化
-                    return Promise.reject(msg || message || '服务器异常,请稍后再试')
+                    return Promise.reject(msg || message)
                 }
             )
         }
         return Promise.resolve(this.axiosInstance)
     }
 
-    private request = async (url: string, method: Method, params?: unknown) => {
+    private request = async (url: string, method: Method, payload?: unknown) => {
         const requestConfig: AxiosRequestConfig = {
             url,
             method,
         }
-        if (params instanceof Object) {
+        if (payload instanceof Object) {
             if (['post', 'POST', 'put', 'PUT', 'patch', 'PATCH'].includes(method)) {
-                requestConfig.data = params
+                requestConfig.data = payload
             } else {
-                requestConfig.params = params
+                requestConfig.params = payload
             }
         } else {
-            requestConfig.url = url + (params ?? '')
+            requestConfig.url = url + (payload ?? '')
         }
         const instance = await this.init()
         return instance(requestConfig)
     }
 
-    commonRequest = async <T extends Payload>(url: string, method: Method, params: CommonParams<T>) => {
+    commonRequest = async <T extends Payload>(url: string, method: Method, params: CommonParams<T>, errMsg?: string) => {
         const { data, success, fail, complete } = params
         await this.request(url, method, data).then((res) => {
             const data = res.data as T['rsp']
             success && success(data)
         }).catch((err) => {
-            fail && fail(err)
-            return Promise.reject(err)
+            const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '服务异常,请稍后重试')
+            fail && fail(msg)
+            return Promise.reject(msg)
         }).finally(() => {
             complete && complete()
         })
     }
 
-    httpRequest = async <T extends Payload>(url: string, method: Method, params: HttpParams<T>) => {
+    httpRequest = async <T extends Payload>(url: string, method: Method, params: HttpParams<T>, errMsg?: string) => {
         const { data, success, fail, complete } = params
         await this.request(url, method, data).then((res) => {
             const data = res.data as HttpResponse<T['rsp']>
@@ -115,8 +116,9 @@ const httpService = new (class {
                     return Promise.reject(data.msg)
             }
         }).catch((err) => {
-            fail && fail(err)
-            return Promise.reject(err)
+            const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '服务异常,请稍后重试')
+            fail && fail(msg)
+            return Promise.reject(msg)
         }).finally(() => {
             complete && complete()
         })

+ 21 - 22
src/services/http/pending/index.ts

@@ -1,46 +1,45 @@
-import axios, { AxiosRequestConfig } from 'axios';
-import qs from 'qs';
+import axios, { AxiosRequestConfig } from 'axios'
+import qs from 'qs'
 
 /**
  * 缓存请求列表,防止重复请求
  */
-const pending = new Map();
+const pending = new Map()
 
-/**
- * 添加请求
- */
-export function addPending(config: AxiosRequestConfig) {
-    const url = [
+const getUrl = (config: AxiosRequestConfig) => {
+    return [
         config.method,
         config.url,
         qs.stringify(config.params),
         qs.stringify(config.data),
-    ].join('&');
+    ].join('&')
+}
+
+/**
+ * 添加请求
+ */
+export function addPending(config: AxiosRequestConfig) {
+    const url = getUrl(config)
     config.cancelToken =
         config.cancelToken ||
         new axios.CancelToken((cancel) => {
             if (!pending.has(url)) {
                 //如果列表中不存在当前请求,则添加进去
-                pending.set(url, cancel);
+                pending.set(url, cancel)
             }
-        });
+        })
 }
 
 /**
  * 移除请求
  */
 export function removePending(config: AxiosRequestConfig) {
-    const url = [
-        config.method,
-        config.url,
-        qs.stringify(config.params),
-        qs.stringify(config.data),
-    ].join('&');
+    const url = getUrl(config)
     if (pending.has(url)) {
         //如果在列表中存在当前请求,需要取消当前请求,并且移除
-        const cancel = pending.get(url);
-        cancel(url);
-        pending.delete(url);
+        const cancel = pending.get(url)
+        cancel(url)
+        pending.delete(url)
     }
 }
 
@@ -49,7 +48,7 @@ export function removePending(config: AxiosRequestConfig) {
  */
 export function clearPending() {
     for (const [url, cancel] of pending) {
-        cancel(url);
+        cancel(url)
     }
-    pending.clear();
+    pending.clear()
 }