account.vue 6.7 KB

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