index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <view class="wrap">
  3. <navbar v-model="screenHeight" title-color="#ffffff" background="#1b43c4" :iSimmersive="false" :isPrev="false" :placeholder="true" title="购物车"></navbar>
  4. <view class="list-wrap">
  5. <mescroll-body
  6. ref="mescrollRef"
  7. @init="mescrollInit"
  8. @down="downCallback"
  9. @up="upCallback"
  10. :down="downOption"
  11. :up="upOption"
  12. :height="screenHeight+'px'"
  13. >
  14. <checkbox-group @change="checkboxChange">
  15. <view class="list-box">
  16. <view
  17. class="list-item"
  18. v-for="item in goods"
  19. :key="item.id"
  20. >
  21. <view class="left-pic">
  22. <view class="item-check">
  23. <checkbox :value="item.id+''" :checked="item.checked" />
  24. </view>
  25. <view class="pic" @click="$utils.navigateTo('goods/view',{id: item.goods_id})">
  26. <image :src="item.photo"></image>
  27. </view>
  28. </view>
  29. <view class="goods">
  30. <view class="t" @click="$utils.navigateTo('goods/view',{id: item.goods_id})">{{ item.title }}</view>
  31. <view class="m" @click="$utils.navigateTo('goods/view',{id: item.goods_id})">{{ item.attr }}</view>
  32. <view class="b">
  33. <view>¥{{ item.price }}</view>
  34. <view>
  35. <uni-number-box
  36. :min="1"
  37. :max="item.nums"
  38. :value="item.goods_nums"
  39. :disabled="item.disabled"
  40. @change="stepperNum($event,item)"
  41. />
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </checkbox-group>
  48. </mescroll-body>
  49. <view class="cart-bottom" v-if="goods.length > 0">
  50. <view class="check-box">
  51. <checkbox-group @change="checkboxAll">
  52. <checkbox :checked="isChecked" />
  53. </checkbox-group>
  54. </view>
  55. <view v-if="isDeleteBtn" class="deleteGoods" @click="deleteGoods">删除</view>
  56. <view class="cart-bottom-text">
  57. <view>合计:</view>
  58. <view>
  59. <text>¥</text>
  60. <text>{{total}}</text>
  61. </view>
  62. </view>
  63. <view class="btn" @click="onSubmit">
  64. 提交订单
  65. </view>
  66. </view>
  67. </view>
  68. <authorize v-model="isAuthShow"></authorize>
  69. </view>
  70. </template>
  71. <script>
  72. import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
  73. import navbar from "@/components/navbar/navbar";
  74. import authorize from "@/components/authorize/authorize";
  75. export default {
  76. mixins: [MescrollMixin],
  77. components: {
  78. navbar,authorize
  79. },
  80. data(){
  81. return {
  82. screenHeight: 0,
  83. isAuthShow: false,
  84. isDeleteBtn:false,
  85. isChecked: false,
  86. goods: [], // 数据列表
  87. array: [],
  88. total: 0.00,
  89. downOption: {
  90. auto: false
  91. },
  92. upOption: {
  93. use: true, // 是否启用上拉加载; 默认true
  94. auto: false, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
  95. isLock: false, // 是否锁定上拉加载,默认false;
  96. isBoth: true, // 上拉加载时,如果滑动到列表顶部是否可以同时触发下拉刷新;默认true,两者可同时触发;
  97. page: {
  98. num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
  99. size: 10, // 每页数据的数量
  100. time: null // 加载第一页数据服务器返回的时间; 防止用户翻页时,后台新增了数据从而导致下一页数据重复;
  101. },
  102. noMoreSize: 3, // 如果列表已无数据,可设置列表的总数量要大于等于5条才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看
  103. offset: 80, // 距底部多远时,触发upCallback(仅mescroll-uni生效, 对于mescroll-body则需在pages.json设置"onReachBottomDistance")
  104. bgColor: "transparent", // 背景颜色 (建议在pages.json中再设置一下backgroundColorTop)
  105. textColor: "gray", // 文本颜色 (当bgColor配置了颜色,而textColor未配置时,则textColor会默认为白色)
  106. textLoading: '加载中 ...', // 加载中的提示文本
  107. textNoMore: '-- END --', // 没有更多数据的提示文本
  108. empty: {
  109. use: true, // 是否显示空布局
  110. icon: this.$http.domain()+"static/images/cart-empty.png", // 图标路径
  111. tip: '购物车为空', // 提示
  112. btnText: '', // 按钮
  113. fixed: false, // 是否使用fixed定位,默认false; 配置fixed为true,以下的top和zIndex才生效 (transform会使fixed失效,最终会降级为absolute)
  114. top: "100rpx", // fixed定位的top值 (完整的单位值,如 "10%"; "100rpx")
  115. zIndex: 99 // fixed定位z-index值
  116. }
  117. },
  118. };
  119. },
  120. onShow() {
  121. this.$store.dispatch("usersStatus").then(()=>{
  122. this.isAuthShow = false;
  123. this.downCallback();
  124. }).catch(()=>{
  125. this.isAuthShow = true;
  126. this.mescroll.showEmpty();
  127. });
  128. },
  129. watch:{
  130. goods:{
  131. handler(newValue, oldValue){
  132. let total = 0.00;
  133. this.goods.forEach((item,index)=>{
  134. if(item.checked){
  135. total += parseFloat(item.price) * item.goods_nums;
  136. }
  137. });
  138. this.isDeleteBtn = total > 0;
  139. this.total = total;
  140. },
  141. deep: true
  142. }
  143. },
  144. methods: {
  145. deleteGoods(){
  146. let arr = [];
  147. this.goods = this.goods.filter((item)=>{
  148. if(item.checked){
  149. arr.push(item);
  150. }
  151. return !item.checked;
  152. });
  153. if(arr.length <= 0){
  154. return 0;
  155. }
  156. let id = [];
  157. arr.forEach((item)=>{
  158. id.push(item.id);
  159. })
  160. this.$http.deleteCart({
  161. id: id.join(",")
  162. }).then((res)=>{
  163. if(res.data > 0){
  164. uni.setTabBarBadge({ index: 2, text: res.data.toString() });
  165. }else{
  166. uni.removeTabBarBadge({ index: 2 });
  167. this.mescroll.showEmpty();
  168. }
  169. this.$store.commit("UPDATECART",res.data);
  170. }).catch((err)=>{});
  171. this.isChecked = false;
  172. return arr.length;
  173. },
  174. onSubmit(){
  175. let array = [];
  176. for(let i in this.goods){
  177. if(this.goods[i].checked){
  178. array.push(this.goods[i].id);
  179. }
  180. }
  181. if(array.length <= 0){
  182. this.$utils.msg("请选择需要结算的商品");
  183. return false;
  184. }
  185. this.$utils.navigateTo("cart/confirm",{
  186. id: array.join(","),
  187. type: "cart"
  188. });
  189. },
  190. checkboxChange: function (e) {
  191. let items = this.goods,values = e.detail.value;
  192. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  193. let item = items[i];
  194. if(values.includes(item.id+'')){
  195. this.$set(this.goods[i],'checked',true);
  196. }else{
  197. this.$set(this.goods[i],'checked',false);
  198. }
  199. }
  200. for(let i = 0; i<this.goods.length; i++){
  201. if(this.goods[i].checked){
  202. this.isChecked = true;
  203. break;
  204. }else{
  205. this.isChecked = false;
  206. }
  207. }
  208. },
  209. checkboxAll: function(e) {
  210. this.isChecked = !this.isChecked;
  211. this.setAllCheckbox(this.isChecked);
  212. },
  213. //全选设置
  214. setAllCheckbox: function(checked) {
  215. this.array = [];
  216. for(let i in this.goods){
  217. this.goods[i].checked = checked;
  218. }
  219. },
  220. /*下拉刷新的回调 */
  221. downCallback() {
  222. setTimeout(()=>{
  223. this.mescroll.resetUpScroll();
  224. },200);
  225. },
  226. triggerDownScroll(){
  227. this.mescroll.triggerDownScroll();
  228. },
  229. /* 上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认 10 */
  230. upCallback(page) {
  231. this.$http.getCartList({
  232. page: page.num
  233. }).then(result=>{
  234. this.mescroll.endByPage(result.data.list.length, result.data.total);
  235. if(result.status==1){
  236. if(page.num == 1) this.goods = [];
  237. result.data.list.forEach((item,index)=>{
  238. result.data.list[index]['checked'] = false;
  239. result.data.list[index]['disabled'] = false;
  240. })
  241. this.goods = this.goods.concat(result.data.list);
  242. }else if(result.status == -1){
  243. this.mescroll.endErr();
  244. }
  245. }).catch(error=>{
  246. this.mescroll.endErr();
  247. });
  248. },
  249. emptyClick(){
  250. this.$utils.switchTab("index/index");
  251. },
  252. /**
  253. * 更新商品数量
  254. * @param value 数量
  255. * @param detail 索引
  256. */
  257. stepperNum(value, item){
  258. for(let i in this.goods){
  259. if(this.goods[i].id==item.id){
  260. if(this.goods[i].goods_nums != value){
  261. this.$http.updateCartGoods({
  262. id: this.goods[i].goods_id,
  263. sku_id: this.goods[i].product_id,
  264. num: value
  265. }).then((res)=>{
  266. if(res.status){
  267. this.goods[i].goods_nums = value;
  268. this.$store.commit("UPDATECART",res.data.count);
  269. if(res.data.count > 0){
  270. uni.setTabBarBadge({ index: 3, text: res.data.count.toString() });
  271. }else{
  272. uni.removeTabBarBadge({ index: 3 })
  273. }
  274. }else{
  275. this.goods[i].disabled = true;
  276. }
  277. }).catch((error)=>{
  278. this.goods[i].disabled = true;
  279. });
  280. }
  281. break;
  282. }
  283. }
  284. return false;
  285. }
  286. }
  287. }
  288. </script>
  289. <style lang="scss">
  290. page { background-color: #fff; }
  291. .cart-bottom{
  292. width: 100%;
  293. height: 100rpx;
  294. position: fixed;
  295. /* #ifdef APP-PLUS || MP */
  296. bottom: 0px;
  297. /* #endif */
  298. /* #ifdef H5 */
  299. bottom: 50px;
  300. /* #endif */
  301. left: 0;
  302. border-top: 2rpx solid #eee;
  303. background-color: #fff;
  304. .check-box{
  305. float: left;
  306. height: 100rpx;
  307. line-height: 100rpx;
  308. padding-left: 45rpx;
  309. }
  310. .deleteGoods {
  311. float: left;
  312. height: 100rpx;
  313. line-height: 110rpx;
  314. padding-left: 10rpx;
  315. color: #666;
  316. }
  317. .cart-bottom-text{
  318. padding-right: 240rpx;
  319. color: #323233;
  320. text-align: right;
  321. font-size: 24rpx;
  322. float: right;
  323. height: 100rpx;
  324. line-height: 100rpx;
  325. view{
  326. display: inline-block;
  327. &:last-child {
  328. color: #ee0a24;
  329. text {
  330. color: #ee0a24;
  331. &:last-child {
  332. font-size: 34rpx;
  333. padding-left: 5rpx;
  334. }
  335. }
  336. }
  337. }
  338. }
  339. .btn{
  340. width: 200rpx;
  341. height: 80rpx;
  342. line-height: 80rpx;
  343. border: none;
  344. position: absolute;
  345. display: inline-block;
  346. margin: 0;
  347. padding: 0;
  348. font-size: 30rpx;
  349. text-align: center;
  350. border-radius: 50rpx;
  351. cursor: pointer;
  352. transition: opacity .2s;
  353. top:10rpx;
  354. right: 20rpx;
  355. color: #fff;
  356. background-color: #ee0a24;
  357. background: linear-gradient(90deg,#5a80fa,#3563f4);
  358. }
  359. }
  360. .wrap{
  361. width: 100%;
  362. background: #fafafa;
  363. }
  364. .list-box{
  365. display: flex;
  366. flex-wrap: wrap;
  367. flex-direction: column;
  368. background: #fafafa;
  369. .list-item{
  370. width: 88%;
  371. height: 180rpx;
  372. margin: 0 auto;
  373. background-color: #fff;
  374. margin-bottom: 20rpx;
  375. border-radius: 10rpx;
  376. font-size: 28rpx;
  377. padding: 20rpx 20rpx;
  378. position: relative;
  379. &:first-child{
  380. margin-top: 20rpx;
  381. }
  382. .left-pic {
  383. position: absolute;
  384. left: 0; top:0; z-index: 999;
  385. width: 240rpx; height: 220rpx;
  386. .item-check{
  387. float: left;
  388. width: 25rpx;
  389. height: 25px;
  390. position: relative;
  391. margin-top: 80rpx; margin-left: 15rpx;
  392. }
  393. .pic{
  394. float: right; margin-top: 30rpx;
  395. width: 160rpx;
  396. image {
  397. width: 160rpx;
  398. height: 160rpx;
  399. }
  400. }
  401. }
  402. .goods{
  403. float: right;
  404. padding-left: 240rpx;
  405. font-size: 28rpx;
  406. color: #333;
  407. .t{
  408. width: 100%;
  409. min-height: 50rpx;
  410. max-height: 80rpx;
  411. display: -webkit-box;
  412. overflow: hidden;
  413. -webkit-line-clamp: 2;
  414. -webkit-box-orient: vertical;
  415. }
  416. .m{
  417. color: #666;
  418. font-size: 26rpx;
  419. display: -webkit-box;
  420. overflow: hidden;
  421. -webkit-line-clamp: 1;
  422. -webkit-box-orient: vertical;
  423. }
  424. .b{
  425. height: 60rpx;
  426. padding-top: 8rpx;
  427. view:first-child{
  428. float: left;
  429. line-height: 60rpx;
  430. color: red; font-size: 34rpx;
  431. }
  432. view:last-child{
  433. float: right;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. </style>