LoginForm.vue 9.3 KB

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