SocialLogin.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <div class="container p-20px h-100% w-100%">
  3. <div class="logo"></div>
  4. <!-- 登录区域 -->
  5. <div class="content">
  6. <!-- 配图 -->
  7. <div class="pic"></div>
  8. <!-- 表单 -->
  9. <div class="field">
  10. <!-- [移动端]标题 -->
  11. <!-- <h2 class="mobile-title">
  12. <h3 class="title">浙江万维后台管理系统</h3>
  13. </h2> -->
  14. <!-- 表单 -->
  15. <div class="form-cont">
  16. <el-tabs class="form" v-model="loginData.isSocialLogin" style="float: none">
  17. <el-tab-pane label="绑定账号" name="uname" />
  18. </el-tabs>
  19. <div>
  20. <el-form
  21. ref="formLogin"
  22. :model="loginData.loginForm"
  23. :rules="loginRules"
  24. class="login-form"
  25. >
  26. <!-- 账号密码登录 -->
  27. <el-form-item prop="username">
  28. <el-input
  29. v-model="loginData.loginForm.username"
  30. :placeholder="t('login.usernamePlaceholder')"
  31. :prefix-icon="iconAvatar"
  32. />
  33. </el-form-item>
  34. <el-form-item prop="password">
  35. <el-input
  36. v-model="loginData.loginForm.password"
  37. :placeholder="t('login.passwordPlaceholder')"
  38. :prefix-icon="iconLock"
  39. show-password
  40. type="password"
  41. @keyup.enter="getCode()"
  42. />
  43. </el-form-item>
  44. <!-- 下方的登录按钮 -->
  45. <el-col :span="24" style="padding-right: 10px; padding-left: 10px">
  46. <el-form-item>
  47. <XButton
  48. :loading="loginLoading"
  49. :title="t('login.login')"
  50. class="w-[100%]"
  51. type="primary"
  52. @click="getCode()"
  53. />
  54. </el-form-item>
  55. </el-col>
  56. <Verify
  57. ref="verify"
  58. :captchaType="captchaType"
  59. :imgSize="{ width: '400px', height: '200px' }"
  60. mode="pop"
  61. @success="handleLogin"
  62. />
  63. </el-form>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </template>
  70. <script lang="ts" setup>
  71. import { ElLoading } from 'element-plus'
  72. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  73. import { useRoute } from 'vue-router'
  74. import { useIcon } from '@/hooks/web/useIcon'
  75. import * as authUtil from '@/utils/auth'
  76. import { usePermissionStore } from '@/store/modules/permission'
  77. import * as LoginApi from '@/api/login'
  78. import { LoginStateEnum, useFormValid, useLoginState } from './components/useLogin'
  79. defineOptions({ name: 'SocialLogin' })
  80. const { t } = useI18n()
  81. const message = useMessage()
  82. const iconHouse = useIcon({ icon: 'ep:house' })
  83. const iconAvatar = useIcon({ icon: 'ep:avatar' })
  84. const iconLock = useIcon({ icon: 'ep:lock' })
  85. const formLogin = ref()
  86. const { validForm } = useFormValid(formLogin)
  87. const { setLoginState, getLoginState } = useLoginState()
  88. const { currentRoute, push } = useRouter()
  89. const permissionStore = usePermissionStore()
  90. const redirect = ref<string>('')
  91. const type = ref<number>('')
  92. const code = ref<string>('')
  93. const state = ref<string>('')
  94. const loginLoading = ref(false)
  95. const verify = ref()
  96. const captchaType = ref('blockPuzzle') // blockPuzzle 滑块 clickWord 点击文字
  97. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN)
  98. const LoginRules = {
  99. username: [required],
  100. password: [required]
  101. }
  102. const loginData = reactive({
  103. isShowPassword: false,
  104. isSocialLogin: true,
  105. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE,
  106. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE,
  107. loginForm: {
  108. tenantName: '',
  109. username: '',
  110. password: '',
  111. captchaVerification: ''
  112. }
  113. })
  114. // 获取验证码
  115. const getCode = async () => {
  116. // 情况一,未开启:则直接登录
  117. if (loginData.captchaEnable === 'false') {
  118. await handleLogin({})
  119. } else {
  120. // 情况二,已开启:则展示验证码;只有完成验证码的情况,才进行登录
  121. // 弹出验证码
  122. verify.value.show()
  123. }
  124. }
  125. // 记住我
  126. const getCookie = () => {
  127. const loginForm = authUtil.getLoginForm()
  128. if (loginForm) {
  129. loginData.loginForm = {
  130. ...loginData.loginForm,
  131. username: loginForm.username ? loginForm.username : loginData.loginForm.username,
  132. password: loginForm.password ? loginForm.password : loginData.loginForm.password,
  133. rememberMe: loginForm.rememberMe ? true : false,
  134. socialCode: code.value,
  135. socialState: state.value,
  136. socialType: type.value,
  137. tenantName: loginForm.tenantName ? loginForm.tenantName : loginData.loginForm.tenantName
  138. }
  139. }
  140. }
  141. //获取租户ID
  142. const getTenantId = async () => {
  143. if (loginData.tenantEnable === 'true') {
  144. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  145. authUtil.setTenantId(res)
  146. }
  147. }
  148. const loading = ref() // ElLoading.service 返回的实例
  149. // 登录
  150. const handleLogin = async (params) => {
  151. loginLoading.value = true
  152. try {
  153. await getTenantId()
  154. const data = await validForm()
  155. if (!data) {
  156. return
  157. }
  158. loginData.loginForm.captchaVerification = params.captchaVerification
  159. const res = await LoginApi.login(loginData.loginForm)
  160. if (!res) {
  161. return
  162. }
  163. loading.value = ElLoading.service({
  164. lock: true,
  165. text: '正在加载系统中...',
  166. background: 'rgba(0, 0, 0, 0.7)'
  167. })
  168. if (loginData.loginForm.rememberMe) {
  169. authUtil.setLoginForm(loginData.loginForm)
  170. } else {
  171. authUtil.removeLoginForm()
  172. }
  173. authUtil.setToken(res)
  174. if (!redirect.value) {
  175. redirect.value = '/'
  176. }
  177. // 判断是否为SSO登录
  178. if (redirect.value.indexOf('sso') !== -1) {
  179. window.location.href = window.location.href.replace('/login?redirect=', '')
  180. } else {
  181. // push({ path: redirect.value || permissionStore.addRouters[0].path })
  182. window.parent.pushWin() //触发iframe父页面的方法
  183. }
  184. } finally {
  185. loginLoading.value = false
  186. loading.value.close()
  187. }
  188. }
  189. const socialLoading = ref() // ElLoading.service 返回的实例
  190. // 社交登录
  191. const socialLogin = async () => {
  192. const route = useRoute()
  193. // 社交登录相关
  194. type.value = route?.query?.type as number
  195. code.value = route?.query?.authCode as string
  196. state.value = route?.query?.state as string
  197. if (!type.value) {
  198. type.value = 20
  199. }
  200. let obj = {
  201. socialCode: code.value,
  202. socialState: state.value,
  203. socialType: type.value
  204. }
  205. loginData.loginForm = Object.assign(loginData.loginForm, obj)
  206. // 尝试登录一下
  207. loginLoading.value = true
  208. try {
  209. const res = await LoginApi.socialLogin({
  210. type: type.value,
  211. code: code.value,
  212. state: state.value
  213. })
  214. if (!res) {
  215. return
  216. }
  217. socialLoading.value = ElLoading.service({
  218. lock: true,
  219. text: '正在加载系统中...',
  220. background: 'rgba(0, 0, 0, 0.7)'
  221. })
  222. authUtil.setToken(res)
  223. if (!redirect.value) {
  224. redirect.value = '/'
  225. }
  226. // 判断是否为SSO登录
  227. if (redirect.value.indexOf('sso') !== -1) {
  228. window.location.href = window.location.href.replace('/login?redirect=', '')
  229. } else {
  230. // push({ path: redirect.value || permissionStore.addRouters[0].path })
  231. window.parent.pushWin() //触发iframe父页面的方法
  232. }
  233. } finally {
  234. loginLoading.value = false
  235. socialLoading.value.close()
  236. }
  237. }
  238. watch(
  239. () => currentRoute.value,
  240. (route: RouteLocationNormalizedLoaded) => {
  241. redirect.value = route?.query?.redirect as string
  242. },
  243. {
  244. immediate: true
  245. }
  246. )
  247. onMounted(() => {
  248. window.parent.iframeZzc()
  249. getCookie()
  250. socialLogin()
  251. })
  252. </script>
  253. <style lang="scss" scoped>
  254. :deep(.anticon) {
  255. &:hover {
  256. color: var(--el-color-primary) !important;
  257. }
  258. }
  259. </style>