cwg-system.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. 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(type = false) {
  76. const cached = getCache()
  77. console.log(1)
  78. if (!type){
  79. if (cached && cached.timestamp && Date.now() - cached.timestamp < CACHE_DURATION) {
  80. applySystemList(cached.data || [])
  81. return
  82. }
  83. }
  84. console.log('qingqiule')
  85. try {
  86. const res: any = await customApi.getSystemList({})
  87. if (res && res.code === Code.StatusOK) {
  88. const data = res.data || []
  89. applySystemList(data)
  90. setCache(data)
  91. } else {
  92. uni.showToast({ title: (res && (res.msg || res.message)) || '获取系统列表失败', icon: 'none' })
  93. }
  94. } catch (e) {
  95. uni.showToast({ title: '获取系统列表失败', icon: 'none' })
  96. }
  97. }
  98. function getSystemDisplayName(item: SystemItem) {
  99. if (!item) return ''
  100. if (locale.value === 'en') return item.sysNameEn || item.sysName || item.sysCode
  101. return item.sysName || item.sysNameEn || item.sysCode
  102. }
  103. function switchSystem(item: SystemItem) {
  104. if (!item || !item.sysCode) return
  105. if (item.sysCode === currentSystemCode.value) return
  106. uni.showModal({
  107. title: '系统提示',
  108. content: '是否切换系统?',
  109. confirmText: '确认',
  110. cancelText: '取消',
  111. success: async (res) => {
  112. if (!res.confirm) return
  113. await confirmSwitchSystem(item)
  114. },
  115. })
  116. }
  117. async function confirmSwitchSystem(item: SystemItem) {
  118. try {
  119. const res: any = await customApi.switchSystem({ sysCode: item.sysCode })
  120. if (res && res.code === Code.StatusOK) {
  121. const accessToken = res.data
  122. const token = typeof accessToken === 'string' ? accessToken : String(accessToken || '')
  123. const baseUrl = `${item.sysUrl}/#/signin?sysLoginToken=${btoa(token)}`
  124. console.log(baseUrl)
  125. // #ifdef H5
  126. window.location.replace(baseUrl)
  127. // #endif
  128. // #ifndef H5
  129. uni.showToast({ title: '仅支持在H5端切换系统', icon: 'none' })
  130. // #endif
  131. } else {
  132. uni.showToast({ title: (res && (res.msg || res.message)) || '切换系统失败', icon: 'none' })
  133. }
  134. } catch (e) {
  135. uni.showToast({ title: '切换系统失败', icon: 'none' })
  136. }
  137. }
  138. function handleMenuClick({ value }: { value: any }) {
  139. switchSystem(value)
  140. }
  141. const isInit = ref(false)
  142. onMounted(() => {
  143. if (isInit.value) return
  144. isInit.value = true
  145. getSystemList()
  146. uni.$on('updateSystemList', () => {
  147. getSystemList(true)
  148. })
  149. })
  150. onUnmounted(() => {
  151. uni.$off('updateSystemList')
  152. })
  153. </script>
  154. <style scoped lang="scss">
  155. @import "@/uni.scss";
  156. .cwg-system {
  157. @media screen and (max-width: 991px) {
  158. :deep(.cwg-dropdown-menu-container) {
  159. right: px2rpx(-20) !important;
  160. //max-width: px2rpx(400);
  161. }
  162. }
  163. }
  164. .pc-header-btn {
  165. display: flex;
  166. align-items: center;
  167. cursor: pointer;
  168. width: 80px;
  169. gap: px2rpx(4);
  170. }
  171. .current-system-name {
  172. font-size: px2rpx(14);
  173. color: var(--bs-emphasis-color);
  174. }
  175. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  176. min-height: px2rpx(36);
  177. }
  178. :deep(.cwg-dropdown) {
  179. overflow: visible !important;
  180. }
  181. </style>