uni-coods.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if(!item.hasCunzai){
  81. this.$store.state.vuex_cartNums = this.$store.state.vuex_cartNums + 1
  82. }
  83. uni.showToast({
  84. icon: 'none',
  85. title:'加入购物车成功'
  86. })
  87. }else{
  88. uni.hideLoading()
  89. }
  90. })
  91. },
  92. // 判断当前商品是否已存在购物车中
  93. hasCunzai(data){
  94. let list = this.$store.state.vuex_cartList
  95. let row = list.find(item=> item.goodsTypeNo == data.goodsTypeNo)
  96. return row
  97. },
  98. // 加入购物车
  99. addCart(item) {
  100. this.addGoodToCart({goodsNo:item.goodsNo,buyQty:1,hasCunzai:this.hasCunzai(item)})
  101. },
  102. // 商品详情
  103. toDetail(item){
  104. uni.navigateTo({
  105. url:"/pages/goods/goodsDetail?id="+item.id + "&goldLimit="+this.goldLimit
  106. })
  107. }
  108. },
  109. }
  110. </script>
  111. <style lang="scss" scoped>
  112. .goods-list{
  113. display: flex;
  114. flex-wrap: wrap;
  115. justify-content: space-between;
  116. padding-top: 20upx;
  117. .goods-item{
  118. background: #FFFFFF;
  119. width: 48%;
  120. margin-bottom: 5upx;
  121. .goods-imgs{
  122. position: relative;
  123. border-radius: 28upx 28upx 0 0;
  124. overflow: hidden;
  125. .goods-staus{
  126. width: 100%;
  127. height: 100%;
  128. position: absolute;
  129. z-index: 100;
  130. border-radius: 15rpx 15rpx 0 0;
  131. background: rgba($color: #000000, $alpha: 0.4);
  132. text-align: center;
  133. line-height: 250rpx;
  134. left: 0;
  135. top: 0;
  136. color: #eee;
  137. font-size: 30upx;
  138. font-weight: bold;
  139. }
  140. }
  141. .name{
  142. padding: 10upx 0;
  143. font-weight: bold;
  144. font-size: 30rpx;
  145. }
  146. .price{
  147. display: flex;
  148. align-items: center;
  149. justify-content: space-between;
  150. padding: 15upx 0;
  151. text{
  152. color: #d42a26;
  153. font-size: 36upx;
  154. margin-right: 6upx;
  155. font-weight: bold;
  156. }
  157. >view{
  158. &:first-child{
  159. font-size: 22upx;
  160. display: flex;
  161. align-items: center;
  162. }
  163. &:last-child{
  164. &:active{
  165. opacity: 0.5;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. </style>