SocialLogin.vue 12 KB

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