yangyin
2024-11-29 3c0f2cd6720032f83ab34daff947bf31f3bb96b3
src/layout/component/sidebar/components/MenuList.vue
@@ -37,100 +37,42 @@
         </div>
      </div>
   </div>
   <div class="pc-login items-center justify-center" style="display: flex" v-show="state.isShowLogin">
      <div class="login_box">
         <div class="sign_in">
            <i class="ywifont ywicon-guanbi closes" @click="handleClose"></i>
            <h1><span>登录 WI 水务智能</span></h1>
            <el-tabs v-model="state.activeLoginName" class="mt-[24px]" @tab-change="handleUserClick">
               <el-tab-pane label="账户密码登录" name="accountUser">
                  <el-form
                     ref="loginFormRef"
                     :model="state.loginForm"
                     :rules="loginRules"
                     class="demo-ruleForm mt-[24px] min-h-[140px]"
                     size="large"
                  >
                     <el-form-item label="账号" prop="account">
                        <el-input v-model="state.loginForm.account" clearable />
                     </el-form-item>
                     <el-form-item label="密码" prop="pwd">
                        <el-input v-model="state.loginForm.pwd" type="password" autocomplete="off" clearable />
                     </el-form-item>
                  </el-form>
               </el-tab-pane>
               <el-tab-pane label="手机号登录" name="phoneUser">
                  <el-form
                     ref="formPhoneRef"
                     :rules="loginPhoneRules"
                     :model="state.loginPhoneForm"
                     size="large"
                     class="mt-[24px] min-h-[140px]"
                  >
                     <el-form-item label="手机号" prop="phoneUser">
                        <el-input v-model="state.loginPhoneForm.phoneUser" placeholder="请输入手机号码" clearable>
                           <template #prepend>+86</template>
                        </el-input>
                     </el-form-item>
                     <el-form-item prop="verifyCode" label="验证码">
                        <el-input v-model="state.loginPhoneForm.verifyCode" placeholder="请输入四位验证码" maxlength="6" clearable>
                           <template #append>
                              <el-button type="primary" @click="handleSendVerifyCode" :disabled="countdown > 0">{{
                                 countdown > 0 ? `${countdown}秒后重试` : '获取验证码'
                              }}</el-button>
                           </template>
                        </el-input>
                     </el-form-item>
                  </el-form>
               </el-tab-pane>
            </el-tabs>
            <div class="mt-[24px]">
               <el-button type="primary" @click="onSubmit" class="set-login_btn">登录</el-button>
            </div>
         </div>
      </div>
   </div>
