cwg-system.vue 4.8 KB

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