wujingjing
2025-03-13 8a3dd5392dc008dc44d6798ef48240cd5500a5e9
src/layout/component/login/Login.vue
@@ -5,7 +5,7 @@
            <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-tabs v-model="state.activeLoginName" class="mt-[24px]" @tab-change="handleTabChange">
                  <el-tab-pane label="账户密码登录" name="accountUser">
                     <el-form
                        ref="loginFormRef"
@@ -44,10 +44,15 @@
                        </el-form-item>
                     </el-form>
                  </el-tab-pane>
                  <el-tab-pane label="微信登录" name="wechat">
                     <div id="wechat-login"></div>
                  </el-tab-pane>
               </el-tabs>
               <div class="mt-[24px]">
                  <el-button type="primary" @click="onSubmit" class="set-login_btn">登录</el-button>
                  <!-- <el-button type="primary" @click="loginByWechat">微信登录</el-button> -->
               </div>
            </div>
         </div>
@@ -56,19 +61,57 @@
</template>
<script setup lang="ts">
import { ElMessage, type FormInstance } from 'element-plus';
import { computed, reactive, ref } from 'vue';
import { PostLogin, loginMessageUser, loginVerifyMessage } from '/@/api/ai/user';
import { isLoginStatus, isShowLogin, userInfo } from '/@/stores/chatRoom';
import { LoginInfo } from '/@/utils/storage';
import { computed, nextTick, reactive, ref, watch } from 'vue';
import { ACCOUNT_EXPIRE_DAY, handleAfterLogin, LOGIN_CLIENT, STORED_ACCOUNT_KEY } from './login';
import { loginMessageUser, loginVerifyMessage, PostLogin } from '/@/api/ai/user';
import { isShowLogin } from '/@/stores/chatRoom';
import { encrypt } from '/@/utils/cypto';
import { LocalPlus } from '/@/utils/storage';
import './wxLogin.js';
import { SERVE_URL } from '/@/constants';
const handleClose = () => {
   isShowLogin.value = false;
};
//切换用户登录页面
const handleUserClick = (item) => {
   state.activeLoginName = item;
const getWechartQrCode = () => {
   const state = (new Date().getTime() / 1000).toString();
   // JJJHHH 表示#号
   const url = `${SERVE_URL}JJJHHH/home?isWxLogin=Y`;
   const obj = new window.WxLogin({
      id: 'wechat-login',
      appid: 'wx4ea2dca37170074c',
      scope: 'snsapi_login',
      redirect_uri: encodeURIComponent(`http://apiv3.xpump.net/User/wxUserLoginCB.html?from=wi&url=${url}`),
      state: state,
   });
};
//切换用户登录页面
const handleTabChange = (item) => {
   state.activeLoginName = item;
   switch (item) {
      case 'wechat':
         getWechartQrCode();
         break;
      case 'accountUser':
         break;
      case 'phoneUser':
         break;
   }
};
watch(
   () => isShowLogin.value,
   (val) => {
      if (val && state.activeLoginName === 'wechat') {
         nextTick(() => {
            getWechartQrCode();
         });
      }
   }
);
const state = reactive({
   activeLoginName: 'accountUser',
   loginForm: {
@@ -91,7 +134,6 @@
   ],
   verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
};
const LOGIN_CLIENT = 'Web端';
const loginFormRef = ref<FormInstance>(null); //账户密码登录
const formPhoneRef = ref(); //手机号登录
const hasSended = computed(() => {
@@ -102,11 +144,12 @@
});
//登录
const onSubmit = async () => {
   let res;
   if (state.activeLoginName === 'accountUser') {
      //账户密码登录
      const isValid = await loginFormRef.value.validate().catch(() => {});
      if (!isValid) return;
      const res = await PostLogin({
      res = await PostLogin({
         user: state.loginForm.account,
         pass: state.loginForm.pwd,
         client: LOGIN_CLIENT,
@@ -114,13 +157,11 @@
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      userInfo.set(res);
      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({
      res = await loginMessageUser({
         phone: state.loginPhoneForm.phoneUser,
         code: state.loginPhoneForm.verifyCode,
         client: LOGIN_CLIENT,
@@ -128,12 +169,16 @@
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      LoginInfo.set(res.hswatersession, state.loginPhoneForm.phoneUser);
      userInfo.set(res);
   }
   isShowLogin.value = false;
   isLoginStatus.value = true;
   window.location.reload();
   if (state.activeLoginName === 'accountUser') {
      const accountEncryptStr = encrypt({
         username: state.loginForm.account,
         password: state.loginForm.pwd,
      });
      LocalPlus.set(STORED_ACCOUNT_KEY, accountEncryptStr, ACCOUNT_EXPIRE_DAY);
   }
   handleAfterLogin(res);
};
const countdown = ref(null);