LoginForm.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 LoginCaptchaRules = {
  50. tenantName: [required],
  51. username: [required],
  52. password: [required],
  53. code: [required]
  54. }
  55. const LoginRules = {
  56. tenantName: [required],
  57. username: [required],
  58. password: [required]
  59. }
  60. const loginLoading = ref(false)
  61. const loginData = reactive({
  62. codeImg: '',
  63. isShowPassword: false,
  64. captchaEnable: true,
  65. tenantEnable: true,
  66. token: '',
  67. loading: {
  68. signIn: false
  69. },
  70. loginForm: {
  71. tenantName: '芋道源码',
  72. username: 'admin',
  73. password: 'admin123',
  74. rememberMe: false,
  75. code: '',
  76. uuid: ''
  77. }
  78. })
  79. // 获取验证码
  80. const getCode = async () => {
  81. const res = await LoginApi.getCodeImgApi()
  82. loginData.captchaEnable = res.enable
  83. if (res.enable) {
  84. loginData.codeImg = 'data:image/gif;base64,' + res.img
  85. loginData.loginForm.uuid = res.uuid
  86. }
  87. }
  88. //获取租户ID
  89. const getTenantId = async () => {
  90. const res = await LoginApi.getTenantIdByNameApi(loginData.loginForm.tenantName)
  91. setTenantId(res)
  92. }
  93. // 记住我
  94. const getCookie = () => {
  95. const username = getUsername()
  96. const password = getPassword()
  97. const rememberMe = getRememberMe()
  98. const tenantName = getTenantName()
  99. loginData.loginForm = {
  100. ...loginData.loginForm,
  101. username: username ? username : loginData.loginForm.username,
  102. password: password ? password : loginData.loginForm.password,
  103. rememberMe: rememberMe ? getRememberMe() : false,
  104. tenantName: tenantName ? tenantName : loginData.loginForm.tenantName
  105. }
  106. }
  107. // 登录
  108. const handleLogin = async () => {
  109. await getTenantId()
  110. const data = await validForm()
  111. if (!data) return
  112. loginLoading.value = true
  113. await LoginApi.loginApi(loginData.loginForm)
  114. .then(async (res) => {
  115. setToken(res)
  116. const userInfo = await LoginApi.getInfoApi()
  117. await userStore.getUserInfoAction(userInfo)
  118. await getRoutes()
  119. })
  120. .catch(() => {
  121. getCode()
  122. })
  123. .finally(() => {
  124. loginLoading.value = false
  125. })
  126. }
  127. // 获取路由
  128. const getRoutes = async () => {
  129. // 后端过滤菜单
  130. const res = await LoginApi.getAsyncRoutesApi()
  131. wsCache.set('roleRouters', res)
  132. await permissionStore.generateRoutes(res)
  133. permissionStore.getAddRouters.forEach((route) => {
  134. addRoute(route as RouteRecordRaw) // 动态添加可访问路由表
  135. })
  136. permissionStore.setIsAddRouters(true)
  137. push({ path: redirect.value || permissionStore.addRouters[0].path })
  138. }
  139. // 社交登录
  140. const doSocialLogin = async (type: string) => {
  141. loginLoading.value = true
  142. // 计算 redirectUri
  143. const redirectUri =
  144. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  145. // 进行跳转
  146. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  147. window.open = res
  148. }
  149. watch(
  150. () => currentRoute.value,
  151. (route: RouteLocationNormalizedLoaded) => {
  152. redirect.value = route?.query?.redirect as string
  153. },
  154. {
  155. immediate: true
  156. }
  157. )
  158. onMounted(async () => {
  159. await getCode()
  160. getCookie()
  161. })
  162. </script>
  163. <template>
  164. <el-form
  165. :model="loginData.loginForm"
  166. :rules="loginData.captchaEnable ? LoginCaptchaRules : LoginRules"
  167. label-position="top"
  168. class="login-form"
  169. label-width="120px"
  170. size="large"
  171. v-show="getShow"
  172. ref="formLogin"
  173. >
  174. <el-row style="maring-left: -10px; maring-right: -10px">
  175. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  176. <el-form-item>
  177. <LoginFormTitle style="width: 100%" />
  178. </el-form-item>
  179. </el-col>
  180. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  181. <el-form-item prop="tenantName">
  182. <el-input
  183. type="text"
  184. v-model="loginData.loginForm.tenantName"
  185. :placeholder="t('login.tenantNamePlaceholder')"
  186. :prefix-icon="iconHouse"
  187. />
  188. </el-form-item>
  189. </el-col>
  190. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  191. <el-form-item prop="username">
  192. <el-input
  193. v-model="loginData.loginForm.username"
  194. :placeholder="t('login.usernamePlaceholder')"
  195. :prefix-icon="iconAvatar"
  196. />
  197. </el-form-item>
  198. </el-col>
  199. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  200. <el-form-item prop="password">
  201. <el-input
  202. v-model="loginData.loginForm.password"
  203. type="password"
  204. :placeholder="t('login.passwordPlaceholder')"
  205. show-password
  206. @keyup.enter="handleLogin"
  207. :prefix-icon="iconLock"
  208. />
  209. </el-form-item>
  210. </el-col>
  211. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  212. <el-form-item prop="code" v-if="loginData.captchaEnable">
  213. <el-row justify="space-between" style="width: 100%">
  214. <el-col :span="14">
  215. <el-input
  216. v-model="loginData.loginForm.code"
  217. :placeholder="t('login.codePlaceholder')"
  218. @keyup.enter="handleLogin"
  219. :prefix-icon="iconCircleCheck"
  220. style="width: 90%"
  221. />
  222. </el-col>
  223. <el-col :span="10">
  224. <div class="login-code">
  225. <img :src="loginData.codeImg" @click="getCode" class="login-code-img" alt="" />
  226. </div>
  227. </el-col>
  228. </el-row>
  229. </el-form-item>
  230. </el-col>
  231. <el-col
  232. :span="24"
  233. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  234. >
  235. <el-form-item>
  236. <el-row justify="space-between" style="width: 100%">
  237. <el-col :span="6">
  238. <el-checkbox v-model="loginData.loginForm.rememberMe">
  239. {{ t('login.remember') }}
  240. </el-checkbox>
  241. </el-col>
  242. <el-col :span="12" :offset="6">
  243. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  244. </el-col>
  245. </el-row>
  246. </el-form-item>
  247. </el-col>
  248. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  249. <el-form-item>
  250. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="handleLogin">
  251. {{ t('login.login') }}
  252. </el-button>
  253. </el-form-item>
  254. </el-col>
  255. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  256. <el-form-item>
  257. <el-row justify="space-between" style="width: 100%" :gutter="5">
  258. <el-col :span="8">
  259. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  260. {{ t('login.btnMobile') }}
  261. </el-button>
  262. </el-col>
  263. <el-col :span="8">
  264. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  265. {{ t('login.btnQRCode') }}
  266. </el-button>
  267. </el-col>
  268. <el-col :span="8">
  269. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  270. {{ t('login.btnRegister') }}
  271. </el-button>
  272. </el-col>
  273. </el-row>
  274. </el-form-item>
  275. </el-col>
  276. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  277. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  278. <el-form-item>
  279. <div class="flex justify-between w-[100%]">
  280. <Icon
  281. icon="ant-design:github-filled"
  282. :size="iconSize"
  283. class="cursor-pointer anticon"
  284. :color="iconColor"
  285. @click="doSocialLogin('github')"
  286. />
  287. <Icon
  288. icon="ant-design:wechat-filled"
  289. :size="iconSize"
  290. class="cursor-pointer anticon"
  291. :color="iconColor"
  292. @click="doSocialLogin('wechat')"
  293. />
  294. <Icon
  295. icon="ant-design:alipay-circle-filled"
  296. :size="iconSize"
  297. :color="iconColor"
  298. class="cursor-pointer anticon"
  299. @click="doSocialLogin('alipay')"
  300. />
  301. <Icon
  302. icon="ant-design:dingtalk-circle-filled"
  303. :size="iconSize"
  304. :color="iconColor"
  305. class="cursor-pointer anticon"
  306. @click="doSocialLogin('dingtalk')"
  307. />
  308. </div>
  309. </el-form-item>
  310. </el-col>
  311. </el-row>
  312. </el-form>
  313. </template>
  314. <style lang="less" scoped>
  315. :deep(.anticon) {
  316. &:hover {
  317. color: var(--el-color-primary) !important;
  318. }
  319. }
  320. .login-code {
  321. width: 100%;
  322. height: 38px;
  323. float: right;
  324. img {
  325. cursor: pointer;
  326. width: 100%;
  327. max-width: 100px;
  328. height: auto;
  329. vertical-align: middle;
  330. }
  331. }
  332. </style>