onlineOrder.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. <template>
  2. <view class="container">
  3. <!-- 空白页 -->
  4. <view v-if="!hasLogin || empty === true" class="empty">
  5. <image src="/static/error/emptyCart.png" class="emptyImg" mode="aspectFit"></image>
  6. <view v-if="hasLogin" class="empty-tips">
  7. 空空如也
  8. <navigator class="navigator" v-if="hasLogin" url="../index/index" open-type="switchTab">随便逛逛>
  9. </navigator>
  10. </view>
  11. <view v-else class="empty-tips">
  12. 空空如也
  13. <view class="navigator" @click="navToLogin">去登陆></view>
  14. </view>
  15. </view>
  16. <view v-else>
  17. <!-- 购物车头部 -->
  18. <view class="cart-hand flex">
  19. <view class="hand-tit">
  20. 购物车共 <text>{{ ' '+cartList.length}} 件</text>商品
  21. </view>
  22. <view class="hand-btn" @click="clearCart()">
  23. 清空购物车
  24. </view>
  25. </view>
  26. <!-- 列表 -->
  27. <view class="cart-list">
  28. <block v-for="(item, index) in cartList" :key="item.id">
  29. <view class="cart-item" :class="{ 'b-b': index !== cartList.length - 1 }">
  30. <view class="image-wrapper">
  31. <image :src="item.productInfo.image" :class="[item.loaded]" mode="aspectFill" lazy-load
  32. @load="onImageLoad('cartList', index)" @error="onImageError('cartList', index)"></image>
  33. <view class="iconfont iconroundcheckfill checkbox" :class="{ checked: item.checked }"
  34. @click="check('item', index)"></view>
  35. </view>
  36. <view class="item-right">
  37. <text class="clamp title">{{ item.productInfo.store_name }}</text>
  38. <!-- <text class="attr">{{ item.productInfo.attrInfo.suk }}</text> -->
  39. <text class="price">¥{{ item.productInfo.price }}</text>
  40. <!-- <uni-number-box
  41. class="step"
  42. :min="1"
  43. :max="item.productInfo.stock"
  44. :value="item.cart_num > item.productInfo.stock ? item.productInfo.stock : item.cart_num"
  45. :isMax="item.cart_num >= item.productInfo.stock ? true : false"
  46. :isMin="item.cart_num === 1"
  47. :index="index"
  48. @eventChange="numberChange"
  49. ></uni-number-box> -->
  50. <view class="munbox flex">
  51. <image src="../../static/icon/reduce.png" mode="" @click="reduce(item,index)"></image>
  52. <input type="number" :value="item.cart_num" disabled />
  53. <image src="../../static/icon/add.png" mode="" @click="add(item)"></image>
  54. </view>
  55. </view>
  56. <!-- <text class="del-btn iconfont iconclose" @click="deleteCartItem(index)"></text> -->
  57. </view>
  58. </block>
  59. </view>
  60. <!-- 底部菜单栏 -->
  61. <view class="action-section">
  62. <view class="checkbox">
  63. <view class="iconfont iconroundcheckfill icon-checked-box" @click="check('all')"
  64. :class="{ 'icon-checked': allChecked }"></view>
  65. <!-- <view class="clear-btn" @click="allChecked ? clearCart() : ''" :class="{ show: allChecked }"><text>清空</text></view> -->
  66. </view>
  67. <view class="total-box">
  68. <text class="price">合计:¥{{ total }}</text>
  69. <!-- <text class="coupon">
  70. 已优惠
  71. <text>74.35</text>
  72. </text> -->
  73. </view>
  74. <button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
  75. </view>
  76. </view>
  77. <view class="btm-btn">
  78. <view class="btn-item" @click="openQr">
  79. 扫码添加
  80. </view>
  81. <view class="btn-item btn-item1" @click="entQr">
  82. 输入条码
  83. </view>
  84. </view>
  85. <uni-popup ref="popuphx" class="agree-wrapper">
  86. <view class="hx-wrapper">
  87. <view class="hx-img">
  88. <image src="../../static/img/hxbg.png" mode=""></image>
  89. </view>
  90. <view class="hx-body">
  91. <view class="hx-title">
  92. 输入商品条码
  93. </view>
  94. <input type="text" v-model="code" placeholder="请输入条码" placeholder-class="hx-placeholder" />
  95. <view class="hx-btn" @click="confirmAdd">
  96. 确认添加
  97. </view>
  98. </view>
  99. <view class="hx-close" @click="close">
  100. <image src="../../static/icon/close.png" mode=""></image>
  101. </view>
  102. </view>
  103. </uni-popup>
  104. </view>
  105. </template>
  106. <script>
  107. import {
  108. getCartList,
  109. getCartNum,
  110. cartDel
  111. } from '@/api/cart.js';
  112. import {
  113. mapState
  114. } from 'vuex';
  115. import uniNumberBox from '@/components/uni-number-box.vue';
  116. import uniPopup from '@/components/uni-popup/uni-popup.vue'
  117. import {
  118. saveUrl,
  119. interceptor
  120. } from '@/utils/loginUtils.js';
  121. export default {
  122. components: {
  123. uniNumberBox,
  124. uniPopup
  125. },
  126. data() {
  127. return {
  128. total: 0, //总价格
  129. allChecked: false, //全选状态 true|false
  130. empty: false, //空白页现实 true|false
  131. cartList: [],
  132. code: '', //商品条码
  133. };
  134. },
  135. onShow() {
  136. // 只有登录时才加载数据
  137. if (this.hasLogin) {
  138. this.loadData();
  139. }
  140. },
  141. watch: {
  142. //显示空白页
  143. cartList(e) {
  144. let empty = e.length === 0 ? true : false;
  145. if (this.empty !== empty) {
  146. this.empty = empty;
  147. }
  148. }
  149. },
  150. computed: {
  151. ...mapState(['weichatObj']),
  152. ...mapState('user', ['hasLogin'])
  153. },
  154. methods: {
  155. reduce(item, index) {
  156. if (item.cart_num == 1) {
  157. uni.showModal({
  158. content: '删除该商品?',
  159. success: e => {
  160. if (e.confirm) {
  161. this.deleteCartItem(index)
  162. }
  163. }
  164. });
  165. } else {
  166. item.cart_num--
  167. this.newNumberChange(item)
  168. }
  169. },
  170. add(item) {
  171. console.log(item)
  172. if (item.productInfo.stock > item.cart_num) {
  173. item.cart_num++
  174. this.newNumberChange(item)
  175. } else {
  176. return
  177. }
  178. },
  179. //请求数据
  180. async loadData() {
  181. let obj = this;
  182. getCartList({})
  183. .then(function(e) {
  184. // 获取当前购物车物品增加数量
  185. let nub = obj.cartList.length;
  186. // 获取之前对象数组
  187. let aArray = obj.cartList.reverse();
  188. // 获取返回数据对象数组
  189. let bArray = e.data.valid.reverse();
  190. obj.cartList = bArray
  191. .map((item, ind) => {
  192. // 设置返回数据默认为勾选状态
  193. item.checked = true;
  194. // 获取相同数组之前对象的数据
  195. let carlist = aArray[ind];
  196. // 判断之前是否已经加载完毕
  197. if (carlist && carlist.loaded == 'loaded') {
  198. item.loaded = 'loaded';
  199. }
  200. return item;
  201. })
  202. .reverse();
  203. obj.calcTotal(); //计算总价
  204. })
  205. .catch(function(e) {
  206. console.log(e);
  207. });
  208. },
  209. //监听image加载完成
  210. onImageLoad(key, index) {
  211. // 修改载入完成后图片class样式
  212. this.$set(this[key][index], 'loaded', 'loaded');
  213. },
  214. //监听image加载失败
  215. onImageError(key, index) {
  216. this[key][index].image = '/static/error/errorImage.jpg';
  217. },
  218. // 跳转到登录页
  219. navToLogin() {
  220. // 保存地址
  221. saveUrl();
  222. // 登录拦截
  223. interceptor();
  224. },
  225. //选中状态处理
  226. check(type, index) {
  227. if (type === 'item') {
  228. this.cartList[index].checked = !this.cartList[index].checked;
  229. } else {
  230. const checked = !this.allChecked;
  231. const list = this.cartList;
  232. list.forEach(item => {
  233. item.checked = checked;
  234. });
  235. this.allChecked = checked;
  236. }
  237. this.calcTotal(type);
  238. },
  239. //数量
  240. numberChange(data) {
  241. let arr = this.cartList[data.index];
  242. arr.cart_num = data.number;
  243. getCartNum({
  244. id: arr.id,
  245. number: data.number
  246. })
  247. .then(e => {
  248. console.log(e);
  249. })
  250. .catch(function(e) {
  251. console.log(e);
  252. });
  253. this.calcTotal();
  254. },
  255. newNumberChange(item) {
  256. getCartNum({
  257. id: item.id,
  258. number: item.cart_num
  259. })
  260. .then(e => {
  261. console.log(e);
  262. })
  263. .catch(function(e) {
  264. console.log(e);
  265. });
  266. this.calcTotal();
  267. },
  268. //删除
  269. deleteCartItem(index) {
  270. let list = this.cartList;
  271. let row = list[index];
  272. let id = row.id;
  273. cartDel({
  274. ids: id
  275. });
  276. this.cartList.splice(index, 1);
  277. uni.hideLoading();
  278. this.calcTotal();
  279. },
  280. //清空
  281. clearCart() {
  282. uni.showModal({
  283. content: '清空购物车?',
  284. success: e => {
  285. if (e.confirm) {
  286. let st = this.cartList.map(e => {
  287. return e.id;
  288. });
  289. cartDel({
  290. ids: st.join(',')
  291. }).then(e => {
  292. console.log(e);
  293. });
  294. this.cartList = [];
  295. }
  296. }
  297. });
  298. },
  299. //计算总价
  300. calcTotal() {
  301. let list = this.cartList;
  302. if (list.length === 0) {
  303. this.empty = true;
  304. return;
  305. }
  306. let total = 0;
  307. let checked = true;
  308. list.forEach(item => {
  309. if (item.checked === true) {
  310. total += item.productInfo.price * item.cart_num;
  311. } else if (checked === true) {
  312. checked = false;
  313. }
  314. });
  315. this.allChecked = checked;
  316. this.total = Number(total.toFixed(2));
  317. },
  318. //创建订单
  319. createOrder() {
  320. let list = this.cartList;
  321. let goodsData = [];
  322. list.forEach(item => {
  323. if (item.checked) {
  324. goodsData.push(item.id);
  325. }
  326. });
  327. uni.navigateTo({
  328. url: '/pages/order/createOrder?id=' + goodsData.join(',')
  329. });
  330. },
  331. entQr() {
  332. this.$refs.popuphx.open()
  333. },
  334. close() {
  335. this.$refs.popuphx.close()
  336. this.code = ''
  337. },
  338. confirmAdd() {
  339. console.log('确认添加', this.code)
  340. this.close()
  341. },
  342. openQr() {
  343. let obj = this;
  344. // #ifndef H5
  345. uni.scanCode({
  346. success(e) {
  347. // obj.orderVerific(e.result);
  348. console.log(e.result)
  349. }
  350. });
  351. // #endif
  352. // #ifdef H5
  353. this.weichatObj.scanQRCode({
  354. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  355. scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
  356. success: function(res) {
  357. // obj.orderVerific(res.resultStr); // 当needResult 为 1 时,扫码返回的结果
  358. console.log(res.resultStr)
  359. }
  360. });
  361. // #endif
  362. },
  363. }
  364. };
  365. </script>
  366. <style lang="scss">
  367. .container {
  368. padding-bottom: 134rpx;
  369. background-color: $page-color-base;
  370. /* 空白页 */
  371. .empty {
  372. position: fixed;
  373. left: 0;
  374. top: 0;
  375. width: 100%;
  376. height: 100vh;
  377. padding-bottom: 100rpx;
  378. display: flex;
  379. justify-content: center;
  380. flex-direction: column;
  381. align-items: center;
  382. background: #fff;
  383. .emptyImg {
  384. width: 300rpx;
  385. height: 250rpx;
  386. margin-bottom: 30rpx;
  387. }
  388. .empty-tips {
  389. display: flex;
  390. font-size: $font-sm + 2rpx;
  391. color: $font-color-disabled;
  392. .navigator {
  393. color: $uni-color-primary;
  394. margin-left: 16rpx;
  395. }
  396. }
  397. }
  398. }
  399. /* 购物车列表项 */
  400. .cart-item {
  401. width: 710rpx;
  402. height: 210rpx;
  403. background: #FFFFFF;
  404. box-shadow: 0px 0px 10rpx 0rpx rgba(0, 0, 0, 0.1);
  405. border-radius: 10rpx;
  406. margin: 20rpx auto;
  407. display: flex;
  408. position: relative;
  409. padding: 30rpx 26rpx 30rpx 80rpx;
  410. .image-wrapper {
  411. width: 150rpx;
  412. height: 150rpx;
  413. flex-shrink: 0;
  414. position: relative;
  415. image {
  416. border-radius: 8rpx;
  417. }
  418. }
  419. .checkbox {
  420. display: flex;
  421. position: absolute;
  422. top: 0;
  423. bottom: 0;
  424. left: -65rpx;
  425. margin: auto 0;
  426. height: 50rpx;
  427. z-index: 8;
  428. font-size: 44rpx;
  429. line-height: 1;
  430. padding: 4rpx;
  431. color: $font-color-disabled;
  432. background: #fff;
  433. border-radius: 50px;
  434. }
  435. .item-right {
  436. display: flex;
  437. flex-direction: column;
  438. flex: 1;
  439. overflow: hidden;
  440. position: relative;
  441. padding-left: 30rpx;
  442. .munbox {
  443. width: 144rpx;
  444. height: 44rpx;
  445. position: absolute;
  446. bottom: 0;
  447. right: 0;
  448. input {
  449. display: inline-block;
  450. text-align: center;
  451. }
  452. image {
  453. flex-shrink: 0;
  454. width: 44rpx;
  455. height: 44rpx;
  456. }
  457. }
  458. .title,
  459. .price {
  460. font-size: $font-base + 2rpx;
  461. color: $font-color-dark;
  462. height: 40rpx;
  463. line-height: 40rpx;
  464. }
  465. .attr {
  466. font-size: $font-sm + 2rpx;
  467. color: $font-color-light;
  468. height: 50rpx;
  469. line-height: 50rpx;
  470. font-size: 26rpx;
  471. font-family: PingFang SC;
  472. font-weight: bold;
  473. color: #999999;
  474. }
  475. .price {
  476. // height: 50rpx;
  477. // line-height: 50rpx;
  478. padding-top: 20rpx;
  479. font-size: 34rpx;
  480. font-family: PingFang SC;
  481. font-weight: bold;
  482. color: #FF4C4C;
  483. }
  484. .step {
  485. margin-top: 20rpx;
  486. }
  487. .title {
  488. font-size: 34rpx;
  489. font-family: PingFang SC;
  490. font-weight: bold;
  491. color: #333333;
  492. }
  493. }
  494. .del-btn {
  495. padding: 4rpx 10rpx;
  496. font-size: 34rpx;
  497. height: 50rpx;
  498. color: $font-color-light;
  499. }
  500. }
  501. /* 底部栏 */
  502. .action-section {
  503. /* #ifdef H5 */
  504. margin-bottom: 97rpx;
  505. /* #endif */
  506. position: fixed;
  507. left: 0rpx;
  508. bottom: 0rpx;
  509. z-index: 95;
  510. display: flex;
  511. align-items: center;
  512. width: 750rpx;
  513. height: 100rpx;
  514. padding: 0 30rpx;
  515. // background: rgba(255, 255, 255, 0.9);
  516. background: #fff;
  517. // box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, 0.5);
  518. // border-radius: 16rpx;
  519. .checkbox {
  520. height: 52rpx;
  521. position: relative;
  522. .icon-checked-box {
  523. border-radius: 50rpx;
  524. background-color: #ffffff;
  525. width: 52rpx;
  526. height: 100%;
  527. position: relative;
  528. z-index: 5;
  529. font-size: 53rpx;
  530. line-height: 1;
  531. color: $font-color-light;
  532. &::after {
  533. content: '全选';
  534. width: 100rpx;
  535. position: absolute;
  536. top: 0;
  537. bottom: 0;
  538. margin: auto;
  539. line-height: 1.5;
  540. padding-left: 10rpx;
  541. font-size: 32rpx;
  542. font-family: PingFang SC;
  543. font-weight: bold;
  544. color: #767477;
  545. }
  546. }
  547. .icon-checked {
  548. color: $base-color;
  549. }
  550. }
  551. .clear-btn {
  552. position: absolute;
  553. left: 26rpx;
  554. top: 0;
  555. z-index: 4;
  556. width: 0;
  557. height: 52rpx;
  558. line-height: 52rpx;
  559. padding-left: 38rpx;
  560. font-size: $font-base;
  561. color: #fff;
  562. background: $font-color-disabled;
  563. border-radius: 0 50px 50px 0;
  564. opacity: 0;
  565. transition: 0.2s;
  566. &.show {
  567. opacity: 1;
  568. width: 120rpx;
  569. }
  570. }
  571. .total-box {
  572. flex: 1;
  573. display: flex;
  574. flex-direction: column;
  575. text-align: right;
  576. padding-right: 40rpx;
  577. color: #FF6F0F;
  578. font-weight: bold;
  579. .price {
  580. font-size: 32rpx;
  581. // font-size: $font-lg;
  582. // color: $font-color-dark;
  583. }
  584. .coupon {
  585. font-size: $font-sm;
  586. // color: $font-color-light;
  587. text {
  588. font-weight: bold;
  589. color: $font-color-dark;
  590. }
  591. }
  592. }
  593. .confirm-btn {
  594. padding: 0 38rpx;
  595. margin: 0;
  596. border-radius: 100px;
  597. height: 76rpx;
  598. line-height: 76rpx;
  599. font-size: $font-base + 2rpx;
  600. background: $base-color;
  601. font-weight: bold;
  602. }
  603. }
  604. /* 复选框选中状态 */
  605. .action-section .checkbox.checked,
  606. .cart-item .checkbox.checked {
  607. color: $base-color;
  608. }
  609. .cart-hand {
  610. width: 750rpx;
  611. height: 88rpx;
  612. background: #FFFFFF;
  613. font-size: 30rpx;
  614. font-family: PingFang SC;
  615. font-weight: bold;
  616. color: #333333;
  617. line-height: 88rpx;
  618. padding-left: 28rpx;
  619. padding-right: 26rpx;
  620. .hand-tit {
  621. text {
  622. color: #FF4C4C;
  623. }
  624. }
  625. .hand-btn {
  626. width: 164rpx;
  627. height: 62rpx;
  628. border: 2rpx solid #FF4C4C;
  629. border-radius: 31rpx;
  630. font-size: 26rpx;
  631. font-family: PingFang SC;
  632. font-weight: bold;
  633. color: #FF4C4C;
  634. line-height: 62rpx;
  635. text-align: center;
  636. }
  637. }
  638. .btm-btn {
  639. z-index: 95;
  640. display: flex;
  641. position: fixed;
  642. bottom: 0;
  643. text-align: center;
  644. font-size: 32rpx;
  645. font-family: PingFang SC;
  646. font-weight: bold;
  647. color: #FFFFFF;
  648. line-height: 97rpx;
  649. width: 750rpx;
  650. .btn-item {
  651. height: 100%;
  652. width: 50%;
  653. background-color: #31332d;
  654. }
  655. .btn-item1 {
  656. background-color: #5DBC7C;
  657. }
  658. }
  659. .cart-list {
  660. padding-bottom: 97rpx;
  661. }
  662. .hx-wrapper {
  663. width: 536rpx;
  664. height: 630rpx;
  665. position: relative;
  666. // background-color: #fff;
  667. .hx-img {
  668. width: 536rpx;
  669. height: 281rpx;
  670. image {
  671. width: 536rpx;
  672. height: 281rpx;
  673. }
  674. }
  675. .hx-close {
  676. position: absolute;
  677. left: 243rpx;
  678. bottom: -80rpx;
  679. width: 52rpx;
  680. height: 52rpx;
  681. image {
  682. width: 52rpx;
  683. height: 52rpx;
  684. }
  685. }
  686. .hx-body {
  687. width: 536rpx;
  688. height: 349rpx;
  689. background-color: #fff;
  690. border-radius: 0 0 10rpx 10rpx;
  691. .hx-title {
  692. width: 536rpx;
  693. font-size: 36rpx;
  694. font-weight: 500;
  695. color: #333333;
  696. line-height: 1;
  697. padding-top: 42rpx;
  698. text-align: center;
  699. }
  700. input {
  701. width: 439rpx;
  702. height: 68rpx;
  703. background: #DBF3E9;
  704. border-radius: 10rpx;
  705. margin: 39rpx auto 0;
  706. padding-left: 26rpx;
  707. .hx-placeholder {
  708. font-size: 26rpx;
  709. font-weight: 500;
  710. color: #52C696;
  711. }
  712. }
  713. .hx-btn {
  714. margin: 44rpx auto 0;
  715. width: 353rpx;
  716. height: 71rpx;
  717. background: #52C696;
  718. border-radius: 34rpx;
  719. font-size: 36rpx;
  720. font-weight: 500;
  721. color: #F8F9F9;
  722. line-height: 71rpx;
  723. text-align: center;
  724. }
  725. }
  726. }
  727. </style>