account.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <el-form size="large" class="login-content-form" :model="state.ruleForm" :rules="state.rules">
  3. <el-form-item class="login-animation1" prop="name">
  4. <el-input text placeholder="请输入手机号"
  5. v-model="state.ruleForm.name" clearable autocomplete="off">
  6. <template #prefix>
  7. <el-icon class="el-input__icon">
  8. <ele-User/>
  9. </el-icon>
  10. </template>
  11. </el-input>
  12. </el-form-item>
  13. <el-form-item class="login-animation2" prop="pwd">
  14. <el-input
  15. type="password"
  16. :placeholder="$t('message.account.accountPlaceholder2')"
  17. v-model="state.ruleForm.pwd"
  18. autocomplete="off"
  19. >
  20. <template #prefix>
  21. <el-icon class="el-input__icon">
  22. <ele-Unlock/>
  23. </el-icon>
  24. </template>
  25. <!-- <template #suffix>
  26. <i
  27. class="iconfont el-input__icon login-content-password"
  28. :class="state.isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
  29. @click="state.isShowPassword = !state.isShowPassword"
  30. >
  31. </i>
  32. </template>-->
  33. </el-input>
  34. </el-form-item>
  35. <el-form-item class="login-animation4">
  36. <el-button type="primary" class="login-content-submit" round v-waves @click="onSignIn" :loading="state.loading.signIn">
  37. <span>{{ $t('message.account.accountBtnText') }}</span>
  38. </el-button>
  39. <el-button type="danger" text class="login-content-forget" @click="handleForgetPassword">忘记密码?</el-button>
  40. </el-form-item>
  41. </el-form>
  42. </template>
  43. <script setup lang="ts" name="loginAccount">
  44. import {reactive, computed, onMounted} from 'vue';
  45. import {useRoute, useRouter} from 'vue-router';
  46. import {ElMessage, ElNotification} from 'element-plus';
  47. import {useI18n} from 'vue-i18n';
  48. import Cookies from 'js-cookie';
  49. import {storeToRefs} from 'pinia';
  50. import {useThemeConfig} from '/@/stores/themeConfig';
  51. import {Session} from '/@/utils/storage';
  52. import {formatAxis} from '/@/utils/formatTime';
  53. import {NextLoading} from '/@/utils/loading';
  54. import {$body, $get} from '/@/utils/request'
  55. import {useUserInfo} from "/@/stores/userInfo";
  56. import { JSEncrypt } from 'jsencrypt'
  57. import u from "/@/utils/u";
  58. // 定义变量内容
  59. const {t} = useI18n();
  60. const storesThemeConfig = useThemeConfig();
  61. const {themeConfig} = storeToRefs(storesThemeConfig);
  62. const storesUserInfo = useUserInfo();
  63. const route = useRoute();
  64. const router = useRouter();
  65. const state = reactive({
  66. isShowPassword: false,
  67. ruleForm: {
  68. name: 'yaopeng',
  69. pwd: '1234',
  70. code: '1234',
  71. sign: '' as string
  72. },
  73. loading: {
  74. signIn: false,
  75. },
  76. verifySrc: '' as string,
  77. userName: ' ' as string,
  78. nickName: ' ' as string,
  79. rules:{
  80. name:[u.validator.required],
  81. pwd:[u.validator.required],
  82. code:[u.validator.required],
  83. }
  84. });
  85. // 时间获取
  86. const currentTime = computed(() => {
  87. return formatAxis(new Date());
  88. });
  89. onMounted(() => {
  90. });
  91. const handleForgetPassword = () => {
  92. // Message.alert(`请联系管理员400-1234567`)
  93. //forgetPasswordRef.open();
  94. }
  95. const encryptData = (str:string)=>{
  96. let encryptor = new JSEncrypt()
  97. // 设置公钥 (这是后端直接给我的,看你们项目情况是需要调接口获得,还是程序中直接写死)
  98. let publicKey = import.meta.env.VITE_PUBLIC_KEY
  99. encryptor.setPublicKey(publicKey) // publicKey为公钥
  100. // 加密数据
  101. return encryptor.encrypt(str)
  102. }
  103. const initData = () => {
  104. $body("/dataDict/list", {}).then((res: any) => {
  105. Session.set("dicts", res);
  106. });
  107. $get("/admin-user/profile").then((obj: any) => {
  108. if (obj) {
  109. let {user, permissionList} = obj;
  110. let userInfo = {...user, permList: permissionList}
  111. /* app.user = user;
  112. app.userId = user.id;
  113. app.permissions = permissionList;
  114. app.departmentMemberList = departmentMemberList;*/
  115. storesUserInfo.setUserInfos(userInfo);
  116. state.loading.signIn = false;
  117. ElNotification.success({
  118. title: 'Success',
  119. message: "欢迎," + user.username,
  120. offset: 100,
  121. })
  122. }
  123. }).catch(err => {
  124. ElMessage.error("登录状态失效,请重新登录管理控制台");
  125. // Session.clear();
  126. state.loading.signIn = false;
  127. });
  128. }
  129. const refreshLogin = () => {
  130. setInterval(() => {
  131. $get(`admin-user/refresh`);
  132. }, 1600 * 1000)
  133. }
  134. // 登录
  135. const onSignIn = async () => {
  136. state.loading.signIn = true;
  137. let temp = {
  138. mobilePhone:state.ruleForm.name,
  139. password:encryptData(state.ruleForm.pwd)
  140. }
  141. $body(`/admin-user/login`, temp).then((res: any) => {
  142. let {id,satoken} = res;
  143. console.log(res)
  144. if (satoken) {
  145. // 存储 token 到浏览器缓存
  146. Session.set('token', satoken);
  147. Cookies.set('userId', id);
  148. initData();
  149. // refreshLogin();
  150. state.loading.signIn = false;
  151. signInSuccess(false);
  152. } else {
  153. ElMessage.error(res.msg);
  154. state.loading.signIn = false;
  155. }
  156. }).catch(e => {
  157. state.loading.signIn = false;
  158. console.error(e)
  159. });
  160. };
  161. // 登录成功后的跳转
  162. const signInSuccess = (isNoPower: boolean | undefined) => {
  163. if (isNoPower) {
  164. ElMessage.warning('抱歉,您没有登录权限');
  165. Session.clear();
  166. } else {
  167. // 初始化登录成功时间问候语
  168. let currentTimeInfo = currentTime.value;
  169. // 登录成功,跳到转首页
  170. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  171. if (route.query?.redirect) {
  172. router.push({
  173. path: <string>route.query?.redirect,
  174. query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
  175. });
  176. } else {
  177. router.push('/');
  178. }
  179. // router.replace('/');
  180. // 登录成功提示
  181. const signInText = t('message.signInText');
  182. ElMessage.success(`${currentTimeInfo},${signInText}`);
  183. // 添加 loading,防止第一次进入界面时出现短暂空白
  184. NextLoading.start();
  185. }
  186. state.loading.signIn = false;
  187. };
  188. </script>
  189. <style scoped lang="scss">
  190. .login-content-form {
  191. margin-top: 20px;
  192. @for $i from 1 through 4 {
  193. .login-animation#{$i} {
  194. opacity: 0;
  195. animation-name: error-num;
  196. animation-duration: 0.5s;
  197. animation-fill-mode: forwards;
  198. animation-delay: calc($i/10) + s;
  199. }
  200. }
  201. .login-content-password {
  202. display: inline-block;
  203. width: 20px;
  204. cursor: pointer;
  205. &:hover {
  206. color: #909399;
  207. }
  208. }
  209. .login-content-code {
  210. width: 100%;
  211. padding: 0;
  212. font-weight: bold;
  213. letter-spacing: 5px;
  214. }
  215. .login-content-submit {
  216. width: 80%;
  217. letter-spacing: 2px;
  218. font-weight: 300;
  219. margin-top: 15px;
  220. }
  221. .login-content-forget {
  222. width: 13%;
  223. letter-spacing: 2px;
  224. font-weight: 300;
  225. margin-top: 15px;
  226. margin-left: 5%;
  227. }
  228. }
  229. </style>