LoginForm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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="margin-right: -10px; margin-left: -10px">
  13. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  14. <el-form-item>
  15. <LoginFormTitle style="width: 100%" />
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span="24" style="padding-right: 10px; padding-left: 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. link
  25. type="primary"
  26. />
  27. </el-form-item>
  28. </el-col>
  29. <el-col :span="24" style="padding-right: 10px; padding-left: 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-right: 10px; padding-left: 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-right: 10px; padding-left: 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-right: 10px; padding-left: 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-right: 10px; padding-left: 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-right: 10px; padding-left: 10px">
  114. <el-form-item>
  115. <div class="w-[100%] flex justify-between">
  116. <Icon
  117. v-for="(item, key) in socialList"
  118. :key="key"
  119. :icon="item.icon"
  120. :size="30"
  121. class="anticon cursor-pointer"
  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-right: 10px; padding-left: 10px">
  130. <el-form-item>
  131. <div class="w-[100%] flex justify-between">
  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: true // 默认记录我。如果不需要,可手动修改
  186. }
  187. })
  188. const socialList = [
  189. { icon: 'ant-design:wechat-filled', type: 30 },
  190. { icon: 'ant-design:dingtalk-circle-filled', type: 20 },
  191. { icon: 'ant-design:github-filled', type: 0 },
  192. { icon: 'ant-design:alipay-circle-filled', type: 0 }
  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 getLoginFormCache = () => {
  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,
  221. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  222. }
  223. }
  224. }
  225. // 根据域名,获得租户信息
  226. const getTenantByWebsite = async () => {
  227. const website = location.host
  228. const res = await LoginApi.getTenantByWebsite(website)
  229. if (res) {
  230. loginData.loginForm.tenantName = res.name
  231. authUtil.setTenantId(res.id)
  232. }
  233. }
  234. const loading = ref() // ElLoading.service 返回的实例
  235. // 登录
  236. const handleLogin = async (params) => {
  237. loginLoading.value = true
  238. try {
  239. await getTenantId()
  240. const data = await validForm()
  241. if (!data) {
  242. return
  243. }
  244. loginData.loginForm.captchaVerification = params.captchaVerification
  245. const res = await LoginApi.login(loginData.loginForm)
  246. if (!res) {
  247. return
  248. }
  249. loading.value = ElLoading.service({
  250. lock: true,
  251. text: '正在加载系统中...',
  252. background: 'rgba(0, 0, 0, 0.7)'
  253. })
  254. if (loginData.loginForm.rememberMe) {
  255. authUtil.setLoginForm(loginData.loginForm)
  256. } else {
  257. authUtil.removeLoginForm()
  258. }
  259. authUtil.setToken(res)
  260. if (!redirect.value) {
  261. redirect.value = '/'
  262. }
  263. // 判断是否为SSO登录
  264. if (redirect.value.indexOf('sso') !== -1) {
  265. window.location.href = window.location.href.replace('/login?redirect=', '')
  266. } else {
  267. push({ path: redirect.value || permissionStore.addRouters[0].path })
  268. }
  269. } finally {
  270. loginLoading.value = false
  271. loading.value.close()
  272. }
  273. }
  274. // 社交登录
  275. const doSocialLogin = async (type: number) => {
  276. if (type === 0) {
  277. message.error('此方式未配置')
  278. } else {
  279. loginLoading.value = true
  280. if (loginData.tenantEnable === 'true') {
  281. // 尝试先通过 tenantName 获取租户
  282. await getTenantId()
  283. // 如果获取不到,则需要弹出提示,进行处理
  284. if (!authUtil.getTenantId()) {
  285. await message.prompt('请输入租户名称', t('common.reminder')).then(async ({ value }) => {
  286. const res = await LoginApi.getTenantIdByName(value)
  287. authUtil.setTenantId(res)
  288. })
  289. }
  290. }
  291. // 计算 redirectUri
  292. // tricky: type、redirect需要先encode一次,否则钉钉回调会丢失。
  293. // 配合 Login/SocialLogin.vue#getUrlValue() 使用
  294. const redirectUri =
  295. location.origin +
  296. '/social-login?' +
  297. encodeURIComponent(`type=${type}&redirect=${redirect.value || '/'}`)
  298. // 进行跳转
  299. const res = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri))
  300. window.location.href = res
  301. }
  302. }
  303. watch(
  304. () => currentRoute.value,
  305. (route: RouteLocationNormalizedLoaded) => {
  306. redirect.value = route?.query?.redirect as string
  307. },
  308. {
  309. immediate: true
  310. }
  311. )
  312. onMounted(() => {
  313. getLoginFormCache()
  314. getTenantByWebsite()
  315. })
  316. </script>
  317. <style lang="scss" scoped>
  318. :deep(.anticon) {
  319. &:hover {
  320. color: var(--el-color-primary) !important;
  321. }
  322. }
  323. .login-code {
  324. float: right;
  325. width: 100%;
  326. height: 38px;
  327. img {
  328. width: 100%;
  329. height: auto;
  330. max-width: 100px;
  331. vertical-align: middle;
  332. cursor: pointer;
  333. }
  334. }
  335. </style>