index.vue 14 KB

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