uni-coods.vue 4.0 KB

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