index.vue 12 KB

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