LoginForm.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 { ElLoading } from 'element-plus'
  132. import LoginFormTitle from './LoginFormTitle.vue'
  133. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  134. import { useIcon } from '@/hooks/web/useIcon'
  135. import * as authUtil from '@/utils/auth'
  136. import { usePermissionStore } from '@/store/modules/permission'
  137. import * as LoginApi from '@/api/login'
  138. import { LoginStateEnum, useFormValid, useLoginState } from './useLogin'
  139. const { t } = useI18n()
  140. const message = useMessage()
  141. const iconHouse = useIcon({ icon: 'ep:house' })
  142. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  143. const iconLock = useIcon({ icon: 'ep:lock' })
  144. const formLogin = ref()
  145. const { validForm } = useFormValid(formLogin)
  146. const { setLoginState, getLoginState } = useLoginState()
  147. const { currentRoute, push } = useRouter()
  148. const permissionStore = usePermissionStore()
  149. const redirect = ref<string>('')
  150. const loginLoading = ref(false)
  151. const verify = ref()
  152. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  153. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  154. const LoginRules = {
  155. tenantName: [required],
  156. username: [required],
  157. password: [required]
  158. }
  159. const loginData = reactive({
  160. isShowPassword: false,
  161. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  162. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  163. loginForm: {
  164. tenantName: '芋道源码',
  165. username: 'admin',
  166. password: 'admin123',
  167. captchaVerification: '',
  168. rememberMe: false
  169. }
  170. })
  171. const socialList = [
  172. { icon: 'ant-design:github-filled', type: 0 },
  173. { icon: 'ant-design:wechat-filled', type: 30 },
  174. { icon: 'ant-design:alipay-circle-filled', type: 0 },
  175. { icon: 'ant-design:dingtalk-circle-filled', type: 20 }
  176. ]
  177. // 获取验证码
  178. const getCode = async () => {
  179. // 情况一,未开启:则直接登录
  180. if (loginData.captchaEnable === 'false') {
  181. await handleLogin({})
  182. } else {
  183. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  184. // 弹出验证码
  185. verify.value.show()
  186. }
  187. }
  188. //获取租户ID
  189. const getTenantId = async () => {
  190. if (loginData.tenantEnable === 'true') {
  191. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  192. authUtil.setTenantId(res)
  193. }
  194. }
  195. // 记住我
  196. const getCookie = () => {
  197. const loginForm = authUtil.getLoginForm()
  198. if (loginForm) {
  199. loginData.loginForm = {
  200. ...loginData.loginForm,
  201. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  202. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  203. rememberMe: loginForm.rememberMe ? true : false,
  204. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  205. }
  206. }
  207. }
  208. // 登录
  209. const handleLogin = async (params) => {
  210. loginLoading.value = true
  211. try {
  212. await getTenantId()
  213. const data = await validForm()
  214. if (!data) {
  215. return
  216. }
  217. loginData.loginForm.captchaVerification = params.captchaVerification
  218. const res = await LoginApi.login(loginData.loginForm)
  219. if (!res) {
  220. return
  221. }
  222. ElLoading.service({
  223. lock: true,
  224. text: '正在加载系统中...',
  225. background: 'rgba(0, 0, 0, 0.7)'
  226. })
  227. if (loginData.loginForm.rememberMe) {
  228. authUtil.setLoginForm(loginData.loginForm)
  229. } else {
  230. authUtil.removeLoginForm()
  231. }
  232. authUtil.setToken(res)
  233. if (!redirect.value) {
  234. redirect.value = '/'
  235. }
  236. // 判断是否为SSO登录
  237. if (redirect.value.indexOf('sso') !== -1) {
  238. window.location.href = window.location.href.replace('/login?redirect=', '')
  239. } else {
  240. push({ path: redirect.value || permissionStore.addRouters[0].path })
  241. }
  242. } catch {
  243. loginLoading.value = false
  244. } finally {
  245. setTimeout(() => {
  246. const loadingInstance = ElLoading.service()
  247. loadingInstance.close()
  248. }, 400)
  249. }
  250. }
  251. // 社交登录
  252. const doSocialLogin = async (type: number) => {
  253. if (type === 0) {
  254. message.error('此方式未配置')
  255. } else {
  256. loginLoading.value = true
  257. if (loginData.tenantEnable === 'true') {
  258. await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  259. const res = await LoginApi.getTenantIdByName(value)
  260. authUtil.setTenantId(res)
  261. })
  262. }
  263. // 计算 redirectUri
  264. const redirectUri =
  265. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  266. // 进行跳转
  267. const res = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri))
  268. window.location.href = res
  269. }
  270. }
  271. watch(
  272. () => currentRoute.value,
  273. (route: RouteLocationNormalizedLoaded) => {
  274. redirect.value = route?.query?.redirect as string
  275. },
  276. {
  277. immediate: true
  278. }
  279. )
  280. onMounted(() => {
  281. getCookie()
  282. })
  283. </script>
  284. <style lang="scss" 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>