index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <template>
  2. <view>
  3. <view class='coupon-list-window animated' :class='coupon.status==true?"slideInUp":""'>
  4. <view class='title'>
  5. <view class="item">优惠券</view>
  6. </view>
  7. <block v-if="couponArr.length">
  8. <view class='coupon-list'>
  9. <view class='item acea-row row-center-wrapper' v-for="(item,index) in couponArr" @click="getCouponUser(index,item)"
  10. :key='index'>
  11. <view class='money acea-row row-column row-center-wrapper'>
  12. <view>¥<text class='num'>{{item.coupon_price}}</text></view>
  13. <view class="pic-num">满{{item.use_min_price}}元可用</view>
  14. </view>
  15. <view class='text'>
  16. <view class='condition line1'>
  17. <span class='line-title' v-if='item.coupon.type===0'>店铺券</span>
  18. <span class='line-title' v-else-if='item.coupon.type===1'>商品券</span>
  19. <span>{{item.coupon_title}}</span>
  20. </view>
  21. <view class='data acea-row row-between-wrapper'>
  22. <view>{{ item.start_time |timeYMD }} ~ {{ item.end_time |timeYMD}}</view>
  23. <view class="iconfont icon-weixuanzhong" v-if="!item.checked"></view>
  24. <view class='iconfont icon-xuanzhong1' v-else></view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="foot-box">
  30. <view class="left">
  31. 已选择{{allNum}}张,可优惠<text>¥{{allCouponNum}}</text>
  32. </view>
  33. <view class="btn" @click="confirm">确定</view>
  34. </view>
  35. </block>
  36. <!-- 无优惠券 -->
  37. <view class='pictrue' v-else>
  38. <image src='../../static/images/noCoupon.png'></image>
  39. </view>
  40. </view>
  41. <view class='mask' catchtouchmove="true" :hidden='coupon.status==false' @click='close'></view>
  42. </view>
  43. </template>
  44. <script>
  45. import {
  46. setCouponReceive
  47. } from '@/api/api.js';
  48. export default {
  49. props: {
  50. //打开状态 0=领取优惠券,1=使用优惠券
  51. openType: {
  52. type: Number,
  53. default: 0,
  54. },
  55. coupon: {
  56. type: Object,
  57. default: function() {
  58. return {};
  59. }
  60. },
  61. subCoupon: {
  62. type: Object
  63. }
  64. },
  65. filters: {
  66. timeYMD: function(value) {
  67. if(value){
  68. var newDate=/\d{4}-(\d{1,2}\d{1,2}-\d{1,2}\d{1,2})/g.exec(value)
  69. return newDate?.[1]||''
  70. }
  71. }
  72. },
  73. data() {
  74. return {
  75. couponArr: [],
  76. couponData: {},
  77. // 选中的数据存放
  78. active: {},
  79. allNum: 0,
  80. allCouponNum: 0,
  81. // 选中店铺优惠券id
  82. use_store_coupon: 0,
  83. // 单个店铺总价
  84. pay_price: 0,
  85. // 商品有优惠订单
  86. goodsOrder: ''
  87. };
  88. },
  89. mounted() {
  90. this.couponData = this.coupon
  91. // 深拷贝数据 不影响原来数据使用
  92. this.couponArr = JSON.parse(JSON.stringify(this.coupon.coupon))
  93. // 深拷贝数据 不影响原来数据使用
  94. this.goodsOrder = JSON.parse(JSON.stringify(this.coupon.order))
  95. let tempObj = this.active[this.couponData.mer_id] = {}
  96. tempObj.product = []
  97. tempObj.store = ''
  98. this.allActive()
  99. },
  100. methods: {
  101. close: function() {
  102. this.$emit('ChangCouponsClone');
  103. },
  104. // 使用优惠券
  105. getCouponUser: function(index, item) {
  106. let self = this
  107. // 先判断是哪个券 1商品 0店铺
  108. if (item.coupon.type == 1) {
  109. let order = this.goodsOrder
  110. let orderToalPrice = 0
  111. if (item.checked) {
  112. /**
  113. * 取消选中 并且删除 use_coupon_product里的值
  114. * use_coupon_product 哪些商品可以用的券的id
  115. * */
  116. for (let key in order.use_coupon_product) {
  117. if (order.use_coupon_product[key] == item.coupon_user_id) {
  118. delete order.use_coupon_product[key]
  119. }
  120. }
  121. item.checked = false
  122. } else {
  123. /**
  124. * 选中
  125. * @item.product 该优惠券可以使用的商品
  126. * order.product_price 产品的价格 key是id
  127. * */
  128. for (let i = 0; i < item.product.length; i++) {
  129. if (order.product_price[item.product[i].product_id]) {
  130. orderToalPrice = order.product_price[item.product[i].product_id]
  131. //价格的值大于等于最小值就可以使用
  132. if (orderToalPrice >= parseFloat(item.use_min_price)) {
  133. // 可以用
  134. if (!order.use_coupon_product[item.product[i].product_id]) {
  135. item.checked = true
  136. order.use_coupon_product[item.product[i].product_id] = item.coupon_user_id
  137. } else {
  138. // 上个商品用了就取消选中,点击的这个添加选中
  139. this.couponArr.forEach(el => {
  140. if (el.coupon_user_id == order.use_coupon_product[item.product[i].product_id]) {
  141. el.checked = false
  142. }
  143. })
  144. item.checked = true
  145. order.use_coupon_product[item.product[i].product_id] = item.coupon_user_id
  146. }
  147. break
  148. }
  149. }
  150. }
  151. }
  152. } else {
  153. let order = this.couponData.order
  154. // 店铺券
  155. if (item.checked) {
  156. item.checked = false
  157. // this.pay_price = order.total_price
  158. } else {
  159. this.couponArr.forEach(el => {
  160. if (el.coupon.type == 0 && el.checked) {
  161. el.checked = false
  162. }
  163. })
  164. item.checked = true
  165. }
  166. this.pay_price = this.$util.$h.Sub(order.total_price, item.coupon_price)
  167. }
  168. this.allActive()
  169. },
  170. // 选中计算
  171. allActive() {
  172. let tempObj = this.active[this.couponData.mer_id]
  173. let sotreTotal = 0 //商铺券优惠
  174. let goodsTotal = 0 //商品券优惠
  175. tempObj.product = []
  176. tempObj.store = ''
  177. this.couponArr.forEach(el => {
  178. /**
  179. * @el.coupon.type 0店铺 1商品
  180. */
  181. if (el.coupon.type == 0 && el.checked) {
  182. tempObj.store = el.coupon_user_id
  183. sotreTotal = el.coupon_price
  184. this.use_store_coupon = el.coupon_user_id
  185. }
  186. if (el.coupon.type == 1 && el.checked) {
  187. tempObj.product.push(el.coupon_user_id)
  188. goodsTotal = this.$util.$h.Add(goodsTotal, el.coupon_price)
  189. }
  190. })
  191. if (tempObj.store) {
  192. this.allNum = this.$util.$h.Add(tempObj.product.length, 1)
  193. } else {
  194. this.allNum = tempObj.product.length
  195. }
  196. let tempAllCouponNum = this.$util.$h.Add(sotreTotal, goodsTotal)
  197. if (parseFloat(tempAllCouponNum) >= parseFloat(this.couponData.order.total_price)) {
  198. this.allCouponNum = this.couponData.order.total_price
  199. } else {
  200. if(this.allNum !== 0 ){
  201. if(sotreTotal || goodsTotal){
  202. this.allCouponNum = tempAllCouponNum
  203. }else{
  204. this.allCouponNum = this.couponData.order.total_price
  205. }
  206. }else{
  207. this.allCouponNum = tempAllCouponNum
  208. }
  209. }
  210. },
  211. // 确认
  212. confirm() {
  213. // 商品类
  214. this.couponData.order = this.goodsOrder
  215. // 店铺类
  216. // 支付价格
  217. let tempTotal = 0
  218. tempTotal = this.$util.$h.Sub(this.couponData.order.total_price, this.allCouponNum)
  219. if (tempTotal > 0) {
  220. this.couponData.order.pay_price = this.$util.$h.Add(tempTotal, this.couponData.order.postage_price)
  221. } else {
  222. // 如果没有用优惠券
  223. if(this.allNum == 0){
  224. this.couponData.order.pay_price = this.$util.$h.Add(this.couponData.order.total_price, this.couponData.order.postage_price)
  225. }else{
  226. this.couponData.order.pay_price = this.couponData.order.postage_price
  227. }
  228. }
  229. // 列表的优惠总金额
  230. this.couponData.order.coupon_price = this.allCouponNum
  231. this.couponData.order.use_store_coupon = this.use_store_coupon
  232. this.couponData.coupon = this.couponArr
  233. this.active[this.coupon.mer_id].product = this.goodsOrder.use_coupon_product
  234. this.subCoupon[this.coupon.mer_id] = this.active[this.coupon.mer_id]
  235. this.$emit('ChangCoupons',this.couponData);
  236. }
  237. }
  238. }
  239. </script>
  240. <style scoped lang="scss">
  241. .animated {
  242. animation-duration: .3s
  243. }
  244. .title {
  245. display: flex;
  246. .item {
  247. position: relative;
  248. flex: 1;
  249. font-size: 28rpx;
  250. color: #999999;
  251. &::after {
  252. content: ' ';
  253. position: absolute;
  254. left: 50%;
  255. bottom: 18rpx;
  256. width: 50rpx;
  257. height: 5rpx;
  258. background: transparent;
  259. border-radius: 3px;
  260. transform: translateX(-50%);
  261. }
  262. &.on {
  263. color: #282828;
  264. &::after {
  265. background: $theme-color;
  266. }
  267. }
  268. }
  269. }
  270. .coupon-list {
  271. padding: 30rpx;
  272. .item {
  273. box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.06);
  274. }
  275. }
  276. .coupon-list-window {
  277. position: fixed;
  278. bottom: 0;
  279. left: 0;
  280. width: 100%;
  281. background-color: #fff;
  282. border-radius: 16rpx 16rpx 0 0;
  283. z-index: 555;
  284. transform: translate3d(0, 100%, 0);
  285. transition: all .3s cubic-bezier(.25, .5, .5, .9);
  286. }
  287. .coupon-list-window.on {
  288. // transform: translate3d(0, 0, 0);
  289. animation: aminup;
  290. }
  291. .coupon-list-window .title {
  292. height: 106rpx;
  293. width: 100%;
  294. text-align: center;
  295. line-height: 106rpx;
  296. font-size: 32rpx;
  297. font-weight: bold;
  298. position: relative;
  299. border: 1px solid #f5f5f5;
  300. }
  301. .coupon-list-window .title .iconfont {
  302. position: absolute;
  303. right: 30rpx;
  304. top: 50%;
  305. transform: translateY(-50%);
  306. font-size: 35rpx;
  307. color: #8a8a8a;
  308. font-weight: normal;
  309. }
  310. .coupon-list-window .coupon-list {
  311. margin: 0 0 0rpx 0;
  312. height: 550rpx;
  313. overflow: auto;
  314. }
  315. .coupon-list-window .pictrue {
  316. width: 414rpx;
  317. height: 336rpx;
  318. margin: 0 auto 50rpx auto;
  319. }
  320. .coupon-list-window .pictrue image {
  321. width: 100%;
  322. height: 100%;
  323. }
  324. .pic-num {
  325. color: #fff;
  326. font-size: 24rpx;
  327. }
  328. .line-title {
  329. width: 90rpx;
  330. padding: 0 10rpx;
  331. box-sizing: border-box;
  332. background: rgba(255, 247, 247, 1);
  333. border: 1px solid rgba(232, 51, 35, 1);
  334. opacity: 1;
  335. border-radius: 20rpx;
  336. font-size: 20rpx;
  337. color: #E83323;
  338. margin-right: 12rpx;
  339. }
  340. .line-title.gray {
  341. border-color: #BBB;
  342. color: #bbb;
  343. background-color: #F5F5F5;
  344. }
  345. .foot-box {
  346. display: flex;
  347. align-items: center;
  348. justify-content: space-between;
  349. height: 100rpx;
  350. padding: 0 30rpx;
  351. border-top: 1px solid #F5F5F5;
  352. .btn {
  353. width: 240rpx;
  354. height: 70rpx;
  355. line-height: 70rpx;
  356. text-align: center;
  357. background: $theme-color;
  358. border-radius: 35rpx;
  359. color: #fff;
  360. font-size: 30rpx;
  361. }
  362. .left {
  363. text {
  364. color: $theme-color;
  365. }
  366. }
  367. }
  368. .coupon-list .item .text .data .iconfont {
  369. font-size: 36rpx;
  370. &.icon-weixuanzhong {
  371. color: #BFBFBF;
  372. }
  373. &.icon-xuanzhong1 {
  374. color: $theme-color;
  375. }
  376. }
  377. </style>