wujingjing
2025-03-14 863b5f27a3c9c89551b67569fd4e74e3ea8fb8e2
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="w-full mt-[24px]" @tab-change="handleTabChange">
                  <el-tab-pane label="账户密码登录" name="accountUser">
                     <el-form
                        ref="loginFormRef"
@@ -44,10 +44,23 @@
                        </el-form-item>
                     </el-form>
                  </el-tab-pane>
                  <el-tab-pane label="微信登录" name="wechat">
                     <!-- <div id="wechat-login"></div> -->
                     <div id="wechat-login" class="flex justify-center items-center">
                        <iframe
                           ref="wechatQrRef"
                           sandbox="allow-top-navigation allow-scripts"
                           style="width: 300px; height: 232px; overflow: hidden"
                           frameborder="0"
                        ></iframe>
                     </div>
                  </el-tab-pane>
               </el-tabs>
               <div class="mt-[24px]">
               <div class="mt-[24px]" v-if="state.activeLoginName !== 'wechat'">
                  <el-button type="primary" @click="onSubmit" class="set-login_btn">登录</el-button>
                  <!-- <el-button type="primary" @click="loginByWechat">微信登录</el-button> -->
               </div>
            </div>
         </div>
@@ -56,20 +69,100 @@
</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 { Local, LoginInfo } from '/@/utils/storage';
import { userInfoKey } from '/@/utils/request';
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 base64 =
      'LmltcG93ZXJCb3ggLnRpdGxlIHtkaXNwbGF5Om5vbmU7fQoKLmltcG93ZXJCb3ggLnN0YXR1cy5zdGF0dXNfYnJvd3NlciAgcDpudGgtb2YtdHlwZSgyKXsKICAgIGRpc3BsYXk6IG5vbmU7Cn0=';
   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,
      href: `data:text/css;base64,${base64}`,
   });
};
const wechatQrRef = ref<HTMLIFrameElement>();
const openWechatLogin = () => {
   if (!wechatQrRef.value) return;
   const url = `${SERVE_URL}JJJHHH/home?isWxLogin=Y`;
   const appid = 'wx4ea2dca37170074c';
   const state = (new Date().getTime() / 1000).toString();
   const base64 = btoa(`
   .impowerBox .title {display:none;}
   .impowerBox .status.status_browser  p:nth-of-type(2){
      display: none;
   }
   .impowerBox .qrcode {
   width: 160px;
   margin-top:0;
   }
   .impowerBox .status{
   }
   #tpl_for_iframe{
      height:100%;
      overflow: hidden;
          display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
   }
   `);
   const wechatAuthUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${appid}&redirect_uri=${encodeURIComponent(
      `http://apiv3.xpump.net/User/wxUserLoginCB.html?from=wi&url=${url}`
   )}&response_type=code&scope=snsapi_login&state=${state}&href=data:text/css;base64,${base64}#wechat_redirect`;
   wechatQrRef.value.src = wechatAuthUrl;
};
//切换用户登录页面
const handleTabChange = (item) => {
   state.activeLoginName = item;
   switch (item) {
      case 'wechat':
         // getWechartQrCode();
         openWechatLogin();
         break;
      case 'accountUser':
         break;
      case 'phoneUser':
         break;
   }
};
watch(
   () => isShowLogin.value,
   (val) => {
      if (val && state.activeLoginName === 'wechat') {
         nextTick(() => {
            openWechatLogin();
         });
      }
   }
);
const state = reactive({
   activeLoginName: 'accountUser',
   loginForm: {
@@ -92,7 +185,6 @@
   ],
   verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
};
const LOGIN_CLIENT = 'Web端';
const loginFormRef = ref<FormInstance>(null); //账户密码登录
const formPhoneRef = ref(); //手机号登录
const hasSended = computed(() => {
@@ -116,17 +208,6 @@
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      Local.set(userInfoKey, {
         id: res.id,
         realName: res.real_name,
         department: res.part,
         sex: res.sex,
         note: res.note,
         userName: res.name,
         phoneNumber: res.phone,
         email: res.email,
      } as any);
      LoginInfo.set(res.hswatersession, state.loginForm.account);
   } else if (state.activeLoginName === 'phoneUser') {
      //手机登录
      const isValid = await formPhoneRef.value.validate().catch(() => {});
@@ -139,22 +220,16 @@
      if (!res.json_ok) {
         return ElMessage.error(res.json_msg);
      }
      Local.set(userInfoKey, {
         id: res.id,
         realName: res.real_name,
         department: res.part,
         sex: res.sex,
         note: res.note,
         userName: res.name,
         phoneNumber: res.phone,
         email: res.email,
      } as any);
      LoginInfo.set(res.hswatersession, state.loginPhoneForm.phoneUser);
   }
   Local.set('isNewUser', !res?.web_login);
   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);