index.vue 12 KB

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