index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <template>
  2. <div>
  3. <!-- 币种 -->
  4. <a-form-model-item :label="$t('scope.text_453')" prop="currency">
  5. <a-select v-decorator="decorators.currency">
  6. <a-select-option v-for="obj in newCurrencys" :key="obj.item_id" :value="obj.item_id">
  7. {{ obj.item_name }}
  8. </a-select-option>
  9. </a-select>
  10. </a-form-model-item>
  11. <!-- 云环境和云平台 -->
  12. <a-form-item v-if="decorators.cloud_env" :label="$t('dashboard.text_98')">
  13. <a-row :gutter="8">
  14. <a-col :span="12">
  15. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0">
  16. <a-select
  17. allowClear
  18. class="w-100"
  19. :labelInValue="labelInValue"
  20. v-decorator="decorators.cloud_env"
  21. :placeholder="$t('dashboard.text_99')"
  22. @change="cloudEnvChange"
  23. :filterOption="filterOption">
  24. <a-select-option v-for="item of cloudEnvs" :value="item.key" :key="item.key">{{ item.label }}</a-select-option>
  25. </a-select>
  26. </a-form-item>
  27. </a-col>
  28. <a-col :span="12">
  29. <a-form-item :wrapperCol="{ span: 24 }" class="mb-0">
  30. <a-select
  31. allowClear
  32. class="w-100"
  33. :labelInValue="labelInValue"
  34. v-decorator="decorators.brand"
  35. :placeholder="$t('dashboard.text_99')"
  36. @change="brandChange"
  37. :filterOption="filterOption">
  38. <a-select-option v-for="item of brands" :value="item.key" :key="item.key">{{ item.label }}</a-select-option>
  39. </a-select>
  40. </a-form-item>
  41. </a-col>
  42. </a-row>
  43. </a-form-item>
  44. <!-- 云账号 -->
  45. <a-form-item v-if="decorators.account" :label="$t('scope.text_568')" class="mb-0">
  46. <a-select
  47. allowClear
  48. class="w-100"
  49. :labelInValue="labelInValue"
  50. v-decorator="decorators.account"
  51. :placeholder="$t('dashboard.text_99')"
  52. @change="accountChange"
  53. :filterOption="filterOption">
  54. <a-select-option v-for="item of accounts" :value="item.key" :key="item.key">{{ item.label }}</a-select-option>
  55. </a-select>
  56. </a-form-item>
  57. </div>
  58. </template>
  59. <script>
  60. import * as R from 'ramda'
  61. import _ from 'lodash'
  62. import { mapGetters, mapState } from 'vuex'
  63. import { CURRENCYS } from '@/constants'
  64. import { currencyUnitMap } from '@/constants/currency'
  65. import { typeClouds } from '@/utils/common/hypervisor'
  66. export default {
  67. name: 'ConsumptionConfig',
  68. props: {
  69. labelInValue: {
  70. type: Boolean,
  71. },
  72. decorators: {
  73. type: Object,
  74. },
  75. fc: {
  76. type: Object,
  77. required: true,
  78. },
  79. fd: {
  80. type: Object,
  81. required: false,
  82. },
  83. },
  84. data () {
  85. return {
  86. CURRENCYS,
  87. cloudEnvs: [],
  88. cloudEnvData: {},
  89. brands: [],
  90. brandData: {},
  91. accounts: [],
  92. accountLoading: false,
  93. }
  94. },
  95. computed: {
  96. ...mapState('common', {
  97. currency: state => state.bill.currency,
  98. currencyOpts: state => state.bill.currencyOpts,
  99. }),
  100. ...mapGetters(['scope', 'capability']),
  101. cloudEnv () {
  102. return R.is(Object, this.cloudEnvData) ? this.cloudEnvData.key : this.cloudEnvData
  103. },
  104. brand () {
  105. return R.is(Object, this.brandData) ? this.brandData.key : this.brandData
  106. },
  107. newCurrencys () {
  108. return this.currencyOpts.map(val => {
  109. const localItem = currencyUnitMap[val.item_id]
  110. const text = localItem ? localItem.label : val.item_name
  111. const currencyItem = { ...val }
  112. if (val.item_id.indexOf('*_') !== -1) {
  113. currencyItem.item_name = this.$t('common.show_origin_2', [val.item_name.replace('*_', '')])
  114. } else if (val.item_id.indexOf('_') !== -1) {
  115. currencyItem.item_name = this.$t('common.unit_shows_all_bills', [val.item_name.replace('_', '')])
  116. } else if (val.item_id.indexOf('*') !== -1) {
  117. currencyItem.item_name = this.$t('common.show_origin_1', [val.item_name.replace('*', '')])
  118. } else {
  119. currencyItem.item_name = this.$t('common.only_show_bills', [text])
  120. }
  121. return currencyItem
  122. })
  123. },
  124. },
  125. destroyed () {
  126. this.am = null
  127. this.rm = null
  128. },
  129. mounted () {
  130. this.am = new this.$Manager('cloudaccounts')
  131. this.fetchCloudEnvs()
  132. this.fetchAccounts()
  133. },
  134. methods: {
  135. filterOption (input, option) {
  136. return (
  137. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  138. )
  139. },
  140. /*
  141. * @params {Object} cloudEnv { key: <cloudEnvId> }
  142. */
  143. _setInitCloudEnv (cloudEnv) {
  144. if (!R.isNil(cloudEnv) && !R.isEmpty(cloudEnv)) {
  145. if (this.labelInValue) {
  146. this.fc.setFieldsValue({
  147. cloud_env: { key: cloudEnv.key, label: cloudEnv.label },
  148. })
  149. } else {
  150. this.fc.setFieldsValue({
  151. cloud_env: cloudEnv.key,
  152. })
  153. }
  154. }
  155. },
  156. /*
  157. * @params {Object} brand { key: <brandId> }
  158. */
  159. _setInitBrand (brand) {
  160. if (!R.isNil(brand) && !R.isEmpty(brand)) {
  161. if (this.labelInValue) {
  162. this.fc.setFieldsValue({
  163. brand: { key: brand.key, label: brand.label },
  164. })
  165. } else {
  166. this.fc.setFieldsValue({
  167. brand: brand.key,
  168. })
  169. }
  170. }
  171. },
  172. /*
  173. * @params {Object} account { key: <accountId> }
  174. */
  175. _setInitAccount (account) {
  176. if (!R.isNil(account) && !R.isEmpty(account)) {
  177. if (this.labelInValue) {
  178. this.fc.setFieldsValue({
  179. account: { key: account.key, label: account.label },
  180. })
  181. } else {
  182. this.fc.setFieldsValue({
  183. account: account.key,
  184. })
  185. }
  186. }
  187. },
  188. fetchCloudEnvs () {
  189. const cloudEnvs = []
  190. let brands = this.capability.brands || []
  191. brands = R.concat(brands, this.capability.disabled_brands || [])
  192. brands = R.uniq(brands)
  193. for (let i = 0, len = brands.length; i < len; i++) {
  194. const data = R.find(R.propEq('key', typeClouds.brandMap[brands[i]].cloud_env))(cloudEnvs)
  195. if (!data) {
  196. cloudEnvs.push({ key: typeClouds.brandMap[brands[i]].cloud_env, label: this.$t(`cloud_env.${typeClouds.brandMap[brands[i]].cloud_env}`) })
  197. }
  198. }
  199. this.cloudEnvs = cloudEnvs.map(val => ({ ...val, key: val.key, label: val.label }))
  200. let defaultData
  201. const initialValue = _.get(this.decorators, 'cloud_env[1].initialValue')
  202. if (initialValue) {
  203. const findInitValue = this.cloudEnvs.find(val => val.key === (initialValue.key || initialValue))
  204. if (findInitValue) {
  205. defaultData = { key: findInitValue.key, label: findInitValue.label }
  206. }
  207. }
  208. this._setInitCloudEnv(defaultData)
  209. this.cloudEnvChange(defaultData || {})
  210. },
  211. fetchBrands () {
  212. let brands = []
  213. R.forEachObjIndexed((value, key) => {
  214. if (value.cloud_env === this.cloudEnv) {
  215. brands.push(value)
  216. }
  217. }, typeClouds.brandMap)
  218. brands = brands.filter(item => this.capability.brands.includes(item.brand) || this.capability.disabled_brands.includes(item.brand))
  219. this.brands = brands.map(val => ({ ...val, key: val.key, label: val.label }))
  220. let defaultData
  221. const initialValue = _.get(this.decorators, 'brand[1].initialValue')
  222. if (initialValue) {
  223. const findInitValue = this.brands.find(val => val.key === (initialValue.key || initialValue))
  224. if (findInitValue) {
  225. defaultData = { key: findInitValue.key, label: findInitValue.label }
  226. }
  227. }
  228. this._setInitBrand(defaultData)
  229. this.brandChange(defaultData || {})
  230. },
  231. async fetchAccounts () {
  232. this.accountLoading = true
  233. try {
  234. const params = {
  235. scope: this.scope,
  236. }
  237. if (this.cloudEnv) params.cloud_env = this.cloudEnv
  238. if (this.brand) params.brand = this.brand
  239. const response = await this.am.list({ params })
  240. const data = response.data.data
  241. this.accounts = data.map(val => ({ ...val, key: val.id, label: val.name })) || []
  242. let defaultData
  243. const initialValue = _.get(this.decorators, 'account[1].initialValue')
  244. if (initialValue) {
  245. const findInitValue = this.accounts.find(val => val.key === (initialValue.key || initialValue))
  246. if (findInitValue) {
  247. defaultData = { key: findInitValue.key, label: findInitValue.label }
  248. }
  249. }
  250. this.accountChange(defaultData || {})
  251. this._setInitAccount(defaultData || {})
  252. } catch (error) {
  253. throw error
  254. } finally {
  255. this.accountLoading = false
  256. }
  257. },
  258. cloudEnvChange (cloudEnv) {
  259. const cloudEnvId = R.is(Object, cloudEnv) ? cloudEnv.key : cloudEnv
  260. this.cloudEnvData = cloudEnv
  261. if (cloudEnvId) {
  262. this.fc.setFieldsValue({
  263. brand: undefined,
  264. account: undefined,
  265. })
  266. this.fetchBrands()
  267. this.fetchAccounts()
  268. this.$emit('update:env', cloudEnvId)
  269. } else {
  270. this.fc.setFieldsValue({
  271. cloud_env: undefined,
  272. brand: undefined,
  273. account: undefined,
  274. })
  275. }
  276. // this.updateUsages()
  277. },
  278. brandChange (brand) {
  279. const brandId = R.is(Object, brand) ? brand.key : brand
  280. this.brandData = brand
  281. if (brandId) {
  282. this.fc.setFieldsValue({
  283. account: undefined,
  284. })
  285. this.fetchAccounts()
  286. this.$emit('update:brand', brandId)
  287. this.fc.getFieldDecorator('brand', { preserve: true, initialValue: brand })
  288. } else {
  289. this.fc.setFieldsValue({
  290. brand: undefined,
  291. account: undefined,
  292. })
  293. }
  294. },
  295. accountChange (account) {
  296. const accountId = R.is(Object, account) ? account.key : account
  297. this.$emit('update:account', accountId)
  298. },
  299. },
  300. }
  301. </script>