index.vue 12 KB

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