uni-coods.vue 3.9 KB

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