cwg-system.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <view class="cwg-system" v-if="systemMenuList.length">
  3. <cwg-dropdown :menu-list="systemMenuList" @menuClick="handleMenuClick">
  4. <view class="pc-header-btn">
  5. <text class="current-system-name">{{ currentSystemName }}</text>
  6. <cwg-icon name="crm-chevron-down" :color="iconColor" darkColor="#000" :size="14" />
  7. </view>
  8. </cwg-dropdown>
  9. </view>
  10. <cwg-global-popup/>
  11. </template>
  12. <script setup lang="ts">
  13. import { computed, ref, onMounted,onUnmounted,nextTick} from 'vue'
  14. import { useI18n } from 'vue-i18n'
  15. import Config from '@/config/index'
  16. import { customApi } from '@/service/custom'
  17. import { userToken } from "@/composables/config";
  18. import { usePopup } from '@/hooks/usePopup'
  19. import useUserStore from '@/stores/use-user-store'
  20. const userStore = useUserStore()
  21. const { confirm } = usePopup()
  22. const props = defineProps({
  23. iconColor: {
  24. type: String,
  25. default: '#97A1C0'
  26. },
  27. textColor: {
  28. type: String,
  29. default: '#97A1C0'
  30. },
  31. // 事件来源标识,用于区分不同组件的事件监听
  32. eventSource: {
  33. type: String,
  34. default: ''
  35. }
  36. })
  37. const { Code } = Config
  38. const { t, locale } = useI18n()
  39. type SystemItem = {
  40. sysCode: string
  41. sysUrl: string
  42. sysName?: string
  43. sysNameEn?: string
  44. localSystem?: number
  45. }
  46. const systemList = ref<SystemItem[]>([])
  47. const currentSystemCode = ref<string | null>(null)
  48. const systemMenuList = computed(() =>{
  49. return systemList.value.map((item) => ({
  50. ...item,
  51. label: getSystemDisplayName(item),
  52. }))
  53. })
  54. const currentSystemName = computed(() => {
  55. if (!currentSystemCode.value || systemList.value.length === 0) {
  56. return t('vu.system.title1')
  57. }
  58. const currentSystem = systemList.value.find((item) => item.sysCode === currentSystemCode.value)
  59. if (!currentSystem) return t('vu.system.title1')
  60. return getSystemDisplayName(currentSystem)
  61. })
  62. const CACHE_KEY = 'systemListCache'
  63. // 4 * 60 * 60 * 1000
  64. const CACHE_DURATION = 14400000
  65. const getCache = () => {
  66. const cached = uni.getStorageSync(CACHE_KEY)
  67. if (!cached) return null
  68. try {
  69. return typeof cached === 'string' ? JSON.parse(cached) : cached
  70. } catch (e) {
  71. return null
  72. }
  73. }
  74. const setCache = (data: any) => {
  75. uni.setStorageSync(CACHE_KEY, JSON.stringify({ data, timestamp: Date.now() }))
  76. }
  77. const applySystemList = (data: any[]) => {
  78. // console.log(data,props.eventSource)
  79. systemList.value = Array.isArray(data) ? data : []
  80. if (systemList.value.length > 0) {
  81. const localSystem = systemList.value.find((item) => item && item.localSystem === 1)
  82. currentSystemCode.value = localSystem ? localSystem.sysCode : null
  83. } else {
  84. currentSystemCode.value = null
  85. }
  86. }
  87. async function getSystemList(type = false) {
  88. const cached = getCache()
  89. // console.log('system',type,props.eventSource)
  90. // top-window组件中,没有token不去请求接口
  91. if (props.eventSource === 'top') {
  92. if (!userToken.value) return
  93. }
  94. // if (!type){
  95. if (cached && cached.timestamp && Date.now() - cached.timestamp < CACHE_DURATION) {
  96. applySystemList(cached.data || [])
  97. return
  98. }
  99. // }
  100. try {
  101. const res: any = await customApi.getSystemList({})
  102. // console.log('res',res)
  103. if (res && res.code === Code.StatusOK) {
  104. const data = res.data || []
  105. applySystemList(data)
  106. setCache(data)
  107. } else {
  108. uni.showToast({ title: (res && res.msg ) || t('vu.system.tip1'), icon: 'none' })
  109. }
  110. } catch (e) {
  111. uni.showToast({ title: t('vu.system.tip1'), icon: 'none' })
  112. }
  113. }
  114. function getSystemDisplayName(item: SystemItem) {
  115. if (!item) return ''
  116. if (locale.value === 'en') return item.sysNameEn || item.sysName || item.sysCode
  117. return item.sysName || item.sysNameEn || item.sysCode
  118. }
  119. async function switchSystem(item: SystemItem) {
  120. if (!item || !item.sysCode) return
  121. if (item.sysCode === currentSystemCode.value) return
  122. const result = await confirm({
  123. title: t('Msg.SystemPrompt'),
  124. content: t('vu.system.content'),
  125. confirmText: t('Btn.Confirm'),
  126. cancelText: t('Btn.Cancel'),
  127. })
  128. if (result){
  129. await confirmSwitchSystem(item)
  130. }
  131. // uni.showModal({
  132. // title: '系统提示',
  133. // content: '是否切换系统?',
  134. // confirmText: '确认',
  135. // cancelText: '取消',
  136. // success: async (res) => {
  137. // if (!res.confirm) return
  138. //
  139. // },
  140. // })
  141. }
  142. async function confirmSwitchSystem(item: SystemItem) {
  143. try {
  144. if (props.eventSource === 'login'){
  145. const baseUrl = `${item.sysUrl}/#/signin`
  146. // #ifdef H5
  147. userStore.clearUserInfo()
  148. window.location.replace(baseUrl)
  149. // window.open(baseUrl, '_blank')
  150. // #endif
  151. // #ifndef H5
  152. uni.showToast({ title: '仅支持在H5端切换系统', icon: 'none' })
  153. // #endif
  154. }else{
  155. const res: any = await customApi.switchSystem({ sysCode: item.sysCode })
  156. if (res && res.code === Code.StatusOK) {
  157. const accessToken = res.data
  158. const token = typeof accessToken === 'string' ? accessToken : String(accessToken || '')
  159. const baseUrl = `${item.sysUrl}/#/signin?sysLoginToken=${btoa(token)}`
  160. // #ifdef H5
  161. userStore.clearUserInfo()
  162. window.location.replace(baseUrl)
  163. // window.open(baseUrl, '_blank')
  164. // #endif
  165. // #ifndef H5
  166. uni.showToast({ title: '仅支持在H5端切换系统', icon: 'none' })
  167. // #endif
  168. } else {
  169. uni.showToast({ title: (res && (res.msg || res.message)) || '切换系统失败', icon: 'none' })
  170. }
  171. }
  172. } catch (e) {
  173. uni.showToast({ title: '切换系统失败', icon: 'none' })
  174. }
  175. }
  176. function handleMenuClick({ value }: { value: any }) {
  177. switchSystem(value)
  178. }
  179. const isInit = ref(false)
  180. onMounted(() => {
  181. if (isInit.value) return
  182. isInit.value = true
  183. getSystemList()
  184. uni.$on('updateSystemList', (source) => {
  185. // console.log(source)
  186. // 如果指定了事件来源,只处理来自该来源的事件
  187. // if (props.eventSource && source !== props.eventSource) return
  188. getSystemList(true)
  189. })
  190. })
  191. onUnmounted(() => {
  192. uni.$off('updateSystemList')
  193. })
  194. </script>
  195. <style scoped lang="scss">
  196. @import "@/uni.scss";
  197. .cwg-system {
  198. @media screen and (max-width: 991px) {
  199. :deep(.cwg-dropdown-menu-container) {
  200. right: px2rpx(-20) !important;
  201. //max-width: px2rpx(400);
  202. }
  203. }
  204. }
  205. .pc-header-btn {
  206. display: flex;
  207. align-items: center;
  208. cursor: pointer;
  209. width: px2rpx(80);
  210. gap: px2rpx(4);
  211. }
  212. .current-system-name {
  213. font-size: px2rpx(14);
  214. color: var(--bs-emphasis-color);
  215. }
  216. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  217. min-height: px2rpx(36);
  218. }
  219. :deep(.cwg-dropdown) {
  220. overflow: visible !important;
  221. }
  222. </style>