index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { Tag, Space, Badge, Modal, Select, Avatar as AntdAvatar } from 'antd';
  2. import React, { useCallback, useEffect, useState } from 'react';
  3. import { useModel } from 'umi';
  4. import Avatar from './AvatarDropdown';
  5. import styles from './index.less';
  6. import { exitFullScreen, requestFullScreen, isFull } from '@/utils/fullScreen'
  7. import { BellOutlined, UserOutlined } from '@ant-design/icons';
  8. import { getAppListApi } from '@/services/iaaData';
  9. import { useAjax } from '@/Hook/useAjax';
  10. export type SiderTheme = 'light' | 'dark';
  11. const GlobalHeaderRight: React.FC<{}> = () => {
  12. const [left, setLeft] = useState(0)
  13. const { initialState, setInitialState } = useModel('@@initialState');
  14. const { state: { versionsOpen }, dispatch, getversions } = useModel('useOperating.useWxGroupList')
  15. if (!initialState || !initialState.settings) {
  16. return null;
  17. }
  18. const [appList, setApplist] = useState<{ label: string, value: string, productType: string; mediaPlatform: string; icon?: string }[]>([])
  19. const getAppList = useAjax(() => getAppListApi())
  20. const [productTypeList, setProductTypeList] = useState<{ label: string, value: string }[]>([])
  21. const [mediaPlatformList, setMediaPlatformList] = useState<{ label: string, value: string }[]>([])
  22. const [productType, setProductType] = useState<string>()
  23. const [isJr, setIsJr] = useState<boolean>(false)
  24. const haveTo = (type: 'MEDIA_PLATFORM_TENCENT' | 'MEDIA_PLATFORM_OCENAENGINE') => {
  25. let menus = initialState?.menu?.data?.find((item: { path: string; }) => item.path === "/iaaData")?.routes
  26. const firstTencent = menus?.find((item: { path: string; }) => item.path === "/iaaData/tencentIaa")?.routes?.[0]?.path
  27. const firstOcenaengine = menus?.find((item: { path: string; }) => item.path === "/iaaData/ocenaengineIaa")?.routes?.[0]?.path
  28. switch (type) {
  29. case 'MEDIA_PLATFORM_TENCENT':
  30. if (!location.hash.includes('tencentIaa')) {
  31. window.location.href = `/#${firstTencent}`
  32. }
  33. break
  34. case 'MEDIA_PLATFORM_OCENAENGINE':
  35. if (!location.hash.includes('ocenaengineIaa')) {
  36. window.location.href = `/#${firstOcenaengine}`
  37. }
  38. break
  39. }
  40. }
  41. useEffect(() => {
  42. if (location.hash.includes('/iaaSystem')) {
  43. setIsJr(() => true)
  44. }
  45. }, [location.hash])
  46. useEffect(() => {
  47. if (location.hash.includes('/iaaData') && !isJr) {
  48. setIsJr(() => true)
  49. getAppList.run().then(res => {
  50. if (res?.data) {
  51. let menus = initialState?.menu?.data?.find((item: { path: string; }) => item.path === "/iaaData")?.routes
  52. const firstTencent = menus?.find((item: { path: string; }) => item.path === "/iaaData/tencentIaa")?.routes?.[0]?.path
  53. const firstOcenaengine = menus?.find((item: { path: string; }) => item.path === "/iaaData/ocenaengineIaa")?.routes?.[0]?.path
  54. const productTypeSet = new Set();
  55. const mediaPlatformSet = new Set();
  56. const data: { label: string, value: string, productType: string; mediaPlatform: string; icon?: string }[] = res?.data?.filter((item: { mediaPlatform: string; }) => (firstTencent && item.mediaPlatform === 'MEDIA_PLATFORM_TENCENT') || (firstOcenaengine && item.mediaPlatform === 'MEDIA_PLATFORM_OCENAENGINE'))?.map((item: { name: string; id: number; productType: string; mediaPlatform: string; icon?: string }) => {
  57. productTypeSet.add(item.productType)
  58. mediaPlatformSet.add(item.mediaPlatform)
  59. return { label: item.name, value: item.id + '||' + item.productType, productType: item.productType, mediaPlatform: item.mediaPlatform, icon: item.icon }
  60. })
  61. if (data?.length) {
  62. setProductTypeList([{ label: '游戏业务', value: 'PRODUCT_TYPE_GAME' }, { label: '小说业务', value: 'PRODUCT_TYPE_NOVEL' }, { label: '短剧业务', value: 'PRODUCT_TYPE_PLAYLET' }].filter(item => [...productTypeSet].includes(item.value)))
  63. setMediaPlatformList([{ label: '腾讯', value: 'MEDIA_PLATFORM_TENCENT' }, { label: '头条', value: 'MEDIA_PLATFORM_OCENAENGINE' }].filter(item => [...mediaPlatformSet].includes(item.value)))
  64. const app = initialState?.iaaApp
  65. let iaaApp: string;
  66. let mediaPlatform: string;
  67. const d = data.find(item => item.value === app)
  68. if (app && d) {
  69. iaaApp = app
  70. mediaPlatform = d.mediaPlatform
  71. } else {
  72. iaaApp = data?.[0].value || undefined as any
  73. mediaPlatform = data?.[0].mediaPlatform || undefined as any
  74. }
  75. const productType = iaaApp ? iaaApp.split('||')?.[1] : undefined
  76. haveTo(mediaPlatform as any)
  77. sessionStorage.setItem('mediaPlatform', mediaPlatform)
  78. setProductType(productType)
  79. setInitialState({ ...initialState, iaaApp, mediaPlatform })
  80. setApplist(data)
  81. }
  82. }
  83. })
  84. }
  85. }, [location.hash, isJr])
  86. useEffect(() => {
  87. setTimeout(() => {
  88. let nav: any = document.querySelector('.top-nav-menu')
  89. let navLi: any = document.querySelectorAll('.top-nav-menu > li')
  90. if (nav && navLi) {
  91. let l = nav.offsetLeft
  92. let lw = 0
  93. for (const iterator of navLi) {
  94. lw += iterator.offsetWidth
  95. }
  96. setLeft(l + lw - 50)
  97. }
  98. }, 30)
  99. }, [])
  100. const { navTheme, layout } = initialState.settings;
  101. let className = styles.right;
  102. if ((navTheme === 'dark' && layout === 'top') || layout === 'mix') {
  103. className = `${styles.right} ${styles.dark}`;
  104. }
  105. let handleFullScreen = () => {
  106. if (isFull()) {
  107. exitFullScreen()
  108. } else {
  109. requestFullScreen()
  110. }
  111. }
  112. //消息弹窗更新
  113. const inform = useCallback(() => {
  114. let bdVersions = localStorage.getItem('versions') //本地
  115. getversions.run().then((res) => {
  116. let fwqVersions = ''//服务器版本
  117. let content: any = ''//版本更新内容
  118. if (res) {
  119. fwqVersions = res?.configValue
  120. content = <div dangerouslySetInnerHTML={{ __html: res?.remark?.replace(/\n/ig, '<br/>') }}></div >
  121. if (fwqVersions && bdVersions !== fwqVersions) {//不一样
  122. //执行逻辑
  123. Modal.confirm({
  124. title: '版本更新!',
  125. content,
  126. keyboard: false,
  127. cancelText: '稍后我自己刷新页面',
  128. okText: '现在就去刷新页面',
  129. onOk: () => {
  130. localStorage.setItem('versions', fwqVersions)//重新设置服务器版本
  131. location?.reload()
  132. },
  133. onCancel: () => {
  134. dispatch({ type: 'versionsOpen', params: { versionsOpen: true } })
  135. }
  136. })
  137. console.log('不一样')
  138. }
  139. }
  140. })
  141. }, [])
  142. return (
  143. <div style={{ display: 'flex', justifyContent: 'space-between' }}>
  144. {location.hash.includes('/iaaData') && <div style={{ left: left + 10, position: 'absolute' }}>
  145. <Select
  146. showSearch
  147. style={{ width: 100 }}
  148. placeholder={'请选择业务类型'}
  149. filterOption={(input, option) =>
  150. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  151. }
  152. onChange={(type) => {
  153. let newApplist = appList.filter(item => item.productType === type)
  154. sessionStorage.setItem('mediaPlatform', newApplist?.[0]?.mediaPlatform)
  155. newApplist?.[0]?.mediaPlatform && haveTo(newApplist?.[0]?.mediaPlatform as any);
  156. setInitialState({ ...initialState, iaaApp: newApplist?.[0]?.value, mediaPlatform: newApplist?.[0]?.mediaPlatform })
  157. setProductType(type)
  158. }}
  159. value={productType}
  160. options={productTypeList}
  161. />
  162. <Select
  163. showSearch
  164. style={{ width: 100, marginLeft: 10 }}
  165. placeholder={'请选择媒体'}
  166. filterOption={(input, option) =>
  167. (option?.label as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0
  168. }
  169. onChange={(type) => {
  170. let newApplist = appList.filter(item => item.productType === productType).filter(item => item.mediaPlatform === type)
  171. sessionStorage.setItem('mediaPlatform', type)
  172. haveTo(type as any)
  173. setInitialState({ ...initialState, iaaApp: newApplist?.[0]?.value, mediaPlatform: type })
  174. }}
  175. value={initialState?.mediaPlatform}
  176. options={mediaPlatformList}
  177. />
  178. <Select
  179. showSearch
  180. style={{ width: 200, marginLeft: 10 }}
  181. placeholder={'请选择应用'}
  182. filterOption={(input, option) => (option?.name as any)?.toLowerCase().indexOf(input.toLowerCase()) >= 0}
  183. value={initialState?.iaaApp}
  184. onChange={(e) => setInitialState({ ...initialState, iaaApp: e })}
  185. loading={getAppList.loading}
  186. >
  187. {appList.filter(item => item.productType === productType).filter(item => item.mediaPlatform === initialState?.mediaPlatform).map(item => {
  188. return <Select.Option key={item.value} value={item.value} name={item.label}>
  189. <Space align='center'>
  190. <AntdAvatar shape="square" style={{ marginBottom: 4 }} size={24} icon={<UserOutlined />} src={item?.icon} />
  191. <span>{item.label}</span>
  192. </Space>
  193. </Select.Option>
  194. })}
  195. </Select>
  196. </div>}
  197. <Space className={className}>
  198. {versionsOpen && <div onClick={versionsOpen && inform}>
  199. <Badge count={versionsOpen && 1} size='small' offset={[2, 0]}>
  200. <span style={{ cursor: 'pointer' }} ><BellOutlined /></span>
  201. </Badge>
  202. </div>}
  203. <Avatar />
  204. <Tag color="#87d068" onClick={handleFullScreen}>全屏模式</Tag>
  205. </Space>
  206. </div>
  207. );
  208. };
  209. export default GlobalHeaderRight;