Procházet zdrojové kódy

fix: 修复移动端工作量回填失败问题

qiny před 1 rokem
rodič
revize
e1e754758b

+ 9 - 3
client_h5/src/pages/myLogs/Daily/index.vue

@@ -47,6 +47,7 @@ import { getUserInfo } from "@/utils/tools";
 import { http } from "../http";
 import { IReport } from "../interface";
 import { onSubmitCheck } from "../service";
+import PubsubService from "@/utils/PubsubService";
 interface FormData {
   reportContent: string; // 内容
   weeklyWorkloadList: {
@@ -105,7 +106,7 @@ onBeforeRouteLeave((ev: any) => {
   if (ev.name == "LogsDetail") return;
   // 如果日志内容不为空则触发暂存,否则直接退出
   if (formData.reportContent?.length > 0) {
-    onSubmit(true);
+    onSubmit(true, true);
   }
 });
 
@@ -123,6 +124,10 @@ const receiveData = (dailyDetail: any) => {
       projectId: work.projectId,
       workTime: work.workTime,
     }));
+    PubsubService.publish(
+      "callback-full-project-select",
+      formData.weeklyWorkloadList
+    );
     clearTimeout(timer);
   }, 1000);
 };
@@ -180,7 +185,7 @@ const projectChange = (data: any) => {
 
 // 提交
 const { replace } = useRouter();
-const onSubmit = async (isTemp: boolean) => {
+const onSubmit = async (isTemp: boolean, isRouteLeave?: boolean) => {
   const userInfo = getUserInfo();
   const params: IReport = {
     ...formData,
@@ -193,7 +198,7 @@ const onSubmit = async (isTemp: boolean) => {
   };
   // 提交前校验
   const submitCheck = onSubmitCheck(params);
-  if (!submitCheck.success) {
+  if (!submitCheck.success && !isRouteLeave) {
     showToast({
       message: submitCheck.msg,
       position: "top",
@@ -201,6 +206,7 @@ const onSubmit = async (isTemp: boolean) => {
     return;
   }
   const result: any = await http.submitReport(params);
+  if (isRouteLeave) return; // 如果是退出,则不提示暂存
   const type = isTemp ? "暂存" : "发送";
   if (result.msg == "success") {
     showSuccessToast(`${type}成功`);

+ 9 - 3
client_h5/src/pages/myLogs/Weekly/index.vue

@@ -53,6 +53,7 @@ import { getUserInfo } from "@/utils/tools";
 import { http } from "../http";
 import { IReport } from "../interface";
 import { onSubmitCheck, setWorkDayListToWeek } from "../service";
+import PubsubService from "@/utils/PubsubService";
 
 interface FormData {
   reportContent: string; // 内容
@@ -132,7 +133,7 @@ onBeforeRouteLeave((ev: any) => {
   if (ev.name == "LogsDetail") return;
   // 如果日志内容不为空则触发暂存,否则直接退出
   if (formData.reportContent?.length > 0) {
-    onSubmit(true);
+    onSubmit(true, true);
   }
 });
 
@@ -152,6 +153,10 @@ const receiveData = (weekDetail: any) => {
       projectId: work.projectId,
       workTime: work.workTime,
     }));
+    PubsubService.publish(
+      "callback-full-project-select",
+      formData.weeklyWorkloadList
+    );
     clearTimeout(timer);
   }, 1000);
 };
@@ -167,7 +172,7 @@ const projectChange = (data: any) => {
 
 // 提交
 const { replace } = useRouter();
-const onSubmit = async (isTemp: boolean) => {
+const onSubmit = async (isTemp: boolean, isRouteLeave?: boolean) => {
   const date = thisWeek.value.split(" ~ ");
   const userInfo = getUserInfo();
   const params: IReport = {
@@ -181,7 +186,7 @@ const onSubmit = async (isTemp: boolean) => {
   };
   // 提交前校验
   const submitCheck = onSubmitCheck(params);
-  if (!submitCheck.success) {
+  if (!submitCheck.success && !isRouteLeave) {
     showToast({
       message: submitCheck.msg,
       position: "top",
@@ -189,6 +194,7 @@ const onSubmit = async (isTemp: boolean) => {
     return;
   }
   const result: any = await http.submitReport(params);
+  if (isRouteLeave) return; // 如果是退出,则不提示暂存
   const type = isTemp ? "暂存" : "发送";
   if (result.msg == "success") {
     showSuccessToast(`${type}成功`);

+ 31 - 5
client_h5/src/pages/myLogs/components/ProjectList.vue

@@ -31,8 +31,9 @@
 /**
  * @description 接收人组件
  */
-import { ref, onMounted } from "vue";
+import { ref, onMounted, watch } from "vue";
 import { http } from "../http";
+import PubsubService from "@/utils/PubsubService";
 interface IProp {
   type: "weekly" | "daily";
   onChange: (data: any) => any;
@@ -42,6 +43,29 @@ interface IProp {
 const { type, onChange } = defineProps<IProp>();
 
 const dataSource = ref<any[]>([]);
+const filteredDataSource = ref<any[]>([]);
+const searchQuery = ref<string>("");
+const active = ref(0);
+const activeProject = ref(null);
+
+// 回填工作量
+const callFullSelect = (defaultData: any[]) => {
+  if (defaultData && defaultData.length) {
+    dataSource.value = dataSource.value
+      .map((item) => {
+        const defaultItem = defaultData.find(
+          (dItem) => dItem.projectId === item.projectId
+        );
+        if (defaultItem) {
+          item.workTime = defaultItem.workTime;
+          item.workTimeName = `${defaultItem.workTime}小时`;
+        }
+        return item;
+      })
+      .sort((a, b) => b.workTime - a.workTime);
+    filteredDataSource.value = dataSource.value;
+  }
+};
 
 onMounted(async () => {
   // 获取项目列表
@@ -56,8 +80,14 @@ onMounted(async () => {
       };
     }) ?? [];
   filteredDataSource.value = dataSource.value;
+  // 接收回填事件
+  PubsubService.subscribe("callback-full-project-select", (params) => {
+    callFullSelect(params);
+  });
 });
 
+// watch(() => defaultData, callFullSelect, { immediate: true, deep: true });
+
 // 生成工作时长数据
 const workTimeList = (type: string) => {
   const arrays =
@@ -71,8 +101,6 @@ const workTimeList = (type: string) => {
 };
 
 // 过滤
-const searchQuery = ref<string>("");
-const filteredDataSource = ref<any[]>([]);
 const setFilteredDataSource = () => {
   filteredDataSource.value = dataSource.value;
   if (!searchQuery.value) {
@@ -88,8 +116,6 @@ const setFilteredDataSource = () => {
 watch(searchQuery, setFilteredDataSource);
 
 // 设置工作量
-const active = ref(0);
-const activeProject = ref(null);
 const projectTitme = (project: any) => {
   if (!project.workTimeName || project.workTime == "") {
     return project.projectName;

+ 53 - 0
client_h5/src/utils/PubsubService.ts

@@ -0,0 +1,53 @@
+/**
+ * @description 发布订阅对象
+ * 一个简单的发布者、订阅者模式,提供了发布、订阅、删除的方法
+ * from chinyan
+ */
+class PubsubService {
+  private topics: Record<string, ((params?: any) => void)[]> = {}
+
+  // 发布事件
+  publish(topic: string, data: any) {
+    if (!this.topics[topic]) return
+    this.topics[topic].forEach((fn) => fn(data))
+  }
+
+  // 订阅事件
+  subscribe(topic: string, callback: (params?: any) => void) {
+    if (!this.topics[topic]) {
+      this.topics[topic] = []
+    }
+
+    if (!this.topics[topic].includes(callback)) {
+      this.topics[topic].push(callback)
+    }
+
+    // 返回取消订阅的方法
+    return () => {
+      const index = this.topics[topic].indexOf(callback)
+      if (index !== -1) {
+        this.topics[topic].splice(index, 1)
+      }
+    }
+  }
+
+  // 清空所有订阅者
+  clearAllSub() {
+    this.topics = {}
+  }
+
+  // 清空某个主题的订阅者
+  clearSubsByTopic(topic: string) {
+    if (this.topics[topic]) {
+      this.topics[topic] = []
+    }
+  }
+
+  // 判断是否有某个主题的订阅者
+  hasSubsByTopic(topic: string) {
+    return this.topics[topic] && this.topics[topic].length > 0
+  }
+}
+
+// 实例化并导出单例
+export default new PubsubService()