cwg-system.vue 5.6 KB

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