uni-coods.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. hasLogin:{
  54. type: Boolean,
  55. default: false
  56. }
  57. },
  58. data() {
  59. return {
  60. customStyle:{
  61. color: '#fff',
  62. borderRadius: '150rpx',
  63. border: 0,
  64. width: '50rpx',
  65. height: '50rpx',
  66. lineHeight: '50rpx',
  67. fontSize: '25px'
  68. },
  69. loadingImg: '/static/loading.gif',
  70. errorImg: '/static/imgError.png',
  71. timer: null,
  72. finger: {x:0,y:0},
  73. linePos: null,
  74. }
  75. },
  76. methods: {
  77. // 添加购物车
  78. addGoodToCart(item){
  79. uni.showLoading({
  80. mask: true,
  81. title: "正在加入购物车..."
  82. })
  83. addGoodsToCart(item).then(res => {
  84. if(res.status == 200){
  85. // 刷新购物车数量
  86. if(!item.hasCunzai){
  87. this.$store.state.vuex_cartNums = this.$store.state.vuex_cartNums + 1
  88. }
  89. setTimeout(()=>{
  90. uni.showToast({
  91. icon: 'none',
  92. title:'添加成功'
  93. })
  94. // 刷新购物车数据
  95. uni.$emit('getCartList')
  96. },50)
  97. }
  98. })
  99. },
  100. // 判断当前商品是否已存在购物车中
  101. hasCunzai(data){
  102. let list = this.$store.state.vuex_cartList
  103. let row = list.find(item=> item.goodsTypeNo == data.goodsTypeNo)
  104. return row
  105. },
  106. // 加入购物车
  107. addCart(item) {
  108. if(!this.hasLogin){
  109. uni.showModal({
  110. title: '提示',
  111. content: '您尚未登录或登录已过期,请先进行登录',
  112. success (res) {
  113. if (res.confirm) {
  114. uni.navigateTo({
  115. url: '/pages/login/login'
  116. });
  117. } else if (res.cancel) {
  118. console.log('用户点击取消');
  119. }
  120. }
  121. });
  122. } else {
  123. this.addGoodToCart({goodsNo:item.goodsNo,buyQty:1,hasCunzai:this.hasCunzai(item)})
  124. }
  125. },
  126. // 商品详情
  127. toDetail(item){
  128. uni.navigateTo({
  129. url:"/pagesA/goods/goodsDetail?id="+item.id + "&goldLimit="+this.goldLimit
  130. })
  131. }
  132. },
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .goods-list{
  137. display: flex;
  138. flex-wrap: wrap;
  139. justify-content: space-between;
  140. padding-top: 20upx;
  141. .goods-item{
  142. background: #FFFFFF;
  143. width: 48%;
  144. margin-bottom: 20upx;
  145. display: flex;
  146. flex-direction: column;
  147. justify-content: space-between;
  148. .goods-imgs{
  149. position: relative;
  150. border-radius: 28upx 28upx 0 0;
  151. border: 1px solid #eee;
  152. overflow: hidden;
  153. .goods-staus{
  154. width: 100%;
  155. height: 100%;
  156. position: absolute;
  157. z-index: 100;
  158. border-radius: 15rpx 15rpx 0 0;
  159. background: rgba($color: #000000, $alpha: 0.4);
  160. text-align: center;
  161. line-height: 250rpx;
  162. left: 0;
  163. top: 0;
  164. color: #eee;
  165. font-size: 30upx;
  166. font-weight: bold;
  167. }
  168. }
  169. .name{
  170. padding: 10upx 0;
  171. font-weight: bold;
  172. font-size: 30rpx;
  173. }
  174. .price{
  175. display: flex;
  176. align-items: center;
  177. justify-content: space-between;
  178. text{
  179. color: #d42a26;
  180. font-size: 36upx;
  181. margin-right: 6upx;
  182. font-weight: bold;
  183. }
  184. >view{
  185. &:first-child{
  186. font-size: 22upx;
  187. display: flex;
  188. align-items: center;
  189. }
  190. &:last-child{
  191. &:active{
  192. opacity: 0.5;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. </style>