login.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <view class="mobile-login-page">
  3. <view class="login-top">
  4. <img class="login-logo" :src="systemLogo">
  5. <text class="login-text">
  6. {{systemName}}
  7. </text>
  8. </view>
  9. <view class="login-body">
  10. <view class="input-container">
  11. <icon class="login-body-icon user-icon"></icon>
  12. <input type="text" v-model="userName" placeholder="请输入用户名" class="login-body-input" />
  13. </view>
  14. <view class="input-container">
  15. <icon class="login-body-icon mm-icon"></icon>
  16. <input type="password" v-model="password" placeholder="请输入密码" class="login-body-input" />
  17. </view>
  18. <view class="login-change">
  19. <checkbox-group @change="rememberBtn" class="remember-checkbox-group">
  20. <checkbox value="remember" color="#0550e5" checked="true" style="transform:scale(0.9)" />记住此账号
  21. </checkbox-group>
  22. <text class="checkbox-zhanghao" @click="forgetPassword"> 忘记密码?</text>
  23. </view>
  24. <button class="login-btn" @click="handleLogin">登录</button>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup>
  29. import CryptoJS from 'crypto-js';
  30. import {
  31. toast
  32. } from "@/utils/common";
  33. import {
  34. onLoad
  35. } from "@dcloudio/uni-app";
  36. import {
  37. getAppConfig,
  38. login
  39. } from '@/api/login.js'
  40. import cacheManager from '@/utils/cacheManager.js'
  41. import JSEncrypt from 'jsencrypt';
  42. import {
  43. ref
  44. } from 'vue';
  45. import {
  46. getAllImgList
  47. } from '@/api/index.js';
  48. const passLLiRef = ref(null)
  49. let systemName = ref('')
  50. let systemLogo = ref('')
  51. let userName = ref('')
  52. let password = ref('')
  53. let configData = ref(null)
  54. let encryptedPassword = ref(''); // 用于存储加密后的密码
  55. let rememberFlag = ref('remember');
  56. // 密钥和偏移量(IV),需要转换为CryptoJS的WordArray格式
  57. const key = CryptoJS.enc.Utf8.parse('ax58bc32hp54kt68');
  58. const iv = CryptoJS.enc.Utf8.parse('cf46xy53kb64wt25');
  59. // 加密算法:AES/CBC 偏移:PKCS5Padding
  60. function encryptPassword(plainText) {
  61. const ciphertext = CryptoJS.AES.encrypt(plainText, key, {
  62. iv: iv,
  63. mode: CryptoJS.mode.CBC,
  64. padding: CryptoJS.pad.Pkcs7
  65. });
  66. return ciphertext.toString();
  67. }
  68. onLoad(() => {
  69. uni.showLoading({
  70. title: '加载中'
  71. });
  72. // 获得全部图片
  73. getAllImg()
  74. })
  75. function getAllImg() {
  76. getAllImgList({}).then(res => {
  77. cacheManager.set('projectImg', res.data)
  78. getLoginInit()
  79. });
  80. }
  81. // 记住密码
  82. function geiRemember() {
  83. if (rememberFlag.value === 'remember') {
  84. if (cacheManager.get('auth').userInfo) {
  85. userName.value = cacheManager.get('auth').userInfo.userName;
  86. password.value = cacheManager.get('auth').userInfo.password;
  87. }
  88. }
  89. }
  90. function rememberBtn(data) {
  91. rememberFlag.value = data.detail.value.toString();
  92. }
  93. function getLoginInit() {
  94. getLoginConfig();
  95. geiRemember();
  96. }
  97. function getLoginConfig() {
  98. getAppConfig().then(res => {
  99. console.log(res.data, 'res');
  100. systemName.value = res.data.name;
  101. // if (res.data.logo) {
  102. // systemLogo.value = res.data.logo;
  103. // } else {
  104. // // login-logo-sj
  105. // systemLogo.value = cacheManager.get('projectImg').login_logo_sj;
  106. // }
  107. systemLogo.value = cacheManager.get('projectImg').login_logo_sj;
  108. configData.value = res.data;
  109. uni.hideLoading();
  110. })
  111. }
  112. function handleLogin() {
  113. if (userName.value.length === 0) {
  114. toast('请输入手机号!')
  115. return
  116. }
  117. if (password.value.length === 0) {
  118. toast('请输入密码!')
  119. return
  120. }
  121. encryptedPassword.value = encryptPassword(password.value.trim());
  122. // 去除 userName 两端的空格
  123. const trimmedUserName = userName.value.trim();
  124. let req = {
  125. userName: trimmedUserName,
  126. password: encryptedPassword.value,
  127. }
  128. login(req).then(res => {
  129. // 接口数据记缓存
  130. if (rememberFlag.value === 'remember') {
  131. let userInfo = {
  132. userName: trimmedUserName,
  133. password: password.value.trim(),
  134. }
  135. let mergedData = {
  136. ...res.data,
  137. ...configData.value,
  138. userInfo
  139. };
  140. cacheManager.set('auth', mergedData)
  141. } else {
  142. let mergedData = {
  143. ...res.data,
  144. ...configData.value
  145. };
  146. cacheManager.set('auth', mergedData)
  147. }
  148. // 待补充----判断checked信息,记缓存用户信息
  149. // 待补充根据后台返回值跳转不同页面
  150. if (res.data.modules === '1') {
  151. // 考试
  152. uni.switchTab({
  153. url: '/pages/index/index'
  154. });
  155. } else {
  156. // 培训
  157. uni.switchTab({
  158. url: '/pages/index/index'
  159. });
  160. }
  161. })
  162. }
  163. function forgetPassword() {
  164. toast('请联系管理员修改密码!');
  165. }
  166. </script>