Interceptor.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { memo, useMemo, useState, useCallback, useEffect } from 'react'
  21. import { useDispatch, useSelector } from 'react-redux'
  22. import { useInjectReducer } from 'utils/injectReducer'
  23. import { useInjectSaga } from 'utils/injectSaga'
  24. import { ActionTypes } from './constants'
  25. import { AppActions } from './actions'
  26. import { makeSelectShareType, makeSelectPermissionLoading } from './selectors'
  27. import Password from 'share/components/Password'
  28. import Login from 'share/components/Login'
  29. import {
  30. querystring,
  31. isAuthPassword,
  32. localStorageCRUD,
  33. removeNoAuthedPassword,
  34. getExpirationTime
  35. } from '../../util'
  36. import reducer from './reducer'
  37. import saga from './sagas'
  38. import { Tmode } from 'app/components/SharePanel/types'
  39. import { message } from 'antd'
  40. import { makeSelectLoginLoading } from '../App/selectors'
  41. const Interceptor: React.FC<any> = (props) => {
  42. useInjectReducer({ key: 'global', reducer })
  43. useInjectSaga({ key: 'global', saga })
  44. const dispatch = useDispatch()
  45. const shareType: Tmode = useSelector(makeSelectShareType())
  46. const loading: boolean = useSelector(makeSelectPermissionLoading())
  47. const loginLoading: boolean = useSelector(makeSelectLoginLoading())
  48. const { shareToken } = useMemo(
  49. () => querystring(window.location.search.substr(1)),
  50. [window.location.search]
  51. )
  52. const [authPassword, setAuthPwd] = useState<boolean>(() => {
  53. const shareTokenObj = localStorageCRUD(
  54. ActionTypes.PASSWORD_SHARE_TOKENS
  55. ).retrieve(shareToken)
  56. return isAuthPassword(shareTokenObj)
  57. })
  58. const [islegitimate, setLegitimate] = useState<boolean>(false)
  59. const CheckPassword = useCallback(
  60. (password: string) => {
  61. dispatch(
  62. AppActions.getPermissions(
  63. shareToken,
  64. password,
  65. () => {
  66. setAuthPwd(true)
  67. localStorageCRUD(ActionTypes.PASSWORD_SHARE_TOKENS).update(
  68. shareToken,
  69. {
  70. password,
  71. expire: getExpirationTime(7)
  72. }
  73. )
  74. },
  75. () => {
  76. return message.error('口令输入错误')
  77. }
  78. )
  79. )
  80. },
  81. [shareToken]
  82. )
  83. const afterLogin = useCallback(() => {
  84. localStorage.removeItem('shareToken')
  85. localStorage.removeItem('shareRoute')
  86. setLegitimate(true)
  87. dispatch(AppActions.getPermissions(shareToken))
  88. }, [islegitimate])
  89. const content = useMemo(() => {
  90. if (shareType && shareType.length) {
  91. if (shareType === 'PASSWORD' && !authPassword) {
  92. return <Password onCheck={CheckPassword} loading={loading} />
  93. }
  94. if (shareType === 'AUTH' && !islegitimate) {
  95. return (
  96. <Login
  97. loading={loginLoading}
  98. shareToken={shareToken}
  99. loginCallback={afterLogin}
  100. />
  101. )
  102. }
  103. return <>{props.children}</>
  104. }
  105. return <></>
  106. }, [shareType, authPassword, islegitimate])
  107. useEffect(() => {
  108. // delete expire token
  109. removeNoAuthedPassword(ActionTypes.PASSWORD_SHARE_TOKENS)
  110. }, [])
  111. return content
  112. }
  113. export default memo(Interceptor)