张诺
7 小时以前 37a9da0f73da9b69e7d1c8488673520821e90b72
src/views/productionManagement/workOrder/index.vue
@@ -304,7 +304,7 @@
</template>
<script setup>
  import { onMounted, ref, nextTick } from "vue";
  import { onMounted, ref, nextTick, computed } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import dayjs from "dayjs";
  import {
@@ -318,6 +318,85 @@
  import { getCurrentInstance, reactive, toRefs } from "vue";
  import FilesDia from "./components/filesDia.vue";
  const { proxy } = getCurrentInstance();
  const currentUserId = ref("");
  const currentUserName = ref("");
  const ensureCurrentUser = async () => {
    if (currentUserId.value) return;
    try {
      const res = await getUserProfile();
      if (res?.code === 200) {
        currentUserId.value = String(res?.data?.userId ?? "");
        currentUserName.value = String(res?.data?.nickName ?? "");
      }
    } catch (err) {
      console.error("获取用户信息失败", err);
    }
  };
  const normalizeArray = (val) => {
    if (val === null || val === undefined) return [];
    if (Array.isArray(val)) return val;
    if (typeof val === "string") {
      return val
        .split(/[,,;;\s]+/g)
        .map((s) => s.trim())
        .filter(Boolean);
    }
    return [val];
  };
  const isCurrentUserReportWorker = (row) => {
    const uid = String(currentUserId.value || "");
    if (!uid) return false;
    if (!row) return false;
    const candidateIds = [
      row.reportUserIds,
      row.reportWorkerIds,
      row.userIds,
      row.userIdList,
      row.reportUserId,
      row.userId,
    ]
      .flatMap((v) => normalizeArray(v))
      .map((v) => String(v))
      .filter(Boolean);
    if (candidateIds.includes(uid)) return true;
    const candidateNames = [
      row.userNames,
      row.reportUserNames,
      row.reportWorkerNames,
      row.userName,
    ]
      .flatMap((v) => normalizeArray(v))
      .map((v) => String(v))
      .filter(Boolean);
    if (currentUserName.value && candidateNames.includes(currentUserName.value)) {
      return true;
    }
    if (Array.isArray(row.reportWorkerList)) {
      const list = row.reportWorkerList
        .map((item) => String(item?.userId ?? item?.id ?? ""))
        .filter(Boolean);
      if (list.includes(uid)) return true;
      const nameList = row.reportWorkerList
        .map((item) => String(item?.userName ?? item?.nickName ?? ""))
        .filter(Boolean);
      if (currentUserName.value && nameList.includes(currentUserName.value)) return true;
    }
    return false;
  };
  const canOperateByReportWorker = computed(() => {
    return (row) => isCurrentUserReportWorker(row);
  });
  const tableColumn = ref([
    {
@@ -374,6 +453,11 @@
      width: "140",
    },
    {
      label: "报工人",
      prop: "userNames",
      width: "140",
    },
    {
      label: "计划开始时间",
      prop: "planStartTime",
      width: "140",
@@ -406,7 +490,9 @@
            handleEdit(row);
          },
          // 当需求数量等于完成数量的时候,按钮不可点击
          disabled: row => Number(row?.planQuantity) === Number(row?.completeQuantity),
          disabled: row =>
            Number(row?.planQuantity) === Number(row?.completeQuantity) ||
            !canOperateByReportWorker.value(row),
        },
        {
          name: "流转卡",
@@ -425,7 +511,10 @@
          clickFun: row => {
            showReportDialog(row);
          },
          disabled: row => Number(row?.planQuantity) <= Number(row?.completeQuantity) || row.planQuantity <= 0,
          disabled: row =>
            Number(row?.planQuantity) <= Number(row?.completeQuantity) ||
            row.planQuantity <= 0 ||
            !canOperateByReportWorker.value(row),
        },
        // {
        //   name:"审核",
@@ -737,6 +826,10 @@
  };
  const handleEdit = row => {
    if (!isCurrentUserReportWorker(row)) {
      ElMessage.warning("当前用户不是该工单的报工人,无法编辑");
      return;
    }
    editrow.value = JSON.parse(JSON.stringify(row));
    editDialogVisible.value = true;
  };
@@ -756,6 +849,10 @@
  };
  const showReportDialog = row => {
    if (!isCurrentUserReportWorker(row)) {
      ElMessage.warning("当前用户不是该工单的报工人,无法报工");
      return;
    }
    currentReportRowData.value = row;
    reportForm.planQuantity = row.planQuantity - row.completeQuantity;
    reportForm.quantity =
@@ -773,17 +870,10 @@
    nextTick(() => {
      reportFormRef.value?.clearValidate();
    });
    // 获取当前登录用户信息,设置为默认选中
    getUserProfile()
      .then(res => {
        if (res.code === 200) {
          reportForm.userId = res.data.userId;
          reportForm.userName = res.data.nickName;
        }
      })
      .catch(err => {
        console.error("获取用户信息失败", err);
      });
    ensureCurrentUser().then(() => {
      reportForm.userId = currentUserId.value;
      reportForm.userName = currentUserName.value;
    });
    reportDialogVisible.value = true;
  };
@@ -925,6 +1015,7 @@
  }
  onMounted(() => {
    ensureCurrentUser();
    getList();
    getUserList();
  });