index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <template>
  2. <view class="container">
  3. <view class="bg-shape shadow"></view>
  4. <view class="bg-shape2 shadow"></view>
  5. <view v-if="showOAuthLoading" class="loading-box">
  6. <uv-loading-icon color="#3b82f6" size="30"></uv-loading-icon>
  7. <text class="loading-text">正在登录中...</text>
  8. </view>
  9. <view v-else class="login-box">
  10. <view class="header">
  11. <text class="title">欢迎登录</text>
  12. <text class="subtitle">登录科研微信平台</text>
  13. </view>
  14. <view class="form-group">
  15. <text class="label">账号</text>
  16. <uv-input v-model="loginForm.userName" placeholder="请输入您的账号" placeholderClass="placeholder-style" border="none"
  17. shape="circle" clearable
  18. customStyle="background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;">
  19. <template #prefix>
  20. <uv-icon name="account" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  21. </template>
  22. </uv-input>
  23. </view>
  24. <view class="form-group">
  25. <text class="label">密码</text>
  26. <uv-input v-model="loginForm.password" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码"
  27. placeholderClass="placeholder-style" border="none" shape="circle"
  28. customStyle="background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;">
  29. <template #prefix>
  30. <uv-icon name="lock" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  31. </template>
  32. <template #suffix>
  33. <uv-icon :name="showPassword ? 'eye-fill' : 'eye-off'" size="22" color="#94a3b8"
  34. @click="showPassword = !showPassword" customStyle="margin-left: 16rpx; cursor: pointer"></uv-icon>
  35. </template>
  36. </uv-input>
  37. </view>
  38. <view class="form-group" v-if="configSetting.isCaptcha === '10'">
  39. <text class="label">验证码</text>
  40. <view class="captcha-group">
  41. <uv-input v-model="loginForm.idValueC" placeholder="请输入验证码" placeholderClass="placeholder-style" border="none"
  42. shape="circle" clearable
  43. customStyle="flex: 1; background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;">
  44. <template #prefix>
  45. <uv-icon name="photo" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  46. </template>
  47. </uv-input>
  48. <image v-if="codeUrl" :src="codeUrl" class="captcha-img" @click="getCaptchaImg" mode="aspectFit"></image>
  49. </view>
  50. </view>
  51. <view class="options">
  52. <uv-checkbox-group v-model="rememberMeArr">
  53. <uv-checkbox label="记住密码" name="remember" shape="square" activeColor="#3b82f6" labelSize="13" iconSize="14"
  54. size="16" labelColor="#475569"></uv-checkbox>
  55. </uv-checkbox-group>
  56. <!-- <text class="forgot">忘记密码?</text> -->
  57. </view>
  58. <uv-button text="登 录" @click="handleLogin" :loading="loading" shape="circle"
  59. color="linear-gradient(135deg, #3b82f6 0%, #2563eb 100%)"
  60. customStyle="height: 100rpx; font-size: 32rpx; font-weight: 600; letter-spacing: 2rpx; box-shadow: 0 10rpx 20rpx rgba(37, 99, 235, 0.3); border: none; margin-top: 20rpx;" />
  61. <!-- 企业微信一键登录 -->
  62. <!-- <view style="margin-top: 30rpx;">
  63. <uv-button text="企业微信登录" @click="handleEnterpriseLoginClick" :loading="loading" shape="circle" plain
  64. color="#07c160"
  65. customStyle="height: 100rpx; font-size: 30rpx; font-weight: 500; border: 2rpx solid #07c160; color: #07c160;">
  66. <template #prefix>
  67. <uv-icon name="weixin-fill" size="24" color="#07c160" customStyle="margin-right: 12rpx"></uv-icon>
  68. </template>
  69. </uv-button>
  70. </view> -->
  71. <!-- <view class="footer">
  72. <text class="footer-text">还没有账号?</text>
  73. <text class="footer-link" @click="goToRegister">立即注册</text>
  74. </view> -->
  75. </view>
  76. <uv-toast ref="toastRef"></uv-toast>
  77. </view>
  78. </template>
  79. <script setup lang="ts">
  80. import { ref, reactive, onMounted } from 'vue';
  81. import { storeToRefs } from 'pinia';
  82. import { useUserStore } from '@/store/modules/user';
  83. import type { LoginParams } from '@/types/user';
  84. import { CACHE_KEY } from '@/constants/index';
  85. // @ts-ignore
  86. import { Local } from '@/utils/storage';
  87. import { useLoginApi } from '@/api/system/login';
  88. import { encryptWithBackendConfig } from '@/utils/aesCrypto';
  89. const userStore = useUserStore();
  90. const { configSetting } = storeToRefs(userStore);
  91. const { checkCaptcha, login } = userStore;
  92. const loginApi = useLoginApi();
  93. const loginForm = reactive<LoginParams>({
  94. userName: '',
  95. password: '',
  96. idValueC: '',
  97. idKeyC: '',
  98. saltValue: '',
  99. });
  100. const codeUrl = ref('');
  101. const loading = ref(false);
  102. const showPassword = ref(false);
  103. const rememberMeArr = ref<string[]>([]);
  104. const toastRef = ref<any>(null);
  105. // 企业微信免登回调:显示加载态(避免用户看到账号/密码输入框)
  106. const showOAuthLoading = ref<boolean>(false);
  107. const getUrlCode = (): string | undefined => {
  108. const rawCode = (uni.getLaunchOptionsSync()?.query as any)?.code as undefined | string | string[];
  109. const urlCode = Array.isArray(rawCode) ? rawCode[0] : rawCode;
  110. if (urlCode) return urlCode;
  111. // H5: 形如 ?code=xxx&state=STATE#/pages/login/index,code 在 search 中
  112. if (typeof window !== 'undefined') {
  113. try {
  114. const params = new URLSearchParams(window.location.search);
  115. const searchCode = params.get('code');
  116. if (searchCode) return searchCode;
  117. // 兜底:如果 code 出现在 hash 的参数里,也尝试解析
  118. const hash = window.location.hash || '';
  119. const hashParamsIndex = hash.indexOf('?');
  120. if (hashParamsIndex !== -1) {
  121. const hashParams = hash.substring(hashParamsIndex + 1);
  122. const hashSearchParams = new URLSearchParams(hashParams);
  123. const hashCode = hashSearchParams.get('code');
  124. if (hashCode) return hashCode;
  125. }
  126. } catch (e) {
  127. // ignore
  128. }
  129. }
  130. return undefined;
  131. };
  132. onMounted(async () => {
  133. // --- 新增:登录拦截 ---
  134. // 如果缓存里已经有 Token,说明当前处于已登录状态
  135. // 不应该停留在当前登录大厅,直接跳走回首页
  136. const existingToken = Local.get(CACHE_KEY.TOKEN);
  137. if (existingToken) {
  138. showOAuthLoading.value = false;
  139. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  140. console.warn('switchTab failed, trying reLaunch:', err);
  141. uni.reLaunch({ url: '/pages/home/index' });
  142. });
  143. return; // 不要继续执行后面的加载验证码之类
  144. }
  145. // 检查是否有记住的密码
  146. const savedUser = Local.get(CACHE_KEY.REMEMBER_USER);
  147. if (savedUser) {
  148. // 可能是对象也可能是 JSON
  149. const user = typeof savedUser === 'string' ? JSON.parse(savedUser) : savedUser;
  150. loginForm.userName = user.userName || user.username;
  151. loginForm.password = user.password;
  152. rememberMeArr.value = ['remember'];
  153. }
  154. // 检测 URL 是否携带 code (企业微信免登)
  155. const urlCode = getUrlCode();
  156. if (urlCode) {
  157. showOAuthLoading.value = true;
  158. await handleEnterpriseLogin(urlCode);
  159. return;
  160. }
  161. await checkCaptcha();
  162. if (configSetting.value.isCaptcha === '10') {
  163. getCaptchaImg();
  164. }
  165. });
  166. const getCaptchaImg = async () => {
  167. try {
  168. const res = await loginApi.getCaptchaImg();
  169. codeUrl.value = res?.data?.base64stringC || '';
  170. loginForm.idKeyC = res?.data?.idKeyC || '';
  171. } catch (error) {
  172. console.error('获取验证码失败', error);
  173. }
  174. };
  175. const handleLogin = async () => {
  176. if (!loginForm.userName) {
  177. toastRef.value.show({ message: '请输入账号', type: 'error' });
  178. return;
  179. }
  180. if (!loginForm.password) {
  181. toastRef.value.show({ message: '请输入密码', type: 'error' });
  182. return;
  183. }
  184. if (configSetting.value.isCaptcha === '10' && !loginForm.idValueC) {
  185. toastRef.value.show({ message: '请输入验证码', type: 'error' });
  186. return;
  187. }
  188. loading.value = true;
  189. try {
  190. // 使用与后端完全匹配的加密函数加密密码
  191. const encryptedPassword = encryptWithBackendConfig(loginForm.password);
  192. loginForm.saltValue = encryptedPassword; // 后端AES IV常量 登录的时候 增加saltValue
  193. // 调用 store 处理真实请求和持久化
  194. await login(loginForm);
  195. // 记住密码逻辑独立于全局token的持久化
  196. if (rememberMeArr.value.includes('remember')) {
  197. Local.set(CACHE_KEY.REMEMBER_USER, {
  198. userName: loginForm.userName,
  199. password: loginForm.password
  200. });
  201. } else {
  202. Local.remove(CACHE_KEY.REMEMBER_USER);
  203. }
  204. toastRef.value.show({ message: '登录成功', type: 'success' });
  205. // 登录成功跳转首页
  206. setTimeout(() => {
  207. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  208. console.warn('switchTab failed, trying reLaunch:', err);
  209. uni.reLaunch({ url: '/pages/home/index' });
  210. });
  211. }, 1000);
  212. } catch (error) {
  213. console.error('Login Failed', error);
  214. // 登录失败若有验证码,刷新验证码
  215. if (configSetting.value.isCaptcha === '10') {
  216. loginForm.idValueC = '';
  217. getCaptchaImg();
  218. }
  219. } finally {
  220. loading.value = false;
  221. }
  222. };
  223. const goToRegister = () => {
  224. uni.navigateTo({ url: '/pages/login/register' });
  225. };
  226. const handleEnterpriseLogin = async (code: string) => {
  227. try {
  228. loading.value = true;
  229. await userStore.oAuthLogin(code);
  230. toastRef.value.show({ message: '登录成功', type: 'success' });
  231. // 成功跳转首页
  232. setTimeout(() => {
  233. uni.switchTab({ url: '/pages/home/index' }).catch(() => {
  234. uni.reLaunch({ url: '/pages/home/index' });
  235. });
  236. }, 800);
  237. } catch (error: any) {
  238. console.error('Enterprise Login Failure:', error);
  239. toastRef.value.show({
  240. message: error.message || '企业快捷登录失败',
  241. type: 'error'
  242. });
  243. // 免登失败后切回手动登录表单
  244. showOAuthLoading.value = false;
  245. // 如果免登失败,正常加载验证码进入手动登录模式
  246. await checkCaptcha();
  247. if (configSetting.value.isCaptcha === '10') {
  248. getCaptchaImg();
  249. }
  250. } finally {
  251. loading.value = false;
  252. }
  253. };
  254. /**
  255. * 企业微信登录点击 (重定向获取 code)
  256. */
  257. const handleEnterpriseLoginClick = () => {
  258. // 注意:实际 AppID 需要根据后台配置,此处假设通过环境变量或逻辑获取
  259. // 如果是免登,通常是直接进入这个页面时 URL 就已经带了 code
  260. // 如果需要手动点击跳转获取,则执行如下逻辑:
  261. const appid = import.meta.env.VITE_WECOM_APPID || '';
  262. const redirectUri = encodeURIComponent(window.location.href.split('?')[0]);
  263. const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
  264. // H5 环境下跳转
  265. window.location.href = url;
  266. };
  267. </script>
  268. <style scoped>
  269. .container {
  270. min-height: 100vh;
  271. position: relative;
  272. background: #f8fafc;
  273. overflow: hidden;
  274. display: flex;
  275. justify-content: center;
  276. align-items: center;
  277. padding: 0 40rpx;
  278. }
  279. .loading-box {
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. z-index: 10;
  284. border-radius: 20rpx;
  285. background: #f1f5f9;
  286. flex-shrink: 0;
  287. padding: 40rpx 30rpx;
  288. }
  289. .loading-text {
  290. margin-top: 20rpx;
  291. font-size: 26rpx;
  292. color: #64748b;
  293. font-weight: 500;
  294. }
  295. .bg-shape {
  296. position: absolute;
  297. top: -100rpx;
  298. right: -100rpx;
  299. width: 500rpx;
  300. height: 500rpx;
  301. border-radius: 50%;
  302. background: linear-gradient(135deg, #3b82f6 0%, #2dd4bf 100%);
  303. opacity: 0.15;
  304. filter: blur(40rpx);
  305. }
  306. .bg-shape2 {
  307. position: absolute;
  308. bottom: -150rpx;
  309. left: -100rpx;
  310. width: 600rpx;
  311. height: 600rpx;
  312. border-radius: 50%;
  313. background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  314. opacity: 0.15;
  315. filter: blur(50rpx);
  316. }
  317. .login-box {
  318. width: 100%;
  319. max-width: 600rpx;
  320. background: rgba(255, 255, 255, 0.9);
  321. backdrop-filter: blur(20px);
  322. border-radius: 40rpx;
  323. padding: 60rpx 40rpx;
  324. box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.05);
  325. position: relative;
  326. z-index: 10;
  327. border: 2rpx solid rgba(255, 255, 255, 0.5);
  328. }
  329. .header {
  330. margin-bottom: 60rpx;
  331. }
  332. .title {
  333. font-size: 56rpx;
  334. font-weight: 800;
  335. color: #1e293b;
  336. display: block;
  337. margin-bottom: 16rpx;
  338. font-family: 'Helvetica Neue', Arial, sans-serif;
  339. }
  340. .subtitle {
  341. font-size: 28rpx;
  342. color: #64748b;
  343. display: block;
  344. }
  345. .form-group {
  346. margin-bottom: 40rpx;
  347. }
  348. .label {
  349. font-size: 26rpx;
  350. font-weight: 600;
  351. color: #334155;
  352. margin-bottom: 16rpx;
  353. display: block;
  354. }
  355. /* 移除原有的 input-container 和 checkbox 样式,采用 uvui 内置属性或者 customStyle 修改样式 */
  356. :deep(.placeholder-style) {
  357. color: #94a3b8 !important;
  358. font-size: 28rpx;
  359. }
  360. .captcha-group {
  361. display: flex;
  362. align-items: center;
  363. gap: 20rpx;
  364. }
  365. .captcha-img {
  366. width: 200rpx;
  367. height: 104rpx;
  368. /* 与左侧输入框 60(height) + 40(padding) + 4(border) 高度保持一致 */
  369. border-radius: 20rpx;
  370. background: #f1f5f9;
  371. flex-shrink: 0;
  372. cursor: pointer;
  373. }
  374. .options {
  375. display: flex;
  376. justify-content: space-between;
  377. align-items: center;
  378. margin-bottom: 50rpx;
  379. }
  380. .forgot {
  381. font-size: 26rpx;
  382. color: #3b82f6;
  383. font-weight: 500;
  384. padding: 10rpx 0;
  385. }
  386. .footer {
  387. margin-top: 40rpx;
  388. display: flex;
  389. justify-content: center;
  390. align-items: center;
  391. }
  392. .footer-text {
  393. font-size: 26rpx;
  394. color: #64748b;
  395. }
  396. .footer-link {
  397. font-size: 26rpx;
  398. color: #3b82f6;
  399. font-weight: 600;
  400. margin-left: 10rpx;
  401. }
  402. </style>