BottomBar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <template>
  2. <div class="create-server-result-wrap">
  3. <page-footer>
  4. <template v-slot:left>
  5. <div
  6. v-for="(tip, idx) of tips"
  7. :key="idx"
  8. class="d-flex flex-column justify-content-center flex-grow-1 content">
  9. <div
  10. v-for="obj of tip"
  11. :key="obj.label"
  12. class="d-flex align-items-center">
  13. <span class="label" :class="obj.labelClass">{{ obj.label }}:</span>
  14. <template v-if="obj.value">
  15. <span class="value config text-truncate" :class="obj.valueClass">{{ obj.value }}</span>
  16. </template>
  17. <template v-else>
  18. <span class="value placeholder text-truncate" :class="obj.valueClass">------</span>
  19. </template>
  20. </div>
  21. </div>
  22. </template>
  23. <template v-slot:right>
  24. <div class="d-flex align-items-center">
  25. <div v-if="hasMeterService" class="mr-4 d-flex align-items-center">
  26. <div class="text-truncate">{{$t('compute.text_286')}}</div>
  27. <div class="ml-2 prices">
  28. <div class="hour d-flex">
  29. <template v-if="price">
  30. <m-animated-number :value="price" :formatValue="priceFormat" />
  31. <discount-price class="ml-2 mini-text" :discount="discount" :origin="originPrice" />
  32. </template>
  33. <template v-else>---</template>
  34. </div>
  35. <div class="tips text-truncate">
  36. <span v-html="priceTips" />
  37. </div>
  38. </div>
  39. </div>
  40. <!-- <a-dropdown-button
  41. v-if="$appConfig.isPrivate && !$store.getters.isSysCE && hasCartPermission"
  42. :title="confirmText"
  43. class="text-truncate"
  44. type="primary"
  45. native-type="submit"
  46. html-type="submit"
  47. :loading="loading"
  48. placement="topLeft"
  49. :disabled="disabled || !!errors.length">
  50. {{ confirmText }}
  51. <a-menu slot="overlay" @click="handleMenuClick">
  52. <a-menu-item key="add">
  53. {{ $t('scope.shopcart.add') }}
  54. </a-menu-item>
  55. </a-menu>
  56. <a-icon slot="icon" type="down" />
  57. </a-dropdown-button> -->
  58. <a-button
  59. :title="confirmText"
  60. class="text-truncate"
  61. type="primary"
  62. native-type="submit"
  63. html-type="submit"
  64. :loading="loading"
  65. :disabled="disabled || !!errors.length">{{ confirmText }}</a-button>
  66. <a-button class="ml-3" @click="handleCancel">{{$t('common.cancel')}}</a-button>
  67. </div>
  68. <side-errors :error-title="$t('compute.text_290')" :errors="errors" @update:errors="changeErrors" />
  69. </template>
  70. </page-footer>
  71. </div>
  72. </template>
  73. <script>
  74. import * as R from 'ramda'
  75. import _ from 'lodash'
  76. import { SERVER_TYPE, BILL_TYPES_MAP, EIP_TYPES_MAP } from '@Compute/constants'
  77. import { sizestrWithUnit } from '@/utils/utils'
  78. import { hasPermission, hasServices } from '@/utils/auth'
  79. import { PriceFetcher } from '@/utils/common/price'
  80. import SideErrors from '@/sections/SideErrors'
  81. import DiscountPrice from '@/sections/DiscountPrice'
  82. import { diskSupportTypeMedium, getOriginDiskKey } from '@/utils/common/hypervisor'
  83. export default {
  84. name: 'BottomBar',
  85. components: {
  86. SideErrors,
  87. DiscountPrice,
  88. },
  89. props: {
  90. loading: {
  91. type: Boolean,
  92. default: false,
  93. },
  94. form: {
  95. type: Object,
  96. required: true,
  97. },
  98. errors: {
  99. type: Object,
  100. required: true,
  101. },
  102. type: {
  103. type: String,
  104. required: true,
  105. },
  106. resourceType: { // 资源池类型
  107. type: String,
  108. },
  109. hasMeterService: {
  110. type: Boolean,
  111. default: true,
  112. },
  113. dataDiskSizes: {
  114. type: Array,
  115. default: () => [],
  116. },
  117. cloudaccountId: String,
  118. },
  119. data () {
  120. this.getPriceList = _.debounce(this._getPriceList2, 1500)
  121. return {
  122. origin_price: null,
  123. discount: 0,
  124. price: null,
  125. priceFormat: null,
  126. currency: '',
  127. priceTips: '--',
  128. disabled: false,
  129. }
  130. },
  131. computed: {
  132. fd () {
  133. return this.form.fd
  134. },
  135. fi () {
  136. return this.form.fi
  137. },
  138. isPublic () {
  139. return this.type === SERVER_TYPE.public
  140. },
  141. isIDC () {
  142. return this.type === SERVER_TYPE.idc
  143. },
  144. // 是否为包年包月
  145. isPackage () {
  146. return this.fd.billType === BILL_TYPES_MAP.package.key
  147. },
  148. name () {
  149. return this.fd.name
  150. },
  151. zone () {
  152. let ret = this.fd.zone ? this.fd.zone.label : ''
  153. if (this.isPublic) {
  154. ret = this.fd.sku ? this.fd.sku.zone : ''
  155. }
  156. return ret
  157. },
  158. vmType () {
  159. let ret = this.$t('compute.text_291', [this.$t('dictionary.server')])
  160. if (this.fd.gpuEnable) {
  161. ret = `GPU${this.$t('dictionary.server')}`
  162. }
  163. return ret
  164. },
  165. dataDisk () {
  166. const diskValueArr = []
  167. R.forEachObjIndexed(value => {
  168. diskValueArr.push(value)
  169. }, this.fd.dataDiskSizes)
  170. return diskValueArr.reduce((prevDisk, diskValue) => prevDisk + diskValue, 0)
  171. },
  172. disk () {
  173. const diskValueArr = [this.fd.systemDiskSize]
  174. R.forEachObjIndexed(value => {
  175. diskValueArr.push(value)
  176. }, this.fd.dataDiskSizes)
  177. return diskValueArr.reduce((prevDisk, diskValue) => prevDisk + diskValue, 0)
  178. },
  179. config () {
  180. const ret = []
  181. const { gpu, gpuCount, vcpu, vmem, sku = {} } = this.fd
  182. if (this.fd.gpuEnable) {
  183. ret.push(this.$t('compute.text_1134', [gpuCount, gpu]))
  184. }
  185. if (sku.cpu_core_count && sku.memory_size_mb) {
  186. ret.push(this.$t('compute.text_292', [sku.cpu_core_count]))
  187. ret.push(this.$t('compute.text_293', [sizestrWithUnit(sku.memory_size_mb, 'M', 1024)]))
  188. } else if (vcpu && vmem) {
  189. ret.push(this.$t('compute.text_292', [vcpu]))
  190. ret.push(this.$t('compute.text_293', [sizestrWithUnit(vmem, 'M', 1024)]))
  191. }
  192. let diskStr = ''
  193. if (this.fd.systemDiskSize) diskStr = `${this.$t('compute.text_49')}:${this.fd.systemDiskSize}GB ${_.get(this.fd, 'systemDiskType.label')}`
  194. if (this.dataDisk) diskStr += `,${this.$t('compute.text_50')}:${this.dataDisk}GB ${this.dataDiskLabel}`
  195. ret.push(diskStr)
  196. return ret.join('、')
  197. },
  198. image () {
  199. return _.get(this.fd, 'image.label') || ''
  200. },
  201. tips () {
  202. const ret = [
  203. [
  204. { label: this.$t('compute.text_228'), labelClass: 'label-w-50', value: this.name, valueClass: 'name-value' },
  205. { label: this.$t('compute.text_294'), labelClass: 'label-w-50', value: this.fd.count },
  206. ],
  207. [
  208. { label: this.$t('compute.text_177'), labelClass: 'label-w-50', value: this.zone },
  209. { label: this.$t('compute.text_175'), labelClass: 'label-w-50', value: this.vmType },
  210. ],
  211. [
  212. { label: this.$t('compute.text_295'), labelClass: 'label-w-80', value: this.config },
  213. ],
  214. ]
  215. return ret
  216. },
  217. durationNum () {
  218. if (this.isPackage) {
  219. const { duration } = this.fd
  220. let num = parseInt(duration)
  221. if (num && duration.endsWith('Y')) {
  222. num *= 12 // 1年=12月
  223. } else if (num && duration.endsWith('W')) {
  224. num *= 0.25 // 1周=0.25月
  225. }
  226. return num
  227. }
  228. return 0
  229. },
  230. confirmText () {
  231. return this.$t('compute.text_289')
  232. },
  233. dataDiskObj () {
  234. if (R.is(Object, this.fd.dataDiskTypes)) {
  235. const keys = Object.keys(this.fd.dataDiskTypes)
  236. if (keys && keys.length) {
  237. return this.fd.dataDiskTypes[keys[0]]
  238. }
  239. }
  240. if (R.is(Object, this.fd.dataDiskSizes)) {
  241. const keys = Object.keys(this.fd.dataDiskSizes)
  242. if (keys && keys.length) {
  243. const disk = this.fd[`dataDiskTypes[${keys[0]}]`]
  244. return disk
  245. }
  246. }
  247. return null
  248. },
  249. dataDiskType () {
  250. if (this.dataDiskObj && this.dataDiskObj.key) return this.dataDiskObj.key
  251. return ''
  252. },
  253. dataDiskLabel () {
  254. if (this.dataDiskObj && this.dataDiskObj.label) return this.dataDiskObj.label
  255. return ''
  256. },
  257. originPrice () {
  258. if (this.origin_price) {
  259. this.$emit('getOriginPrice', this.origin_price)
  260. }
  261. return this.origin_price
  262. },
  263. hasCartPermission () {
  264. return hasServices('billing') && hasPermission({ key: 'resource_order_sets_create' })
  265. },
  266. },
  267. watch: {
  268. priceTips: {
  269. handler (val) {
  270. let ret = `${this.currency} ${this.price && this.price.toFixed(2)}`
  271. ret += !this.isPackage ? this.$t('compute.text_296') : ''
  272. this.$bus.$emit('VMGetPrice', `${ret} ${val}`)
  273. },
  274. immediate: true,
  275. },
  276. dataDiskType (val, oldV) {
  277. if (val !== oldV) {
  278. this.getPriceList()
  279. }
  280. },
  281. 'fd.eip_type' (val, oldV) {
  282. this.getPriceList()
  283. },
  284. 'fd.eip_bw' (val, oldV) {
  285. this.getPriceList()
  286. },
  287. 'fd.backupEnable' (val, oldV) {
  288. this.getPriceList()
  289. },
  290. 'fd.eip_bgp_type' (val, oldV) {
  291. this.getPriceList()
  292. },
  293. 'fd.gpuEnable' (val, oldV) {
  294. this.getPriceList()
  295. },
  296. 'fd.backupEnbale' (val, oldV) {
  297. this.calcPrice()
  298. },
  299. },
  300. created () {
  301. this.baywatch([
  302. 'fd.sku.id',
  303. 'fd.gcounts',
  304. 'fd.duration',
  305. 'fd.billType',
  306. 'fd.systemDiskSize',
  307. 'fd.systemDiskType.key',
  308. 'fd.count',
  309. 'dataDiskSizes',
  310. 'fd.gpu',
  311. 'fd.gpuCount',
  312. ], (val, oldval) => {
  313. if (val) {
  314. this.getPriceList()
  315. }
  316. })
  317. this.$bus.$on('VMCreateDisabled', (val) => {
  318. this.disabled = val
  319. })
  320. },
  321. methods: {
  322. handleMenuClick (e) {
  323. if (e.key === 'add') {
  324. this.$emit('add-cart')
  325. }
  326. },
  327. changeErrors (errors) {
  328. this.$emit('update:errors', {})
  329. },
  330. baywatch (props, watcher) {
  331. const iterator = function (prop) {
  332. this.$watch(prop, watcher)
  333. }
  334. props.forEach(iterator, this)
  335. },
  336. async _getPriceList2 () {
  337. const f = this.fd
  338. if (!this.hasMeterService || !this.$appConfig.isPrivate) return // 如果没有 meter 服务则取消调用
  339. if (R.isEmpty(f.sku) || R.isNil(f.sku)) return
  340. if (this.fi.createType === SERVER_TYPE.public && (R.isNil(f.sku.region_ext_id) || R.isEmpty(f.sku.region_ext_id))) return
  341. if (!R.is(Number, f.count)) return
  342. if (R.isNil(f.systemDiskSize)) return
  343. const pf = new PriceFetcher()
  344. pf.initialForm(this.$store.getters.scope, f.sku, f.duration, f.billType, this.isPublic, this.cloudaccountId)
  345. // add price items
  346. if (this.fi.createType !== SERVER_TYPE.public) {
  347. // server instance
  348. pf.addCpu(f.vcpu)
  349. pf.addMem(f.vmem / 1024)
  350. // gpu
  351. if (f.gpuEnable && f.gpu && f.gpu.indexOf('=') >= 0) {
  352. const tmps = f.gpu.split('=')[1].split(':')
  353. if (tmps.length >= 2) {
  354. pf.addGpu(`${tmps[0]}.${tmps[1]}`, f.gpuCount || 0)
  355. }
  356. }
  357. } else {
  358. // server instance
  359. pf.addServer(f.sku.name, 1)
  360. // others
  361. }
  362. // disks
  363. const { systemDiskSize, systemDiskType, hypervisor } = f
  364. const { systemDiskMedium, dataDiskMedium } = this.form.fi
  365. let systemDisk = systemDiskType.key
  366. // 磁盘区分介质
  367. if (diskSupportTypeMedium(hypervisor)) {
  368. systemDisk = getOriginDiskKey(systemDisk)
  369. }
  370. if (this.fi.createType !== SERVER_TYPE.public) systemDisk = `${systemDiskMedium}::${systemDisk}`
  371. pf.addDisk(systemDisk, systemDiskSize)
  372. if (this.dataDiskType) {
  373. const datadisks = this.dataDiskSizes || (this.dataDisk ? [this.dataDisk] : [])
  374. let dataDisk = this.dataDiskType
  375. // 磁盘区分介质
  376. if (diskSupportTypeMedium(hypervisor)) {
  377. dataDisk = getOriginDiskKey(dataDisk)
  378. }
  379. if (this.fi.createType !== SERVER_TYPE.public) dataDisk = `${dataDiskMedium}::${dataDisk}`
  380. pf.addDisks(dataDisk, datadisks)
  381. }
  382. // eip
  383. if (f.eip_bw && f.eip_type === EIP_TYPES_MAP.new.key) {
  384. pf.addEipBandwidth(f.eip_bgp_type || '', f.eip_bw)
  385. }
  386. const price = await pf.getPriceObj()
  387. this.priceObj = price
  388. this.calcPrice()
  389. },
  390. calcPrice () {
  391. const price = this.priceObj
  392. if (!price) return
  393. price.setOptions({ count: this.fd.count || 0, backupEnbale: this.fd.backupEnable })
  394. this.currency = price.currency
  395. this.price = price.price
  396. this.discount = price.discount
  397. this.priceFormat = price.priceFormat
  398. this.origin_price = price.originPrice
  399. this.priceTips = price.priceTips
  400. },
  401. handleCancel () {
  402. this.$emit('cancel')
  403. },
  404. },
  405. }
  406. </script>
  407. <style lang="less" scoped>
  408. @import '../../../../../../src/styles/less/theme';
  409. .create-server-result-wrap {
  410. position: relative;
  411. font-size: 12px;
  412. .content {
  413. width: 80%;
  414. .label {
  415. &.label-w-50 {
  416. width: 50px;
  417. }
  418. &.label-w-80 {
  419. width: 80px;
  420. }
  421. }
  422. .value {
  423. &.name-value {
  424. width: 100px;
  425. }
  426. &.placeholder {
  427. color: #888;
  428. font-style: italic;
  429. }
  430. }
  431. @media screen and (max-width: 1366px) {
  432. .value {
  433. max-width: 154px;
  434. }
  435. }
  436. }
  437. .prices {
  438. .hour {
  439. color: @error-color;
  440. font-size: 24px;
  441. }
  442. .tips {
  443. color: #999;
  444. font-size: 12px;
  445. }
  446. }
  447. .btns-wrapper {
  448. position: absolute;
  449. right: 20px;
  450. }
  451. }
  452. </style>