chooseListBar.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <template>
  2. <view class="all" v-if="show">
  3. <uni-popup ref="popup" type="bottom">
  4. <view class="box">
  5. <view class="top">
  6. <view class="top-left">
  7. <view class="quan"><view class="qzq"></view></view>
  8. <view class="top-font">已选商品</view>
  9. </view>
  10. <view class="top-right" @click="clearCart">
  11. <image src="../../../static/icon/clear.png" mode=""></image>
  12. <view class="right-clear">清空购物车</view>
  13. </view>
  14. </view>
  15. <scroll-view scroll-y class="scoroll">
  16. <view v-for="(item, index) in cartList" :key="item.id">
  17. <view class="cart-item" :class="{ 'b-b': index !== cartList.length - 1 }">
  18. <view class="image-wrapper">
  19. <image
  20. :src="item.productInfo.image"
  21. :class="[item.loaded]"
  22. mode="aspectFill"
  23. lazy-load
  24. @load="onImageLoad('cartList', index)"
  25. @error="onImageError('cartList', index)"
  26. ></image>
  27. </view>
  28. <view class="item-right">
  29. <text class="clamp title">{{ item.productInfo.store_name }}</text>
  30. <text class="attr">{{ item.productInfo.attrInfo.suk }}</text>
  31. <text class="price" style="margin-top: 30rpx;">¥{{ item.productInfo.attrInfo.price }}</text>
  32. </view>
  33. <view class="item-num">
  34. <view class="munbox flex">
  35. <image src="../../../static/icon/reduce.png" mode="" @click="reduce(item, index)"></image>
  36. <input type="number" :value="item.cart_num" disabled />
  37. <image src="../../../static/icon/add.png" mode="" @click="add(item)"></image>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. <view class="db"></view>
  43. </scroll-view>
  44. </view>
  45. </uni-popup>
  46. <view class="action-section">
  47. <view class="total-box" @click.stop="showPopup">
  48. <view class="bg">
  49. <image src="../../../static/img/pay.png" mode=""></image>
  50. <view class="num" v-if="num != 0">{{ num }}</view>
  51. </view>
  52. <view class="total-price">
  53. <text>{{ total }}</text>
  54. </view>
  55. </view>
  56. <button type="primary" class="confirm-btn" @click.stop="createOrder">去结算</button>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import { getCartList, getCartNum, cartDel } from '@/api/user.js';
  62. import { mapState } from 'vuex';
  63. import uniNumberBox from '@/components/uni-number-box.vue';
  64. import { saveUrl, interceptor } from '@/utils/loginUtils.js';
  65. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  66. export default {
  67. components: {
  68. uniNumberBox,
  69. uniPopup
  70. },
  71. data() {
  72. return {
  73. total: 0, //总价格
  74. allChecked: false, //全选状态 true|false
  75. empty: false, //空白页现实 true|false
  76. cartList: [],
  77. num: '',
  78. show: false
  79. };
  80. },
  81. mounted() {
  82. this.loadData();
  83. },
  84. watch: {
  85. cartList: {
  86. deep: true, //深度监听
  87. immediate: true,
  88. handler(newValue, oldValue) {
  89. let empty = newValue.length === 0 ? true : false;
  90. if (this.empty !== empty) {
  91. this.empty = empty;
  92. }
  93. }
  94. }
  95. },
  96. computed: {
  97. ...mapState('user', ['hasLogin'])
  98. },
  99. methods: {
  100. reduce(item, index) {
  101. if (item.cart_num == 1) {
  102. uni.showModal({
  103. content: '删除该商品?',
  104. success: e => {
  105. if (e.confirm) {
  106. this.deleteCartItem(index);
  107. }
  108. }
  109. });
  110. } else {
  111. item.cart_num--;
  112. getCartNum({
  113. id: item.id,
  114. number: item.cart_num
  115. })
  116. .then(e => {
  117. console.log(e);
  118. })
  119. .catch(function(e) {
  120. console.log(e);
  121. });
  122. this.calcTotal(item);
  123. }
  124. },
  125. add(item) {
  126. console.log(item);
  127. if (item.productInfo.stock > item.cart_num) {
  128. item.cart_num++;
  129. getCartNum({
  130. id: item.id,
  131. number: item.cart_num
  132. })
  133. .then(e => {
  134. console.log(e);
  135. })
  136. .catch(function(e) {
  137. console.log(e);
  138. });
  139. this.calcTotal(item);
  140. } else {
  141. return;
  142. }
  143. },
  144. //请求数据
  145. async loadData() {
  146. let obj = this;
  147. getCartList({})
  148. .then(function(e) {
  149. console.log(e, '111');
  150. // 获取当前购物车物品增加数量
  151. let nub = obj.cartList.length;
  152. // 获取之前对象数组
  153. let aArray = obj.cartList.reverse();
  154. // 获取返回数据对象数组
  155. let bArray = e.data.valid.reverse();
  156. obj.cartList = bArray
  157. .map((item, ind) => {
  158. // 设置返回数据默认为勾选状态
  159. item.checked = true;
  160. // 获取相同数组之前对象的数据
  161. let carlist = aArray[ind];
  162. // 判断之前是否已经加载完毕
  163. if (carlist && carlist.loaded == 'loaded') {
  164. item.loaded = 'loaded';
  165. }
  166. return item;
  167. })
  168. .reverse();
  169. obj.calcTotal(); //计算总价
  170. obj.show = true;
  171. })
  172. .catch(function(e) {
  173. console.log(e);
  174. });
  175. },
  176. //监听image加载完成
  177. onImageLoad(key, index) {
  178. // 修改载入完成后图片class样式
  179. this.$set(this[key][index], 'loaded', 'loaded');
  180. },
  181. //监听image加载失败
  182. onImageError(key, index) {
  183. this[key][index].image = '/static/error/errorImage.jpg';
  184. },
  185. showPopup() {
  186. this.$refs.popup.open();
  187. },
  188. // 跳转到登录页
  189. navToLogin() {
  190. // 保存地址
  191. saveUrl();
  192. // 登录拦截
  193. interceptor();
  194. },
  195. //数量
  196. numberChange(data) {
  197. let arr = this.cartList[data.index];
  198. arr.cart_num = data.number;
  199. getCartNum({
  200. id: arr.id,
  201. number: data.number
  202. })
  203. .then(e => {
  204. console.log(e);
  205. })
  206. .catch(function(e) {
  207. console.log(e);
  208. });
  209. this.calcTotal();
  210. },
  211. //删除
  212. deleteCartItem(index) {
  213. const obj = this;
  214. let list = this.cartList;
  215. let row = list[index];
  216. let id = row.id;
  217. cartDel({
  218. ids: id
  219. });
  220. this.cartList.splice(index, 1);
  221. uni.hideLoading();
  222. this.calcTotal();
  223. if (this.num == 0) {
  224. obj.$emit('delChoose');
  225. }
  226. },
  227. //清空
  228. clearCart() {
  229. const obj = this;
  230. uni.showModal({
  231. content: '清空购物车?',
  232. success: e => {
  233. if (e.confirm) {
  234. let st = this.cartList.map(e => {
  235. return e.id;
  236. });
  237. cartDel({
  238. ids: st.join(',')
  239. }).then(e => {
  240. obj.$emit('delChoose');
  241. console.log(e);
  242. });
  243. this.cartList = [];
  244. this.$refs.popup.close();
  245. (this.total = 0), (this.num = 0);
  246. }
  247. }
  248. });
  249. },
  250. //计算总价
  251. calcTotal() {
  252. console.log('111111');
  253. let list = this.cartList;
  254. let total = 0;
  255. let num = 0;
  256. let checked = true;
  257. list.forEach(item => {
  258. console.log(item, '123123');
  259. num += item.cart_num;
  260. total += item.productInfo.attrInfo.price * item.cart_num;
  261. });
  262. this.num = num * 1;
  263. this.allChecked = checked;
  264. this.total = Number(total.toFixed(2));
  265. },
  266. //创建订单
  267. createOrder() {
  268. let list = this.cartList;
  269. let goodsData = [];
  270. list.forEach(item => {
  271. if (item.checked) {
  272. goodsData.push(item.id);
  273. }
  274. });
  275. this.$emit('createOrder', goodsData);
  276. }
  277. }
  278. };
  279. </script>
  280. <style lang="scss">
  281. .container {
  282. padding-bottom: 134rpx;
  283. background-color: $page-color-base;
  284. /* 空白页 */
  285. .empty {
  286. position: fixed;
  287. left: 0;
  288. top: 0;
  289. width: 100%;
  290. height: 100vh;
  291. padding-bottom: 100rpx;
  292. display: flex;
  293. justify-content: center;
  294. flex-direction: column;
  295. align-items: center;
  296. background: #fff;
  297. .emptyImg {
  298. width: 300rpx;
  299. height: 250rpx;
  300. margin-bottom: 30rpx;
  301. }
  302. .empty-tips {
  303. display: flex;
  304. font-size: $font-sm + 2rpx;
  305. color: $font-color-disabled;
  306. .navigator {
  307. color: $uni-color-primary;
  308. margin-left: 16rpx;
  309. }
  310. }
  311. }
  312. }
  313. .box {
  314. max-height: 700rpx;
  315. position: relative;
  316. background: #ffffff;
  317. border-radius: 20rpx 20rpx 0rpx 0rpx;
  318. .top {
  319. height: 90rpx;
  320. width: 100%;
  321. display: flex;
  322. justify-content: space-between;
  323. align-items: center;
  324. padding: 0 30rpx 0 36rpx;
  325. .top-left {
  326. display: flex;
  327. justify-content: flex-start;
  328. align-items: center;
  329. .quan {
  330. width: 24rpx;
  331. height: 24rpx;
  332. border: 1px solid #ee2f72;
  333. border-radius: 50%;
  334. position: relative;
  335. flex-shrink: 0;
  336. .qzq {
  337. position: absolute;
  338. top: 50%;
  339. left: 50%;
  340. margin: -8rpx 0 0 -8rpx;
  341. width: 12rpx;
  342. height: 12rpx;
  343. background: #ee2f72;
  344. border-radius: 50%;
  345. }
  346. }
  347. .top-font {
  348. margin-left: 10rpx;
  349. font-size: 28rpx;
  350. font-family: PingFang SC;
  351. font-weight: 500;
  352. color: #333333;
  353. }
  354. }
  355. .top-right {
  356. font-size: 28rpx;
  357. font-family: PingFang SC;
  358. font-weight: 500;
  359. color: #999999;
  360. display: flex;
  361. align-items: center;
  362. image {
  363. width: 28rpx;
  364. height: 28rpx;
  365. }
  366. .right-clear {
  367. margin-left: 6rpx;
  368. }
  369. }
  370. }
  371. .scoroll {
  372. max-height: 600rpx;
  373. }
  374. }
  375. .db {
  376. width: 750rpx;
  377. height: 150rpx;
  378. }
  379. /* 购物车列表项 */
  380. .cart-item {
  381. position: relative;
  382. display: flex;
  383. padding: 18rpx 29rpx 60rpx 30rpx;
  384. border-top: 1px solid #eeeeee;
  385. .image-wrapper {
  386. width: 160rpx;
  387. height: 160rpx;
  388. flex-shrink: 0;
  389. position: relative;
  390. image {
  391. border-radius: 2rpx;
  392. }
  393. }
  394. .checkbox {
  395. position: absolute;
  396. left: -16rpx;
  397. top: -16rpx;
  398. z-index: 8;
  399. font-size: 44rpx;
  400. line-height: 1;
  401. padding: 4rpx;
  402. color: $font-color-disabled;
  403. background: #fff;
  404. border-radius: 50px;
  405. }
  406. .item-right {
  407. display: flex;
  408. flex-direction: column;
  409. flex: 1;
  410. overflow: hidden;
  411. position: relative;
  412. padding-left: 30rpx;
  413. .title,
  414. .price {
  415. font-size: $font-base + 2rpx;
  416. color: #ee2f72;
  417. height: 40rpx;
  418. line-height: 40rpx;
  419. }
  420. .attr {
  421. font-size: $font-sm + 2rpx;
  422. color: $font-color-light;
  423. height: 50rpx;
  424. line-height: 50rpx;
  425. }
  426. .price {
  427. height: 50rpx;
  428. line-height: 50rpx;
  429. }
  430. .step {
  431. margin-top: 20rpx;
  432. }
  433. }
  434. .item-num {
  435. position: absolute;
  436. bottom: 60rpx;
  437. right: 30rpx;
  438. .munbox {
  439. width: 144rpx;
  440. height: 44rpx;
  441. input {
  442. display: inline-block;
  443. text-align: center;
  444. }
  445. image {
  446. flex-shrink: 0;
  447. width: 44rpx;
  448. height: 44rpx;
  449. }
  450. }
  451. }
  452. }
  453. /* 底部栏 */
  454. .action-section {
  455. /* #ifdef H5 */
  456. margin-bottom: 100rpx;
  457. /* #endif */
  458. position: fixed;
  459. bottom: 0;
  460. z-index: 1000;
  461. display: flex;
  462. align-items: center;
  463. width: 100%;
  464. height: 100rpx;
  465. background: #ffffff;
  466. .total-box {
  467. width: 490rpx;
  468. height: 100rpx;
  469. background: #fff6e1;
  470. padding-left: 50rpx;
  471. display: flex;
  472. align-items: center;
  473. .bg {
  474. position: relative;
  475. width: 56rpx;
  476. height: 58rpx;
  477. image {
  478. width: 100%;
  479. height: 100%;
  480. }
  481. .num {
  482. position: absolute;
  483. top: -10rpx;
  484. right: -10rpx;
  485. min-width: 30rpx;
  486. padding: 0 5rpx;
  487. display: inline-block;
  488. height: 30rpx;
  489. background: #ff7144;
  490. border-radius: 50%;
  491. font-size: 24rpx;
  492. font-family: PingFang SC;
  493. font-weight: bold;
  494. color: #ffffff;
  495. text-align: center;
  496. line-height: 30rpx;
  497. }
  498. }
  499. .total-price {
  500. font-size: 24rpx;
  501. font-family: PingFang SC;
  502. font-weight: bold;
  503. color: #ee2f72;
  504. margin-left: 30rpx;
  505. text {
  506. font-size: 32rpx;
  507. }
  508. }
  509. }
  510. .confirm-btn {
  511. border-radius: 0;
  512. width: 260rpx;
  513. height: 100rpx;
  514. background: #ee2f72;
  515. text-align: center;
  516. line-height: 100rpx;
  517. font-size: 38rpx;
  518. font-family: PingFang SC;
  519. font-weight: 500;
  520. color: #ffffff;
  521. }
  522. }
  523. /* 复选框选中状态 */
  524. .action-section .checkbox.checked,
  525. .cart-item .checkbox.checked {
  526. color: $base-color;
  527. }
  528. </style>