LoginForm.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
  156. defineOptions({ name: 'LoginForm' })
  157. const { t } = useI18n()
  158. const message = useMessage()
  159. const iconHouse = useIcon({ icon: 'ep:house' })
  160. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  161. const iconLock = useIcon({ icon: 'ep:lock' })
  162. const formLogin = ref()
  163. const { validForm } = useFormValid(formLogin)
  164. const { setLoginState, getLoginState } = useLoginState()
  165. const { currentRoute, push } = useRouter()
  166. const permissionStore = usePermissionStore()
  167. const redirect = ref<string>('')
  168. const loginLoading = ref(false)
  169. const verify = ref()
  170. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  171. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  172. const LoginRules = {
  173. tenantName: [required],
  174. username: [required],
  175. password: [required]
  176. }
  177. const loginData = reactive({
  178. isShowPassword: false,
  179. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  180. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  181. loginForm: {
  182. tenantName: import.meta.env.VITE_APP_DEFAULT_LOGIN_TENANT || '',
  183. username: import.meta.env.VITE_APP_DEFAULT_LOGIN_USERNAME || '',
  184. password: import.meta.env.VITE_APP_DEFAULT_LOGIN_PASSWORD || '',
  185. captchaVerification: '',
  186. rememberMe: true // 默认记录我。如果不需要,可手动修改
  187. }
  188. })
  189. const socialList = [
  190. { icon: 'ant-design:wechat-filled', type: 30 },
  191. { icon: 'ant-design:dingtalk-circle-filled', type: 20 },
  192. { icon: 'ant-design:github-filled', type: 0 },
  193. { icon: 'ant-design:alipay-circle-filled', type: 0 }
  194. ]
  195. // 获取验证码
  196. const getCode = async () => {
  197. // 情况一,未开启:则直接登录
  198. if (loginData.captchaEnable === 'false') {
  199. await handleLogin({})
  200. } else {
  201. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  202. // 弹出验证码
  203. verify.value.show()
  204. }
  205. }
  206. // 获取租户 ID
  207. const getTenantId = async () => {
  208. if (loginData.tenantEnable === 'true') {
  209. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  210. authUtil.setTenantId(res)
  211. }
  212. }
  213. // 记住我
  214. const getLoginFormCache = () => {
  215. const loginForm = authUtil.getLoginForm()
  216. if (loginForm) {
  217. loginData.loginForm = {
  218. ...loginData.loginForm,
  219. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  220. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  221. rememberMe: loginForm.rememberMe,
  222. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  223. }
  224. }
  225. }
  226. // 根据域名,获得租户信息
  227. const getTenantByWebsite = async () => {
  228. const website = location.host
  229. const res = await LoginApi.getTenantByWebsite(website)
  230. if (res) {
  231. loginData.loginForm.tenantName = res.name
  232. authUtil.setTenantId(res.id)
  233. }
  234. }
  235. const loading = ref() // ElLoading.service 返回的实例
  236. // 登录
  237. const handleLogin = async (params) => {
  238. loginLoading.value = true
  239. try {
  240. await getTenantId()
  241. const data = await validForm()
  242. if (!data) {
  243. return
  244. }
  245. const loginDataLoginForm = { ...loginData.loginForm }
  246. loginDataLoginForm.captchaVerification = params.captchaVerification
  247. const res = await LoginApi.login(loginDataLoginForm)
  248. if (!res) {
  249. return
  250. }
  251. loading.value = ElLoading.service({
  252. lock: true,
  253. text: '正在加载系统中...',
  254. background: 'rgba(0, 0, 0, 0.7)'
  255. })
  256. if (loginDataLoginForm.rememberMe) {
  257. authUtil.setLoginForm(loginDataLoginForm)
  258. } else {
  259. authUtil.removeLoginForm()
  260. }
  261. authUtil.setToken(res)
  262. const userType = await LoginApi.getInfo()
  263. console.log(userType,"我在这里")
  264. console.log(userType); // 查看 userType 的结构
  265. if (userType && Array.isArray(userType.roles)) {
  266. if (userType.roles.indexOf("student") !== -1 ) {
  267. console.log("我是学生");
  268. window.location.href = '/system/studentSelf/index';
  269. } else {
  270. console.log("我不是学生");
  271. window.location.href = '/system/Home/index';
  272. }
  273. } else {
  274. console.error("userType 或 roles 格式错误");
  275. }
  276. } finally {
  277. loginLoading.value = false
  278. loading.value.close()
  279. }
  280. }
  281. // 社交登录
  282. const doSocialLogin = async (type: number) => {
  283. if (type === 0) {
  284. message.error('此方式未配置')
  285. } else {
  286. loginLoading.value = true
  287. if (loginData.tenantEnable === 'true') {
  288. // 尝试先通过 tenantName 获取租户
  289. await getTenantId()
  290. // 如果获取不到,则需要弹出提示,进行处理
  291. if (!authUtil.getTenantId()) {
  292. try {
  293. const data = await message.prompt('请输入租户名称', t('common.reminder'))
  294. if (data?.action !== 'confirm') throw 'cancel'
  295. const res = await LoginApi.getTenantIdByName(data.value)
  296. authUtil.setTenantId(res)
  297. } catch (error) {
  298. if (error === 'cancel') return
  299. } finally {
  300. loginLoading.value = false
  301. }
  302. }
  303. }
  304. // 计算 redirectUri
  305. // tricky: type、redirect需要先encode一次,否则钉钉回调会丢失。
  306. // 配合 Login/SocialLogin.vue#getUrlValue() 使用
  307. const redirectUri =
  308. location.origin +
  309. '/social-login?' +
  310. encodeURIComponent(`type=${type}&redirect=${redirect.value || '/'}`)
  311. // 进行跳转
  312. const res = await LoginApi.socialAuthRedirect(type, encodeURIComponent(redirectUri))
  313. window.location.href = res
  314. }
  315. }
  316. watch(
  317. () => currentRoute.value,
  318. (route: RouteLocationNormalizedLoaded) => {
  319. redirect.value = route?.query?.redirect as string
  320. },
  321. {
  322. immediate: true
  323. }
  324. )
  325. onMounted(() => {
  326. getLoginFormCache()
  327. getTenantByWebsite()
  328. })
  329. </script>
  330. <style lang="scss" scoped>
  331. :deep(.anticon) {
  332. &:hover {
  333. color: var(--el-color-primary) !important;
  334. }
  335. }
  336. .login-code {
  337. float: right;
  338. width: 100%;
  339. height: 38px;
  340. img {
  341. width: 100%;
  342. height: auto;
  343. max-width: 100px;
  344. vertical-align: middle;
  345. cursor: pointer;
  346. }
  347. }
  348. </style>