index.vue 15 KB

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