goods_item_v.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <!-- 商品组件:竖直方向
  2. 点击进入商品详情页
  3. 加入购物车事件
  4. -->
  5. <template name="goodsItemV">
  6. <view class="goods_v_item flex_column_start_start" @click="goGoodsDetail(goods_info)" :style="{width:'calc((750rpx - '+page_margin*4+'rpx - '+goods_margin*2+'rpx)/2)',borderRadius:border_radius+'px',border:border_style == 'border_eee'?'1rpx solid #eee':'',boxShadow:border_style == 'card-shadow'?'rgba(93, 113, 127, 0.08) 0px 2px 8px':''}">
  7. <view class="goods_desc_top" :style="{height:height*2+'rpx'}">
  8. <view class="goods-img" :style="{backgroundImage: 'url('+(goods_info.mainImage||goods_info.goodsImage)+')',width:'calc((750rpx - '+page_margin*4+'rpx - '+goods_margin*2+'rpx)/2)',borderTopLeftRadius:border_radius*2+'rpx',borderTopRightRadius:border_radius*2+'rpx'}"></view>
  9. <text class="goods-name">{{goods_info.goodsName}}</text>
  10. <!-- <view class="goods_des">{{goods_info.goodsBrief}}</view> -->
  11. </view>
  12. <view :class="show_sale == true?'goods-price':'goods-price have_no_sale'" :style="{marginTop:isIos?'26rpx':0}">
  13. <view class="left">
  14. <text class="unit">¥</text>
  15. <text class="price_int">{{this.$getPartNumber(goods_info.goodsPrice,'int')}}</text>
  16. <text class="price_decimal">{{this.$getPartNumber(goods_info.goodsPrice,'decimal')}}</text>
  17. </view>
  18. <view class="pre_bottom" :class="show_sale == true?'goods_item_bottom':''">
  19. <view class="have_sold_text" v-if="show_sale == true">已售{{goods_info.actualSales}}件</view>
  20. <image :src="imgUrl+'add-cart.png'" @click.stop="addCart(goods_info.productId || goods_info.defaultProductId)" v-if="icon_type == 1"></image>
  21. <image :src="icon2" @click.stop="addCart(goods_info.productId || goods_info.defaultProductId)" v-if="icon_type == 2"></image>
  22. <image :src="icon3" @click.stop="addCart(goods_info.productId || goods_info.defaultProductId)" v-if="icon_type == 3"></image>
  23. <image :src="icon4" @click.stop="addCart(goods_info.productId || goods_info.defaultProductId)" v-if="icon_type == 4"></image>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import {
  30. mapState,
  31. } from 'vuex';
  32. export default {
  33. name: "goodsItemV",
  34. data() {
  35. return {
  36. imgUrl: getApp().globalData.imgUrl,
  37. icon2:getApp().globalData.imgUrl+'index/add2.png',
  38. icon3:getApp().globalData.imgUrl+'index/add.png',
  39. icon4:getApp().globalData.imgUrl+'index/add3.png',
  40. icon5:getApp().globalData.imgUrl+'index/stop.png',
  41. icon_url:'', //加车图标
  42. goods_pic:'' , //商品图片
  43. goods_sale:'', //销量
  44. isIos: getApp().globalData.systemInfo.platform == 'ios', //是否ios手机
  45. // // #ifdef H5
  46. // isIos: getApp().globalData.systemInfo.platform == 'ios', //是否ios手机
  47. // // #endif
  48. }
  49. },
  50. computed: {
  51. ...mapState(['userInfo'])
  52. },
  53. props: {
  54. goods_info: {
  55. type: Object,
  56. value: {}
  57. },
  58. icon_type:{
  59. type:Number,
  60. },
  61. show_sale:{
  62. type:Boolean
  63. },
  64. border_radius:{
  65. type:Number
  66. },
  67. border_style:{
  68. type:String
  69. },
  70. // 商品边距
  71. goods_margin:{
  72. type:Number,
  73. default:10
  74. },
  75. // 页面边距
  76. page_margin:{
  77. type:Number,
  78. default:10
  79. },
  80. height:{
  81. type:Number,
  82. default:258
  83. }
  84. },
  85. onLoad(){
  86. },
  87. methods: {
  88. //进入商品详情页
  89. goGoodsDetail(goods_info) {
  90. uni.navigateTo({
  91. url: '/pages/product/detail?productId=' + (goods_info.productId || goods_info.defaultProductId) + '&goodsId='+goods_info.goodsId
  92. })
  93. },
  94. // 加入购物车
  95. addCart(productId){
  96. if(this.userInfo.access_token){ //登录
  97. let param = {}
  98. param.url = 'v3/business/front/cart/add'
  99. param.method = 'POST'
  100. param.data = {
  101. productId:productId,
  102. number:1
  103. }
  104. this.$request(param).then(res=>{
  105. if(res.state == 200){
  106. uni.showToast({
  107. title: res.msg,
  108. icon:'none'
  109. })
  110. this.$emit('reloadCartList',true)
  111. }else{
  112. uni.showToast({
  113. title:res.msg,
  114. icon:'none',
  115. duration:700
  116. })
  117. }
  118. })
  119. }else{ //未登录
  120. let cart_list = {
  121. storeCartGroupList:[{
  122. promotionCartGroupList:[{
  123. cartList: [{
  124. buyNum: 1,
  125. goodsId: this.goods_info.goodsId,
  126. productId: this.goods_info.productId || this.goods_info.defaultProductId,
  127. productImage: this.goods_info.goodsPic?this.goods_info.goodsPic:this.goods_info.goodsImage,
  128. goodsName: this.goods_info.goodsName,
  129. isChecked: 1,
  130. productPrice: this.goods_info.goodsPrice,
  131. productStock: this.goods_info.productStock
  132. }],
  133. }],
  134. storeId:this.goods_info.storeId,
  135. storeName:this.goods_info.storeName,
  136. checkedAll:true
  137. }],
  138. checkedAll: true,
  139. invalidList: []
  140. }
  141. let local_cart_list = uni.getStorageSync('cart_list') //购物车列表本地缓存
  142. if (local_cart_list) {
  143. let tmp_list1 = []
  144. let tmp_list2 = []
  145. cart_list.storeCartGroupList.forEach(item=>{
  146. item.promotionCartGroupList.forEach(item1=>{
  147. item1.cartList.forEach(item2=>{
  148. local_cart_list.storeCartGroupList.forEach(v=>{
  149. v.promotionCartGroupList.forEach(v1=>{
  150. v1.cartList.forEach(v2=>{
  151. if(v2.productId == item2.productId && v.storeId == item.storeId){
  152. tmp_list1.push(v)
  153. }
  154. })
  155. tmp_list2 = local_cart_list.storeCartGroupList.filter(v=>{
  156. return v.storeId == item.storeId
  157. })
  158. })
  159. })
  160. })
  161. })
  162. })
  163. if(tmp_list1.length > 0 && tmp_list2.length > 0){ //同一店铺同一商品
  164. local_cart_list.storeCartGroupList.map(item=>{
  165. item.promotionCartGroupList.map(item1=>{
  166. item1.cartList.map(item2=>{
  167. if(item2.productId == (this.goods_info.productId || this.goods_info.defaultProductId) && item.storeId == this.goods_info.storeId){
  168. item2.buyNum += 1
  169. }
  170. })
  171. })
  172. })
  173. }else if(tmp_list1.length == 0 && tmp_list2.length > 0){ //同一店铺不同商品
  174. local_cart_list.storeCartGroupList.map(item=>{
  175. if(item.storeId == this.goods_info.storeId){
  176. item.promotionCartGroupList.map(item2=>{
  177. item2.cartList.push(cart_list.storeCartGroupList[0].promotionCartGroupList[0].cartList[0])
  178. })
  179. }
  180. })
  181. }else{ //不同店铺不同商品
  182. local_cart_list.storeCartGroupList.push(cart_list.storeCartGroupList[0])
  183. }
  184. uni.showToast({
  185. title: '加入购物车成功!',
  186. icon:'none'
  187. })
  188. uni.setStorageSync('cart_list',local_cart_list);
  189. this.$emit('addCart',local_cart_list)
  190. } else {
  191. uni.showToast({
  192. title: '加入购物车成功!',
  193. icon:'none'
  194. })
  195. uni.setStorageSync('cart_list',cart_list);
  196. this.$emit('addCart',cart_list)
  197. }
  198. }
  199. },
  200. changeImg(val){
  201. return val.mainImage?val.mainImage:val.goodsImage
  202. },
  203. saleNum(val){
  204. if(val.actualSales){
  205. return val.actualSales
  206. }else{
  207. return val.saleNum
  208. }
  209. // return val.actualSales?val.actualSales:val.saleNum
  210. }
  211. },
  212. }
  213. </script>
  214. <style lang='scss'>
  215. .goods_v_item {
  216. background: #fff;
  217. border-radius: 20rpx;
  218. overflow: hidden;
  219. margin-bottom: 22rpx;
  220. display: flex;
  221. flex-direction: column;
  222. &{
  223. margin-right:0 !important;
  224. }
  225. .goods_desc_top{
  226. height:516rpx;
  227. .goods-img {
  228. background-size: cover;
  229. background-position: center center;
  230. background-repeat: no-repeat;
  231. height: calc((750rpx - 60rpx)/2);
  232. overflow: hidden;
  233. background-color: #fff;
  234. }
  235. .goods-name {
  236. height:173rpx;
  237. margin-top: 20rpx;
  238. font-size: 28rpx;
  239. color: $com-main-font-color;
  240. line-height: 40rpx;
  241. height: 80rpx;
  242. overflow: hidden;
  243. text-overflow: ellipsis;
  244. display: -webkit-box;
  245. -webkit-line-clamp: 2;
  246. -webkit-box-orient: vertical;
  247. word-break: break-word;
  248. padding: 0 20rpx;
  249. }
  250. .goods_des{
  251. font-size: 24rpx;
  252. font-family: PingFang SC;
  253. font-weight: 500;
  254. color: #888888;
  255. line-height: 36rpx;
  256. padding: 0 20rpx;
  257. box-sizing: border-box;
  258. width: 355rpx;
  259. overflow: hidden;
  260. text-overflow:ellipsis;
  261. white-space: nowrap;
  262. }
  263. }
  264. .goods-price {
  265. padding: 0 20rpx;
  266. width: 100%;
  267. display: flex;
  268. flex-direction: column;
  269. .left {
  270. width:100%;
  271. color: $main-color;
  272. .unit,
  273. .price_decimal {
  274. font-size: 24rpx;
  275. margin-right: 3rpx;
  276. }
  277. .price_int {
  278. font-size: 34rpx;
  279. line-height: 34rpx;
  280. }
  281. }
  282. image {
  283. width: 42rpx;
  284. height: 42rpx;
  285. }
  286. }
  287. }
  288. .goods_item_bottom{
  289. width:100%;
  290. display: flex;
  291. justify-content: space-between;
  292. margin-top:20rpx;
  293. padding-bottom: 20rpx;
  294. }
  295. .have_sold_text{
  296. font-size:24rpx;
  297. color:#9a9a9a;
  298. }
  299. .pre_bottom{
  300. display: flex;
  301. align-items: center;
  302. }
  303. .have_no_sale{
  304. width: 100%;
  305. flex-direction: row !important;
  306. justify-content: space-between !important;
  307. padding:20rpx !important;
  308. }
  309. </style>