cwg-system.vue 4.6 KB

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