index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="container">
  3. <view class="bg-shape shadow"></view>
  4. <view class="bg-shape2 shadow"></view>
  5. <view class="login-box">
  6. <view class="header">
  7. <text class="title">欢迎登录</text>
  8. <text class="subtitle">登录科研钉钉平台</text>
  9. </view>
  10. <view class="form-group">
  11. <text class="label">账号</text>
  12. <uv-input
  13. v-model="loginForm.userName"
  14. placeholder="请输入您的账号"
  15. placeholderClass="placeholder-style"
  16. border="none"
  17. shape="circle"
  18. clearable
  19. customStyle="background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;"
  20. >
  21. <template #prefix>
  22. <uv-icon name="account" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  23. </template>
  24. </uv-input>
  25. </view>
  26. <view class="form-group">
  27. <text class="label">密码</text>
  28. <uv-input
  29. v-model="loginForm.password"
  30. :type="showPassword ? 'text' : 'password'"
  31. placeholder="请输入密码"
  32. placeholderClass="placeholder-style"
  33. border="none"
  34. shape="circle"
  35. customStyle="background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;"
  36. >
  37. <template #prefix>
  38. <uv-icon name="lock" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  39. </template>
  40. <template #suffix>
  41. <uv-icon
  42. :name="showPassword ? 'eye-fill' : 'eye-off'"
  43. size="22"
  44. color="#94a3b8"
  45. @click="showPassword = !showPassword"
  46. customStyle="margin-left: 16rpx; cursor: pointer"
  47. ></uv-icon>
  48. </template>
  49. </uv-input>
  50. </view>
  51. <view class="form-group" v-if="configSetting.isCaptcha === '10'">
  52. <text class="label">验证码</text>
  53. <view class="captcha-group">
  54. <uv-input
  55. v-model="loginForm.idValueC"
  56. placeholder="请输入验证码"
  57. placeholderClass="placeholder-style"
  58. border="none"
  59. shape="circle"
  60. clearable
  61. customStyle="flex: 1; background: #f1f5f9; padding: 20rpx 30rpx; height: 60rpx; border: 2rpx solid transparent; transition: all 0.3s ease; border-radius: 20rpx;"
  62. >
  63. <template #prefix>
  64. <uv-icon name="photo" size="22" color="#94a3b8" customStyle="margin-right: 16rpx"></uv-icon>
  65. </template>
  66. </uv-input>
  67. <image v-if="codeUrl" :src="codeUrl" class="captcha-img" @click="getCaptchaImg" mode="aspectFit"></image>
  68. </view>
  69. </view>
  70. <view class="options">
  71. <uv-checkbox-group v-model="rememberMeArr">
  72. <uv-checkbox
  73. label="记住密码"
  74. name="remember"
  75. shape="square"
  76. activeColor="#3b82f6"
  77. labelSize="13"
  78. iconSize="14"
  79. size="16"
  80. labelColor="#475569"
  81. ></uv-checkbox>
  82. </uv-checkbox-group>
  83. <!-- <text class="forgot">忘记密码?</text> -->
  84. </view>
  85. <uv-button
  86. text="登 录"
  87. @click="handleLogin"
  88. :loading="loading"
  89. shape="circle"
  90. color="linear-gradient(135deg, #3b82f6 0%, #2563eb 100%)"
  91. 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;"
  92. />
  93. <!-- <view class="footer">
  94. <text class="footer-text">还没有账号?</text>
  95. <text class="footer-link" @click="goToRegister">立即注册</text>
  96. </view> -->
  97. </view>
  98. <uv-toast ref="toastRef"></uv-toast>
  99. </view>
  100. </template>
  101. <script setup lang="ts">
  102. import { ref, reactive, onMounted } from 'vue';
  103. import { storeToRefs } from 'pinia';
  104. import { useUserStore } from '@/store/modules/user';
  105. import type { LoginParams } from '@/types/user';
  106. import { CACHE_KEY } from '@/constants/index';
  107. // @ts-ignore
  108. import { Local } from '@/utils/storage';
  109. import { useLoginApi } from '@/api/system/login';
  110. import { encryptWithBackendConfig } from '@/utils/aesCrypto';
  111. const userStore = useUserStore();
  112. const { configSetting } = storeToRefs(userStore);
  113. const { checkCaptcha, login } = userStore;
  114. const loginApi = useLoginApi();
  115. const loginForm = reactive<LoginParams>({
  116. userName: '',
  117. password: '',
  118. idValueC: '',
  119. idKeyC: '',
  120. saltValue: '',
  121. });
  122. const codeUrl = ref('');
  123. const loading = ref(false);
  124. const showPassword = ref(false);
  125. const rememberMeArr = ref<string[]>([]);
  126. const toastRef = ref<any>(null);
  127. onMounted(async () => {
  128. // --- 新增:登录拦截 ---
  129. // 如果缓存里已经有 Token,说明当前处于已登录状态
  130. // 不应该停留在当前登录大厅,直接跳走回首页
  131. const existingToken = Local.get(CACHE_KEY.TOKEN);
  132. if (existingToken) {
  133. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  134. console.warn('switchTab failed, trying reLaunch:', err);
  135. uni.reLaunch({ url: '/pages/home/index' });
  136. });
  137. return; // 不要继续执行后面的加载验证码之类
  138. }
  139. // 检查是否有记住的密码
  140. const savedUser = Local.get(CACHE_KEY.REMEMBER_USER);
  141. if (savedUser) {
  142. // 可能是对象也可能是 JSON
  143. const user = typeof savedUser === 'string' ? JSON.parse(savedUser) : savedUser;
  144. loginForm.userName = user.userName || user.username;
  145. loginForm.password = user.password;
  146. rememberMeArr.value = ['remember'];
  147. }
  148. await checkCaptcha();
  149. if (configSetting.value.isCaptcha === '10') {
  150. getCaptchaImg();
  151. }
  152. });
  153. const getCaptchaImg = async () => {
  154. try {
  155. const res = await loginApi.getCaptchaImg();
  156. codeUrl.value = res?.data?.base64stringC || '';
  157. loginForm.idKeyC = res?.data?.idKeyC || '';
  158. } catch (error) {
  159. console.error('获取验证码失败', error);
  160. }
  161. };
  162. const handleLogin = async () => {
  163. if (!loginForm.userName) {
  164. toastRef.value.show({ message: '请输入账号', type: 'error' });
  165. return;
  166. }
  167. if (!loginForm.password) {
  168. toastRef.value.show({ message: '请输入密码', type: 'error' });
  169. return;
  170. }
  171. if (configSetting.value.isCaptcha === '10' && !loginForm.idValueC) {
  172. toastRef.value.show({ message: '请输入验证码', type: 'error' });
  173. return;
  174. }
  175. loading.value = true;
  176. try {
  177. // 使用与后端完全匹配的加密函数加密密码
  178. const encryptedPassword = encryptWithBackendConfig(loginForm.password);
  179. loginForm.saltValue = encryptedPassword; // 后端AES IV常量 登录的时候 增加saltValue
  180. // 调用 store 处理真实请求和持久化
  181. await login(loginForm);
  182. // 记住密码逻辑独立于全局token的持久化
  183. if (rememberMeArr.value.includes('remember')) {
  184. Local.set(CACHE_KEY.REMEMBER_USER, {
  185. userName: loginForm.userName,
  186. password: loginForm.password
  187. });
  188. } else {
  189. Local.remove(CACHE_KEY.REMEMBER_USER);
  190. }
  191. toastRef.value.show({ message: '登录成功', type: 'success' });
  192. // 登录成功跳转首页
  193. setTimeout(() => {
  194. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  195. console.warn('switchTab failed, trying reLaunch:', err);
  196. uni.reLaunch({ url: '/pages/home/index' });
  197. });
  198. }, 1000);
  199. } catch (error) {
  200. console.error('Login Failed', error);
  201. // 登录失败若有验证码,刷新验证码
  202. if (configSetting.value.isCaptcha === '10') {
  203. loginForm.idValueC = '';
  204. getCaptchaImg();
  205. }
  206. } finally {
  207. loading.value = false;
  208. }
  209. };
  210. </script>
  211. <style scoped>
  212. .container {
  213. min-height: 100vh;
  214. position: relative;
  215. background: #f8fafc;
  216. overflow: hidden;
  217. display: flex;
  218. justify-content: center;
  219. align-items: center;
  220. padding: 0 40rpx;
  221. }
  222. .bg-shape {
  223. position: absolute;
  224. top: -100rpx;
  225. right: -100rpx;
  226. width: 500rpx;
  227. height: 500rpx;
  228. border-radius: 50%;
  229. background: linear-gradient(135deg, #3b82f6 0%, #2dd4bf 100%);
  230. opacity: 0.15;
  231. filter: blur(40rpx);
  232. }
  233. .bg-shape2 {
  234. position: absolute;
  235. bottom: -150rpx;
  236. left: -100rpx;
  237. width: 600rpx;
  238. height: 600rpx;
  239. border-radius: 50%;
  240. background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  241. opacity: 0.15;
  242. filter: blur(50rpx);
  243. }
  244. .login-box {
  245. width: 100%;
  246. max-width: 600rpx;
  247. background: rgba(255, 255, 255, 0.9);
  248. backdrop-filter: blur(20px);
  249. border-radius: 40rpx;
  250. padding: 60rpx 40rpx;
  251. box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.05);
  252. position: relative;
  253. z-index: 10;
  254. border: 2rpx solid rgba(255, 255, 255, 0.5);
  255. }
  256. .header {
  257. margin-bottom: 60rpx;
  258. }
  259. .title {
  260. font-size: 56rpx;
  261. font-weight: 800;
  262. color: #1e293b;
  263. display: block;
  264. margin-bottom: 16rpx;
  265. font-family: 'Helvetica Neue', Arial, sans-serif;
  266. }
  267. .subtitle {
  268. font-size: 28rpx;
  269. color: #64748b;
  270. display: block;
  271. }
  272. .form-group {
  273. margin-bottom: 40rpx;
  274. }
  275. .label {
  276. font-size: 26rpx;
  277. font-weight: 600;
  278. color: #334155;
  279. margin-bottom: 16rpx;
  280. display: block;
  281. }
  282. /* 移除原有的 input-container 和 checkbox 样式,采用 uvui 内置属性或者 customStyle 修改样式 */
  283. :deep(.placeholder-style) {
  284. color: #94a3b8 !important;
  285. font-size: 28rpx;
  286. }
  287. .captcha-group {
  288. display: flex;
  289. align-items: center;
  290. gap: 20rpx;
  291. }
  292. .captcha-img {
  293. width: 200rpx;
  294. height: 104rpx; /* 与左侧输入框 60(height) + 40(padding) + 4(border) 高度保持一致 */
  295. border-radius: 20rpx;
  296. background: #f1f5f9;
  297. flex-shrink: 0;
  298. cursor: pointer;
  299. }
  300. .options {
  301. display: flex;
  302. justify-content: space-between;
  303. align-items: center;
  304. margin-bottom: 50rpx;
  305. }
  306. .forgot {
  307. font-size: 26rpx;
  308. color: #3b82f6;
  309. font-weight: 500;
  310. padding: 10rpx 0;
  311. }
  312. .footer {
  313. margin-top: 40rpx;
  314. display: flex;
  315. justify-content: center;
  316. align-items: center;
  317. }
  318. .footer-text {
  319. font-size: 26rpx;
  320. color: #64748b;
  321. }
  322. .footer-link {
  323. font-size: 26rpx;
  324. color: #3b82f6;
  325. font-weight: 600;
  326. margin-left: 10rpx;
  327. }
  328. </style>