index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. <template>
  2. <view class="pages">
  3. <!-- head -->
  4. <view class="scrollPage-header">
  5. <view :style="{height:statusBarHeight+'px'}"></view>
  6. <view class="header-title">
  7. <view class="header-left" @click="goBack">
  8. <image style="width: 16px; height: 16px;margin-right:3px;" mode="aspectFit" src="../static/arrow-left.png"></image>
  9. <text>返回</text>
  10. </view>
  11. <view class="header-title"><text class="ellipsis-one" overflow="ellipsis">购物车</text></view>
  12. <view class="header-right"></view>
  13. </view>
  14. </view>
  15. <view class="cart-body">
  16. <!-- 内容 -->
  17. <scroll-view
  18. scroll-y
  19. class="cart-list"
  20. type="custom"
  21. @scrolltolower="onreachBottom">
  22. <sticky-header>
  23. <!-- 头部 -->
  24. <view class="cart-head" v-if="total>0">
  25. <view class="cart-head-left">共<text>{{total}}</text>款商品</view>
  26. <view class="cart-head-right">
  27. <view class="cart-head-btton blue" @click="editAll">
  28. <text>{{editFlag?'完成':'编辑'}}</text>
  29. </view>
  30. </view>
  31. </view>
  32. </sticky-header>
  33. <productItem
  34. v-for="(item, index) in list"
  35. :key="index"
  36. :index="index"
  37. :list="item"
  38. @changeQty="changeQty"
  39. @check="chooseItem"
  40. @remove="removeCart">
  41. </productItem>
  42. <!-- loading -->
  43. <view class="loading-bar" v-if="total>0 && status!='loading'">{{noDataText}}</view>
  44. <view class="empty-bar" v-if="total==0 && status!='loading'">
  45. <image mode="aspectFit" :src="empty.imgUrl"></image>
  46. <view>{{empty.tip}}</view>
  47. </view>
  48. </scroll-view>
  49. <!-- 底部 -->
  50. <view class="cart-footer" v-if="total>0">
  51. <view class="cart-footer-box">
  52. <view class="cart-footer-left">
  53. <view class="cart-footer-left-all" @click="chooseAll">
  54. <view class="cart-footer-left-all-icon">
  55. <image style="width: 24px;height: 24px;" mode="aspectFill" :src="'../static/'+(allChecked?'round_check_fill':'round')+'.png'"></image>
  56. </view>
  57. <view class="cart-footer-left-all-text">
  58. <text>全选</text>
  59. </view>
  60. </view>
  61. </view>
  62. <view class="cart-footer-right">
  63. <view class="cart-footer-left-price" v-if="!editFlag && totalAmount">
  64. <view class="cart-footer-left-price-text">
  65. <text>合计:</text>
  66. </view>
  67. <view class="cart-footer-left-price-num">
  68. <text>¥{{Number(totalAmount).toFixed(2)}}</text>
  69. </view>
  70. </view>
  71. <!-- <view class="cart-head-btton red" v-if="editFlag" @click="clearAll">
  72. <text>清空购物车</text>
  73. </view> -->
  74. <view class="cart-footer-right-button">
  75. <button class="btns" v-if="!editFlag" type="warn" size="mini" @click="settlement">去结算({{totalNum}})</button>
  76. <button v-else class="btns" type="warn" plain size="mini" @click="batchRemove">删除</button>
  77. </view>
  78. </view>
  79. </view>
  80. <view :style="{height:safeAreaBottom+'px'}"></view>
  81. </view>
  82. </view>
  83. </view>
  84. </template>
  85. <script>
  86. import {
  87. mapState,
  88. mapMutations,
  89. } from 'vuex'
  90. import productItem from '@/pagesB/cart/productitem.vue'
  91. import { getCartList, updateCart, deleteCart } from '@/api/cart.js'
  92. import { purchaseSave } from '@/api/purchase.js'
  93. export default {
  94. components:{
  95. productItem
  96. },
  97. onLoad() {
  98. // 计算区域高度
  99. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
  100. this.screenWidth = uni.getSystemInfoSync().windowWidth
  101. this.screenHeight = uni.getSystemInfoSync().windowHeight
  102. this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
  103. // 获取列表数据
  104. this.pageInit()
  105. },
  106. data() {
  107. return {
  108. status: 'loading',
  109. noDataText: '暂无活动',
  110. empty: {
  111. tip: '暂无产品',
  112. imgUrl: '/static/nodata.png'
  113. },
  114. // 查询条件
  115. pageNo: 1,
  116. pageSize: 5,
  117. list: [],
  118. total: 0,
  119. totalAmount: 0,
  120. totalNum: 0,
  121. editFlag: false,
  122. allChecked: false,
  123. screenWidth: 325,
  124. screenHeight: 0,
  125. statusBarHeight: 44,
  126. safeAreaBottom: 0,
  127. }
  128. },
  129. computed: {
  130. ...mapState(['hasLogin']),
  131. userInfo(){
  132. return this.$store.state.vuex_userInfo
  133. }
  134. },
  135. methods: {
  136. goBack(){
  137. uni.navigateBack()
  138. },
  139. pageInit(){
  140. this.pageNo = 1
  141. this.list = []
  142. this.getRow()
  143. },
  144. // 查询列表
  145. getRow (pageNo) {
  146. let _this = this
  147. if (pageNo) {
  148. this.pageNo = pageNo
  149. }
  150. let params = {
  151. pageNo: this.pageNo,
  152. pageSize: this.pageSize,
  153. queryWord: this.queryWord
  154. }
  155. this.status = "loading"
  156. getCartList(params).then(res => {
  157. if (res.status == 200) {
  158. const list = res.data.list
  159. const ret = []
  160. list.forEach(key => {
  161. ret.push({
  162. id: key.id,
  163. checked: this.allChecked,
  164. cartSn: key.cartSn,
  165. price: key.price,
  166. qty: key.qty,
  167. productSn: key.productSn,
  168. productName: key.productEntity.productName,
  169. productImage: key.productEntity.images,
  170. productCode: key.productEntity.code,
  171. productOrigCode: key.productEntity.origCode,
  172. unit: key.productEntity.unit,
  173. enabledFlag: key.enabledFlag
  174. })
  175. })
  176. _this.list.push(ret)
  177. _this.total = res.data.count || 0
  178. } else {
  179. _this.list = []
  180. _this.total = 0
  181. _this.noDataText = res.message
  182. }
  183. _this.status = _this.total>=_this.list.length ? "nomore" : 'loadmore'
  184. })
  185. },
  186. // scroll-view到底部加载更多
  187. onreachBottom() {
  188. // 判断是否最后一页
  189. const maxPages = Math.ceil(this.total / this.pageSize)
  190. if(this.pageNo >= maxPages){
  191. this.status = "nomore"
  192. this.noDataText = '没有更多了'
  193. }else{
  194. this.pageNo += 1
  195. this.getRow()
  196. }
  197. },
  198. // 根据cartSn查找
  199. getItemById(cartSn){
  200. const a = this.list.findIndex(k => k.find(s=> s.cartSn == cartSn))
  201. const b = a>=0 ? this.list[a].findIndex(s=> s.cartSn == cartSn) : -1
  202. return {p:a,s:b}
  203. },
  204. // 已选产品变更数量
  205. changeQty(val,cartSn){
  206. uni.showLoading({
  207. title: '加载中'
  208. })
  209. updateCart({
  210. cartSn: cartSn,
  211. qty: val
  212. }).then(res => {
  213. if(res.status == 200){
  214. const crow = this.getItemById(cartSn)
  215. if(crow.p>=0&&crow.s>=0){
  216. this.list[crow.p][crow.s].qty = val
  217. this.list[crow.p].splice()
  218. // 合计
  219. this.getChooseHeji()
  220. }
  221. }
  222. uni.hideLoading()
  223. })
  224. },
  225. // 选择
  226. chooseItem(cartSn){
  227. const crow = this.getItemById(cartSn)
  228. if(crow.p>=0&&crow.s>=0){
  229. this.list[crow.p][crow.s].checked = !this.list[crow.p][crow.s].checked
  230. // 合计
  231. this.getChooseHeji()
  232. // 判断是否全选
  233. if(this.list.every(key => key.every(item => item.checked))){
  234. this.allChecked = true
  235. }else{
  236. this.allChecked = false
  237. }
  238. }
  239. },
  240. // 编辑
  241. editAll(){
  242. this.editFlag = !this.editFlag
  243. this.list.forEach(key => {
  244. key.forEach(item => {
  245. item.edit = this.editFlag
  246. })
  247. })
  248. },
  249. // 清空购物车
  250. clearAll(){
  251. const _this = this
  252. uni.showModal({
  253. title: '提示',
  254. content: '确定清空购物车?',
  255. confirmText: '确定',
  256. success(res) {
  257. if(res.confirm){
  258. uni.showLoading({
  259. title: '正在清空...',
  260. mask: true
  261. })
  262. }
  263. }
  264. })
  265. },
  266. // 全选
  267. chooseAll(){
  268. this.allChecked = !this.allChecked
  269. this.list.forEach(key => {
  270. key.forEach(item => {
  271. item.checked = this.allChecked
  272. })
  273. })
  274. // 合计
  275. this.getChooseHeji()
  276. },
  277. //单个删除
  278. removeChoose(cartSn,isBatch){
  279. // 删除项
  280. const crow = this.getItemById(cartSn)
  281. if(crow.p>=0&&crow.s>=0){
  282. this.list[crow.p].splice(crow.s, 1);
  283. this.total = this.total - 1
  284. // 合计
  285. if(!isBatch){
  286. this.getChooseHeji()
  287. }
  288. }
  289. },
  290. // 批量删除
  291. batchRemove(){
  292. if(this.totalNum==0){
  293. uni.showToast({
  294. title: '请选择产品',
  295. icon: 'none'
  296. })
  297. return false
  298. }
  299. const _this = this
  300. uni.showModal({
  301. title: '提示',
  302. content: '确定删除商品吗?',
  303. confirmText: '确定',
  304. success(res) {
  305. if(res.confirm){
  306. const snList = []
  307. _this.list.forEach(item => {
  308. item.filter(k=>k.checked).forEach(s=>{
  309. snList.push(s.cartSn)
  310. })
  311. })
  312. // 删除购物车
  313. _this.removeCart(snList,true)
  314. }
  315. }
  316. })
  317. },
  318. // 删除商品
  319. removeCart(snList,isBatch){
  320. const _this = this
  321. uni.showLoading({
  322. title: '删除中'
  323. })
  324. deleteCart({cartSns:snList}).then(ret => {
  325. if(ret.status == 200){
  326. uni.showToast({
  327. title: '删除成功',
  328. icon: 'none'
  329. })
  330. snList.forEach(sn => {
  331. _this.removeChoose(sn,isBatch)
  332. })
  333. // 合计,批量操作
  334. if(isBatch){
  335. // 如果全选
  336. if(_this.allChecked){
  337. _this.list = []
  338. _this.allChecked = false
  339. }
  340. _this.getChooseHeji()
  341. }
  342. }
  343. uni.hideLoading()
  344. })
  345. },
  346. // 去结算
  347. settlement(){
  348. if(this.totalAmount>0){
  349. this.submitOrder()
  350. }else{
  351. uni.showToast({
  352. title: '请选择产品',
  353. icon: 'none'
  354. })
  355. }
  356. },
  357. // 去结算
  358. submitOrder(){
  359. uni.showLoading({
  360. title: '提交中...',
  361. mask: true
  362. })
  363. // 获取已选商品
  364. const detailList = []
  365. const cartSn = []
  366. this.list.forEach(key => {
  367. key.filter(item=>item.checked).forEach(item => {
  368. detailList.push({
  369. productSn:item.productSn,
  370. productCode:item.productCode,
  371. qty: item.qty,
  372. price:item.price,
  373. })
  374. cartSn.push(item.cartSn)
  375. })
  376. })
  377. purchaseSave({
  378. detailList: detailList
  379. }).then(res => {
  380. if(res.status == 200){
  381. // 删除已提交产品
  382. this.removeCart(cartSn,true)
  383. res.data.detailList = []
  384. this.$store.state.vuex_tempData = res.data
  385. uni.navigateTo({
  386. url: "/pagesB/procureOrder"
  387. })
  388. }else{
  389. uni.showToast({
  390. title: res.message,
  391. icon: 'none'
  392. })
  393. }
  394. })
  395. },
  396. // 计算总款数、合计、数量
  397. getChooseHeji(){
  398. this.totalAmount = 0
  399. this.totalNum = 0
  400. this.list.forEach(key => {
  401. key.filter(item=>item.checked).forEach(item => {
  402. this.totalAmount += item.price * item.qty // 总金额
  403. this.totalNum += item.qty // 总数量
  404. })
  405. })
  406. },
  407. }
  408. }
  409. </script>
  410. <style lang="less">
  411. .pages{
  412. height: 100vh;
  413. display: flex;
  414. flex-direction: column;
  415. .scrollPage-header{
  416. z-index: 1;
  417. .header-title{
  418. display: flex;
  419. align-items: center;
  420. justify-content: space-between;
  421. padding: 0 10px;
  422. height: 44px;
  423. background-color: #FFFFFF;
  424. box-shadow: 0px 1px 0px 0px #E6E6E6;
  425. .header-title{
  426. font-size: 16px;
  427. color: #333333;
  428. max-width: 70%;
  429. }
  430. .header-left{
  431. display: flex;
  432. align-items: center;
  433. text{
  434. font-size: 14px;
  435. color: #333333;
  436. margin-left: 5px;
  437. }
  438. }
  439. .header-right{
  440. width: 50px;
  441. }
  442. }
  443. }
  444. .cart-body{
  445. position: relative;
  446. flex-grow: 1;
  447. display: flex;
  448. flex-direction: column;
  449. .cart-head{
  450. display: flex;
  451. justify-content: space-between;
  452. align-items: center;
  453. padding: 0 20rpx;
  454. background-color: #FFFFFF;
  455. height: 40px;
  456. .cart-head-left{
  457. display: flex;
  458. align-items: center;
  459. font-size: 28rpx;
  460. color: #333333;
  461. height: 100%;
  462. text{
  463. color: #d81e06;
  464. margin: 0 10rpx;
  465. }
  466. }
  467. .cart-head-right{
  468. display: flex;
  469. align-items: center;
  470. }
  471. .red{
  472. color: #d81e06;
  473. }
  474. .blue{
  475. color: #2196F3;
  476. }
  477. .cart-head-btton{
  478. height: 100%;
  479. font-size: 28rpx;
  480. margin-right: 10px;
  481. }
  482. }
  483. .cart-list{
  484. flex-grow: 1;
  485. background-color: #F5F5F5;
  486. width: 100%;
  487. .loading-bar{
  488. text-align: center;
  489. padding: 10px 0;
  490. color: #999999;
  491. }
  492. .empty-bar{
  493. display: flex;
  494. flex-direction: column;
  495. align-items: center;
  496. justify-content: center;
  497. padding: 20px 0;
  498. image{
  499. width: 100px;
  500. height: 100px;
  501. }
  502. view{
  503. font-size: 14px;
  504. color: #999999;
  505. margin-top: 10px;
  506. }
  507. }
  508. }
  509. .cart-footer{
  510. width: 100%;
  511. background-color: #FFFFFF;
  512. .cart-footer-box{
  513. width: 100%;
  514. height: 100rpx;
  515. display: flex;
  516. justify-content: space-between;
  517. align-items: center;
  518. }
  519. .cart-footer-left{
  520. display: flex;
  521. align-items: center;
  522. padding: 0 20rpx 0 36rpx;
  523. .cart-footer-left-all{
  524. display: flex;
  525. align-items: center;
  526. .cart-footer-left-all-text{
  527. margin-left: 10rpx;
  528. font-size: 28rpx;
  529. color: #333333;
  530. }
  531. }
  532. }
  533. .cart-footer-right{
  534. padding: 0 20rpx;
  535. display: flex;
  536. align-items: center;
  537. justify-content: space-between;
  538. .cart-footer-left-price{
  539. display: flex;
  540. align-items: center;
  541. .cart-footer-left-price-text{
  542. font-size: 28rpx;
  543. color: #333333;
  544. }
  545. .cart-footer-left-price-num{
  546. font-size: 28rpx;
  547. color: #d81e06;
  548. }
  549. }
  550. .red{
  551. color: #d81e06;
  552. }
  553. .cart-head-btton{
  554. height: 100%;
  555. font-size: 28rpx;
  556. color: #333333;
  557. }
  558. .cart-footer-right-button{
  559. display: flex;
  560. align-items: center;
  561. margin-left: 20rpx;
  562. text-align: right;
  563. .btns{
  564. border-radius: 100rpx;
  565. height: 36px;
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. </style>