LoginForm.vue 9.8 KB

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