account.vue 7.5 KB

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