util.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 { uuid } from 'utils/util'
  21. import { message } from 'antd'
  22. import { IPasswordShareToken, IToken } from './types'
  23. import { ActionTypes as AppActions } from 'share/containers/App/constants'
  24. export function getShareClientId(): string {
  25. let shareClientId = ''
  26. try {
  27. shareClientId = localStorage.getItem('SHARE_CLIENT_ID')
  28. if (!shareClientId) {
  29. shareClientId = uuid(32)
  30. localStorage.setItem('SHARE_CLIENT_ID', shareClientId)
  31. }
  32. } catch (err) {
  33. message.error(`获取分享页客户端ID失败:${err.message}`)
  34. }
  35. return shareClientId
  36. }
  37. export function querystring(str) {
  38. return str.split('&').reduce((o, kv) => {
  39. const [key, value] = kv.split('=')
  40. if (!value) {
  41. return o
  42. }
  43. deep_set(
  44. o,
  45. key.split(/[\[\]]/g).filter((x) => x),
  46. value
  47. )
  48. return o
  49. }, {})
  50. }
  51. function deep_set(o, path, value) {
  52. let i = 0
  53. const val = decodeURIComponent(value)
  54. for (; i < path.length - 1; i++) {
  55. if (o[path[i]] === undefined) {
  56. o[decodeURIComponent(path[i])] = path[i + 1].match(/^\d+$/) ? [] : {}
  57. }
  58. o = o[decodeURIComponent(path[i])]
  59. }
  60. if (o[decodeURIComponent(path[i])] && o[decodeURIComponent(path[i])].length) {
  61. const isInclude =
  62. Array.isArray(o[decodeURIComponent(path[i])]) &&
  63. o[decodeURIComponent(path[i])].includes(val)
  64. const isEqual = o[decodeURIComponent(path[i])] === val
  65. if (!(isInclude || isEqual)) {
  66. o[decodeURIComponent(path[i])] = [val].concat(
  67. o[decodeURIComponent(path[i])]
  68. )
  69. }
  70. } else {
  71. o[decodeURIComponent(path[i])] = val
  72. }
  73. }
  74. export function isAuthPassword(passwordShareTokens: IToken | -1) {
  75. let bool: boolean = false
  76. if (passwordShareTokens === -1) {
  77. return bool
  78. } else {
  79. const { expire } = passwordShareTokens
  80. bool = !isExpire(expire)
  81. }
  82. return bool
  83. }
  84. export function getExpirationTime(
  85. days: number,
  86. startTime: number = new Date().getTime()
  87. ): number {
  88. const day2Timestamp = 60 * 60 * 24 * 1000
  89. return startTime + days * day2Timestamp
  90. }
  91. export function isExpire(deadline: number): boolean {
  92. const now = new Date().getTime()
  93. return !!(now > deadline)
  94. }
  95. export function deserialization(str: string) {
  96. if (!(str && str.length)) {
  97. return {}
  98. }
  99. let obj: object = {}
  100. try {
  101. obj = JSON.parse(str)
  102. } catch (error) {
  103. throw new Error('this string can not be deserialization')
  104. }
  105. return obj
  106. }
  107. export function serialization(target: object) {
  108. if (isInvalidObj(target)) {
  109. throw new Error('this object can not be serialization')
  110. }
  111. let str: string = ''
  112. try {
  113. str = JSON.stringify(target)
  114. } catch (error) {
  115. throw new Error('this object can not be serialization')
  116. }
  117. return str
  118. }
  119. function isInvalidObj(target) {
  120. return [null, undefined, 'null', '', 'undefined'].includes(target)
  121. }
  122. export function localStorageCRUD(keys: string) {
  123. const tokens = localStorage.getItem(keys)
  124. const tokenObj = deserialization(tokens)
  125. function save(value: IPasswordShareToken) {
  126. if (isInvalidObj(value)) {
  127. return
  128. }
  129. const ser = serialization(value)
  130. localStorage.setItem(keys, ser)
  131. }
  132. function retrieve(token: string) {
  133. return tokenObj[token] ? tokenObj[token] : -1
  134. }
  135. function update(token: string, tokenValue: IToken) {
  136. const newValue = {
  137. ...tokenObj,
  138. [token]: tokenValue
  139. }
  140. save(newValue)
  141. }
  142. function deleted(token: string) {
  143. const newValue = {
  144. ...tokenObj
  145. }
  146. delete newValue[token]
  147. save(newValue)
  148. }
  149. function getAll() {
  150. return tokenObj
  151. }
  152. return {
  153. retrieve,
  154. update,
  155. deleted,
  156. getAll
  157. }
  158. }
  159. export function removeNoAuthedPassword(localStorageKey: string) {
  160. const { getAll, deleted } = localStorageCRUD(localStorageKey)
  161. Object.entries(getAll()).forEach(([key, value]) => {
  162. const isAuthPwd = isAuthPassword(value)
  163. if (!isAuthPwd) {
  164. deleted(key)
  165. }
  166. })
  167. }
  168. export function getPasswordUrl (shareType: string, token: string, url: string) {
  169. const tokenDetail = localStorageCRUD(AppActions.PASSWORD_SHARE_TOKENS).retrieve(token)
  170. let requestUrl = url
  171. if (shareType === 'PASSWORD' && tokenDetail) {
  172. const { password } = tokenDetail
  173. requestUrl = `${requestUrl}?password=${password}`
  174. }
  175. return requestUrl
  176. }