common.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import Vue from 'vue'
  2. import * as R from 'ramda'
  3. import storage from '@/utils/storage'
  4. import { getCurrency, setCookieVal, getExchangeRateAvailable, getCostConversionOrigin } from '@/utils/common/cookie'
  5. import { Manager } from '@/utils/manager'
  6. export default {
  7. state: {
  8. recentMenus: storage.get('__oc_recent_menus__') || [],
  9. topAlert: {},
  10. bill: {
  11. currency: getCurrency() || '',
  12. currencyOpts: [],
  13. exchangeRateAvailable: getExchangeRateAvailable() || true,
  14. costConversionOrigin: getCostConversionOrigin() || true,
  15. globalConfig: {},
  16. },
  17. k8s: {
  18. cluster: undefined,
  19. namespace: undefined,
  20. },
  21. lbRedirected: {},
  22. // 菜单栏
  23. sidebar: {
  24. staredList: [],
  25. recentList: storage.get('__oc_recent_visit__') || [],
  26. },
  27. jsonschema: {
  28. sku: {},
  29. },
  30. globalConfig: {},
  31. openCloudShell: false,
  32. cloudShellHeight: 350,
  33. globalServices: [],
  34. computeV2GlobalConfig: {},
  35. imageGlobalConfig: {},
  36. yunionapiGlobalConfig: {},
  37. },
  38. mutations: {
  39. UPDATE_OBJECT (state, { name, data }) {
  40. if (!state[name]) Vue.set(state, name, {})
  41. Vue.set(state, name, { ...state[name], ...data })
  42. },
  43. DELETE_OBJECT (state, { name, key }) {
  44. Vue.delete(state[name], key)
  45. },
  46. SET_BILL_CURRENCY (state, payload) {
  47. setCookieVal('currency', payload)
  48. state.bill.currency = payload
  49. },
  50. SET_BILL_CURRENCYOPTS (state, payload) {
  51. let currencyOpts = []
  52. if (!state.bill.exchangeRateAvailable) {
  53. currencyOpts = R.clone(payload)
  54. } else {
  55. // 添加以汇率为单位的某个账单
  56. currencyOpts = payload.map(item => {
  57. return {
  58. item_id: '_' + item.item_id,
  59. item_name: '_' + item.item_name,
  60. }
  61. })
  62. }
  63. if (state.bill.costConversionOrigin) {
  64. payload.map(item => {
  65. if ((process.env.BRAND?.en || '').toLowerCase() === 'multicloud') {
  66. if (item.item_id.indexOf('BRL') === -1) {
  67. currencyOpts.push({
  68. item_id: '*' + (state.bill.exchangeRateAvailable ? '_' : '') + item.item_id,
  69. item_name: '*' + (state.bill.exchangeRateAvailable ? '_' : '') + item.item_name,
  70. })
  71. }
  72. } else {
  73. currencyOpts.push({
  74. item_id: '*' + (state.bill.exchangeRateAvailable ? '_' : '') + item.item_id,
  75. item_name: '*' + (state.bill.exchangeRateAvailable ? '_' : '') + item.item_name,
  76. })
  77. }
  78. })
  79. }
  80. state.bill.currencyOpts = currencyOpts
  81. },
  82. SET_BILL_EXCHANGE_RATE_AVAILABLE (state, payload) {
  83. setCookieVal('exchangeRateAvailable', payload)
  84. state.bill.exchangeRateAvailable = payload
  85. },
  86. SET_BILL_COST_CONVERSION_ORIGIN (state, payload) {
  87. setCookieVal('costConversionOrigin', payload)
  88. state.bill.costConversionOrigin = payload
  89. },
  90. SET_K8S_CLUSTER (state, payload) {
  91. state.k8s.cluster = payload
  92. },
  93. SET_K8S_NAMESPACE (state, payload) {
  94. state.k8s.namespace = payload
  95. },
  96. REST_BILL_CURRENCY (state) {
  97. state.bill.currencyOpts = []
  98. },
  99. SET_GLOBAL_CONFIG (state, payload) {
  100. state.globalConfig = payload
  101. },
  102. SET_GLOBAL_BILL_CONFIG (state, payload) {
  103. state.bill.globalConfig = payload
  104. },
  105. SET_OPEN_CLOUDSHELL (state, payload) {
  106. if (payload) {
  107. state.cloudShellHeight = 350
  108. }
  109. state.openCloudShell = payload
  110. },
  111. SET_CLOUDSHELL_HEIGHT (state, payload) {
  112. state.cloudShellHeight = payload
  113. },
  114. SET_GLOBAL_SERVICE (state, payload) {
  115. state.globalServices = payload
  116. },
  117. SET_COMPUTEV2_GLOBAL_CONFIG (state, payload) {
  118. state.computeV2GlobalConfig = payload
  119. },
  120. SET_IMAGE_GLOBAL_CONFIG (state, payload) {
  121. state.imageGlobalConfig = payload
  122. },
  123. },
  124. actions: {
  125. updateObject ({ commit }, payload) {
  126. commit('UPDATE_OBJECT', payload)
  127. },
  128. deleteObject ({ commit }, payload) {
  129. commit('DELETE_OBJECT', payload)
  130. },
  131. async fetchCurrency ({ commit, state, ...ret }, payload = {}) {
  132. try {
  133. const params = {
  134. query_type: 'currency',
  135. ...payload,
  136. }
  137. const { data: { data = [] } } = await new Manager('bill_conditions', 'v1').list({ params })
  138. commit('SET_BILL_EXCHANGE_RATE_AVAILABLE', data && data[0] ? data[0].exchange_rate_available || false : true)
  139. commit('SET_BILL_COST_CONVERSION_ORIGIN', data && data[0] ? data[0].cost_conversion_origin || false : true)
  140. commit('SET_BILL_CURRENCYOPTS', data)
  141. // 整理当前可展示类型列表,设置选中类型
  142. if (data && data.length > 0) {
  143. let currencyList = []
  144. if (data[0].exchange_rate_available) {
  145. data.map(item => {
  146. currencyList.push({
  147. item_id: '_' + item.item_id,
  148. item_name: '_' + item.item_name,
  149. })
  150. })
  151. } else {
  152. currencyList = data
  153. }
  154. if (data[0].cost_conversion_origin) {
  155. data.map(item => {
  156. if ((process.env.BRAND?.en || '').toLowerCase() === 'multicloud') {
  157. if (item.item_id.indexOf('BRL') === -1) {
  158. currencyList.push({
  159. item_id: '*' + (data[0].exchange_rate_available ? '_' : '') + item.item_id,
  160. item_name: '*' + (data[0].exchange_rate_available ? '_' : '') + item.item_name,
  161. })
  162. }
  163. } else {
  164. currencyList.push({
  165. item_id: '*' + (data[0].exchange_rate_available ? '_' : '') + item.item_id,
  166. item_name: '*' + (data[0].exchange_rate_available ? '_' : '') + item.item_name,
  167. })
  168. }
  169. })
  170. }
  171. const isExsit = currencyList.find(v => v.item_id === state.bill.currency)
  172. commit('SET_BILL_CURRENCY', isExsit ? state.bill.currency : currencyList[0].item_id)
  173. }
  174. } catch (error) {
  175. throw error
  176. }
  177. },
  178. // async fetchGlobalConfig ({ commit }) {
  179. // let manager = new Manager('services', 'v1')
  180. // try {
  181. // const response = await manager.list({
  182. // params: {
  183. // type: ['common', 'yunionapi', 'meter', 'identity', 'compute_v2', 'image'],
  184. // },
  185. // })
  186. // const resData = response?.data?.data
  187. // const commonId = resData.find(v => v.type === 'common')?.id || ''
  188. // if (commonId) {
  189. // const configResponse = await manager.getSpecific({
  190. // id: commonId,
  191. // spec: 'config',
  192. // })
  193. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  194. // commit('SET_GLOBAL_CONFIG', config)
  195. // }
  196. // const yunionapiId = resData.find(v => v.type === 'yunionapi')?.id || ''
  197. // if (yunionapiId) {
  198. // const configResponse = await manager.getSpecific({
  199. // id: yunionapiId,
  200. // spec: 'config',
  201. // })
  202. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  203. // commit('projectTags/SET_DATA', { name: 'enableOrganization', data: config.enable_organization }, { root: true })
  204. // }
  205. // const mneterId = resData.find(v => v.type === 'meter')?.id || ''
  206. // if (mneterId) {
  207. // const configResponse = await manager.getSpecific({
  208. // id: mneterId,
  209. // spec: 'config',
  210. // })
  211. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  212. // commit('SET_GLOBAL_BILL_CONFIG', config)
  213. // }
  214. // const identityId = resData.find(v => v.type === 'identity')?.id || ''
  215. // if (identityId) {
  216. // const configResponse = await manager.getSpecific({
  217. // id: identityId,
  218. // spec: 'config',
  219. // })
  220. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  221. // commit('auth/SET_NO_ACTION_LOGOUT_SECONDS', config.no_action_logout_seconds, { root: true })
  222. // }
  223. // const computeV2Id = resData.find(v => v.type === 'compute_v2')?.id || ''
  224. // if (computeV2Id) {
  225. // const configResponse = await manager.getSpecific({
  226. // id: computeV2Id,
  227. // spec: 'config',
  228. // })
  229. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  230. // commit('SET_COMPUTEV2_GLOBAL_CONFIG', config)
  231. // }
  232. // const imageId = resData.find(v => v.type === 'image')?.id || ''
  233. // if (imageId) {
  234. // const configResponse = await manager.getSpecific({
  235. // id: imageId,
  236. // spec: 'config',
  237. // })
  238. // const config = (configResponse.data.config && configResponse.data.config.default) || {}
  239. // commit('SET_IMAGE_GLOBAL_CONFIG', config)
  240. // }
  241. // } catch (error) {
  242. // throw error
  243. // } finally {
  244. // manager = null
  245. // }
  246. // },
  247. async fetchGlobalServices ({ commit, rootGetters }, paramObj) {
  248. try {
  249. const response = await new Manager('service_settings', 'v1').list()
  250. const { common = {}, yunionapi = {}, meter = {}, identity = {}, compute_v2 = {}, image = {} } = (response.data || {})
  251. commit('SET_GLOBAL_CONFIG', common)
  252. commit('projectTags/SET_DATA', { name: 'enableOrganization', data: yunionapi.enable_organization }, { root: true })
  253. commit('SET_GLOBAL_BILL_CONFIG', meter)
  254. commit('auth/SET_NO_ACTION_LOGOUT_SECONDS', identity.no_action_logout_seconds, { root: true })
  255. commit('SET_COMPUTEV2_GLOBAL_CONFIG', compute_v2)
  256. commit('SET_IMAGE_GLOBAL_CONFIG', image)
  257. commit('UPDATE_OBJECT', { name: 'yunionapiGlobalConfig', data: yunionapi.totp_issuer || '云联壹云' })
  258. if (rootGetters['auth/isAdminMode']) {
  259. const response = await new Manager('services', 'v1').list({
  260. params: {
  261. type: ['common'],
  262. ...paramObj,
  263. },
  264. })
  265. const data = (response.data.data && response.data.data) || []
  266. commit('SET_GLOBAL_SERVICE', data)
  267. }
  268. } catch (error) {
  269. // 忽略错误,避免阻塞页面渲染
  270. console.warn('fetchGlobalServices error:', error)
  271. }
  272. },
  273. },
  274. }