SocialLogin.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <template>
  2. <div
  3. :class="prefixCls"
  4. class="relative h-[100%] lt-md:px-10px lt-sm:px-10px lt-xl:px-10px lt-xl:px-10px">
  5. <div class="relative mx-auto h-full flex">
  6. <div
  7. :class="`${prefixCls}__left flex-1 bg-gray-500 bg-opacity-20 relative p-30px lt-xl:hidden overflow-x-hidden overflow-y-auto`">
  8. <!-- 左上角的 logo + 系统标题 -->
  9. <div class="relative flex items-center text-white">
  10. <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
  11. <span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
  12. </div>
  13. <!-- 左边的背景图 + 欢迎语 -->
  14. <div class="h-[calc(100%-60px)] flex items-center justify-center">
  15. <TransitionGroup
  16. appear
  17. enter-active-class="animate__animated animate__bounceInLeft"
  18. tag="div">
  19. <img key="1" alt="" class="w-350px" src="@/assets/svgs/login-box-bg.svg" />
  20. <div key="2" class="text-3xl text-white">{{ t('login.welcome') }}</div>
  21. <div key="3" class="mt-5 text-14px font-normal text-white">
  22. {{ t('login.message') }}
  23. </div>
  24. </TransitionGroup>
  25. </div>
  26. </div>
  27. <div
  28. class="relative flex-1 p-30px dark:bg-[var(--login-bg-color)] lt-sm:p-10px overflow-x-hidden overflow-y-auto">
  29. <!-- 右上角的主题、语言选择 -->
  30. <div
  31. class="flex items-center justify-between text-white at-2xl:justify-end at-xl:justify-end">
  32. <div class="flex items-center at-2xl:hidden at-xl:hidden">
  33. <img alt="" class="mr-10px h-48px w-48px" src="@/assets/imgs/logo.png" />
  34. <span class="text-20px font-bold">{{ underlineToHump(appStore.getTitle) }}</span>
  35. </div>
  36. <div class="flex items-center justify-end space-x-10px h-48px">
  37. <ThemeSwitch />
  38. <LocaleDropdown class="dark:text-white lt-xl:text-white" />
  39. </div>
  40. </div>
  41. <!-- 右边的登录界面 -->
  42. <Transition appear enter-active-class="animate__animated animate__bounceInRight">
  43. <div
  44. class="m-auto h-[calc(100%-60px)] w-[100%] flex items-center at-2xl:max-w-500px at-lg:max-w-500px at-md:max-w-500px at-xl:max-w-500px">
  45. <!-- 账号登录 -->
  46. <el-form
  47. v-show="getShow"
  48. ref="formLogin"
  49. :model="loginData.loginForm"
  50. :rules="LoginRules"
  51. class="login-form"
  52. label-position="top"
  53. label-width="120px"
  54. size="large">
  55. <el-row style="margin-right: -10px; margin-left: -10px">
  56. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  57. <el-form-item>
  58. <LoginFormTitle style="width: 100%" />
  59. </el-form-item>
  60. </el-col>
  61. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  62. <el-form-item v-if="loginData.tenantEnable" prop="tenantName">
  63. <el-input
  64. v-model="loginData.loginForm.tenantName"
  65. :placeholder="t('login.tenantNamePlaceholder')"
  66. :prefix-icon="iconHouse"
  67. link
  68. type="primary" />
  69. </el-form-item>
  70. </el-col>
  71. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  72. <el-form-item prop="username">
  73. <el-input
  74. v-model="loginData.loginForm.username"
  75. :placeholder="t('login.usernamePlaceholder')"
  76. :prefix-icon="iconAvatar" />
  77. </el-form-item>
  78. </el-col>
  79. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  80. <el-form-item prop="password">
  81. <el-input
  82. v-model="loginData.loginForm.password"
  83. :placeholder="t('login.passwordPlaceholder')"
  84. :prefix-icon="iconLock"
  85. show-password
  86. type="password"
  87. @keyup.enter="getCode()" />
  88. </el-form-item>
  89. </el-col>
  90. <el-col
  91. :span="24"
  92. style="
  93. padding-right: 10px;
  94. padding-left: 10px;
  95. margin-top: -20px;
  96. margin-bottom: -20px;
  97. ">
  98. <el-form-item>
  99. <el-row justify="space-between" style="width: 100%">
  100. <el-col :span="6">
  101. <el-checkbox v-model="loginData.loginForm.rememberMe">
  102. {{ t('login.remember') }}
  103. </el-checkbox>
  104. </el-col>
  105. <el-col :offset="6" :span="12">
  106. <el-link style="float: right" type="primary"
  107. >{{ t('login.forgetPassword') }}
  108. </el-link>
  109. </el-col>
  110. </el-row>
  111. </el-form-item>
  112. </el-col>
  113. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  114. <el-form-item>
  115. <XButton
  116. :loading="loginLoading"
  117. :title="t('login.login')"
  118. class="w-[100%]"
  119. type="primary"
  120. @click="getCode()" />
  121. </el-form-item>
  122. </el-col>
  123. <Verify
  124. v-if="loginData.captchaEnable === 'true'"
  125. ref="verify"
  126. :captchaType="captchaType"
  127. :imgSize="{ width: '400px', height: '200px' }"
  128. mode="pop"
  129. @success="handleLogin" />
  130. </el-row>
  131. </el-form>
  132. </div>
  133. </Transition>
  134. </div>
  135. </div>
  136. </div>
  137. </template>
  138. <script lang="ts" setup>
  139. import { underlineToHump } from '@/utils'
  140. import { ElLoading } from 'element-plus'
  141. import { useDesign } from '@/hooks/web/useDesign'
  142. import { useAppStore } from '@/store/modules/app'
  143. import { useIcon } from '@/hooks/web/useIcon'
  144. import { usePermissionStore } from '@/store/modules/permission'
  145. import * as LoginApi from '@/api/login'
  146. import * as authUtil from '@/utils/auth'
  147. import { ThemeSwitch } from '@/layout/components/ThemeSwitch'
  148. import { LocaleDropdown } from '@/layout/components/LocaleDropdown'
  149. import { LoginStateEnum, useFormValid, useLoginState } from './components/useLogin'
  150. import LoginFormTitle from './components/LoginFormTitle.vue'
  151. import router from '@/router'
  152. defineOptions({ name: 'SocialLogin' })
  153. const { t } = useI18n()
  154. const route = useRoute()
  155. const appStore = useAppStore()
  156. const { getPrefixCls } = useDesign()
  157. const prefixCls = getPrefixCls('login')
  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<any>()
  162. const { validForm } = useFormValid(formLogin)
  163. const { getLoginState } = useLoginState()
  164. const { push } = useRouter()
  165. const permissionStore = usePermissionStore()
  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 !== 'false',
  178. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE !== 'false',
  179. loginForm: {
  180. tenantName: '芋道源码',
  181. username: '',
  182. password: '',
  183. captchaVerification: '',
  184. rememberMe: false
  185. }
  186. })
  187. // 获取验证码
  188. const getCode = async () => {
  189. // 情况一,未开启:则直接登录
  190. if (!loginData.captchaEnable) {
  191. await handleLogin({})
  192. } else {
  193. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  194. // 弹出验证码
  195. verify.value.show()
  196. }
  197. }
  198. //获取租户ID
  199. const getTenantId = async () => {
  200. if (loginData.tenantEnable) {
  201. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  202. authUtil.setTenantId(res)
  203. }
  204. }
  205. // 记住我
  206. const getCookie = () => {
  207. const loginForm = authUtil.getLoginForm()
  208. if (loginForm) {
  209. loginData.loginForm = {
  210. ...loginData.loginForm,
  211. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  212. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  213. rememberMe: loginForm.rememberMe ? true : false,
  214. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  215. }
  216. }
  217. }
  218. const loading = ref() // ElLoading.service 返回的实例
  219. // tricky: 配合LoginForm.vue中redirectUri需要对参数进行encode,需要在回调后进行decode
  220. function getUrlValue(key: string): string {
  221. const url = new URL(decodeURIComponent(location.href))
  222. return url.searchParams.get(key) ?? ''
  223. }
  224. // 尝试登录: 当账号已经绑定,socialLogin会直接获得token
  225. const tryLogin = async () => {
  226. try {
  227. const type = getUrlValue('type')
  228. const redirect = getUrlValue('redirect')
  229. const code = route?.query?.code as string
  230. const state = route?.query?.state as string
  231. await getTenantId()
  232. const res = await LoginApi.socialLogin(type, code, state)
  233. authUtil.setToken(res)
  234. router.push({ path: redirect || '/' })
  235. } catch (err) {}
  236. }
  237. // 登录
  238. const handleLogin = async (params) => {
  239. loginLoading.value = true
  240. try {
  241. await getTenantId()
  242. const data = await validForm()
  243. if (!data) {
  244. return
  245. }
  246. let redirect = getUrlValue('redirect')
  247. const type = getUrlValue('type')
  248. const code = route?.query?.code as string
  249. const state = route?.query?.state as string
  250. const loginDataLoginForm = { ...loginData.loginForm }
  251. const res = await LoginApi.login({
  252. // 账号密码登录
  253. username: loginDataLoginForm.username,
  254. password: loginDataLoginForm.password,
  255. captchaVerification: params.captchaVerification,
  256. // 社交登录
  257. socialCode: code,
  258. socialState: state,
  259. socialType: type
  260. })
  261. if (!res) {
  262. return
  263. }
  264. loading.value = ElLoading.service({
  265. lock: true,
  266. text: '正在加载系统中...',
  267. background: 'rgba(0, 0, 0, 0.7)'
  268. })
  269. if (loginDataLoginForm.rememberMe) {
  270. authUtil.setLoginForm(loginDataLoginForm)
  271. } else {
  272. authUtil.removeLoginForm()
  273. }
  274. authUtil.setToken(res)
  275. if (!redirect) {
  276. redirect = '/'
  277. }
  278. // 判断是否为SSO登录
  279. if (redirect.indexOf('sso') !== -1) {
  280. window.location.href = window.location.href.replace('/login?redirect=', '')
  281. } else {
  282. push({ path: redirect || permissionStore.addRouters[0].path })
  283. }
  284. } finally {
  285. loginLoading.value = false
  286. loading.value?.close()
  287. }
  288. }
  289. onMounted(() => {
  290. debugger
  291. getCookie()
  292. tryLogin()
  293. })
  294. </script>
  295. <style lang="scss" scoped>
  296. $prefix-cls: #{$namespace}-login;
  297. .#{$prefix-cls} {
  298. overflow: auto;
  299. &__left {
  300. &::before {
  301. position: absolute;
  302. top: 0;
  303. left: 0;
  304. z-index: -1;
  305. width: 100%;
  306. height: 100%;
  307. background-image: url('@/assets/svgs/login-bg.svg');
  308. background-position: center;
  309. background-repeat: no-repeat;
  310. content: '';
  311. }
  312. }
  313. }
  314. </style>