productChooseAdd.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. <template>
  2. <view class="product-choose-add">
  3. <view class="content-title">
  4. 产品信息
  5. </view>
  6. <view class="content-body">
  7. <template v-if="productList.length > 0">
  8. <view class="list">
  9. <view
  10. v-for="(item, index) in productList"
  11. :key="index"
  12. class="list-item">
  13. <view class="product-info">
  14. <text class="name">
  15. {{ item.name }}
  16. </text>
  17. <text class="special">
  18. {{ item.subtotal }}元
  19. </text>
  20. </view>
  21. <view class="product-desc">
  22. <view class="box">
  23. <text class="icon">
  24. ¥
  25. </text>
  26. <input
  27. v-model="item.salesPrice"
  28. type="number"
  29. class="input-core"
  30. maxlength="8"
  31. @input="handleChange('salesPrice', index, $event)">
  32. </view>
  33. <view class="num-box">
  34. <view class="sub-icon active" @click="handleChangeNum(index, -1)">
  35. -
  36. </view>
  37. <input v-model="item.num" type="number" class="input-core" maxlength="8" @input="handleChange('num', index, $event)">
  38. <view class="plus-icon active" @click="handleChangeNum(index, 1)">
  39. +
  40. </view>
  41. </view>
  42. <view class="box rate">
  43. <input
  44. v-model="item.discount"
  45. type="number"
  46. class="input-core"
  47. @input="handleChange('discount', index, $event)">
  48. <text class="rate-icon">
  49. %
  50. </text>
  51. </view>
  52. <view class="del-product" @click="delProduct(index)">
  53. <image
  54. :src="$static('images/icon/icon_delete.png')"
  55. class="del" />
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="total-box">
  61. <!-- #ifdef MP-WEIXIN -->
  62. <view class="total-item">
  63. 产品 :<text class="num">
  64. {{ totalCount }}
  65. </text>
  66. </view>
  67. <view class="total-item">
  68. 总金额 :<text class="num">
  69. {{ totalMoney }}
  70. </text>
  71. </view>
  72. <!-- #endif -->
  73. <view class="total">
  74. <text>整单折扣</text>
  75. <input
  76. v-model="discountRate"
  77. type="number"
  78. class="input-core"
  79. @input="handleChange('discountRate', -1, $event)">
  80. <text class="rate-icon">
  81. %
  82. </text>
  83. </view>
  84. </view>
  85. </template>
  86. <template v-else>
  87. <view :style="{backgroundImage: bgUrl('images/no_data.png')}" class="no-data">
  88. 请选择产品
  89. </view>
  90. </template>
  91. </view>
  92. <!-- #ifndef MP-WEIXIN -->
  93. <view class="content-footer">
  94. <view class="total">
  95. 产品 :<text class="num">
  96. {{ totalCount }}
  97. </text>
  98. </view>
  99. <view class="total">
  100. 总金额 :<text class="num">
  101. {{ totalMoney }}
  102. </text>
  103. </view>
  104. <view class="control">
  105. <button class="choose-button" @click="handleChoose">
  106. 添加产品
  107. </button>
  108. </view>
  109. </view>
  110. <uni-popup ref="popup" type="dialog">
  111. <uni-popup-dialog
  112. :content="dialogMsg"
  113. type="warning"
  114. @confirm="handleDialogConfirm" />
  115. </uni-popup>
  116. <!-- #endif -->
  117. </view>
  118. </template>
  119. <script>
  120. import {
  121. multiOperation,
  122. addOperation,
  123. subOperation,
  124. divideOperation
  125. } from '@/utils/lib.js'
  126. import { deepCopy } from '@/utils/lib.js'
  127. export default {
  128. name: 'ProductChooseAdd',
  129. props: {
  130. defaultValue: {
  131. type: Object,
  132. default: () => {}
  133. }
  134. },
  135. data() {
  136. return {
  137. guid: null,
  138. productList: [],
  139. totalCount: 0,
  140. totalMoney: 0,
  141. discountRate: 0,
  142. dialogMsg: '',
  143. productIndex: null
  144. }
  145. },
  146. watch: {
  147. defaultValue: {
  148. handler() {
  149. console.log('change')
  150. const obj = this.defaultValue || {}
  151. this.productList = deepCopy(obj.product || [])
  152. this.discountRate = obj.discountRate || 0
  153. this.totalMoney = obj.totalPrice || 0
  154. this.calcMoney(true)
  155. },
  156. deep: true,
  157. immediate: true
  158. }
  159. },
  160. created() {
  161. this.guid = this.$guid()
  162. },
  163. methods: {
  164. bgUrl(val) {
  165. return `url(${this.$static(val)})`
  166. },
  167. handleChoose() {
  168. const bridge = getApp().globalData.selectedValBridge
  169. bridge.product = {
  170. guid: this.guid,
  171. title: '选择产品',
  172. defaultVal: []
  173. }
  174. uni.$on('selected-product', this.selectedProduct)
  175. this.$Router.navigateTo({
  176. url: '/pages_common/selectList/product',
  177. query: {}
  178. })
  179. },
  180. selectedProduct(data) {
  181. if (this.guid === data.guid) {
  182. console.log('product list: ', data)
  183. // this.$emit('input', this.optionsList)
  184. // this.$emit('change', {
  185. // index: this.index,
  186. // field: this.field,
  187. // value: this.optionsList
  188. // })
  189. this.renderList(data.data)
  190. }
  191. uni.$off('selected-product')
  192. },
  193. renderList(list, flag = false) {
  194. const arr = []
  195. let keyArr = ['categoryId', 'categoryName', 'price', 'productId', 'unit']
  196. list.forEach(item => {
  197. const salesPrice = flag ? item.salesPrice : item.price
  198. const product = {
  199. name: item.name || item.productName || '',
  200. num: flag ? parseInt(item.num) : 1,
  201. salesPrice: salesPrice,
  202. subtotal: flag ? (Number(salesPrice) * item.num) : item.price,
  203. discount: item.discount || 0
  204. }
  205. keyArr.forEach(key => {
  206. product[key] = item[key]
  207. })
  208. let index = this.productList.findIndex(o => {
  209. return o.productId === product.productId
  210. })
  211. if (index !== -1) {
  212. this.handleChangeNum(index, 1)
  213. } else {
  214. arr.push(product)
  215. }
  216. })
  217. this.productList = this.productList.concat(arr)
  218. this.calcMoney()
  219. },
  220. /**
  221. * 改变数量
  222. * @param index
  223. * @param step
  224. */
  225. handleChangeNum(index, step) {
  226. const item = this.productList[index]
  227. item.num = Number(item.num)
  228. item.num += step
  229. if (item.num <= 0) {
  230. this.productList.splice(index, 1)
  231. }
  232. this.calcMoney()
  233. },
  234. handleChange(field, index, event) {
  235. // console.log('change: ', field, event)
  236. // console.log('change:- ', this.productList[index])
  237. if (index === -1) {
  238. this.calcMoney()
  239. return
  240. }
  241. const item = this.productList[index]
  242. if (field === 'salesPrice') {
  243. // item.discount = Number(100 - (item.salesPrice / item.price * 100).toFixed(2))
  244. // 计算折扣比率
  245. let r = divideOperation(item.salesPrice, item.price)
  246. r = multiOperation(r, 100)
  247. r = subOperation(100, r).toFixed(2)
  248. item.discount = Number(r)
  249. console.log('dis', item.discount)
  250. } else if (field === 'discount') {
  251. // 计算售价
  252. let r = subOperation(100, item.discount)
  253. r = multiOperation(item.price, r)
  254. r = (r / 100).toFixed(2)
  255. item.salesPrice = r
  256. }
  257. this.calcMoney()
  258. },
  259. /**
  260. * 保留小数点后两位
  261. * @param {String} val
  262. */
  263. formatFloatNumber(val) {
  264. let valueStr = String(val)
  265. if (isNaN(Number(val))) {
  266. valueStr = valueStr.slice(0, -1)
  267. this.formatFloatNumber(valueStr)
  268. }
  269. valueStr = valueStr.replace(/[^0-9.]/g, '')
  270. let arr = valueStr.split('.')
  271. if (arr[1] && arr[1].length > 2) {
  272. val = valueStr.slice(0, -1)
  273. this.formatFloatNumber(val)
  274. }
  275. return val
  276. },
  277. /**
  278. * 计算总价
  279. */
  280. calcMoney(isDefault = false) {
  281. this.totalCount = 0
  282. this.totalMoney = 0
  283. this.productList.forEach(item => {
  284. item.num = Number(item.num)
  285. item.subtotal = multiOperation(item.salesPrice, item.num)
  286. this.totalCount += item.num
  287. this.totalMoney = addOperation(this.totalMoney, item.subtotal)
  288. })
  289. this.totalMoney = multiOperation(this.totalMoney, subOperation(100, this.discountRate)) / 100
  290. this.totalMoney = this.totalMoney.toFixed(2)
  291. if (!isDefault) {
  292. this.$emit('change', {
  293. list: this.productList,
  294. totalMoney: this.totalMoney,
  295. totalCount: this.totalCount
  296. })
  297. }
  298. },
  299. getForm() {
  300. return {
  301. product: this.productList || [],
  302. discountRate: this.discountRate || 0,
  303. totalPrice: this.totalMoney
  304. }
  305. },
  306. delProduct(index) {
  307. this.productIndex = index
  308. this.dialogMsg = '确认删除该产品吗?'
  309. this.$refs.popup.open()
  310. },
  311. handleDialogConfirm(next) {
  312. const item = this.productList[this.productIndex]
  313. item.num = Number(item.num)
  314. this.productList.splice(this.productIndex, 1)
  315. next()
  316. },
  317. }
  318. }
  319. </script>
  320. <style scoped lang="scss">
  321. .input-core {
  322. width: 100%;
  323. height: 46rpx;
  324. border: 1rpx solid $border-color;
  325. line-height: 1;
  326. font-size: 26rpx;
  327. text-align: left;
  328. display: block;
  329. }
  330. .product-choose-add {
  331. padding-bottom: 120rpx;
  332. .content-title {
  333. width: 100%;
  334. font-size: 24rpx;
  335. color: $gray;
  336. padding: 20rpx 30rpx;
  337. // margin-left: -30px;
  338. background-color: $container-bg;
  339. }
  340. .content-body {
  341. width: 100%;
  342. background-color: white;
  343. overflow-y: scroll;
  344. .list {
  345. padding: 0 30rpx;
  346. .list-item {
  347. padding: 32rpx 0;
  348. border-bottom: 1rpx solid $border-color;
  349. .product-info {
  350. display: flex;
  351. align-items: center;
  352. justify-content: space-between;
  353. .name {
  354. flex: 1;
  355. font-size: 32rpx;
  356. color: $dark;
  357. }
  358. .special {
  359. font-size: 32rpx;
  360. color: $warning;
  361. }
  362. }
  363. .product-desc {
  364. display: flex;
  365. align-items: center;
  366. justify-content: space-between;
  367. .box {
  368. position: relative;
  369. height: 46rpx;
  370. margin-top: 20rpx;
  371. @include left;
  372. .input-core {
  373. width: 180rpx;
  374. padding-left: 46rpx;
  375. }
  376. .icon {
  377. position: absolute;
  378. top: 0;
  379. left: 0;
  380. width: 46rpx;
  381. height: 46rpx;
  382. font-size: 24rpx;
  383. color: $theme-color;
  384. @include center;
  385. }
  386. .rate-icon {
  387. font-size: 24rpx;
  388. margin-left: 5rpx;
  389. }
  390. &.rate .input-core {
  391. padding: 0;
  392. text-align: center;
  393. }
  394. }
  395. .del-product {
  396. width: 46rpx;
  397. height: 46rpx;
  398. margin-top: 20rpx;
  399. .del {
  400. width: 26rpx;
  401. height: 26rpx;
  402. }
  403. }
  404. .num-box {
  405. width: 200rpx;
  406. margin-top: 20rpx;
  407. @include left;
  408. .sub-icon, .plus-icon {
  409. width: 46rpx;
  410. height: 46rpx;
  411. font-size: 24rpx;
  412. color: $theme-color;
  413. border: 1rpx solid $border-color;
  414. @include center;
  415. }
  416. .sub-icon {
  417. border-right: 0 none;
  418. }
  419. .plus-icon {
  420. border-left: 0 none;
  421. }
  422. .input-core {
  423. flex: 1;
  424. text-align: center;
  425. }
  426. }
  427. }
  428. }
  429. }
  430. .total {
  431. font-size: 26rpx;
  432. color: $gray;
  433. @include right;
  434. .input-core {
  435. width: 180rpx;
  436. margin-left: 30rpx;
  437. text-align: center;
  438. }
  439. .rate-icon {
  440. font-size: 24rpx;
  441. margin-left: 5rpx;
  442. }
  443. }
  444. }
  445. .total-box {
  446. width: 100%;
  447. font-size: $wk-font-sm;
  448. background-color: white;
  449. padding: 30rpx;
  450. flex-wrap: wrap;
  451. @include left;
  452. .total-item:first-child {
  453. margin-right: 30rpx;
  454. }
  455. .total {
  456. flex: 1;
  457. padding: 0 0 0 15rpx;
  458. .input-core {
  459. width: 100rpx;
  460. }
  461. }
  462. .num {
  463. color: $warning;
  464. margin-left: 20rpx;
  465. }
  466. }
  467. .content-footer {
  468. position: fixed;
  469. left: 0;
  470. bottom: 0;
  471. z-index: 10;
  472. width: 100%;
  473. height: 120rpx;
  474. background-color: white;
  475. box-shadow: 0 0 10rpx #E5E5E5;
  476. padding: 0 30rpx;
  477. @include left;
  478. .total {
  479. font-size: 28rpx;
  480. color: $dark;
  481. margin-right: 36rpx;
  482. .num {
  483. color: $warning;
  484. margin-left: 20rpx;
  485. }
  486. }
  487. .control {
  488. flex: 1;
  489. @include right;
  490. .choose-button {
  491. width: 200rpx;
  492. height: 80rpx;
  493. font-size: 30rpx;
  494. color: white;
  495. background-color: $theme-color;
  496. @include center;
  497. }
  498. }
  499. }
  500. }
  501. .no-data {
  502. width: 100%;
  503. text-align: center;
  504. font-size: 26rpx;
  505. color: #BBBBBB;
  506. background-position: center 50rpx;
  507. background-repeat: no-repeat;
  508. background-size: 25%;
  509. padding-top: 270rpx;
  510. padding-bottom: 30rpx;
  511. }
  512. </style>