account.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <el-form ref="formRef" size="large" class="login-content-form" :model="state.ruleForm" :rules="rules">
  3. <el-form-item class="login-animation1" prop="username">
  4. <el-input
  5. type="text"
  6. :placeholder="$t('message.account.accountPlaceholder1')"
  7. v-model="ruleForm.username"
  8. clearable
  9. autocomplete="off">
  10. <template #prefix>
  11. <el-icon class="el-input__icon">
  12. <ele-User />
  13. </el-icon>
  14. </template>
  15. </el-input>
  16. </el-form-item>
  17. <el-form-item class="login-animation2" prop="password">
  18. <el-input
  19. :type="isShowPassword ? 'text' : 'password'"
  20. :placeholder="$t('message.account.accountPlaceholder2')"
  21. v-model="ruleForm.password">
  22. <template #prefix>
  23. <el-icon class="el-input__icon">
  24. <ele-Unlock />
  25. </el-icon>
  26. </template>
  27. <template #suffix>
  28. <i
  29. class="iconfont el-input__icon login-content-password"
  30. :class="isShowPassword ? 'icon-yincangmima' : 'icon-xianshimima'"
  31. @click="isShowPassword = !isShowPassword">
  32. </i>
  33. </template>
  34. </el-input>
  35. </el-form-item>
  36. <el-form-item class="login-animation3" v-if="isShowCaptcha" prop="captcha">
  37. <el-col :span="15">
  38. <el-input
  39. type="text"
  40. maxlength="4"
  41. :placeholder="$t('message.account.accountPlaceholder3')"
  42. v-model="ruleForm.captcha"
  43. clearable
  44. autocomplete="off">
  45. <template #prefix>
  46. <el-icon class="el-input__icon">
  47. <ele-Position />
  48. </el-icon>
  49. </template>
  50. </el-input>
  51. </el-col>
  52. <el-col :span="1"></el-col>
  53. <el-col :span="8">
  54. <el-button class="login-content-captcha">
  55. <el-image :src="ruleForm.captchaImgBase" @click="refreshCaptcha" />
  56. </el-button>
  57. </el-col>
  58. </el-form-item>
  59. <el-form-item class="login-animation4">
  60. <el-button
  61. type="primary"
  62. class="login-content-submit"
  63. round
  64. @keyup.enter="loginClick"
  65. @click="loginClick"
  66. :loading="loading.signIn">
  67. <span>{{ $t('message.account.accountBtnText') }}</span>
  68. </el-button>
  69. <el-button type="primary" class="login-content-submit" round @click="qrLoginClick"> 企业微信扫码登录</el-button>
  70. </el-form-item>
  71. </el-form>
  72. </template>
  73. <script lang="ts">
  74. import { toRefs, reactive, defineComponent, computed, onMounted, onUnmounted, ref } from 'vue'
  75. import { useRoute, useRouter } from 'vue-router'
  76. import { ElMessage, FormInstance, FormRules } from 'element-plus'
  77. import { useI18n } from 'vue-i18n'
  78. import Cookies from 'js-cookie'
  79. import { storeToRefs } from 'pinia'
  80. import { useThemeConfig } from '/@/stores/themeConfig'
  81. import { initFrontEndControlRoutes } from '/@/router/frontEnd'
  82. import { initBackEndControlRoutes } from '/@/router/backEnd'
  83. import { Session } from '/@/utils/storage'
  84. import { formatAxis } from '/@/utils/formatTime'
  85. import { NextLoading } from '/@/utils/loading'
  86. import * as loginApi from '/@/views/system/login/api'
  87. import { useUserInfo } from '/@/stores/userInfo'
  88. import { DictionaryStore } from '/@/stores/dictionary'
  89. import { SystemConfigStore } from '/@/stores/systemConfig'
  90. import { BtnPermissionStore } from '/@/plugin/permission/store.permission'
  91. import { Md5 } from 'ts-md5'
  92. import { errorMessage } from '/@/utils/message'
  93. import { getWorkWeChatCode, postWorkWeChatCode } from '../api'
  94. export default defineComponent({
  95. name: 'loginAccount',
  96. setup() {
  97. const { t } = useI18n()
  98. const storesThemeConfig = useThemeConfig()
  99. const { themeConfig } = storeToRefs(storesThemeConfig)
  100. const { userInfos } = storeToRefs(useUserInfo())
  101. const route = useRoute()
  102. const router = useRouter()
  103. const state = reactive({
  104. isShowPassword: false,
  105. ruleForm: {
  106. username: '',
  107. password: '',
  108. captcha: '',
  109. captchaKey: '',
  110. captchaImgBase: '',
  111. },
  112. loading: {
  113. signIn: false,
  114. },
  115. })
  116. const rules = reactive<FormRules>({
  117. username: [{ required: true, message: '请填写账号', trigger: 'blur' }],
  118. password: [
  119. {
  120. required: true,
  121. message: '请填写密码',
  122. trigger: 'blur',
  123. },
  124. ],
  125. captcha: [
  126. {
  127. required: true,
  128. message: '请填写验证码',
  129. trigger: 'blur',
  130. },
  131. ],
  132. })
  133. const formRef = ref()
  134. // 时间获取
  135. const currentTime = computed(() => {
  136. return formatAxis(new Date())
  137. })
  138. // 是否关闭验证码
  139. const isShowCaptcha = computed(() => {
  140. return SystemConfigStore().systemConfig['base.captcha_state']
  141. })
  142. const getCaptcha = async () => {
  143. loginApi.getCaptcha().then((ret: any) => {
  144. state.ruleForm.captchaImgBase = ret.data.image_base
  145. state.ruleForm.captchaKey = ret.data.key
  146. })
  147. }
  148. const refreshCaptcha = async () => {
  149. loginApi.getCaptcha().then((ret: any) => {
  150. state.ruleForm.captchaImgBase = ret.data.image_base
  151. state.ruleForm.captchaKey = ret.data.key
  152. })
  153. }
  154. const loginClick = async () => {
  155. if (!formRef.value) return
  156. await formRef.value.validate((valid: any) => {
  157. if (valid) {
  158. loginApi.login({ ...state.ruleForm, password: Md5.hashStr(state.ruleForm.password) }).then((res: any) => {
  159. if (res.code === 2000) {
  160. Session.set('token', res.data.access)
  161. Cookies.set('username', res.data.name)
  162. if (!themeConfig.value.isRequestRoutes) {
  163. // 前端控制路由,2、请注意执行顺序
  164. initFrontEndControlRoutes()
  165. loginSuccess()
  166. } else {
  167. // 模拟后端控制路由,isRequestRoutes 为 true,则开启后端控制路由
  168. // 添加完动态路由,再进行 router 跳转,否则可能报错 No match found for location with path "/"
  169. initBackEndControlRoutes()
  170. // 执行完 initBackEndControlRoutes,再执行 signInSuccess
  171. loginSuccess()
  172. }
  173. } else if (res.code === 4000) {
  174. // 登录错误之后,刷新验证码
  175. refreshCaptcha()
  176. }
  177. })
  178. } else {
  179. errorMessage('请填写登录信息')
  180. }
  181. })
  182. }
  183. const getUserInfo = () => {
  184. useUserInfo().setUserInfos()
  185. }
  186. // 登录成功后的跳转
  187. const loginSuccess = () => {
  188. //登录成功获取用户信息,获取系统字典数据
  189. getUserInfo()
  190. //获取所有字典
  191. DictionaryStore().getSystemDictionarys()
  192. // 初始化登录成功时间问候语
  193. let currentTimeInfo = currentTime.value
  194. // 登录成功,跳到转首页
  195. // 如果是复制粘贴的路径,非首页/登录页,那么登录成功后重定向到对应的路径中
  196. if (route.query?.redirect) {
  197. router.push({
  198. path: <string>route.query?.redirect,
  199. query: Object.keys(<string>route.query?.params).length > 0 ? JSON.parse(<string>route.query?.params) : '',
  200. })
  201. } else {
  202. router.push('/')
  203. }
  204. // 登录成功提示
  205. // 关闭 loading
  206. state.loading.signIn = true
  207. const signInText = t('message.signInText')
  208. ElMessage.success(`${currentTimeInfo},${signInText}`)
  209. // 添加 loading,防止第一次进入界面时出现短暂空白
  210. NextLoading.start()
  211. }
  212. const qrCode = ref('')
  213. async function qrLoginClick() {
  214. try {
  215. const response = await getWorkWeChatCode()
  216. window.location.href = response.data
  217. qrCode.value = response.data
  218. // if (qrCode.value) {
  219. console.log(123, this.$route.query.code)
  220. // await WorkWeChatCodeLogin()
  221. // }
  222. } catch (error) {
  223. console.log('error:', error)
  224. }
  225. }
  226. async function WorkWeChatCodeLogin() {
  227. const post = {
  228. code: qrCode.value,
  229. state: 'Wechat',
  230. }
  231. try {
  232. const response = await postWorkWeChatCode(post)
  233. console.log('response', response.data)
  234. } catch (error) {
  235. console.log('error:', error)
  236. }
  237. }
  238. onMounted(() => {
  239. getCaptcha()
  240. //获取系统配置
  241. SystemConfigStore().getSystemConfigs()
  242. })
  243. return {
  244. refreshCaptcha,
  245. loginClick,
  246. loginSuccess,
  247. isShowCaptcha,
  248. state,
  249. formRef,
  250. rules,
  251. ...toRefs(state),
  252. qrLoginClick,
  253. qrCode,
  254. WorkWeChatCodeLogin,
  255. }
  256. },
  257. })
  258. </script>
  259. <style scoped lang="scss">
  260. .login-content-form {
  261. margin-top: 20px;
  262. @for $i from 1 through 4 {
  263. .login-animation#{$i} {
  264. opacity: 0;
  265. animation-name: error-num;
  266. animation-duration: 0.5s;
  267. animation-fill-mode: forwards;
  268. animation-delay: calc($i/10) + s;
  269. }
  270. }
  271. .login-content-password {
  272. display: inline-block;
  273. width: 20px;
  274. cursor: pointer;
  275. &:hover {
  276. color: #909399;
  277. }
  278. }
  279. .login-content-captcha {
  280. width: 100%;
  281. padding: 0;
  282. font-weight: bold;
  283. letter-spacing: 5px;
  284. }
  285. .login-content-submit {
  286. width: 100%;
  287. letter-spacing: 2px;
  288. font-weight: 300;
  289. margin-top: 15px;
  290. }
  291. }
  292. </style>