LoginForm.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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-divider content-position="center">萌新必读</el-divider>
  129. <el-col :span="24" style="padding-left: 10px; padding-right: 10px">
  130. <el-form-item>
  131. <div class="flex justify-between w-[100%]">
  132. <el-link href="https://doc.iocoder.cn/" target="_blank">📚开发指南</el-link>
  133. <el-link href="https://doc.iocoder.cn/video/" target="_blank">🔥视频教程</el-link>
  134. <el-link href="https://www.iocoder.cn/Interview/good-collection/" target="_blank">
  135. ⚡面试手册
  136. </el-link>
  137. <el-link href="http://static.yudao.iocoder.cn/mp/Aix9975.jpeg" target="_blank">
  138. 🤝外包咨询
  139. </el-link>
  140. </div>
  141. </el-form-item>
  142. </el-col>
  143. </el-row>
  144. </el-form>
  145. </template>
  146. <script lang="ts" name="LoginForm" setup>
  147. import { ElLoading } from 'element-plus'
  148. import LoginFormTitle from './LoginFormTitle.vue'
  149. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  150. import { useIcon } from '@/hooks/web/useIcon'
  151. import * as authUtil from '@/utils/auth'
  152. import { usePermissionStore } from '@/store/modules/permission'
  153. import * as LoginApi from '@/api/login'
  154. import { LoginStateEnum, useFormValid, useLoginState } 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.getTenantIdByName(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.login(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. // 判断是否为SSO登录
  253. if (redirect.value.indexOf('sso') !== -1) {
  254. window.location.href = window.location.href.replace('/login?redirect=', '')
  255. } else {
  256. push({ path: redirect.value || permissionStore.addRouters[0].path })
  257. }
  258. } catch {
  259. loginLoading.value = false
  260. } finally {
  261. setTimeout(() => {
  262. const loadingInstance = ElLoading.service()
  263. loadingInstance.close()
  264. }, 400)
  265. }
  266. }
  267. // 社交登录
  268. const doSocialLogin = async (type: number) => {
  269. if (type === 0) {
  270. message.error('此方式未配置')
  271. } else {
  272. loginLoading.value = true
  273. if (loginData.tenantEnable === 'true') {
  274. await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  275. const res = await LoginApi.getTenantIdByName(value)
  276. authUtil.setTenantId(res)
  277. })
  278. }
  279. // 计算 redirectUri
  280. const redirectUri =
  281. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  282. // 进行跳转
  283. const res = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri))
  284. window.location.href = res
  285. }
  286. }
  287. watch(
  288. () => currentRoute.value,
  289. (route: RouteLocationNormalizedLoaded) => {
  290. redirect.value = route?.query?.redirect as string
  291. },
  292. {
  293. immediate: true
  294. }
  295. )
  296. onMounted(() => {
  297. getCookie()
  298. })
  299. </script>
  300. <style lang="scss" scoped>
  301. :deep(.anticon) {
  302. &:hover {
  303. color: var(--el-color-primary) !important;
  304. }
  305. }
  306. .login-code {
  307. width: 100%;
  308. height: 38px;
  309. float: right;
  310. img {
  311. cursor: pointer;
  312. width: 100%;
  313. max-width: 100px;
  314. height: auto;
  315. vertical-align: middle;
  316. }
  317. }
  318. </style>