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