index.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React, { useCallback, useEffect, useRef, useState } from 'react'
  2. import dd from 'gdt-jsapi'
  3. import styles from './index.module.scss'
  4. import { MessageFu } from '@/utils/message'
  5. import { domShowFu } from '@/utils/domShow'
  6. import { API_ddLogin } from '@/store/action/layout'
  7. import { setTokenInfo } from '@/utils/storage'
  8. import history from '@/utils/history'
  9. function DingLogin() {
  10. const [txt, setTxt] = useState('正在授权登录中...')
  11. let time = useRef(-1)
  12. const succFu = useCallback((val: string, code?: string) => {
  13. clearTimeout(time.current)
  14. time.current = window.setTimeout(async () => {
  15. domShowFu('#AsyncSpinLoding', false)
  16. if (val === '成功') {
  17. const res = await API_ddLogin(code!)
  18. if (res.code === 0) {
  19. MessageFu.success('登录成功')
  20. // 用户信息存到本地
  21. setTokenInfo(res.data)
  22. history.push('/')
  23. }
  24. }
  25. }, 500)
  26. }, [])
  27. useEffect(() => {
  28. domShowFu('#AsyncSpinLoding', true)
  29. dd.ready(function () {
  30. dd.getAuthCode({
  31. corpId: ''
  32. })
  33. .then(res => {
  34. succFu('成功', res.auth_code)
  35. })
  36. .catch(err => {
  37. succFu('失败')
  38. setTxt(err)
  39. MessageFu.error(err)
  40. })
  41. })
  42. }, [succFu])
  43. return (
  44. <div className={styles.DingLogin}>
  45. <div className='DingLoginBox'>
  46. <h2>{txt}</h2>
  47. </div>
  48. </div>
  49. )
  50. }
  51. const MemoDingLogin = React.memo(DingLogin)
  52. export default MemoDingLogin