cwg-system.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <cwg-dropdown :menu-list="systemMenuList" @menuClick="handleMenuClick">
  3. <view class="pc-header-btn">
  4. <text class="current-system-name">{{ currentSystemName }}</text>
  5. <cwg-icon name="crm-chevron-down" color="#000" :size="14" />
  6. </view>
  7. </cwg-dropdown>
  8. </template>
  9. <script setup lang="ts">
  10. import { computed, ref, onMounted } from 'vue'
  11. import { useI18n } from 'vue-i18n'
  12. import Config from '@/config/index'
  13. import { customApi } from '@/service/custom'
  14. const props = defineProps({
  15. })
  16. const { Code } = Config
  17. const { locale } = useI18n()
  18. type SystemItem = {
  19. sysCode: string
  20. sysUrl: string
  21. sysName?: string
  22. sysNameEn?: string
  23. localSystem?: number
  24. }
  25. const systemList = ref<SystemItem[]>([])
  26. const currentSystemCode = ref<string | null>(null)
  27. const systemMenuList = computed(() =>
  28. systemList.value.map((item) => ({
  29. ...item,
  30. label: getSystemDisplayName(item),
  31. })),
  32. )
  33. const currentSystemName = computed(() => {
  34. if (!currentSystemCode.value || systemList.value.length === 0) {
  35. return '系统切换'
  36. }
  37. const currentSystem = systemList.value.find((item) => item.sysCode === currentSystemCode.value)
  38. if (!currentSystem) return '系统切换'
  39. return getSystemDisplayName(currentSystem)
  40. })
  41. const CACHE_KEY = 'systemListCache'
  42. // 4 * 60 * 60 * 1000
  43. const CACHE_DURATION = 14400000
  44. const getCache = () => {
  45. const cached = uni.getStorageSync(CACHE_KEY)
  46. if (!cached) return null
  47. try {
  48. return typeof cached === 'string' ? JSON.parse(cached) : cached
  49. } catch (e) {
  50. return null
  51. }
  52. }
  53. const setCache = (data: any) => {
  54. uni.setStorageSync(CACHE_KEY, JSON.stringify({ data, timestamp: Date.now() }))
  55. }
  56. const applySystemList = (data: any[]) => {
  57. systemList.value = Array.isArray(data) ? data : []
  58. if (systemList.value.length > 0) {
  59. const localSystem = systemList.value.find((item) => item && item.localSystem === 1)
  60. currentSystemCode.value = localSystem ? localSystem.sysCode : null
  61. } else {
  62. currentSystemCode.value = null
  63. }
  64. }
  65. async function getSystemList() {
  66. const cached = getCache()
  67. if (cached && cached.timestamp && Date.now() - cached.timestamp < CACHE_DURATION) {
  68. applySystemList(cached.data || [])
  69. return
  70. }
  71. try {
  72. const res: any = await customApi.getSystemList({})
  73. if (res && res.code === Code.StatusOK) {
  74. const data = res.data || []
  75. applySystemList(data)
  76. setCache(data)
  77. } else {
  78. uni.showToast({ title: (res && (res.msg || res.message)) || '获取系统列表失败', icon: 'none' })
  79. }
  80. } catch (e) {
  81. uni.showToast({ title: '获取系统列表失败', icon: 'none' })
  82. }
  83. }
  84. function getSystemDisplayName(item: SystemItem) {
  85. if (!item) return ''
  86. if (locale.value === 'en') return item.sysNameEn || item.sysName || item.sysCode
  87. return item.sysName || item.sysNameEn || item.sysCode
  88. }
  89. function switchSystem(item: SystemItem) {
  90. if (!item || !item.sysCode) return
  91. if (item.sysCode === currentSystemCode.value) return
  92. uni.showModal({
  93. title: '系统提示',
  94. content: '是否切换系统?',
  95. confirmText: '确认',
  96. cancelText: '取消',
  97. success: async (res) => {
  98. if (!res.confirm) return
  99. await confirmSwitchSystem(item)
  100. },
  101. })
  102. }
  103. async function confirmSwitchSystem(item: SystemItem) {
  104. try {
  105. const res: any = await customApi.switchSystem({ sysCode: item.sysCode })
  106. if (res && res.code === Code.StatusOK) {
  107. const accessToken = res.data
  108. const token = typeof accessToken === 'string' ? accessToken : String(accessToken || '')
  109. const baseUrl = `${item.sysUrl}/#/signin?sysLoginToken=${btoa(token)}`
  110. console.log(baseUrl)
  111. // #ifdef H5
  112. window.location.replace(baseUrl)
  113. // #endif
  114. // #ifndef H5
  115. uni.showToast({ title: '仅支持在H5端切换系统', icon: 'none' })
  116. // #endif
  117. } else {
  118. uni.showToast({ title: (res && (res.msg || res.message)) || '切换系统失败', icon: 'none' })
  119. }
  120. } catch (e) {
  121. uni.showToast({ title: '切换系统失败', icon: 'none' })
  122. }
  123. }
  124. function handleMenuClick({ value }: { value: any }) {
  125. switchSystem(value)
  126. }
  127. onMounted(() => {
  128. getSystemList()
  129. })
  130. </script>
  131. <style scoped lang="scss">
  132. @import "@/uni.scss";
  133. .pc-header-btn {
  134. display: flex;
  135. align-items: center;
  136. cursor: pointer;
  137. gap: px2rpx(4);
  138. }
  139. .current-system-name {
  140. font-size: px2rpx(14);
  141. }
  142. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  143. min-height: px2rpx(36);
  144. }
  145. :deep(.cwg-dropdown) {
  146. overflow: visible !important;
  147. }
  148. </style>