shopro-goods-card.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <!-- m -->
  3. <view class="goods-box" @tap="click">
  4. <view class="img-box">
  5. <image class="goods-img" lazy-load fade-show :src="image" mode="aspectFill"></image>
  6. </view>
  7. <view class="goods-bottom u-p-14"
  8. :style="type ? 'background-image: url(' + $IMG_URL + typeMap[type].goodsBg + ')' : ''">
  9. <view class="title u-ellipsis-2 u-m-b-10">
  10. <view class=" sm cu-tag radius title-tag u-m-r-10"
  11. :style="{ backgroundColor: typeMap[type].tagBg, color: '#fff' }" v-if="type">
  12. {{ typeMap[type].text }}
  13. </view>
  14. {{ title }}
  15. </view>
  16. <view class="sub-title u-ellipsis-1 u-m-b-10" v-show="subtitle">{{ subtitle }}</view>
  17. <view class="u-m-b-20">
  18. <view class="cu-tag line-red sm radius" v-for="(item, index) in tagTextList" :key="index">{{ item }}
  19. </view>
  20. </view>
  21. <slot name="cardBottom">
  22. <view class="u-flex u-col-center u-row-between">
  23. <view class="price-box">
  24. <view class="price u-m-b-10">{{ price }}</view>
  25. <view class="origin-price">¥{{ originPrice }}</view>
  26. </view>
  27. <!-- 加入购物车 -->
  28. <view class="cart-box">
  29. <!-- 单规格 -->
  30. <view class="" v-if="!detail.is_sku">
  31. <button class="u-reset-button cart-btn u-flex u-col-center u-row-center"
  32. v-if="!isCart(detail.id)" @tap.stop="addCart(detail.sku_price[0])">
  33. <view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>
  34. </button>
  35. <view class="num-step" @tap.stop v-else>
  36. <u-number-box :value="checkCart[detail.id].num" :min="0" :step="1" :long-press="false"
  37. :max="detail.sku_price[0].stock > 999 ? 999 : detail.sku_price[0].stock"
  38. @min="onMin" @plus="plus($event, detail.sku_price[0])"
  39. @change="onChangeNum($event, detail.sku_price[0])"></u-number-box>
  40. </view>
  41. </view>
  42. <!-- 多规格 -->
  43. <button class="u-reset-button item-btn cart-btn u-flex u-col-center u-row-center"
  44. @tap.stop="selSku(detail)" v-else>
  45. <view class="u-iconfont uicon-shopping-cart-fill" style="color: #fff;"></view>
  46. </button>
  47. </view>
  48. </view>
  49. </slot>
  50. </view>
  51. <!-- 规格弹窗 -->
  52. <shopro-sku v-if="showSku && goodsInfo.id" v-model="showSku" :goodsInfo="goodsInfo" buyType="cart"></shopro-sku>
  53. </view>
  54. </template>
  55. <script>
  56. /**
  57. *shoproGoodsCard - 商品列表卡片
  58. * @property {Object} detail - 商品详情
  59. * @property {String} type - 商品类型
  60. * @property {String} image - 商品图片
  61. * @property {String} title - 商品标题
  62. * @property {String} subtitle - 商品副标题
  63. * @property {String | Number} price - 商品价格
  64. * @property {String | Number} originPrice - 商品原价
  65. * @property {String | Number} sales - 商品销量
  66. * @property {Array} tagTextList - 活动标签
  67. * @event {Function} click 商品被点击
  68. */
  69. import { mapMutations, mapActions, mapState, mapGetters } from 'vuex';
  70. export default {
  71. components: {},
  72. data() {
  73. return {
  74. showSku: false,
  75. goodsInfo: {}, //商品详情
  76. typeMap: {
  77. seckill: {
  78. text: '秒杀',
  79. tagBg: '#FF5854',
  80. goodsBg: '/imgs/tag/seckill_y_bg.png'
  81. },
  82. groupon: {
  83. text: '拼团',
  84. tagBg: '#FE832A',
  85. goodsBg: '/imgs/tag/groupon_y_bg.png'
  86. }
  87. }
  88. };
  89. },
  90. computed: {
  91. ...mapGetters(['cartList', 'checkCart'])
  92. },
  93. props: {
  94. detail: {
  95. type: Object,
  96. default: () => {
  97. return {};
  98. }
  99. },
  100. type: {
  101. type: [String, null],
  102. default: ''
  103. },
  104. image: {
  105. type: String,
  106. default: ''
  107. },
  108. title: {
  109. type: String,
  110. default: ''
  111. },
  112. subtitle: {
  113. type: String,
  114. default: ''
  115. },
  116. price: {
  117. type: [String, Number],
  118. default: ''
  119. },
  120. originPrice: {
  121. type: [String, Number],
  122. default: ''
  123. },
  124. sales: {
  125. type: [String, Number],
  126. default: ''
  127. },
  128. tagTextList: {
  129. type: Array,
  130. default: () => []
  131. }
  132. },
  133. methods: {
  134. ...mapActions(['getCartList', 'changeCartList', 'addCartGoods']),
  135. //点击商品
  136. click() {
  137. this.$emit('click');
  138. },
  139. // 检测是否为购物车商品
  140. isCart(id) {
  141. return Object.keys(this.checkCart).includes(id + '');
  142. },
  143. // 检测商品在购物车中的下标
  144. checkGoodsIndex(id) {
  145. let cIndex = 0;
  146. this.cartList.forEach((item, index) => {
  147. if (id == item.goods_id) {
  148. cIndex = index;
  149. }
  150. });
  151. return cIndex;
  152. },
  153. // 更改商品数
  154. async onChangeNum(e, sku) {
  155. let gIndex = this.checkGoodsIndex(sku.goods_id);
  156. if (e.value != this.checkCart[sku.goods_id].num) {
  157. uni.showLoading({
  158. mask: true
  159. });
  160. this.$set(this.cartList[gIndex], 'goods_num', +e.value);
  161. await this.changeCartList({
  162. ids: [this.checkCart[sku.goods_id].cartOrderId],
  163. goodsNum: +e.value,
  164. art: 'change'
  165. });
  166. await uni.hideLoading();
  167. }
  168. },
  169. // 到达最小值
  170. onMin() {
  171. const that = this;
  172. let cartGoodId = 0;
  173. cartGoodId = this.cartList.filter(item => item.goods_id === that.detail.id)[0].id;
  174. uni.showModal({
  175. title: '删除提示',
  176. confirmColor: '#f0c785',
  177. content: `是否确认从购物车中删除此商品?`,
  178. success: res => {
  179. res.confirm && this.changeCartList({ ids: [cartGoodId], art: 'delete' });
  180. }
  181. });
  182. },
  183. // 增加
  184. plus(e, sku) {
  185. if (e.value >= sku.stock) {
  186. this.$u.toast('库存不足');
  187. return;
  188. }
  189. if (this.detail.activity_type === 'seckill' || this.detail.activity_type === 'groupon') {
  190. let rules = this.detail.activity.rules;
  191. if (rules.limit_buy != 0 && e.value >= rules.limit_buy) {
  192. this.$u.toast('本次活动最多购买' + rules.limit_buy + '件');
  193. return;
  194. }
  195. }
  196. },
  197. // 添加购物车,多规格
  198. async selSku(info) {
  199. if (this.detail.activity_type) {
  200. this.$Router.push({ path: '/pages/goods/detail', query: { id: this.detail.id } });
  201. return;
  202. }
  203. this.goodsInfo = {};
  204. this.getGoodsDetail(info.id);
  205. this.showSku = true;
  206. },
  207. // 商品详情
  208. getGoodsDetail(id) {
  209. let that = this;
  210. that.$http('goods.detail', {
  211. id: id
  212. }).then(res => {
  213. if (res.code === 1) {
  214. that.goodsInfo = res.data;
  215. }
  216. });
  217. },
  218. // 加入购物车
  219. addCart(sku) {
  220. if (sku.stock <= 0) {
  221. this.$u.toast('库存不足');
  222. return;
  223. }
  224. if (this.detail.activity_type) {
  225. this.$Router.push({ path: '/pages/goods/detail', query: { id: this.detail.id } });
  226. return;
  227. }
  228. let confirmGoodsList = {
  229. list: [{
  230. goods_id: sku.goods_id,
  231. goods_num: 1,
  232. sku_price_id: sku.id,
  233. goods_price: sku.price
  234. }],
  235. from: 'goods'
  236. };
  237. this.addCartGoods(confirmGoodsList).then(res => {
  238. if (res.code === 1) {
  239. this.$u.toast(res.msg);
  240. }
  241. });
  242. }
  243. }
  244. };
  245. </script>
  246. <style lang="scss">
  247. .goods-box {
  248. width: 345rpx;
  249. background: #fff;
  250. border-radius: 20rpx;
  251. overflow: hidden;
  252. .goods-bottom {
  253. background-size: 100% 100%;
  254. background-position: bottom center;
  255. background-repeat: no-repeat;
  256. }
  257. .img-box {
  258. width: 345rpx;
  259. height: 345rpx;
  260. overflow: hidden;
  261. position: relative;
  262. background-color: #fff;
  263. border-radius: 6rpx;
  264. .goods-img {
  265. width: 345rpx;
  266. height: 345rpx;
  267. background-color: #ccc;
  268. }
  269. }
  270. .title {
  271. width: 330rpx;
  272. vertical-align: center;
  273. font-size: 28rpx;
  274. font-weight: bold;
  275. line-height: 40rpx;
  276. color: #333333;
  277. padding-top: 6rpx;
  278. .title-tag {
  279. position: relative;
  280. top: -6rpx;
  281. }
  282. }
  283. .sub-title {
  284. font-size: 24rpx;
  285. font-weight: 400;
  286. width: 330rpx;
  287. color: #999999;
  288. }
  289. .price-box {
  290. .price {
  291. font-size: 30rpx;
  292. color: #ff3000;
  293. font-weight: 600;
  294. &::before {
  295. content: '¥';
  296. font-size: 24rpx;
  297. }
  298. }
  299. .origin-price {
  300. font-size: 26rpx;
  301. font-weight: 400;
  302. text-decoration: line-through;
  303. color: #c4c4c4;
  304. }
  305. .sales-box {
  306. font-size: 18rpx;
  307. font-weight: 400;
  308. color: #c4c4c4;
  309. line-height: 20rpx;
  310. }
  311. }
  312. // 购物车
  313. .cart-box {
  314. .cart-btn {
  315. width: 54rpx;
  316. height: 54rpx;
  317. border-radius: 50%;
  318. padding: 0;
  319. background: linear-gradient(90deg, #e9b461, #eecc89);
  320. }
  321. }
  322. }
  323. </style>