Pārlūkot izejas kodu

修改处理服务请求时候的loading和

huangbin 4 gadi atpakaļ
vecāks
revīzija
6fa4d9efda

+ 49 - 1
src/common/methods/request/resultInfo.ts

@@ -34,4 +34,52 @@ export function commonResultInfo(fn: Promise<any>, sign: ResultInfo, loading: Re
         }).finally(() => {
             loading.value = false
         })
-}
+}
+
+/**
+ * 控制 请求接口 打开与关闭loading
+ * @param fn 请求接口 函数
+ * @param param 请求接口 参数
+ * @param loading 
+ * @returns 
+ */
+export async function controlLoading(fn: Function, param: any, loading: Ref<boolean>): Promise<any> {
+    loading.value = true;
+    try {
+        return await fn(param)
+    } catch (err) {
+        Promise.resolve(err)
+    } finally {
+        loading.value = false;
+    }
+}
+
+/**
+ * 控制服务请求成功和失败的弹窗提示
+ * @param fn 请求接口 函数
+ * @param msgInfo [成功结果, 失败结果]
+ * @returns 
+ */
+export function controlResultInfo(fn: Promise<any>, msgInfo: [string, string] = ['成功', '失败']) {
+    const [sucInfo, errInfo] = msgInfo
+    return fn.then(res => {
+        message.success(sucInfo)
+        return res;
+    }).catch(err => {
+        console.warn(`${fn}调用失败: err`);
+        const result = errInfo + err
+        message.error(result)
+        Promise.reject(result)
+    })
+}
+/**
+ * 处理服务请求时候的loading和 成功和失败提示
+ * @param fn 
+ * @param param 
+ * @param loading 
+ * @param messageInfo 
+ * @returns 
+ */
+export function requestResultLoadingAndInfo(fn: Function, param: any, loading: Ref<boolean>, messageInfo: [string, string] = ['成功', '失败']) {
+    return controlResultInfo(controlLoading(fn, param, loading), messageInfo)
+}

+ 14 - 7
src/views/information/warehouse-info/compoments/add/index.vue

@@ -135,12 +135,15 @@
 import { defineComponent, PropType, ref, toRaw } from 'vue';
 import { closeModal } from '@/common/setup/modal/index';
 import { getAddress } from '@/services/go/adress';
-import { handleForm, FormState, warehouseApply } from '../setup';
+import { handleForm, FormState, warehouseApply, reqestAction } from '../setup';
 import { ValidateErrorEntity } from 'ant-design-vue/lib/form/interface';
 import { AllEnums } from '@/services/go/commonService/interface';
 import { WarehouseApplyReq } from '@/services/proto/warehouse/interface';
 import { ErmcpWareHouseInfo } from '@/services/go/ermcp/warehouse-info/interface';
 import { getWarehouseTypeEnumList } from '@/common/constants/enumsList';
+import { getSelectedAccountId } from '@/services/bus/account';
+import { addWarehouseApply, warehouseStateChangeReq } from '@/services/proto/warehouse';
+import { requestResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
 
 export default defineComponent({
     name: 'warehouse_info_btn_add',
@@ -175,12 +178,16 @@ export default defineComponent({
                         contactname: param.contactname, // string 联系人
                         contactnum: param.contactnum, // string 联系电话
                     };
-                    warehouseApply(reqParam, loading)
-                        .then((res) => {
-                            cancel();
-                            context.emit('refresh');
-                        })
-                        .catch((err) => {});
+                    requestResultLoadingAndInfo(addWarehouseApply, reqParam, loading, ['新建仓库成功', '新建仓库失败']).then(() => {
+                        cancel();
+                        context.emit('refresh');
+                    });
+                    // warehouseApply(reqParam, loading)
+                    //     .then((res) => {
+                    //         cancel();
+                    //         context.emit('refresh');
+                    //     })
+                    //     .catch((err) => {});
                     console.log('param', param);
                 })
                 .catch((error: ValidateErrorEntity<FormState>) => {

+ 25 - 0
src/views/information/warehouse-info/compoments/setup.ts

@@ -3,6 +3,7 @@ import { validateCommon } from "@/common/setup/validate";
 import { getSelectedAccountId } from "@/services/bus/account";
 import { addWarehouseApply, warehouseStateChangeReq } from "@/services/proto/warehouse";
 import { WarehouseApplyReq, WarehouseStateChangeReq } from "@/services/proto/warehouse/interface";
+import { message } from "ant-design-vue";
 import { RuleObject } from "ant-design-vue/lib/form/interface";
 import { reactive, Ref, ref } from "vue";
 import { warehouseApplyRequestResultMsg } from "../setup";
@@ -119,4 +120,28 @@ export function warehouseApply(param: WarehouseApplyReq, loading: Ref<boolean>)
     const result = addWarehouseApply(param)
     // 接口调用结果处理,提示成功或者失败信息,并关闭loading效果
     return commonResultInfo(result, sign, loading)
+}
+
+/**
+ * 
+ * @param fn 
+ * @param param 
+ * @param loading 
+ * @param messageInfo 
+ * @returns 
+ */
+export async function reqestAction(fn: Function, param: WarehouseApplyReq, loading: Ref<boolean>, messageInfo: [string, string]) {
+    loading.value = true;
+    const [successInfo, errInfo] = messageInfo;
+    try {
+        const result = await fn(param)
+        message.success(successInfo)
+        return Promise.resolve(result)
+    } catch (err) {
+        const info = errInfo + err
+        message.error(info)
+        return Promise.reject(err)
+    } finally {
+        loading.value = false;
+    }
 }