index_his.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. const goToRegister = () => {
  190. uni.navigateTo({ url: '/pages/login/register' });
  191. };
  192. /**
  193. * 钉钉免登逻辑
  194. */
  195. const handleDingTalkLogin = async () => {
  196. dingTalkLoading.value = true;
  197. try {
  198. // 尝试从当前 URL 获取 corpId,或者使用配置
  199. // 注意:如果是通过 HBuilderX 运行到浏览器的,通常需要手动传入 corpId 调试
  200. const corpId = uni.getLaunchOptionsSync()?.query?.corpId || import.meta.env.VITE_DINGTALK_CORPID;
  201. const code = await getDingTalkAuthCode(corpId);
  202. if (!code) {
  203. throw new Error('获取授权码失败');
  204. }
  205. await dingTalkLogin(code);
  206. toastRef.value.show({ message: '钉钉免登成功', type: 'success' });
  207. // 跳转首页
  208. setTimeout(() => {
  209. uni.switchTab({ url: '/pages/home/index' }).catch((err) => {
  210. uni.reLaunch({ url: '/pages/home/index' });
  211. });
  212. }, 1000);
  213. } catch (error: any) {
  214. console.error('DingTalk Login Error:', error);
  215. toastRef.value.show({
  216. message: error.message || error.errorMessage || '钉钉免登失败',
  217. type: 'error'
  218. });
  219. } finally {
  220. dingTalkLoading.value = false;
  221. }
  222. };
  223. </script>
  224. <style scoped>
  225. .container {
  226. min-height: 100vh;
  227. position: relative;
  228. background: #f8fafc;
  229. overflow: hidden;
  230. display: flex;
  231. justify-content: center;
  232. align-items: center;
  233. padding: 0 40rpx;
  234. }
  235. .bg-shape {
  236. position: absolute;
  237. top: -100rpx;
  238. right: -100rpx;
  239. width: 500rpx;
  240. height: 500rpx;
  241. border-radius: 50%;
  242. background: linear-gradient(135deg, #3b82f6 0%, #2dd4bf 100%);
  243. opacity: 0.15;
  244. filter: blur(40rpx);
  245. }
  246. .bg-shape2 {
  247. position: absolute;
  248. bottom: -150rpx;
  249. left: -100rpx;
  250. width: 600rpx;
  251. height: 600rpx;
  252. border-radius: 50%;
  253. background: linear-gradient(135deg, #8b5cf6 0%, #ec4899 100%);
  254. opacity: 0.15;
  255. filter: blur(50rpx);
  256. }
  257. .login-box {
  258. width: 100%;
  259. max-width: 600rpx;
  260. background: rgba(255, 255, 255, 0.9);
  261. backdrop-filter: blur(20px);
  262. border-radius: 40rpx;
  263. padding: 60rpx 40rpx;
  264. box-shadow: 0 20rpx 40rpx rgba(0, 0, 0, 0.05);
  265. position: relative;
  266. z-index: 10;
  267. border: 2rpx solid rgba(255, 255, 255, 0.5);
  268. }
  269. .header {
  270. margin-bottom: 60rpx;
  271. }
  272. .title {
  273. font-size: 56rpx;
  274. font-weight: 800;
  275. color: #1e293b;
  276. display: block;
  277. margin-bottom: 16rpx;
  278. font-family: 'Helvetica Neue', Arial, sans-serif;
  279. }
  280. .subtitle {
  281. font-size: 28rpx;
  282. color: #64748b;
  283. display: block;
  284. }
  285. .form-group {
  286. margin-bottom: 40rpx;
  287. }
  288. .label {
  289. font-size: 26rpx;
  290. font-weight: 600;
  291. color: #334155;
  292. margin-bottom: 16rpx;
  293. display: block;
  294. }
  295. /* 移除原有的 input-container 和 checkbox 样式,采用 uvui 内置属性或者 customStyle 修改样式 */
  296. :deep(.placeholder-style) {
  297. color: #94a3b8 !important;
  298. font-size: 28rpx;
  299. }
  300. .captcha-group {
  301. display: flex;
  302. align-items: center;
  303. gap: 20rpx;
  304. }
  305. .captcha-img {
  306. width: 200rpx;
  307. height: 104rpx;
  308. /* 与左侧输入框 60(height) + 40(padding) + 4(border) 高度保持一致 */
  309. border-radius: 20rpx;
  310. background: #f1f5f9;
  311. flex-shrink: 0;
  312. cursor: pointer;
  313. }
  314. .options {
  315. display: flex;
  316. justify-content: space-between;
  317. align-items: center;
  318. margin-bottom: 50rpx;
  319. }
  320. .forgot {
  321. font-size: 26rpx;
  322. color: #3b82f6;
  323. font-weight: 500;
  324. padding: 10rpx 0;
  325. }
  326. .footer {
  327. margin-top: 40rpx;
  328. display: flex;
  329. justify-content: center;
  330. align-items: center;
  331. }
  332. .footer-text {
  333. font-size: 26rpx;
  334. color: #64748b;
  335. }
  336. .footer-link {
  337. font-size: 26rpx;
  338. color: #3b82f6;
  339. font-weight: 600;
  340. margin-left: 10rpx;
  341. }
  342. </style>