cwg-system.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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. 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. systemList.value = Array.isArray(data) ? data : []
  74. if (systemList.value.length > 0) {
  75. const localSystem = systemList.value.find((item) => item && item.localSystem === 1)
  76. currentSystemCode.value = localSystem ? localSystem.sysCode : null
  77. } else {
  78. currentSystemCode.value = null
  79. }
  80. }
  81. async function getSystemList(type = false) {
  82. const cached = getCache()
  83. console.log('system',type,props.form)
  84. // top-window组件中,没有token不去请求接口
  85. if (props.eventSource === 'top') {
  86. if (!userToken.value) return
  87. }
  88. if (!type){
  89. if (cached && cached.timestamp && Date.now() - cached.timestamp < CACHE_DURATION) {
  90. applySystemList(cached.data || [])
  91. return
  92. }
  93. }
  94. // console.log('qingqiule')
  95. try {
  96. const res: any = await customApi.getSystemList({})
  97. if (res && res.code === Code.StatusOK) {
  98. const data = res.data || []
  99. applySystemList(data)
  100. setCache(data)
  101. } else {
  102. uni.showToast({ title: (res && (res.msg || res.message)) || '获取系统列表失败', icon: 'none' })
  103. }
  104. } catch (e) {
  105. uni.showToast({ title: '获取系统列表失败', icon: 'none' })
  106. }
  107. }
  108. function getSystemDisplayName(item: SystemItem) {
  109. if (!item) return ''
  110. if (locale.value === 'en') return item.sysNameEn || item.sysName || item.sysCode
  111. return item.sysName || item.sysNameEn || item.sysCode
  112. }
  113. function switchSystem(item: SystemItem) {
  114. if (!item || !item.sysCode) return
  115. if (item.sysCode === currentSystemCode.value) return
  116. uni.showModal({
  117. title: '系统提示',
  118. content: '是否切换系统?',
  119. confirmText: '确认',
  120. cancelText: '取消',
  121. success: async (res) => {
  122. if (!res.confirm) return
  123. await confirmSwitchSystem(item)
  124. },
  125. })
  126. }
  127. async function confirmSwitchSystem(item: SystemItem) {
  128. try {
  129. if (props.eventSource === 'login'){
  130. const baseUrl = `${item.sysUrl}/#/signin`
  131. console.log(baseUrl)
  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. console.log(baseUrl)
  145. // #ifdef H5
  146. window.location.replace(baseUrl)
  147. // #endif
  148. // #ifndef H5
  149. uni.showToast({ title: '仅支持在H5端切换系统', icon: 'none' })
  150. // #endif
  151. } else {
  152. uni.showToast({ title: (res && (res.msg || res.message)) || '切换系统失败', icon: 'none' })
  153. }
  154. }
  155. } catch (e) {
  156. console.log(e)
  157. uni.showToast({ title: '切换系统失败', icon: 'none' })
  158. }
  159. }
  160. function handleMenuClick({ value }: { value: any }) {
  161. switchSystem(value)
  162. }
  163. const isInit = ref(false)
  164. onMounted(() => {
  165. if (isInit.value) return
  166. isInit.value = true
  167. getSystemList()
  168. uni.$on('updateSystemList', (source) => {
  169. // console.log(source)
  170. // 如果指定了事件来源,只处理来自该来源的事件
  171. if (props.eventSource && source !== props.eventSource) return
  172. getSystemList(true)
  173. })
  174. })
  175. onUnmounted(() => {
  176. uni.$off('updateSystemList')
  177. })
  178. </script>
  179. <style scoped lang="scss">
  180. @import "@/uni.scss";
  181. .cwg-system {
  182. @media screen and (max-width: 991px) {
  183. :deep(.cwg-dropdown-menu-container) {
  184. right: px2rpx(-20) !important;
  185. //max-width: px2rpx(400);
  186. }
  187. }
  188. }
  189. .pc-header-btn {
  190. display: flex;
  191. align-items: center;
  192. cursor: pointer;
  193. width: px2rpx(80);
  194. gap: px2rpx(4);
  195. }
  196. .current-system-name {
  197. font-size: px2rpx(14);
  198. color: var(--bs-emphasis-color);
  199. }
  200. :deep(.cwg-dropdown-menu-container .menu .menu-item) {
  201. min-height: px2rpx(36);
  202. }
  203. :deep(.cwg-dropdown) {
  204. overflow: visible !important;
  205. }
  206. </style>