index.vue 14 KB

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