zhangwei
2025-08-22 85c54d88f139096614aea4b06f2166cae27729d7
src/views/login/index.vue
@@ -1,4 +1,4 @@
<script setup lang="ts">
<script setup lang="tsx">
import Motion from "./utils/motion";
import { useRouter } from "vue-router";
import { message } from "@/utils/message";
@@ -7,15 +7,26 @@
  reactive,
  computed,
  ref,
  Ref,
  h,
  onMounted,
  defineAsyncComponent,
  onUnmounted,
  watch
} from "vue";
import { debounce } from "@pureadmin/utils";
import { debounce, storageLocal } from "@pureadmin/utils";
import { useNav } from "@/layout/hooks/useNav";
import { useEventListener } from "@vueuse/core";
import type { FormInstance } from "element-plus";
import {
  ElForm,
  ElFormItem,
  ElRadioGroup,
  ElRadio,
  ElRow,
  ElTag,
  ElCol
} from "element-plus";
import { useLayout } from "@/layout/hooks/useLayout";
import { useUserStoreHook } from "@/store/modules/user";
import { initRouter, getTopMenu } from "@/router/utils";
@@ -25,11 +36,6 @@
import { useRoute } from "vue-router";
const route = useRoute();
// import dayIcon from "@/assets/svg/day.svg?component";
// import darkIcon from "@/assets/svg/dark.svg?component";
import Lock from "~icons/ri/lock-fill";
import User from "~icons/ri/user-3-fill";
import { captcha, phoneNumberCode, exRole } from "@/api/register/index";
defineOptions({
@@ -56,6 +62,12 @@
  state.expirySeconds = res.result?.expirySeconds;
  state.ruleForm.codeId = res.result?.id;
};
import {
  addDialog,
  closeDialog,
  updateDialog,
  closeAllDialog
} from "@/components/ReDialog";
const state = reactive({
  isShowPassword: false,
  ruleForm: {
@@ -107,50 +119,22 @@
  expirySeconds: 60, // 验证码过期时间
  phoneSeconds: 0, // 手机验证码倒计时
  roleList: [],
  myEnterpriseList: [],
  nowRole: {}
});
let secondCode = "";
let secondId: Ref<string | number | boolean> = ref("");
// 验证码过期计时器
let timer: any = null;
let phonetimer: any = null;
// 页面初始化
onMounted(async () => {
  // 若URL带有Token参数(第三方登录)
  // if (accessToken) await saveTokenAndInitRoutes(accessToken);
  // watch(
  //   () => themeConfig.value.isLoaded,
  //   isLoaded => {
  //     if (isLoaded) {
  // 获取登录配置
  // state.hideTenantForLogin = themeConfig.value.hideTenantForLogin ?? true;
  // state.secondVerEnabled = themeConfig.value.secondVer ?? true;
  // state.captchaEnabled = themeConfig.value.captcha ?? true;
  // 获取验证码
  getCaptcha();
  exRole().then(res => {
    state.roleList = res.result;
    const role = route.query;
    if (role.code) {
      state.nowRole = role;
    } else {
      state.nowRole = state.roleList[0];
    }
    state.ruleForm.exRoleCode = state.nowRole.code;
  });
  // 注册验证码过期计时器
  // if (state.captchaEnabled) {
  timer = setInterval(() => {
    if (state.expirySeconds > 0) state.expirySeconds -= 1;
  }, 1000);
  // }
  // }
  // },
  // { immediate: true }
  // );
});
// 页面卸载
onUnmounted(() => {
@@ -160,6 +144,125 @@
  clearInterval(phonetimer);
  phonetimer = null;
});
const openDialog = () => {
  addDialog({
    width: "30%",
    title: "选择登录公司",
    contentRenderer: () =>
      h(
        ElForm,
        {
          ref: ruleFormRef,
          model: state.ruleForm,
          rules: loginRules,
          size: "large"
        },
        {
          default: () => {
            return [
              h(
                ElFormItem,
                { prop: "exRoleCode", label: "" },
                {
                  default: () => {
                    return [
                      h(
                        ElRadioGroup,
                        {
                          modelValue: secondId.value,
                          "onUpdate:modelValue": val => (secondId.value = val)
                        },
                        // ElRadioGroup 的内容是循环生成的,用函数包裹
                        () =>
                          state.myEnterpriseList.map(item =>
                            h(
                              ElRow,
                              { style: { width: "100%" } },
                              {
                                default: () => {
                                  return [
                                    h(
                                      ElCol,
                                      { span: 24 },
                                      {
                                        default: () => {
                                          return [
                                            h(
                                              ElRadio,
                                              {
                                                key: item.customerUserID,
                                                value: item.customerUserID
                                              },
                                              // ElRadio 的内容是动态的,用函数包裹
                                              () => [
                                                // 文本直接返回(不要嵌套函数)
                                                item.enterpriseName,
                                                item.isManger
                                                  ? h(
                                                      ElTag,
                                                      {
                                                        type: "primary",
                                                        size: "small",
                                                        style: {
                                                          marginLeft: "8px",
                                                          alignSelf: "center"
                                                        }
                                                      },
                                                      // ElTag 的内容是静态文本,直接返回
                                                      {
                                                        default: () => "管理员"
                                                      }
                                                    )
                                                  : null
                                              ]
                                            )
                                          ];
                                        }
                                      }
                                    )
                                  ];
                                }
                              }
                            )
                          )
                      )
                    ];
                  }
                }
              )
            ];
          }
        }
      ),
    // jsx 语法 (注意在.vue文件启用jsx语法,需要在script开启lang="tsx")
    closeCallBack: ({ options, args }) => {
      if (args?.command === "cancel") {
        // 您点击了取消按钮
      } else if (args?.command === "sure") {
        let obj = {
          phone: state.ruleForm.phone,
          code: secondCode,
          id: secondId.value
        };
        useUserStoreHook()
          .loginByUsername(obj)
          .then(res => {
            if (res?.code == 200) {
              message("登录成功!", { type: "success" });
              router.replace({
                path: "/Index"
              });
            } else {
              message(res?.message, { type: "warning" });
            }
          });
      } else {
      }
    }
  });
};
// 点击登录
const onLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(valid => {
@@ -176,44 +279,35 @@
          if (res.code == 200) {
            // 获取后端路由
            return initRouter().then(() => {
              if (res.result.exRoles.length == 0) {
                disabled.value = true;
              let obj = res.result;
              if (obj.theLastLogo) {
                router
                  .replace({
                    name: "RegisterNav",
                    query: { code: state.ruleForm.exRoleCode }
                    path: "/Index"
                  })
                  .then(() => {
                    message("登录成功", { type: "success" });
                  })
                  .finally(() => (disabled.value = false));
              } else if (res.result.exRoles.length > 0) {
                let data = res.result.exRoles.find(item => {
                  return item.code == state.ruleForm.exRoleCode;
                });
                console.log(data, "------------");
                if (!data) {
                  router.replace({
                    name: "RegisterNav",
                    query: { code: state.ruleForm.exRoleCode }
                  });
                } else if (data?.hasFlsh) {
                  router.replace("index");
                } else {
                  router.replace("mine");
                }
              } else {
                state.myEnterpriseList = obj.customerExs;
                secondCode = obj.code;
                openDialog();
              }
              useUserStoreHook().getCusExtendInfo();
            });
          } else {
            message(res?.message || "登录失败", { type: "error" });
          }
        })
        .finally(() => (loading.value = false));
        .finally(() => {
          loading.value = false;
          disabled.value = false;
        });
    }
  });
};
// 发送手机验证码
const sendValidationCode = async () => {
  if (!state.ruleForm.phone) {
    return message("请先输入手机号", { type: "warning" });
@@ -251,26 +345,12 @@
    <div class="wave">
      <img width="400px" :src="logo1" class="logo1" />
    </div>
    <!-- <img :src="bg" class="wave" /> -->
    <!-- <div class="flex-c absolute right-5 top-3"> -->
    <!-- 主题 -->
    <!-- <el-switch
        v-model="dataTheme"
        inline-prompt
        :active-icon="dayIcon"
        :inactive-icon="darkIcon"
        @change="dataThemeChange"
      />
    </div> -->
    <div class="login-container">
      <div class="img">
        <!-- <component :is="toRaw(illustration)" /> -->
      </div>
      <div class="img" />
      <div class="login-box">
        <div class="login-form">
          <!-- <avatar class="avatar" /> -->
          <Motion>
            <h2 class="logintitle">{{ state.nowRole.name }}登录</h2>
            <h2 class="logintitle">登录</h2>
          </Motion>
          <el-form
@@ -279,18 +359,6 @@
            :rules="loginRules"
            size="large"
          >
            <!-- <Motion :delay="150">
              <el-form-item prop="exRoleCode">
                <el-radio-group v-model="state.ruleForm.exRoleCode">
                  <el-radio
                    v-for="item in state.roleList"
                    :key="item.id"
                    :value="item.code"
                    >{{ item.name }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </Motion> -->
            <Motion :delay="100">
              <el-form-item
                :rules="[
@@ -343,16 +411,6 @@
                  </div>
                </el-col>
              </el-form-item>
              <!-- <el-form-item prop="password">
                <el-input
                  v-model="ruleForm.password"
                  clearable
                  show-password
                  placeholder="密码"
                  :prefix-icon="useRenderIcon(Lock)"
                />
              </el-form-item> -->
            </Motion>
            <Motion :delay="150">
              <el-form-item prop="phoneVCode">
@@ -366,7 +424,7 @@
                      <span
                        id="suffix-span-2"
                        ref="spanRef"
                        @click="sendValidationCode(state.ruleForm.phone)"
                        @click="sendValidationCode"
                      >
                        获取验证码
                      </span>