uni-coods.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="goods-list">
  3. <view class="goods-item" v-for="item in list" :key="item.id">
  4. <view class="goods-imgs" @click="toDetail(item)">
  5. <view v-if="item.inventoryQty == 0&&item.state == 1" class="goods-staus">已售罄</view>
  6. <view v-if="item.state == 0" class="goods-staus">已下架</view>
  7. <u-lazy-load
  8. img-mode="scaleToFill"
  9. :height="imgHeight"
  10. :image="item.homeImage+'?x-oss-process=image/resize,m_pad,h_252,w_325,color_ffffff'"
  11. :loading-img="loadingImg"
  12. :error-img="errorImg">
  13. </u-lazy-load>
  14. </view>
  15. <view class="name ellipsis-two" @click="toDetail(item)">{{item.name}}</view>
  16. <view class="price">
  17. <view>
  18. <text>{{item.sellGold}}</text>
  19. <u-image mode="scaleToFill" width="40rpx" height="40rpx" src="/static/ledou.png"></u-image>
  20. </view>
  21. <view v-if="item.inventoryQty > 0 && item.state == 1 ">
  22. <!-- <u-image mode="scaleToFill" width="45rpx" height="45rpx" src="/static/addCart.png"></u-image> -->
  23. <u-button size="mini" type="error" :custom-style="customStyle" @click="addCart(item)">
  24. +
  25. </u-button>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. import {
  33. addGoodsToCart
  34. } from '@/api/shoppingCart.js'
  35. export default {
  36. props: {
  37. imgHeight: {
  38. type: Number,
  39. default: 250
  40. },
  41. goldLimit: {
  42. type: [Number,String],
  43. default: 0
  44. },
  45. list:{
  46. type: Array,
  47. default: function(){
  48. return []
  49. }
  50. }
  51. },
  52. data() {
  53. return {
  54. customStyle:{
  55. color: '#fff',
  56. borderRadius: '150rpx',
  57. border: 0,
  58. width: '50rpx',
  59. height: '50rpx',
  60. lineHeight: '50rpx',
  61. fontSize: '25px'
  62. },
  63. loadingImg: '/static/loading.gif',
  64. errorImg: '/static/imgError.png',
  65. timer: null,
  66. finger: {x:0,y:0},
  67. linePos: null,
  68. }
  69. },
  70. methods: {
  71. // 添加购物车
  72. addGoodToCart(item){
  73. uni.showLoading({
  74. mask: true,
  75. title: "加入购物车中..."
  76. })
  77. addGoodsToCart(item).then(res => {
  78. if(res.status == 200){
  79. // 刷新购物车数据
  80. uni.$emit('getCartList')
  81. // 刷新购物车数量
  82. if(!item.hasCunzai){
  83. this.$store.state.vuex_cartNums = this.$store.state.vuex_cartNums + 1
  84. }
  85. uni.showToast({
  86. icon: 'none',
  87. title:'加入购物车成功'
  88. })
  89. }else{
  90. uni.hideLoading()
  91. }
  92. })
  93. },
  94. // 判断当前商品是否已存在购物车中
  95. hasCunzai(data){
  96. let list = this.$store.state.vuex_cartList
  97. let row = list.find(item=> item.goodsTypeNo == data.goodsTypeNo)
  98. return row
  99. },
  100. // 加入购物车
  101. addCart(item) {
  102. this.addGoodToCart({goodsNo:item.goodsNo,buyQty:1,hasCunzai:this.hasCunzai(item)})
  103. },
  104. // 商品详情
  105. toDetail(item){
  106. uni.navigateTo({
  107. url:"/pages/goods/goodsDetail?id="+item.id + "&goldLimit="+this.goldLimit
  108. })
  109. }
  110. },
  111. }
  112. </script>
  113. <style lang="scss" scoped>
  114. .goods-list{
  115. display: flex;
  116. flex-wrap: wrap;
  117. justify-content: space-between;
  118. padding-top: 20upx;
  119. .goods-item{
  120. background: #FFFFFF;
  121. width: 48%;
  122. margin-bottom: 5upx;
  123. .goods-imgs{
  124. position: relative;
  125. border-radius: 28upx 28upx 0 0;
  126. overflow: hidden;
  127. .goods-staus{
  128. width: 100%;
  129. height: 100%;
  130. position: absolute;
  131. z-index: 100;
  132. border-radius: 15rpx 15rpx 0 0;
  133. background: rgba($color: #000000, $alpha: 0.4);
  134. text-align: center;
  135. line-height: 250rpx;
  136. left: 0;
  137. top: 0;
  138. color: #eee;
  139. font-size: 30upx;
  140. font-weight: bold;
  141. }
  142. }
  143. .name{
  144. padding: 10upx 0;
  145. font-weight: bold;
  146. font-size: 30rpx;
  147. }
  148. .price{
  149. display: flex;
  150. align-items: center;
  151. justify-content: space-between;
  152. padding: 15upx 0;
  153. text{
  154. color: #d42a26;
  155. font-size: 36upx;
  156. margin-right: 6upx;
  157. font-weight: bold;
  158. }
  159. >view{
  160. &:first-child{
  161. font-size: 22upx;
  162. display: flex;
  163. align-items: center;
  164. }
  165. &:last-child{
  166. &:active{
  167. opacity: 0.5;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. </style>