LoginForm.vue 9.5 KB

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