LoginForm.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. // 社交登录
  132. const doSocialLogin = async (type: string) => {
  133. loginLoading.value = true
  134. // 计算 redirectUri
  135. const redirectUri =
  136. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  137. // 进行跳转
  138. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  139. window.open = res
  140. }
  141. watch(
  142. () => currentRoute.value,
  143. (route: RouteLocationNormalizedLoaded) => {
  144. redirect.value = route?.query?.redirect as string
  145. },
  146. {
  147. immediate: true
  148. }
  149. )
  150. onMounted(async () => {
  151. await getCode()
  152. getCookie()
  153. })
  154. </script>
  155. <template>
  156. <el-form
  157. :model="loginData.loginForm"
  158. :rules="LoginRules"
  159. label-position="top"
  160. class="login-form"
  161. label-width="120px"
  162. size="large"
  163. v-show="getShow"
  164. ref="formLogin"
  165. >
  166. <el-row style="maring-left: -10px; maring-right: -10px">
  167. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  168. <el-form-item>
  169. <LoginFormTitle style="width: 100%" />
  170. </el-form-item>
  171. </el-col>
  172. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  173. <el-form-item prop="tenantName">
  174. <el-input
  175. type="text"
  176. v-model="loginData.loginForm.tenantName"
  177. :placeholder="t('login.tenantNamePlaceholder')"
  178. :prefix-icon="iconHouse"
  179. />
  180. </el-form-item>
  181. </el-col>
  182. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  183. <el-form-item prop="username">
  184. <el-input
  185. v-model="loginData.loginForm.username"
  186. :placeholder="t('login.usernamePlaceholder')"
  187. :prefix-icon="iconAvatar"
  188. />
  189. </el-form-item>
  190. </el-col>
  191. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  192. <el-form-item prop="password">
  193. <el-input
  194. v-model="loginData.loginForm.password"
  195. type="password"
  196. :placeholder="t('login.passwordPlaceholder')"
  197. show-password
  198. @keyup.enter="handleLogin"
  199. :prefix-icon="iconLock"
  200. />
  201. </el-form-item>
  202. </el-col>
  203. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  204. <el-form-item prop="code">
  205. <el-row justify="space-between" style="width: 100%">
  206. <el-col :span="14">
  207. <el-input
  208. v-model="loginData.loginForm.code"
  209. :placeholder="t('login.codePlaceholder')"
  210. @keyup.enter="handleLogin"
  211. :prefix-icon="iconCircleCheck"
  212. style="width: 90%"
  213. />
  214. </el-col>
  215. <el-col :span="10">
  216. <div class="login-code">
  217. <img :src="loginData.codeImg" @click="getCode" class="login-code-img" alt="" />
  218. </div>
  219. </el-col>
  220. </el-row>
  221. </el-form-item>
  222. </el-col>
  223. <el-col
  224. :span="24"
  225. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  226. >
  227. <el-form-item>
  228. <el-row justify="space-between" style="width: 100%">
  229. <el-col :span="6">
  230. <el-checkbox v-model="loginData.loginForm.rememberMe">
  231. {{ t('login.remember') }}
  232. </el-checkbox>
  233. </el-col>
  234. <el-col :span="12" :offset="6">
  235. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  236. </el-col>
  237. </el-row>
  238. </el-form-item>
  239. </el-col>
  240. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  241. <el-form-item>
  242. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="handleLogin">
  243. {{ t('login.login') }}
  244. </el-button>
  245. </el-form-item>
  246. </el-col>
  247. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  248. <el-form-item>
  249. <el-row justify="space-between" style="width: 100%" :gutter="5">
  250. <el-col :span="8">
  251. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  252. {{ t('login.btnMobile') }}
  253. </el-button>
  254. </el-col>
  255. <el-col :span="8">
  256. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  257. {{ t('login.btnQRCode') }}
  258. </el-button>
  259. </el-col>
  260. <el-col :span="8">
  261. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  262. {{ t('login.btnRegister') }}
  263. </el-button>
  264. </el-col>
  265. </el-row>
  266. </el-form-item>
  267. </el-col>
  268. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  269. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  270. <el-form-item>
  271. <div class="flex justify-between w-[100%]">
  272. <Icon
  273. icon="ant-design:github-filled"
  274. :size="iconSize"
  275. class="cursor-pointer anticon"
  276. :color="iconColor"
  277. @click="doSocialLogin('github')"
  278. />
  279. <Icon
  280. icon="ant-design:wechat-filled"
  281. :size="iconSize"
  282. class="cursor-pointer anticon"
  283. :color="iconColor"
  284. @click="doSocialLogin('wechat')"
  285. />
  286. <Icon
  287. icon="ant-design:alipay-circle-filled"
  288. :size="iconSize"
  289. :color="iconColor"
  290. class="cursor-pointer anticon"
  291. @click="doSocialLogin('alipay')"
  292. />
  293. <Icon
  294. icon="ant-design:dingtalk-circle-filled"
  295. :size="iconSize"
  296. :color="iconColor"
  297. class="cursor-pointer anticon"
  298. @click="doSocialLogin('dingtalk')"
  299. />
  300. </div>
  301. </el-form-item>
  302. </el-col>
  303. </el-row>
  304. </el-form>
  305. </template>
  306. <style lang="less" scoped>
  307. :deep(.anticon) {
  308. &:hover {
  309. color: var(--el-color-primary) !important;
  310. }
  311. }
  312. .login-code {
  313. width: 100%;
  314. height: 38px;
  315. float: right;
  316. img {
  317. cursor: pointer;
  318. width: 100%;
  319. max-width: 100px;
  320. height: auto;
  321. vertical-align: middle;
  322. }
  323. }
  324. </style>