LoginForm.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 { usePermissionStore } from '@/store/modules/permission'
  25. import { useRouter } from 'vue-router'
  26. import { useI18n } from '@/hooks/web/useI18n'
  27. import { required } from '@/utils/formRules'
  28. import { Icon } from '@/components/Icon'
  29. import { LoginStateEnum, useLoginState, useFormValid } from './useLogin'
  30. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  31. import { Verify } from '@/components/Verifition'
  32. const { currentRoute, push } = useRouter()
  33. const permissionStore = usePermissionStore()
  34. const formLogin = ref()
  35. const { validForm } = useFormValid(formLogin)
  36. const { setLoginState, getLoginState } = useLoginState()
  37. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  38. const iconSize = 30
  39. const iconColor = '#999'
  40. const redirect = ref<string>('')
  41. const { t } = useI18n()
  42. const iconHouse = useIcon({ icon: 'ep:house' })
  43. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  44. const iconLock = useIcon({ icon: 'ep:lock' })
  45. const LoginRules = {
  46. tenantName: [required],
  47. username: [required],
  48. password: [required]
  49. }
  50. const loginLoading = ref(false)
  51. const loginData = reactive({
  52. isShowPassword: false,
  53. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  54. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  55. token: '',
  56. loading: {
  57. signIn: false
  58. },
  59. loginForm: {
  60. tenantName: '芋道源码',
  61. username: 'admin',
  62. password: 'admin123',
  63. captchaVerification: '',
  64. rememberMe: false
  65. }
  66. })
  67. // blockPuzzle 滑块 clickWord 点击文字
  68. const verify = ref()
  69. const captchaType = ref('blockPuzzle')
  70. // 获取验证码
  71. const getCode = async () => {
  72. // 情况一,未开启:则直接登录
  73. if (!loginData.captchaEnable) {
  74. await handleLogin({})
  75. return
  76. }
  77. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  78. // 弹出验证码
  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. if (!res) {
  112. loginLoading.value = false
  113. return
  114. }
  115. setToken(res)
  116. if (!redirect.value) {
  117. redirect.value = '/'
  118. }
  119. push({ path: redirect.value || permissionStore.addRouters[0].path })
  120. loginLoading.value = false
  121. }
  122. // 社交登录
  123. const doSocialLogin = async (type: string) => {
  124. loginLoading.value = true
  125. // 计算 redirectUri
  126. const redirectUri =
  127. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  128. // 进行跳转
  129. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  130. window.open = res
  131. }
  132. watch(
  133. () => currentRoute.value,
  134. (route: RouteLocationNormalizedLoaded) => {
  135. redirect.value = route?.query?.redirect as string
  136. },
  137. {
  138. immediate: true
  139. }
  140. )
  141. onMounted(() => {
  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="getCode()"
  189. :prefix-icon="iconLock"
  190. />
  191. </el-form-item>
  192. </el-col>
  193. <el-col
  194. :span="24"
  195. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  196. >
  197. <el-form-item>
  198. <el-row justify="space-between" style="width: 100%">
  199. <el-col :span="6">
  200. <el-checkbox v-model="loginData.loginForm.rememberMe">
  201. {{ t('login.remember') }}
  202. </el-checkbox>
  203. </el-col>
  204. <el-col :span="12" :offset="6">
  205. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  206. </el-col>
  207. </el-row>
  208. </el-form-item>
  209. </el-col>
  210. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  211. <el-form-item>
  212. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="getCode()">
  213. {{ t('login.login') }}
  214. </el-button>
  215. </el-form-item>
  216. </el-col>
  217. <Verify
  218. ref="verify"
  219. mode="pop"
  220. :captchaType="captchaType"
  221. :imgSize="{ width: '400px', height: '200px' }"
  222. @success="handleLogin"
  223. />
  224. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  225. <el-form-item>
  226. <el-row justify="space-between" style="width: 100%" :gutter="5">
  227. <el-col :span="8">
  228. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  229. {{ t('login.btnMobile') }}
  230. </el-button>
  231. </el-col>
  232. <el-col :span="8">
  233. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  234. {{ t('login.btnQRCode') }}
  235. </el-button>
  236. </el-col>
  237. <el-col :span="8">
  238. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  239. {{ t('login.btnRegister') }}
  240. </el-button>
  241. </el-col>
  242. </el-row>
  243. </el-form-item>
  244. </el-col>
  245. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  246. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  247. <el-form-item>
  248. <div class="flex justify-between w-[100%]">
  249. <Icon
  250. icon="ant-design:github-filled"
  251. :size="iconSize"
  252. class="cursor-pointer anticon"
  253. :color="iconColor"
  254. @click="doSocialLogin('github')"
  255. />
  256. <Icon
  257. icon="ant-design:wechat-filled"
  258. :size="iconSize"
  259. class="cursor-pointer anticon"
  260. :color="iconColor"
  261. @click="doSocialLogin('wechat')"
  262. />
  263. <Icon
  264. icon="ant-design:alipay-circle-filled"
  265. :size="iconSize"
  266. :color="iconColor"
  267. class="cursor-pointer anticon"
  268. @click="doSocialLogin('alipay')"
  269. />
  270. <Icon
  271. icon="ant-design:dingtalk-circle-filled"
  272. :size="iconSize"
  273. :color="iconColor"
  274. class="cursor-pointer anticon"
  275. @click="doSocialLogin('dingtalk')"
  276. />
  277. </div>
  278. </el-form-item>
  279. </el-col>
  280. </el-row>
  281. </el-form>
  282. </template>
  283. <style lang="less" scoped>
  284. :deep(.anticon) {
  285. &:hover {
  286. color: var(--el-color-primary) !important;
  287. }
  288. }
  289. .login-code {
  290. width: 100%;
  291. height: 38px;
  292. float: right;
  293. img {
  294. cursor: pointer;
  295. width: 100%;
  296. max-width: 100px;
  297. height: auto;
  298. vertical-align: middle;
  299. }
  300. }
  301. </style>