index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. // 企业微信免登回调通常会在 URL 上带 `code`,进入本页后直接走免登并展示加载态
  106. const rawLaunchCode = (uni.getLaunchOptionsSync()?.query as any)?.code as undefined | string | string[];
  107. const initialUrlCode = Array.isArray(rawLaunchCode) ? rawLaunchCode[0] : rawLaunchCode;
  108. const showOAuthLoading = ref<boolean>(!!initialUrlCode);
  109. onMounted(async () => {
  110. // --- 新增:登录拦截 ---
  111. // 如果缓存里已经有 Token,说明当前处于已登录状态
  112. // 不应该停留在当前登录大厅,直接跳走回首页
  113. const existingToken = Local.get(CACHE_KEY.TOKEN);
  114. if (existingToken) {
  115. showOAuthLoading.value = false;
  116. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  117. console.warn('switchTab failed, trying reLaunch:', err);
  118. uni.reLaunch({ url: '/pages/home/index' });
  119. });
  120. return; // 不要继续执行后面的加载验证码之类
  121. }
  122. // 检查是否有记住的密码
  123. const savedUser = Local.get(CACHE_KEY.REMEMBER_USER);
  124. if (savedUser) {
  125. // 可能是对象也可能是 JSON
  126. const user = typeof savedUser === 'string' ? JSON.parse(savedUser) : savedUser;
  127. loginForm.userName = user.userName || user.username;
  128. loginForm.password = user.password;
  129. rememberMeArr.value = ['remember'];
  130. }
  131. // 检测 URL 是否携带 code (企业微信免登)
  132. if (initialUrlCode) {
  133. await handleEnterpriseLogin(initialUrlCode);
  134. return;
  135. }
  136. await checkCaptcha();
  137. if (configSetting.value.isCaptcha === '10') {
  138. getCaptchaImg();
  139. }
  140. });
  141. const getCaptchaImg = async () => {
  142. try {
  143. const res = await loginApi.getCaptchaImg();
  144. codeUrl.value = res?.data?.base64stringC || '';
  145. loginForm.idKeyC = res?.data?.idKeyC || '';
  146. } catch (error) {
  147. console.error('获取验证码失败', error);
  148. }
  149. };
  150. const handleLogin = async () => {
  151. if (!loginForm.userName) {
  152. toastRef.value.show({ message: '请输入账号', type: 'error' });
  153. return;
  154. }
  155. if (!loginForm.password) {
  156. toastRef.value.show({ message: '请输入密码', type: 'error' });
  157. return;
  158. }
  159. if (configSetting.value.isCaptcha === '10' && !loginForm.idValueC) {
  160. toastRef.value.show({ message: '请输入验证码', type: 'error' });
  161. return;
  162. }
  163. loading.value = true;
  164. try {
  165. // 使用与后端完全匹配的加密函数加密密码
  166. const encryptedPassword = encryptWithBackendConfig(loginForm.password);
  167. loginForm.saltValue = encryptedPassword; // 后端AES IV常量 登录的时候 增加saltValue
  168. // 调用 store 处理真实请求和持久化
  169. await login(loginForm);
  170. // 记住密码逻辑独立于全局token的持久化
  171. if (rememberMeArr.value.includes('remember')) {
  172. Local.set(CACHE_KEY.REMEMBER_USER, {
  173. userName: loginForm.userName,
  174. password: loginForm.password
  175. });
  176. } else {
  177. Local.remove(CACHE_KEY.REMEMBER_USER);
  178. }
  179. toastRef.value.show({ message: '登录成功', type: 'success' });
  180. // 登录成功跳转首页
  181. setTimeout(() => {
  182. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  183. console.warn('switchTab failed, trying reLaunch:', err);
  184. uni.reLaunch({ url: '/pages/home/index' });
  185. });
  186. }, 1000);
  187. } catch (error) {
  188. console.error('Login Failed', error);
  189. // 登录失败若有验证码,刷新验证码
  190. if (configSetting.value.isCaptcha === '10') {
  191. loginForm.idValueC = '';
  192. getCaptchaImg();
  193. }
  194. } finally {
  195. loading.value = false;
  196. }
  197. };
  198. const goToRegister = () => {
  199. uni.navigateTo({ url: '/pages/login/register' });
  200. };
  201. const handleEnterpriseLogin = async (code: string) => {
  202. try {
  203. loading.value = true;
  204. await userStore.oAuthLogin(code);
  205. toastRef.value.show({ message: '登录成功', type: 'success' });
  206. // 成功跳转首页
  207. setTimeout(() => {
  208. uni.switchTab({ url: '/pages/home/index' }).catch(() => {
  209. uni.reLaunch({ url: '/pages/home/index' });
  210. });
  211. }, 800);
  212. } catch (error: any) {
  213. console.error('Enterprise Login Failure:', error);
  214. toastRef.value.show({
  215. message: error.message || '企业快捷登录失败',
  216. type: 'error'
  217. });
  218. // 免登失败后切回手动登录表单
  219. showOAuthLoading.value = false;
  220. // 如果免登失败,正常加载验证码进入手动登录模式
  221. await checkCaptcha();
  222. if (configSetting.value.isCaptcha === '10') {
  223. getCaptchaImg();
  224. }
  225. } finally {
  226. loading.value = false;
  227. }
  228. };
  229. /**
  230. * 企业微信登录点击 (重定向获取 code)
  231. */
  232. const handleEnterpriseLoginClick = () => {
  233. // 注意:实际 AppID 需要根据后台配置,此处假设通过环境变量或逻辑获取
  234. // 如果是免登,通常是直接进入这个页面时 URL 就已经带了 code
  235. // 如果需要手动点击跳转获取,则执行如下逻辑:
  236. const appid = import.meta.env.VITE_WECOM_APPID || '';
  237. const redirectUri = encodeURIComponent(window.location.href.split('?')[0]);
  238. 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`;
  239. // H5 环境下跳转
  240. window.location.href = url;
  241. };
  242. </script>
  243. <style scoped>
  244. .container {
  245. min-height: 100vh;
  246. position: relative;
  247. background: #f8fafc;
  248. overflow: hidden;
  249. display: flex;
  250. justify-content: center;
  251. align-items: center;
  252. padding: 0 40rpx;
  253. }
  254. .loading-box {
  255. display: flex;
  256. flex-direction: column;
  257. align-items: center;
  258. z-index: 10;
  259. border-radius: 20rpx;
  260. background: #f1f5f9;
  261. flex-shrink: 0;
  262. padding: 40rpx 30rpx;
  263. }
  264. .loading-text {
  265. margin-top: 20rpx;
  266. font-size: 26rpx;
  267. color: #64748b;
  268. font-weight: 500;
  269. }
  270. .bg-shape {
  271. position: absolute;
  272. top: -100rpx;
  273. right: -100rpx;
  274. width: 500rpx;
  275. height: 500rpx;
  276. border-radius: 50%;
  277. background: linear-gradient(135deg, #3b82f6 0%, #2dd4bf 100%);
  278. opacity: 0.15;
  279. filter: blur(40rpx);
  280. }
  281. .bg-shape2 {
  282. position: absolute;
  283. bottom: -150rpx;
  284. left: -100rpx;
  285. width: 600rpx;
  286. height: 600rpx;
  287. border-radius: 50%;
  288. background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  289. opacity: 0.15;
  290. filter: blur(50rpx);
  291. }
  292. .login-box {
  293. width: 100%;
  294. max-width: 600rpx;
  295. background: rgba(255, 255, 255, 0.9);
  296. backdrop-filter: blur(20px);
  297. border-radius: 40rpx;
  298. padding: 60rpx 40rpx;
  299. box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.05);
  300. position: relative;
  301. z-index: 10;
  302. border: 2rpx solid rgba(255, 255, 255, 0.5);
  303. }
  304. .header {
  305. margin-bottom: 60rpx;
  306. }
  307. .title {
  308. font-size: 56rpx;
  309. font-weight: 800;
  310. color: #1e293b;
  311. display: block;
  312. margin-bottom: 16rpx;
  313. font-family: 'Helvetica Neue', Arial, sans-serif;
  314. }
  315. .subtitle {
  316. font-size: 28rpx;
  317. color: #64748b;
  318. display: block;
  319. }
  320. .form-group {
  321. margin-bottom: 40rpx;
  322. }
  323. .label {
  324. font-size: 26rpx;
  325. font-weight: 600;
  326. color: #334155;
  327. margin-bottom: 16rpx;
  328. display: block;
  329. }
  330. /* 移除原有的 input-container 和 checkbox 样式,采用 uvui 内置属性或者 customStyle 修改样式 */
  331. :deep(.placeholder-style) {
  332. color: #94a3b8 !important;
  333. font-size: 28rpx;
  334. }
  335. .captcha-group {
  336. display: flex;
  337. align-items: center;
  338. gap: 20rpx;
  339. }
  340. .captcha-img {
  341. width: 200rpx;
  342. height: 104rpx;
  343. /* 与左侧输入框 60(height) + 40(padding) + 4(border) 高度保持一致 */
  344. border-radius: 20rpx;
  345. background: #f1f5f9;
  346. flex-shrink: 0;
  347. cursor: pointer;
  348. }
  349. .options {
  350. display: flex;
  351. justify-content: space-between;
  352. align-items: center;
  353. margin-bottom: 50rpx;
  354. }
  355. .forgot {
  356. font-size: 26rpx;
  357. color: #3b82f6;
  358. font-weight: 500;
  359. padding: 10rpx 0;
  360. }
  361. .footer {
  362. margin-top: 40rpx;
  363. display: flex;
  364. justify-content: center;
  365. align-items: center;
  366. }
  367. .footer-text {
  368. font-size: 26rpx;
  369. color: #64748b;
  370. }
  371. .footer-link {
  372. font-size: 26rpx;
  373. color: #3b82f6;
  374. font-weight: 600;
  375. margin-left: 10rpx;
  376. }
  377. </style>