LoginForm.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <script lang="ts" setup>
  2. import { useIcon } from '@/hooks/web/useIcon'
  3. import LoginFormTitle from './LoginFormTitle.vue'
  4. import {
  5. ElForm,
  6. ElFormItem,
  7. ElInput,
  8. ElCheckbox,
  9. ElCol,
  10. ElLink,
  11. ElRow,
  12. ElDivider
  13. } from 'element-plus'
  14. import { reactive, ref, unref, onMounted, computed, watch } from 'vue'
  15. import * as LoginApi from '@/api/login'
  16. import {
  17. setToken,
  18. setTenantId,
  19. getUsername,
  20. getRememberMe,
  21. getPassword,
  22. getTenantName
  23. } from '@/utils/auth'
  24. import { useUserStoreWithOut } from '@/store/modules/user'
  25. import { useCache } from '@/hooks/web/useCache'
  26. import { usePermissionStore } from '@/store/modules/permission'
  27. import { useRouter } from 'vue-router'
  28. import { useI18n } from '@/hooks/web/useI18n'
  29. import { required } from '@/utils/formRules'
  30. import { Icon } from '@/components/Icon'
  31. import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  32. import type { RouteLocationNormalizedLoaded, RouteRecordRaw } from 'vue-router'
  33. const { currentRoute, addRoute, push } = useRouter()
  34. const permissionStore = usePermissionStore()
  35. const userStore = useUserStoreWithOut()
  36. const formLogin = ref()
  37. const { validForm } = useFormValid(formLogin)
  38. const { wsCache } = useCache()
  39. const { setLoginState, getLoginState } = useLoginState()
  40. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  41. const iconSize = 30
  42. const iconColor = '#999'
  43. const redirect = ref<string>('')
  44. const { t } = useI18n()
  45. const iconHouse = useIcon({ icon: 'ep:house' })
  46. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  47. const iconLock = useIcon({ icon: 'ep:lock' })
  48. const iconCircleCheck = useIcon({ icon: 'ep:circle-check' })
  49. const LoginRules = {
  50. tenantName: [required],
  51. username: [required],
  52. password: [required],
  53. code: [required]
  54. }
  55. const loginLoading = ref(false)
  56. const loginData = reactive({
  57. codeImg: '',
  58. isShowPassword: false,
  59. captchaEnable: true,
  60. tenantEnable: true,
  61. token: '',
  62. loading: {
  63. signIn: false
  64. },
  65. loginForm: {
  66. tenantName: '芋道源码',
  67. username: 'admin',
  68. password: 'admin123',
  69. rememberMe: false,
  70. code: '',
  71. uuid: ''
  72. }
  73. })
  74. // 获取验证码
  75. const getCode = async () => {
  76. const res = await LoginApi.getCodeImgApi()
  77. loginData.codeImg = 'data:image/gif;base64,' + res.img
  78. loginData.loginForm.uuid = res.uuid
  79. }
  80. //获取租户ID
  81. const getTenantId = async () => {
  82. const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  83. setTenantId(res)
  84. }
  85. // 记住我
  86. const getCookie = () => {
  87. const username = getUsername()
  88. const password = getPassword()
  89. const rememberMe = getRememberMe()
  90. const tenantName = getTenantName()
  91. loginData.loginForm = {
  92. ...loginData.loginForm,
  93. username: username ? username : loginData.loginForm.username,
  94. password: password ? password : loginData.loginForm.password,
  95. rememberMe: rememberMe ? getRememberMe() : false,
  96. tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
  97. }
  98. }
  99. // 登录
  100. const handleLogin = async () => {
  101. await getTenantId()
  102. const data = await validForm()
  103. if (!data) return
  104. loginLoading.value = true
  105. await LoginApi.loginApi(loginData.loginForm)
  106. .then(async (res) => {
  107. setToken(res)
  108. const userInfo = await LoginApi.getInfoApi()
  109. await userStore.getUserInfoAction(userInfo)
  110. await getRoutes()
  111. })
  112. .catch(() => {
  113. getCode()
  114. })
  115. .finally(() => {
  116. loginLoading.value = false
  117. })
  118. }
  119. // 获取路由
  120. const getRoutes = async () => {
  121. // 后端过滤菜单
  122. const res = await LoginApi.getAsyncRoutesApi()
  123. wsCache.set('roleRouters', res)
  124. await permissionStore.generateRoutes(res)
  125. permissionStore.getAddRouters.forEach((route) => {
  126. addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
  127. })
  128. permissionStore.setIsAddRouters(true)
  129. push({ path: redirect.value || permissionStore.addRouters[0].path })
  130. }
  131. watch(
  132. () => currentRoute.value,
  133. (route: RouteLocationNormalizedLoaded) => {
  134. redirect.value = route?.query?.redirect as string
  135. },
  136. {
  137. immediate: true
  138. }
  139. )
  140. onMounted(async () => {
  141. await getCode()
  142. getCookie()
  143. })
  144. </script>
  145. <template>
  146. <el-form
  147. :model="loginData.loginForm"
  148. :rules="LoginRules"
  149. label-position="top"
  150. class="login-form"
  151. label-width="120px"
  152. size="large"
  153. v-show="getShow"
  154. ref="formLogin"
  155. >
  156. <el-row style="maring-left: -10px; maring-right: -10px">
  157. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  158. <el-form-item>
  159. <LoginFormTitle style="width: 100%" />
  160. </el-form-item>
  161. </el-col>
  162. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  163. <el-form-item prop="tenantName">
  164. <el-input
  165. type="text"
  166. v-model="loginData.loginForm.tenantName"
  167. :placeholder="t('login.tenantNamePlaceholder')"
  168. :prefix-icon="iconHouse"
  169. />
  170. </el-form-item>
  171. </el-col>
  172. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  173. <el-form-item prop="username">
  174. <el-input
  175. v-model="loginData.loginForm.username"
  176. :placeholder="t('login.usernamePlaceholder')"
  177. :prefix-icon="iconAvatar"
  178. />
  179. </el-form-item>
  180. </el-col>
  181. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  182. <el-form-item prop="password">
  183. <el-input
  184. v-model="loginData.loginForm.password"
  185. type="password"
  186. :placeholder="t('login.passwordPlaceholder')"
  187. show-password
  188. @keyup.enter="handleLogin"
  189. :prefix-icon="iconLock"
  190. />
  191. </el-form-item>
  192. </el-col>
  193. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  194. <el-form-item prop="code">
  195. <el-row justify="space-between" style="width: 100%">
  196. <el-col :span="14">
  197. <el-input
  198. v-model="loginData.loginForm.code"
  199. :placeholder="t('login.codePlaceholder')"
  200. @keyup.enter="handleLogin"
  201. :prefix-icon="iconCircleCheck"
  202. style="width: 90%"
  203. />
  204. </el-col>
  205. <el-col :span="10">
  206. <div class="login-code">
  207. <img :src="loginData.codeImg" @click="getCode" class="login-code-img" alt="" />
  208. </div>
  209. </el-col>
  210. </el-row>
  211. </el-form-item>
  212. </el-col>
  213. <el-col
  214. :span="24"
  215. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  216. >
  217. <el-form-item>
  218. <el-row justify="space-between" style="width: 100%">
  219. <el-col :span="6">
  220. <el-checkbox v-model="loginData.loginForm.rememberMe">
  221. {{ t('login.remember') }}
  222. </el-checkbox>
  223. </el-col>
  224. <el-col :span="12" :offset="6">
  225. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  226. </el-col>
  227. </el-row>
  228. </el-form-item>
  229. </el-col>
  230. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  231. <el-form-item>
  232. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="handleLogin">
  233. {{ t('login.login') }}
  234. </el-button>
  235. </el-form-item>
  236. </el-col>
  237. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  238. <el-form-item>
  239. <el-row justify="space-between" style="width: 100%" :gutter="5">
  240. <el-col :span="8">
  241. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  242. {{ t('login.btnMobile') }}
  243. </el-button>
  244. </el-col>
  245. <el-col :span="8">
  246. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  247. {{ t('login.btnQRCode') }}
  248. </el-button>
  249. </el-col>
  250. <el-col :span="8">
  251. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  252. {{ t('login.btnRegister') }}
  253. </el-button>
  254. </el-col>
  255. </el-row>
  256. </el-form-item>
  257. </el-col>
  258. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  259. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  260. <el-form-item>
  261. <div class="flex justify-between w-[100%]">
  262. <Icon
  263. icon="ant-design:github-filled"
  264. :size="iconSize"
  265. class="cursor-pointer anticon"
  266. :color="iconColor"
  267. />
  268. <Icon
  269. icon="ant-design:wechat-filled"
  270. :size="iconSize"
  271. class="cursor-pointer anticon"
  272. :color="iconColor"
  273. />
  274. <Icon
  275. icon="ant-design:alipay-circle-filled"
  276. :size="iconSize"
  277. :color="iconColor"
  278. class="cursor-pointer anticon"
  279. />
  280. <Icon
  281. icon="ant-design:dingtalk-circle-filled"
  282. :size="iconSize"
  283. :color="iconColor"
  284. class="cursor-pointer anticon"
  285. />
  286. </div>
  287. </el-form-item>
  288. </el-col>
  289. </el-row>
  290. </el-form>
  291. </template>
  292. <style lang="less" scoped>
  293. :deep(.anticon) {
  294. &:hover {
  295. color: var(--el-color-primary) !important;
  296. }
  297. }
  298. .login-code {
  299. width: 100%;
  300. height: 38px;
  301. float: right;
  302. img {
  303. cursor: pointer;
  304. width: 100%;
  305. max-width: 100px;
  306. height: auto;
  307. vertical-align: middle;
  308. }
  309. }
  310. </style>