LoginForm.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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" 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. defineOptions({ name: 'LoginForm' })
  156. const { t } = useI18n()
  157. const message = useMessage()
  158. const iconHouse = useIcon({ icon: 'ep:house' })
  159. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  160. const iconLock = useIcon({ icon: 'ep:lock' })
  161. const formLogin = ref()
  162. const { validForm } = useFormValid(formLogin)
  163. const { setLoginState, getLoginState } = useLoginState()
  164. const { currentRoute, push } = useRouter()
  165. const permissionStore = usePermissionStore()
  166. const redirect = ref<string>('')
  167. const loginLoading = ref(false)
  168. const verify = ref()
  169. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  170. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  171. const LoginRules = {
  172. tenantName: [required],
  173. username: [required],
  174. password: [required]
  175. }
  176. const loginData = reactive({
  177. isShowPassword: false,
  178. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  179. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  180. loginForm: {
  181. tenantName: '芋道源码',
  182. username: 'admin',
  183. password: 'admin123',
  184. captchaVerification: '',
  185. rememberMe: false
  186. }
  187. })
  188. const socialList = [
  189. { icon: 'ant-design:github-filled', type: 0 },
  190. { icon: 'ant-design:wechat-filled', type: 30 },
  191. { icon: 'ant-design:alipay-circle-filled', type: 0 },
  192. { icon: 'ant-design:dingtalk-circle-filled', type: 20 }
  193. ]
  194. // 获取验证码
  195. const getCode = async () => {
  196. // 情况一,未开启:则直接登录
  197. if (loginData.captchaEnable === 'false') {
  198. await handleLogin({})
  199. } else {
  200. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  201. // 弹出验证码
  202. verify.value.show()
  203. }
  204. }
  205. //获取租户ID
  206. const getTenantId = async () => {
  207. if (loginData.tenantEnable === 'true') {
  208. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  209. authUtil.setTenantId(res)
  210. }
  211. }
  212. // 记住我
  213. const getCookie = () => {
  214. const loginForm = authUtil.getLoginForm()
  215. if (loginForm) {
  216. loginData.loginForm = {
  217. ...loginData.loginForm,
  218. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  219. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  220. rememberMe: loginForm.rememberMe ? true : false,
  221. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  222. }
  223. }
  224. }
  225. // 登录
  226. const handleLogin = async (params) => {
  227. loginLoading.value = true
  228. try {
  229. await getTenantId()
  230. const data = await validForm()
  231. if (!data) {
  232. return
  233. }
  234. loginData.loginForm.captchaVerification = params.captchaVerification
  235. const res = await LoginApi.login(loginData.loginForm)
  236. if (!res) {
  237. return
  238. }
  239. ElLoading.service({
  240. lock: true,
  241. text: '正在加载系统中...',
  242. background: 'rgba(0, 0, 0, 0.7)'
  243. })
  244. if (loginData.loginForm.rememberMe) {
  245. authUtil.setLoginForm(loginData.loginForm)
  246. } else {
  247. authUtil.removeLoginForm()
  248. }
  249. authUtil.setToken(res)
  250. if (!redirect.value) {
  251. redirect.value = '/'
  252. }
  253. // 判断是否为SSO登录
  254. if (redirect.value.indexOf('sso') !== -1) {
  255. window.location.href = window.location.href.replace('/login?redirect=', '')
  256. } else {
  257. push({ path: redirect.value || permissionStore.addRouters[0].path })
  258. }
  259. } catch {
  260. loginLoading.value = false
  261. } finally {
  262. setTimeout(() => {
  263. const loadingInstance = ElLoading.service()
  264. loadingInstance.close()
  265. }, 400)
  266. }
  267. }
  268. // 社交登录
  269. const doSocialLogin = async (type: number) => {
  270. if (type === 0) {
  271. message.error('此方式未配置')
  272. } else {
  273. loginLoading.value = true
  274. if (loginData.tenantEnable === 'true') {
  275. await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  276. const res = await LoginApi.getTenantIdByName(value)
  277. authUtil.setTenantId(res)
  278. })
  279. }
  280. // 计算 redirectUri
  281. const redirectUri =
  282. location.origin + '/social-login?type=' + type + '&redirect=' + (redirect.value || '/')
  283. // 进行跳转
  284. const res = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri))
  285. window.location.href = res
  286. }
  287. }
  288. watch(
  289. () => currentRoute.value,
  290. (route: RouteLocationNormalizedLoaded) => {
  291. redirect.value = route?.query?.redirect as string
  292. },
  293. {
  294. immediate: true
  295. }
  296. )
  297. onMounted(() => {
  298. getCookie()
  299. })
  300. </script>
  301. <style lang="scss" scoped>
  302. :deep(.anticon) {
  303. &:hover {
  304. color: var(--el-color-primary) !important;
  305. }
  306. }
  307. .login-code {
  308. width: 100%;
  309. height: 38px;
  310. float: right;
  311. img {
  312. cursor: pointer;
  313. width: 100%;
  314. max-width: 100px;
  315. height: auto;
  316. vertical-align: middle;
  317. }
  318. }
  319. </style>