index.vue 15 KB

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