goods_item_v.vue 8.7 KB

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