index.vue 11 KB

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