index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <template>
  2. <view class="goodsList">
  3. <view class="nav">
  4. <view class="left">
  5. <text class="name">宝贝口袋</text>
  6. <text class="num">共{{count}}件商品</text>
  7. </view>
  8. <view class="cart" @click="goIncart">
  9. <image class="image" :src="imgHost + '/statics/images/cart.png'"></image>
  10. <text class="num">购物车</text>
  11. </view>
  12. </view>
  13. <scroll-view @scrolltolower="scrolltolower" :style="'width: '+ Width +'px; height: '+ (Height/num) +'px; display: flex; flex-direction: column;'" :scroll-y="true">
  14. <view class="itemList" v-for="(item,index) in goodList" :key="index" @click="goDetails(item)">
  15. <image class="pictrue" :src="item.image"></image>
  16. <view class="text">
  17. <view class="textCon">
  18. <text class="name line2" v-if="item.store_name.length>=32">{{item.store_name.slice(0,32)}}...</text>
  19. <text class="name line2" v-else>{{item.store_name}}</text>
  20. <view class="labelCon" v-if="item.promotions.title">
  21. <text class="label">{{item.promotions.title}}</text>
  22. </view>
  23. </view>
  24. <view class="bottom">
  25. <view class="money">
  26. <text class="lable">¥</text>
  27. <text class="price">{{item.price}}</text>
  28. </view>
  29. <view class="cart">
  30. <image v-if="item.product_type == 0 && !item.custom_form.length" class="image" @click.stop="goCart(item)" :src="imgHost + '/statics/images/add-cart.png'"></image>
  31. <view v-if="item.stock>0" class="bnt" @click.stop="goBuy(item)">
  32. <text class="name">去抢购</text>
  33. </view>
  34. <view v-else-if="!item.stock" class="bnt on">
  35. <text class="name">已售罄</text>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <text class="loading" v-if="loadend">没有更多内容啦~</text>
  42. </scroll-view>
  43. <uni-popup type="bottom" ref="pinglunGoods" @touchmove.stop.prevent="moveHandle">
  44. <view :style="'width: '+ windowWidth +'px; background-color: #fff; border-top-left-radius: 10px; border-top-right-radius: 10px;'">
  45. <productWindow :attr="attr" :isShow="1" :iSplus="1" :iScart='1' :storeInfo="storeInfo" @closeScrollview="closeScrollview" @ChangeAttr="ChangeAttr" @ChangeCartNum="ChangeCartNum" @attrVal="attrVal" @iptCartNum="iptCartNum" @goCat="goCat" id="product-window"
  46. :is_vip="is_vip"></productWindow>
  47. </view>
  48. </uni-popup>
  49. </view>
  50. </template>
  51. <script>
  52. import {
  53. mapGetters
  54. } from 'vuex';
  55. import { HTTP_REQUEST_URL } from '@/config/app';
  56. import productWindow from '../productWindow/index.vue';
  57. import {
  58. videoProduct
  59. } from '@/api/short-video.js';
  60. import {
  61. getProductDetail,
  62. postCartAdd
  63. } from '@/api/store.js';
  64. export default {
  65. components: {
  66. productWindow
  67. },
  68. computed: mapGetters(['cartNum']),
  69. props: {
  70. Width: {
  71. type:Number,
  72. default:0
  73. },
  74. Height : {
  75. type:Number,
  76. default:0
  77. }
  78. },
  79. data() {
  80. return {
  81. windowWidth: 0,
  82. imgHost: HTTP_REQUEST_URL,
  83. num: 1.15,
  84. goodList: [],
  85. videoID: 0,
  86. attr: {
  87. cartAttr: false,
  88. productAttr: [],
  89. productSelect: {}
  90. },
  91. storeInfo:{},
  92. productValue: [], //系统属性
  93. is_vip: 0, //是否是会员
  94. attrTxt: '请选择', //属性页面提示
  95. attrValue: '', //已选属性
  96. news: 1,
  97. id:0,//商品id
  98. count:0,
  99. limit: 20,
  100. page: 1,
  101. loadend: false,
  102. deliveryType:1,
  103. storeId:0
  104. };
  105. },
  106. created(){
  107. this.windowWidth = uni.getSystemInfoSync().windowWidth;
  108. this.videoID = uni.getStorageSync("videoID");
  109. },
  110. mounted(){
  111. // #ifndef MP
  112. this.productList();
  113. // #endif
  114. },
  115. methods: {
  116. goDetails(item){
  117. uni.navigateTo({
  118. url: `/pages/goods_details/index?id=${item.id}`
  119. });
  120. },
  121. closeScrollview(){
  122. this.$refs.pinglunGoods.close();
  123. },
  124. goCart(item){
  125. this.news = 0;
  126. this.getGoodsDetails(item);
  127. },
  128. goBuy(item){
  129. this.news = 1;
  130. this.getGoodsDetails(item);
  131. },
  132. /*
  133. * 加入购物车
  134. */
  135. goCat: function() {
  136. let that = this,
  137. productSelect = that.productValue[this.attrValue];
  138. //如果有属性,没有选择,提示用户选择
  139. if (that.attr.productAttr.length && productSelect === undefined)
  140. return uni.showToast({
  141. title: '产品库存不足,请选择其它属性',
  142. icon: 'none',
  143. duration: 2000
  144. });
  145. if (that.attr.productSelect.cart_num <= 0) {
  146. that.attr.productSelect.cart_num = 1
  147. return uni.showToast({ title: '请先选择属性', icon: 'none', duration: 2000 });
  148. }
  149. let q = {
  150. productId: that.id,
  151. cartNum: that.attr.productSelect.cart_num,
  152. new: that.news,
  153. uniqueId: that.attr.productSelect !== undefined ? that.attr.productSelect.unique : ''
  154. };
  155. postCartAdd(q)
  156. .then(function(res) {
  157. if (that.news) {
  158. uni.navigateTo({
  159. url: '/pages/goods/order_confirm/index?new=1&cartId=' + res.data.cartId+'&delivery_type=' + that.deliveryType + '&store_id=' + that.storeId
  160. });
  161. } else {
  162. let cartNums = parseInt(that.cartNum) + parseInt(that.attr.productSelect.cart_num);
  163. that.$store.commit('indexData/setCartNum', cartNums + '')
  164. uni.showToast({
  165. title: '添加购物车成功',
  166. icon: 'none',
  167. duration: 2000
  168. });
  169. that.$refs.pinglunGoods.close();
  170. }
  171. })
  172. .catch(err => {
  173. return uni.showToast({ title: err, icon: 'none', duration: 2000 });
  174. });
  175. },
  176. goIncart(){
  177. uni.switchTab({
  178. url: '/pages/order_addcart/order_addcart'
  179. });
  180. },
  181. getGoodsDetails: function(item) {
  182. this.id = item.id;
  183. uni.showLoading({
  184. title: '加载中',
  185. mask: true
  186. });
  187. let that = this;
  188. getProductDetail(item.product_id).then(res => {
  189. uni.hideLoading();
  190. this.$refs.pinglunGoods.open('bottom');
  191. let storeInfo = res.data.storeInfo;
  192. that.$set(that, 'storeInfo', storeInfo);
  193. that.$set(that.attr, 'cartAttr', true);
  194. that.$set(that.attr, 'productAttr', res.data.productAttr);
  195. that.$set(that, 'productValue', res.data.productValue);
  196. that.$set(that, 'is_vip', res.data.storeInfo.is_vip);
  197. let deliverySort = res.data.storeInfo.delivery_type.sort((x,y)=>x - y);
  198. that.$set(that, 'deliveryType', deliverySort[0] || 1);
  199. let storeId = res.data.storeInfo.type==1?res.data.storeInfo.relation_id:0;
  200. that.$set(that, 'storeId', storeId);
  201. that.DefaultSelect();
  202. }).catch(err => {
  203. uni.hideLoading();
  204. })
  205. },
  206. /**
  207. * 默认选中属性
  208. *
  209. */
  210. DefaultSelect: function() {
  211. let productAttr = this.attr.productAttr;
  212. let valueobj = [];
  213. let value = [];
  214. for (var key in this.productValue) {
  215. if (this.productValue[key].stock > 0) {
  216. valueobj = this.attr.productAttr.length ? key.split(',') : [];
  217. break;
  218. }
  219. }
  220. // 处理已售罄时默认选中第一个
  221. if(!valueobj.length && this.attr.productAttr.length){
  222. value = Object.keys(this.productValue)[0].split(',');
  223. }else{
  224. value = valueobj;
  225. }
  226. for (let i = 0; i < productAttr.length; i++) {
  227. this.$set(productAttr[i], 'index', value[i]);
  228. }
  229. //sort();排序函数:数字-英文-汉字;
  230. let productSelect = this.productValue[value.join(',')];
  231. if (productSelect && productAttr.length) {
  232. this.$set(this.attr.productSelect, 'store_name', this.storeInfo.store_name);
  233. this.$set(this.attr.productSelect, 'image', productSelect.image);
  234. this.$set(this.attr.productSelect, 'price', productSelect.price);
  235. this.$set(this.attr.productSelect, 'stock', productSelect.stock);
  236. this.$set(this.attr.productSelect, 'unique', productSelect.unique);
  237. this.$set(this.attr.productSelect, 'cart_num', 1);
  238. this.$set(this, 'attrValue', value.join(','));
  239. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  240. this.$set(this, 'attrTxt', '已选择');
  241. } else if (!productSelect && productAttr.length) {
  242. this.$set(this.attr.productSelect, 'store_name', this.storeInfo.store_name);
  243. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  244. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  245. this.$set(this.attr.productSelect, 'stock', 0);
  246. this.$set(this.attr.productSelect, 'unique', '');
  247. this.$set(this.attr.productSelect, 'cart_num', 0);
  248. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  249. this.$set(this, 'attrValue', '');
  250. this.$set(this, 'attrTxt', '请选择');
  251. } else if (!productSelect && !productAttr.length) {
  252. this.$set(this.attr.productSelect, 'store_name', this.storeInfo.store_name);
  253. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  254. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  255. this.$set(this.attr.productSelect, 'stock', this.storeInfo.stock);
  256. this.$set(this.attr.productSelect, 'unique', this.storeInfo.unique || '');
  257. this.$set(this.attr.productSelect, 'cart_num', 1);
  258. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  259. this.$set(this, 'attrValue', '');
  260. this.$set(this, 'attrTxt', '请选择');
  261. }
  262. },
  263. /**
  264. * 属性变动赋值
  265. *
  266. */
  267. ChangeAttr: function(res) {
  268. let productSelect = this.productValue[res];
  269. this.$set(this, "selectSku", productSelect);
  270. if (productSelect && productSelect.stock >= 0) {
  271. this.$set(this.attr.productSelect, 'image', productSelect.image);
  272. this.$set(this.attr.productSelect, 'price', productSelect.price);
  273. this.$set(this.attr.productSelect, 'stock', productSelect.stock);
  274. this.$set(this.attr.productSelect, 'unique', productSelect.unique);
  275. this.$set(this.attr.productSelect, 'cart_num', 1);
  276. this.$set(this.attr.productSelect, 'vip_price', productSelect.vip_price);
  277. this.$set(this, 'attrValue', res);
  278. this.$set(this, 'attrTxt', '已选择');
  279. } else {
  280. this.$set(this.attr.productSelect, 'image', this.storeInfo.image);
  281. this.$set(this.attr.productSelect, 'price', this.storeInfo.price);
  282. this.$set(this.attr.productSelect, 'stock', 0);
  283. this.$set(this.attr.productSelect, 'unique', '');
  284. this.$set(this.attr.productSelect, 'cart_num', 0);
  285. this.$set(this.attr.productSelect, 'vip_price', this.storeInfo.vip_price);
  286. this.$set(this, 'attrValue', '');
  287. this.$set(this, 'attrTxt', '请选择');
  288. }
  289. },
  290. /**
  291. * 购物车数量加和数量减
  292. *
  293. */
  294. ChangeCartNum: function(changeValue) {
  295. //changeValue:是否 加|减
  296. //获取当前变动属性
  297. let productSelect = this.productValue[this.attrValue];
  298. //如果没有属性,赋值给商品默认库存
  299. if (productSelect === undefined && !this.attr.productAttr.length) productSelect = this.attr
  300. .productSelect;
  301. //无属性值即库存为0;不存在加减;
  302. if (productSelect === undefined) return;
  303. let stock = productSelect.stock || 0;
  304. let num = this.attr.productSelect;
  305. if (changeValue) {
  306. num.cart_num++;
  307. if (num.cart_num > stock) {
  308. this.$set(this.attr.productSelect, 'cart_num', stock ? stock : 1);
  309. this.$set(this, 'cart_num', stock ? stock : 1);
  310. }
  311. } else {
  312. num.cart_num--;
  313. if (num.cart_num < 1) {
  314. this.$set(this.attr.productSelect, 'cart_num', 1);
  315. this.$set(this, 'cart_num', 1);
  316. }
  317. }
  318. },
  319. attrVal(val) {
  320. this.$set(this.attr.productAttr[val.indexw], 'index', this.attr.productAttr[val.indexw].attr_values[val
  321. .indexn]);
  322. },
  323. /**
  324. * 购物车手动填写
  325. *
  326. */
  327. iptCartNum: function(e) {
  328. this.$set(this.attr.productSelect, 'cart_num', e);
  329. },
  330. productList(id,type){
  331. // #ifndef MP
  332. let ids = this.videoID;
  333. // #endif
  334. // #ifdef MP
  335. this.videoID = id;
  336. let ids = id;
  337. if(type){
  338. this.page = 1;
  339. this.goodList = [];
  340. }
  341. // #endif
  342. videoProduct(ids,{
  343. limit: this.limit,
  344. page: this.page
  345. }).then(res=>{
  346. let list = res.data.list;
  347. let loadend = list.length < this.limit;
  348. this.goodList = this.goodList.concat(list);
  349. this.loadend = loadend;
  350. this.page = this.page + 1;
  351. this.count = res.data.count;
  352. }).catch(err=>{
  353. return uni.showToast({
  354. title: err,
  355. icon: 'none',
  356. duration: 2000
  357. });
  358. })
  359. },
  360. scrolltolower(){
  361. this.productList(this.videoID);
  362. },
  363. closeGoods(){
  364. this.$emit('myevent');
  365. }
  366. }
  367. }
  368. </script>
  369. <style lang="scss">
  370. .loading{
  371. text-align: center;
  372. height: 70rpx;
  373. line-height: 70rpx;
  374. font-size: 28rpx;
  375. display: flex;
  376. flex-direction: row;
  377. justify-content: center;
  378. color: #999999;
  379. }
  380. .goodsList{
  381. padding: 0 20rpx;
  382. .itemList{
  383. flex-direction: row;
  384. background-color: #fff;
  385. margin-bottom: 20rpx;
  386. padding: 20rpx;
  387. width: 710rpx;
  388. border-radius: 14rpx;
  389. .pictrue{
  390. width: 220rpx;
  391. height: 220rpx;
  392. border-radius: 10rpx;
  393. margin-right: 16rpx;
  394. }
  395. .text{
  396. width: 420rpx;
  397. .textCon{
  398. height: 154rpx;
  399. }
  400. .bottom{
  401. display: flex;
  402. flex-direction: row;
  403. justify-content: space-between;
  404. align-items: flex-end;
  405. .cart{
  406. display: flex;
  407. flex-direction: row;
  408. align-items: center;
  409. .image{
  410. width: 62rpx;
  411. height: 62rpx;
  412. }
  413. .bnt{
  414. width: 144rpx;
  415. height: 64rpx;
  416. background: #F5222D;
  417. border-radius: 37rpx;
  418. display: flex;
  419. justify-content: center;
  420. align-items: center;
  421. margin-left: 20rpx;
  422. .name{
  423. font-weight: 400;
  424. color: #FFFFFF;
  425. font-size: 28rpx;
  426. }
  427. &.on{
  428. background-color: #bbb;
  429. }
  430. }
  431. }
  432. .money{
  433. display: flex;
  434. flex-direction: row;
  435. align-items: flex-end;
  436. .lable{
  437. font-weight: 600;
  438. color: #E93323;
  439. font-size: 24rpx;
  440. }
  441. .price{
  442. font-weight: 600;
  443. color: #E93323;
  444. font-size: 30rpx;
  445. }
  446. }
  447. }
  448. .labelCon{
  449. display: flex;
  450. flex-direction: row;
  451. margin-top: 16rpx;
  452. .label{
  453. border: 1px solid #E93323;
  454. padding: 2rpx 4rpx;
  455. font-size: 20rpx;
  456. font-weight: 400;
  457. color: #E93323;
  458. border-radius: 5rpx;
  459. }
  460. }
  461. .name{
  462. color:#333333;
  463. font-size: 28rpx;
  464. font-weight: 400;
  465. }
  466. }
  467. }
  468. .nav{
  469. display: flex;
  470. flex-direction: row;
  471. justify-content: space-between;
  472. align-items: center;
  473. height: 126rpx;
  474. .num{
  475. font-weight: 400;
  476. color: #999999;
  477. font-size: 22rpx;
  478. }
  479. .cart{
  480. align-items: center;
  481. .image{
  482. margin-bottom: 5rpx;
  483. width:28rpx;
  484. height: 28rpx;
  485. }
  486. }
  487. .left{
  488. .name{
  489. font-weight: 500;
  490. color: #333333;
  491. font-size: 30rpx;
  492. }
  493. }
  494. }
  495. }
  496. </style>