cwg-system.vue 6.3 KB

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