cwg-system.vue 6.0 KB

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