Account.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <el-form size="large" class="login-content-form" ref="ruleFormRef" :model="state.ruleForm" @submit.native.prevent>
  3. <el-form-item class="login-animation1 mb30" prop="userName" :rules="[{ required: true, message: '请输入账号', trigger: 'blur' }]">
  4. <el-input
  5. type="text"
  6. class="inputDeep"
  7. placeholder="请输入账号"
  8. v-model="state.ruleForm.userName"
  9. clearable
  10. @keyup.enter="onSignIn(ruleFormRef)"
  11. autocomplete="off"
  12. >
  13. <template #prefix>
  14. <SvgIcon name="ele-User" class="el-input__icon" />
  15. </template>
  16. </el-input>
  17. </el-form-item>
  18. <el-form-item class="login-animation2 mb30" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
  19. <el-input
  20. class="inputDeep"
  21. clearable
  22. show-password
  23. placeholder="请输入密码"
  24. v-model="state.ruleForm.password"
  25. @keyup.enter="onSignIn(ruleFormRef)"
  26. autocomplete="off"
  27. >
  28. <template #prefix>
  29. <SvgIcon name="ele-Unlock" class="el-input__icon" />
  30. </template>
  31. </el-input>
  32. </el-form-item>
  33. <!-- <el-form-item class="login-animation3">
  34. <el-col :span="15">
  35. <el-input type="text" maxlength="4" class="inputDeep" placeholder="请输入验证码" v-model="state.ruleForm.code" @keyup.enter="onSignIn(ruleFormRef)" clearable autocomplete="off">
  36. <template #prefix>
  37. <SvgIcon name="ele-Position" class="el-input__icon"/>
  38. </template>
  39. </el-input>
  40. </el-col>
  41. <el-col :span="8" :offset=1>
  42. <el-button class="login-content-code">1234</el-button>
  43. </el-col>
  44. </el-form-item>-->
  45. <el-form-item class="login-animation4">
  46. <el-button type="primary" class="login-content-submit" round @click="onSignIn(ruleFormRef)" v-waves="'light'" :loading="state.loading"
  47. >登录</el-button
  48. >
  49. </el-form-item>
  50. <!-- <div class="font12 mt30 login-animation4 login-msg">
  51. * 温馨提示:建议使用谷歌、Microsoft Edge,版本 79.0.1072.62 及以上浏览器,360浏览器请使用极速模式
  52. </div> -->
  53. <div class="login-msg login-animation4">
  54. <div>联系管理员<b>重置密码</b></div>
  55. <!-- <el-button link type="primary" class="font16" @click="forgetPwd">忘记密码</el-button> -->
  56. </div>
  57. </el-form>
  58. </template>
  59. <script setup lang="ts" name="loginAccount">
  60. import { reactive, computed, ref } from 'vue';
  61. import { useRoute, useRouter } from 'vue-router';
  62. import { ElNotification } from 'element-plus';
  63. import { storeToRefs } from 'pinia';
  64. import { useThemeConfig } from '/@/stores/themeConfig';
  65. import { initFrontEndControlRoutes } from '/@/router/frontEnd';
  66. import { initBackEndControlRoutes } from '/@/router/backEnd';
  67. import { Session, Local, Cookie } from '/@/utils/storage';
  68. import { formatAxis } from '/@/utils/formatTime';
  69. import { NextLoading } from '/@/utils/loading';
  70. import Watermark from '/@/utils/watermark';
  71. import type { FormInstance } from 'element-plus';
  72. import { signIn } from '/@/api/login';
  73. import { JSEncrypt } from 'jsencrypt'; // rsa加密
  74. // 定义变量内容
  75. const storesThemeConfig = useThemeConfig(); // 主题配置
  76. const { themeConfig } = storeToRefs(storesThemeConfig); // 主题配置
  77. const route = useRoute(); // 路由
  78. const router = useRouter(); // 路由
  79. const state = reactive<any>({
  80. ruleForm: {
  81. userName: '', // 账号
  82. password: '', // 密码
  83. },
  84. loading: false, // 加载
  85. });
  86. const ruleFormRef = ref<FormInstance>(); // 表单ref
  87. // 时间获取
  88. const currentTime = computed(() => {
  89. return formatAxis(new Date());
  90. });
  91. // 存储布局配置
  92. const setLocalThemeConfig = () => {
  93. Local.remove('themeConfig');
  94. Local.set('themeConfig', themeConfig.value);
  95. };
  96. // 登录
  97. const onSignIn = async (formEl: FormInstance | undefined) => {
  98. if (!formEl) return;
  99. await formEl.validate((valid: boolean) => {
  100. if (!valid) return;
  101. state.loading = true;
  102. // 新建一个JSEncrypt对象
  103. const encryptor = new JSEncrypt({ default_key_size: '2048' });
  104. // 设置公钥
  105. const publicKey =
  106. '-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgw+/x6IQPkH0A4eoF63jkLThsOXWyNBdcL9LATGy/G1yTHOr1RyKJB//iNug+V8DIoIHuFTlhgLHDbSqxvRWMONxIIF289riS6bDI4Ox/pFmOfmElFRk0lKGihaTE2Aefd6g/N+RfLLaHWztY+/voVeDTiOIw9y3tokIxjKwuJ/mQ66MkKh78AqQjjSD/3jcBP8ZhMyCJOK9XQcqvhD6WBFWkxlAqKOWggDU7YohfrbNkg3bd0oGE6zCE2EHhkcQbzGCh3lu1zf4TfKMXD+PPrr5JWDNYQTXFQklqgae+Puge7xxZGYRoi5YpIUnkQGm6zpPxhIOdxlz+Yb5geSJUQIDAQAB-----END PUBLIC KEY-----';
  107. encryptor.setPublicKey(publicKey); // publicKey为公钥
  108. // 加密数据
  109. const submitObj = {
  110. userName: encryptor.encrypt(state.ruleForm.userName),
  111. password: encryptor.encrypt(state.ruleForm.password),
  112. };
  113. signIn(submitObj)
  114. .then(async (res: any) => {
  115. //登录
  116. // 存储 token 到浏览器缓存
  117. Cookie.set('token', res.result);
  118. if (!themeConfig.value.isRequestRoutes) {
  119. // 前端控制路由,2、请注意执行顺序
  120. const isNoPower = await initFrontEndControlRoutes();
  121. signInSuccess(isNoPower);
  122. } else {
  123. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  124. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  125. const isNoPower = await initBackEndControlRoutes();
  126. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  127. signInSuccess(isNoPower);
  128. }
  129. })
  130. .catch(() => {
  131. state.loading = false;
  132. });
  133. });
  134. };
  135. // 登录成功后的跳转
  136. const signInSuccess = (isNoPower: boolean | undefined) => {
  137. if (isNoPower) {
  138. state.loading = false;
  139. ElNotification({
  140. title: '提示',
  141. message: '抱歉,您没有登录权限,请联系管理员',
  142. type: 'warning',
  143. });
  144. Session.clear();
  145. Cookie.clear();
  146. Local.clear();
  147. } else {
  148. // 初始化登录成功时间问候语
  149. let currentTimeInfo = currentTime.value;
  150. // 登录成功,跳到转首页
  151. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  152. if (route.query?.redirect) {
  153. router.push({
  154. path: <string>route.query?.redirect,
  155. query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
  156. });
  157. } else {
  158. router.push('/');
  159. }
  160. // 设置水印
  161. themeConfig.value.isWatermark = true;
  162. themeConfig.value.watermarkText = state.ruleForm.userName;
  163. Watermark.set(state.ruleForm.userName);
  164. Cookie.set('userName', state.ruleForm.userName);
  165. setLocalThemeConfig();
  166. // 登录成功提示
  167. // 关闭 loading
  168. state.loading = true;
  169. const signInText = '欢迎回来!';
  170. ElNotification({
  171. title: currentTimeInfo,
  172. message: `${currentTimeInfo},${signInText}`,
  173. type: 'success',
  174. });
  175. NextLoading.start();
  176. }
  177. };
  178. // 忘记密码
  179. // const forgetPwd = ()=>{
  180. // router.push({
  181. // path:'/forgetPwd'
  182. // })
  183. // }
  184. </script>
  185. <style scoped lang="scss">
  186. .inputDeep {
  187. :deep(.el-input__wrapper) {
  188. box-shadow: 0 0 0 0 var(--el-input-border-color, var(--el-border-color)) inset;
  189. border-radius: 0;
  190. border-bottom: 1px solid var(--el-input-border-color, var(--el-border-color));
  191. }
  192. }
  193. .login-content-form {
  194. margin-top: 20px;
  195. font-size: 16px;
  196. :deep(.el-input--large) {
  197. font-size: 16px;
  198. }
  199. :deep(.el-form-item__error) {
  200. font-size: 14px;
  201. }
  202. @for $i from 1 through 4 {
  203. .login-animation#{$i} {
  204. opacity: 0;
  205. animation-name: error-num;
  206. animation-duration: 0.5s;
  207. animation-fill-mode: forwards;
  208. animation-delay: calc($i/10) + s;
  209. }
  210. }
  211. .login-content-password {
  212. display: inline-block;
  213. width: 20px;
  214. cursor: pointer;
  215. &:hover {
  216. color: #909399;
  217. }
  218. }
  219. .login-content-code {
  220. width: 100%;
  221. padding: 0;
  222. font-weight: bold;
  223. letter-spacing: 5px;
  224. }
  225. .login-content-submit {
  226. width: 100%;
  227. font-weight: 300;
  228. margin-top: 85px;
  229. height: 50px;
  230. border-radius: 30px;
  231. font-size: 16px;
  232. letter-spacing: 5px;
  233. }
  234. .login-msg {
  235. margin-top: 39px;
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. color: var(--el-color-primary);
  240. b {
  241. color: #999;
  242. padding-left: 4px;
  243. }
  244. }
  245. }
  246. </style>