</template>
<script setup lang="ts">
import { ElMessage, type FormInstance } from 'element-plus';
import { computed, onMounted, reactive, ref, watchEffect } from 'vue';
import { PostLogin, loginMessageUser, loginVerifyMessage } from '/@/api/ai/user';
import router from '/@/router';
import emitter from '/@/utils/mitt';
import { isLoginStatus, isSharePage, isShowLogin } from '/@/stores/chatRoom';
import { accessSessionKey, userNameKey } from '/@/utils/request';
import { gotoRoute } from '/@/utils/route';
import { Local, LoginInfo } from '/@/utils/storage';
const loginFormRef = ref<FormInstance>(null); //账户密码登录
const formPhoneRef = ref(); //手机号登录
const isLoginStatus = ref(!!Local.get(accessSessionKey));
import emitter from '/@/utils/mitt';
const userName = ref('');
const countdown = ref(0);
const firstUserCharacter = computed(() => userName.value?.[0]?.toUpperCase());
isLoginStatus.value = !!Local.get(accessSessionKey);
watchEffect(() => {
   if (!isLoginStatus.value) return;
   userName.value = Local.get(userNameKey);
});
let state = reactive({
   asideTitleList: [
      {
         id: 1,
         icon: '/static/images/wave/AsideIcon.png',
         title: '应用场景',
         routerName: 'AllScenario',
      },
      {
         id: 2,
         icon: '/static/images/wave/AsideIcon.png',
         title: '帮助中心',
         // routerName:'Scenario',
      },
      {
         id: 3,
         icon: '/static/images/wave/AsideIcon.png',
         title: '个人设置',
      },
      // {
      //    id: 1,
      //    icon: '/static/images/wave/AsideIcon.png',
      //    title: '应用场景',
      //    routerName: 'AllScenario',
      // },
      // {
      //    id: 2,
      //    icon: '/static/images/wave/AsideIcon.png',
      //    title: '帮助中心',
      //    // routerName:'Scenario',
      // },
      // {
      //    id: 3,
      //    icon: '/static/images/wave/AsideIcon.png',
      //    title: '个人设置',
      // },
      {
         id: 4,
         icon: '/static/images/wave/AsideIcon.png',
@@ -138,7 +80,6 @@
         routerName: 'AboutUs',
      },
   ],
   isShowLogin: false,
   isShowExitLogin: false,
   loginForm: {
      account: '',
@@ -150,60 +91,16 @@
   },
   activeLoginName: 'accountUser',
});
const loginRules = reactive({
   account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
   pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
});
const loginPhoneRules = {
   phoneUser: [
      { required: true, message: '请输入手机号码', trigger: 'blur' },
      { pattern: /^1\d{10}$/, message: '请输入正确的手机号码', trigger: 'blur' },
   ],
   verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
};
const handleClick = (item) => {
   if(!item.routerName) return;
   if (!item.routerName) return;
   gotoRoute({ name: item.routerName });
};
//登录
const openLoginDlg = async () => {
   state.isShowLogin = true;
};
const handleClose = () => {
   state.isShowLogin = false;
};
//登录
const onSubmit = async () => {
   if (state.activeLoginName === 'accountUser') {
      //账户密码登录
      const isValid = await loginFormRef.value.validate().catch(() => {});
      if (!isValid) return;
      const res = await PostLogin({
         user: state.loginForm.account,
         pass: state.loginForm.pwd,
      });
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      LoginInfo.set(res.hswatersession, state.loginForm.account);
   } else if (state.activeLoginName === 'phoneUser') {
      //手机登录
      const isValid = await formPhoneRef.value.validate().catch(() => {});
      if (!isValid) return;
      const res = await loginMessageUser({
         phone: state.loginPhoneForm.phoneUser,
         code: state.loginPhoneForm.verifyCode,
      });
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      LoginInfo.set(res.hswatersession, state.loginPhoneForm.phoneUser);
   }
   state.isShowLogin = false;
   isLoginStatus.value = true;
   window.location.reload();
   // 分享页不需要
   if (isSharePage.value) return;
   isShowLogin.value = true;
};
const currentRoute = router.currentRoute;
@@ -225,43 +122,16 @@
      state.isShowExitLogin = false;
   }
};
//切换用户登录页面
const handleUserClick = (item) => {
   state.activeLoginName = item;
   // formPhoneRef?.value?.resetFields();
   // loginFormRef.value.resetFields();
};
//获取验证码
const handleSendVerifyCode = async () => {
   formPhoneRef.value.validateField('phoneUser', async (valid: boolean) => {
      if (valid) {
         const res = await loginVerifyMessage({
            phone: state.loginPhoneForm.phoneUser,
         });
         if (res.json_ok) {
            countdown.value = 60; //开启倒计时
            // 倒计时逻辑
            const intervalId = setInterval(() => {
               if (countdown.value > 0) {
                  countdown.value--;
               } else {
                  clearInterval(intervalId);
               }
            }, 1000);
         }
      }
   });
};
onMounted(() => {
   emitter.on('openLoginDlg', () => {
      if (state.isShowLogin || isLoginStatus.value) return;
      if (isShowLogin.value || isLoginStatus.value) return;
      openLoginDlg();
   });
   emitter.on('logout', () => {
      logoutClick();
   });
   document.addEventListener('click', listenClickOtherExit);
});
</script>