BottomBar.vue 14 KB

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