yangyin
2024-11-08 f41e52e3debf30558d556dc0451776f5422fb9b8
src/layout/component/sidebar/components/MenuList.vue
@@ -21,14 +21,14 @@
               ><span class="user_name"> {{ userName }} </span></span
            ><span ref="toggleExitLoginBtnRef"
               ><span
                  class="ywicon text-white"
                  :class="{ 'icon-fold': !state.isShowExitLogin, 'icon-unfold': state.isShowExitLogin }"
                  class="ywifont text-white"
                  :class="{ 'ywicon-fold': !state.isShowExitLogin, 'ywicon-unfold': state.isShowExitLogin }"
                  @click="toggleShowExitLogin"
               ></span
            ></span>
         </div>
         <div class="pop_up actived" v-show="state.isShowExitLogin">
            <div class="exit" @click="logoutClick"><i class="ywicon icon-tuichu"></i> 退出登录</div>
            <div class="exit" @click="logoutClick"><i class="ywifont ywicon-tuichu"></i> 退出登录</div>
         </div>
      </div>
      <div v-else class="user_login">
@@ -37,20 +37,53 @@
         </div>
      </div>
   </div>
   <div class="pc-login" v-show="state.isShowLogin">
   <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="ywicon icon-guanbi closes" @click="handleClose"></i>
            <h1><span class="ml-[33px]">登录 WI 水务</span></h1>
            <el-form ref="loginFormRef" :model="state.loginForm" :rules="loginRules" class="demo-ruleForm" 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>
            <div class="mt-[115px]">
            <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="hasSended">{{ sendCodeMsg }}</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>
@@ -59,19 +92,20 @@
</template>
<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import { ElMessage, type FormInstance } from 'element-plus';
import { computed, onMounted, reactive, ref, watchEffect } from 'vue';
import { PostLogin } from '/@/api/ai/user';
import { PostLogin, loginMessageUser, loginVerifyMessage } from '/@/api/ai/user';
import router from '/@/router';
import emitter from '/@/utils/mitt';
import { accessSessionKey, userNameKey } from '/@/utils/request';
import { gotoRoute } from '/@/utils/route';
import { Local } from '/@/utils/storage';
const loginFormRef = ref<FormInstance>(null);
import { Local, LoginInfo } from '/@/utils/storage';
import { isSharePage } from '/@/stores/chatRoom';
const loginFormRef = ref<FormInstance>(null); //账户密码登录
const formPhoneRef = ref(); //手机号登录
const isLoginStatus = ref(!!Local.get(accessSessionKey));
const userName = ref('');
const countdown = ref(null);
const firstUserCharacter = computed(() => userName.value?.[0]?.toUpperCase());
watchEffect(() => {
   if (!isLoginStatus.value) return;
@@ -79,23 +113,23 @@
});
let state = reactive({
   asideTitleList: [
      {
         id: 1,
         icon: '/static/images/wave/AsideIcon.png',
         title: '应用场景',
         routerName: 'Scenario',
      },
      {
         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',
@@ -109,31 +143,75 @@
      account: '',
      pwd: '',
   },
   loginPhoneForm: {
      phoneUser: '',
      verifyCode: '',
   },
   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;
   gotoRoute({ name: item.routerName });
};
const LOGIN_CLIENT = 'Web端';
//登录
const openLoginDlg = async () => {
   // 分享页不需要
   if (isSharePage.value) return;
   state.isShowLogin = true;
};
const handleClose = () => {
   state.isShowLogin = false;
};
const hasSended = computed(() => {
   return countdown.value !== null;
});
const sendCodeMsg = computed(() => {
   return !hasSended.value ? '获取验证码' : `${countdown.value} 秒后重试`;
});
//登录
const onSubmit = async () => {
   const isValid = await loginFormRef.value.validate().catch(() => {});
   if (!isValid) return;
   const res = await PostLogin({
      user: state.loginForm.account,
      pass: state.loginForm.pwd,
   });
   Local.set(accessSessionKey, res.hswatersession);
   Local.set(userNameKey, state.loginForm.account);
   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,
         client: LOGIN_CLIENT,
      });
      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,
         client: LOGIN_CLIENT,
      });
      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();
@@ -149,7 +227,7 @@
const logoutClick = () => {
   state.isShowExitLogin = false;
   isLoginStatus.value = false;
   Local.remove(accessSessionKey);
   LoginInfo.remove();
};
const toggleExitLoginBtnRef = ref<HTMLDivElement>(null);
@@ -158,7 +236,33 @@
      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;
@@ -343,6 +447,7 @@
   -ms-flex-pack: center;
   justify-content: center;
}
.pc-login {
   position: fixed;
   top: 0;
@@ -354,9 +459,9 @@
   .login_box {
      position: relative;
      width: 450px;
      height: 550px;
      margin: 15vh auto;
      .sign_in {
         padding: 39px;
         position: relative;
         width: 100%;
         height: 100%;
@@ -387,12 +492,10 @@
         h1 {
            box-sizing: content-box;
            width: 100%;
            margin-top: 25px;
            font-size: 30px;
            font-weight: 500;
            color: #1c153a;
            text-align: left !important;
            margin-bottom: 105px;
         }
         .demo-ruleForm {
            :deep(.el-input__wrapper) {
@@ -407,6 +510,9 @@
               background-color: #fff;
               border-radius: 5px;
            }
            :deep(.el-form-item--large .el-form-item__error) {
               padding: unset !important;
            }
         }
         .set-pwd {
            text-align: right;