LoginForm.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. try {
  104. await getTenantId()
  105. const data = await validForm()
  106. if (!data) {
  107. return
  108. }
  109. loginData.loginForm.captchaVerification = params.captchaVerification
  110. const res = await LoginApi.loginApi(loginData.loginForm)
  111. if (!res) {
  112. return
  113. }
  114. setToken(res)
  115. if (!redirect.value) {
  116. redirect.value = '/'
  117. }
  118. push({ path: redirect.value || permissionStore.addRouters[0].path })
  119. } finally {
  120. loginLoading.value = false
  121. }
  122. }
  123. // 社交登录
  124. const doSocialLogin = async (type: string) => {
  125. loginLoading.value = true
  126. // 计算 redirectUri
  127. const redirectUri =
  128. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  129. // 进行跳转
  130. const res = await LoginApi.socialAuthRedirectApi(type, encodeURIComponent(redirectUri))
  131. window.location.href = res
  132. }
  133. watch(
  134. () => currentRoute.value,
  135. (route: RouteLocationNormalizedLoaded) => {
  136. redirect.value = route?.query?.redirect as string
  137. },
  138. {
  139. immediate: true
  140. }
  141. )
  142. onMounted(() => {
  143. getCookie()
  144. })
  145. </script>
  146. <template>
  147. <el-form
  148. :model="loginData.loginForm"
  149. :rules="LoginRules"
  150. label-position="top"
  151. class="login-form"
  152. label-width="120px"
  153. size="large"
  154. v-show="getShow"
  155. ref="formLogin"
  156. >
  157. <el-row style="maring-left: -10px; maring-right: -10px">
  158. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  159. <el-form-item>
  160. <LoginFormTitle style="width: 100%" />
  161. </el-form-item>
  162. </el-col>
  163. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  164. <el-form-item prop="tenantName">
  165. <el-input
  166. type="text"
  167. v-model="loginData.loginForm.tenantName"
  168. :placeholder="t('login.tenantNamePlaceholder')"
  169. :prefix-icon="iconHouse"
  170. />
  171. </el-form-item>
  172. </el-col>
  173. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  174. <el-form-item prop="username">
  175. <el-input
  176. v-model="loginData.loginForm.username"
  177. :placeholder="t('login.usernamePlaceholder')"
  178. :prefix-icon="iconAvatar"
  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="password">
  184. <el-input
  185. v-model="loginData.loginForm.password"
  186. type="password"
  187. :placeholder="t('login.passwordPlaceholder')"
  188. show-password
  189. @keyup.enter="getCode()"
  190. :prefix-icon="iconLock"
  191. />
  192. </el-form-item>
  193. </el-col>
  194. <el-col
  195. :span="24"
  196. style="padding-left: 10px; padding-right: 10px; margin-top: -20px; margin-bottom: -20px"
  197. >
  198. <el-form-item>
  199. <el-row justify="space-between" style="width: 100%">
  200. <el-col :span="6">
  201. <el-checkbox v-model="loginData.loginForm.rememberMe">
  202. {{ t('login.remember') }}
  203. </el-checkbox>
  204. </el-col>
  205. <el-col :span="12" :offset="6">
  206. <el-link type="primary" style="float: right">{{ t('login.forgetPassword') }}</el-link>
  207. </el-col>
  208. </el-row>
  209. </el-form-item>
  210. </el-col>
  211. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  212. <el-form-item>
  213. <el-button :loading="loginLoading" type="primary" class="w-[100%]" @click="getCode()">
  214. {{ t('login.login') }}
  215. </el-button>
  216. </el-form-item>
  217. </el-col>
  218. <Verify
  219. ref="verify"
  220. mode="pop"
  221. :captchaType="captchaType"
  222. :imgSize="{ width: '400px', height: '200px' }"
  223. @success="handleLogin"
  224. />
  225. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  226. <el-form-item>
  227. <el-row justify="space-between" style="width: 100%" :gutter="5">
  228. <el-col :span="8">
  229. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.MOBILE)">
  230. {{ t('login.btnMobile') }}
  231. </el-button>
  232. </el-col>
  233. <el-col :span="8">
  234. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.QR_CODE)">
  235. {{ t('login.btnQRCode') }}
  236. </el-button>
  237. </el-col>
  238. <el-col :span="8">
  239. <el-button class="w-[100%]" @click="setLoginState(LoginStateEnum.REGISTER)">
  240. {{ t('login.btnRegister') }}
  241. </el-button>
  242. </el-col>
  243. </el-row>
  244. </el-form-item>
  245. </el-col>
  246. <el-divider content-position="center">{{ t('login.otherLogin') }}</el-divider>
  247. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  248. <el-form-item>
  249. <div class="flex justify-between w-[100%]">
  250. <Icon
  251. icon="ant-design:github-filled"
  252. :size="iconSize"
  253. class="cursor-pointer anticon"
  254. :color="iconColor"
  255. @click="doSocialLogin('github')"
  256. />
  257. <Icon
  258. icon="ant-design:wechat-filled"
  259. :size="iconSize"
  260. class="cursor-pointer anticon"
  261. :color="iconColor"
  262. @click="doSocialLogin('wechat')"
  263. />
  264. <Icon
  265. icon="ant-design:alipay-circle-filled"
  266. :size="iconSize"
  267. :color="iconColor"
  268. class="cursor-pointer anticon"
  269. @click="doSocialLogin('alipay')"
  270. />
  271. <Icon
  272. icon="ant-design:dingtalk-circle-filled"
  273. :size="iconSize"
  274. :color="iconColor"
  275. class="cursor-pointer anticon"
  276. @click="doSocialLogin('dingtalk')"
  277. />
  278. </div>
  279. </el-form-item>
  280. </el-col>
  281. </el-row>
  282. </el-form>
  283. </template>
  284. <style lang="less" scoped>
  285. :deep(.anticon) {
  286. &:hover {
  287. color: var(--el-color-primary) !important;
  288. }
  289. }
  290. .login-code {
  291. width: 100%;
  292. height: 38px;
  293. float: right;
  294. img {
  295. cursor: pointer;
  296. width: 100%;
  297. max-width: 100px;
  298. height: auto;
  299. vertical-align: middle;
  300. }
  301. }
  302. </style>