cart.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <view style="height: 100vh;background-color: #F5F5F5;">
  3. <van-skeleton :loading="loading">
  4. <view class="titleright" @click="changeShop" v-if="isdelete">编辑</view>
  5. <view class="titleright" @click="changeShop" v-else>取消</view>
  6. <view v-if="list.length > 0" style="margin-bottom: 150upx;">
  7. <scroll-view
  8. style="height: 88vh;"
  9. class="floor-list"
  10. :scroll-top="scrollTop"
  11. scroll-y="true"
  12. @scroll="scroll"
  13. @scrolltoupper="upper"
  14. @scrolltolower="lower"
  15. :refresher-enabled="false">
  16. <view class="cart-list">
  17. <view style="display: flex;align-items: center;padding: 20upx 36upx; border-bottom: 1px solid #F5F5F5;">
  18. <image :src="pictureUrl + '/uploads/home/store/' + store.store_id + '/' + store.store_banner" style="border-radius: 50%;width: 50upx;height: 50upx;"></image>
  19. <span style="padding-left: 8upx;">{{ store.store_name }}</span>
  20. </view>
  21. <view v-for="(item, index) in list" :key="index" class="shoplist" style="background-color: #fff;">
  22. <view style="border-radius: 100%;">
  23. <van-checkbox-group :value="checkboxList" @change="checkboxChange()">
  24. <van-checkbox checked-color="#D9332E" :name="index"></van-checkbox>
  25. </van-checkbox-group>
  26. </view>
  27. <view style="flex:1;position: relative;overflow: hidden;">
  28. <van-card style="width: 100%;"
  29. title-class="changewidth"
  30. price-class="priceClass"
  31. :price="item.goods_price"
  32. :desc="item.descire + 'X' + item.goods_num"
  33. :title="item.desarray"
  34. :thumb="pictureUrl + '/uploads/home/store/goods/' + item.goods_image.substr(0, item.goods_image.indexOf('\_')) + '/' + item.goods_image">
  35. </van-card>
  36. <uninumberbox class="step" :min="1" :value="item.goods_num" :index="index" @eventChange="bindChange"></uninumberbox>
  37. </view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. <view v-else>
  43. <view style="margin: 120upx auto;text-align: center;">
  44. <van-icon size="160rpx" color="#909399" name="cart-o" />
  45. <view @click="gostore" style="text-align: center; color: #909399;border: 1px solid #909399; padding: 5upx 10upx; margin: 10upx auto;width: 150rpx;">
  46. 去逛逛
  47. </view>
  48. </view>
  49. </view>
  50. <view style="position: fixed;bottom: 0;border-top:1px solid #eee;height: 8vh;">
  51. <van-submit-bar v-if="isdelete" :price="prices" :button-text="listip" @submit="onClickButton" :tip="true">
  52. <van-checkbox checked-color="#D9332E" :value="isAll" shape="round" @change="onChange">全选</van-checkbox>
  53. </van-submit-bar>
  54. <van-submit-bar bar-class="betweenclass" v-if="!isdelete" :button-text="listip" @submit="onClickButton" :tip="true">
  55. <van-checkbox checked-color="#D9332E" :value="isAll" shape="round" @change="onChange">全选</van-checkbox>
  56. </van-submit-bar>
  57. </view>
  58. </van-skeleton>
  59. </view>
  60. </template>
  61. <script>
  62. import uniNavBar from '@/component/uni-nav-bar/uni-nav-bar.vue';
  63. import uninumberbox from '@/component/uinNumberCart.vue';
  64. export default {
  65. components: {
  66. uniNavBar,
  67. uninumberbox
  68. },
  69. data() {
  70. return {
  71. firstloading: 0,
  72. loading: true,
  73. ismax: 0,
  74. lengthnumber: 0,
  75. pricelist: [],
  76. pictureUrl: this.pictureUrl,
  77. scrollTop: 0,
  78. old: {
  79. scrollTop: 0
  80. },
  81. nums: 0,
  82. store: {},
  83. ispull: true,
  84. page: 1,
  85. store_id: 0,
  86. prices: 0,
  87. listip: '',
  88. isdelete: true,
  89. isAll: false,
  90. checkboxList: [],
  91. list: []
  92. };
  93. },
  94. // 隐藏分享菜单
  95. onLoad: function(options) {
  96. if(options.store_id){
  97. this.store_id = options.store_id;
  98. this.listip = '结算' + '(' + this.nums + ')';
  99. this.getshoplist();
  100. }
  101. },
  102. methods: {
  103. // 返回酒店
  104. gostore() {
  105. uni.navigateBack();
  106. },
  107. onClickButton(e) {
  108. let array = [];
  109. if (this.pricelist.length == 0) {
  110. if (this.isdelete) {
  111. this.$msg('请选择商品');
  112. } else {
  113. this.$msg('请选择删除商品');
  114. }
  115. return;
  116. }
  117. this.pricelist.forEach(item => {
  118. array.push(item.goods_id);
  119. });
  120. let ids = array.toString();
  121. if (this.isdelete) {
  122. // 结算
  123. this.getorderId(); // 保存订单生成订单id 购买
  124. } else {
  125. this.deleteshop(ids);
  126. }
  127. },
  128. // 保存订单生成订单id
  129. getorderId() {
  130. let goods = [];
  131. let prices = this.prices;
  132. this.pricelist.forEach((item, index) => {
  133. goods.push({});
  134. goods[index].goods_id = item.goods_id;
  135. goods[index].buy_num = item.goods_num;
  136. });
  137. this.request({
  138. url: '/v1/order/save_og',
  139. method: 'post',
  140. data: {
  141. store_id: this.store.store_id,
  142. goods: JSON.stringify(goods)
  143. },
  144. success: res => {
  145. if (res.data.code == 1000) {
  146. let order_id = res.data.data.order_id;
  147. let store = this.store.store_id;
  148. uni.navigateTo({
  149. url: `/pages/index/sureBuy?store=${store}&prices=${prices}&order_id=${order_id}`
  150. });
  151. }
  152. }
  153. });
  154. },
  155. // 购物车删除
  156. deleteshop(ids) {
  157. this.request({
  158. url: '/v2/order/cart_drop',
  159. method: 'post',
  160. data: {
  161. ids: ids,
  162. store_id: this.store_id
  163. },
  164. success: (res) => {
  165. console.log(res);
  166. if(res.data.code==1000){
  167. this.nums = 0;
  168. this.listip = '删除' + '(' + this.nums + ')';
  169. this.prices=0;
  170. this.checkboxList = [];
  171. this.getshoplist();
  172. }
  173. }
  174. });
  175. },
  176. // 滚动到顶部
  177. upper(e) {
  178. console.log('顶部');
  179. },
  180. // 滚到底部
  181. lower(e) {
  182. // this.page = this.page + 1;
  183. // if(this.ispull) {
  184. // this.getshoplist();
  185. // }
  186. },
  187. // 滚动时触发
  188. scroll(e) {
  189. this.old.scrollTop = e.detail.scrollTop;
  190. },
  191. changeShop() {
  192. this.isdelete = !this.isdelete;
  193. this.listip = this.isdelete ? '结算' + '(' + this.nums + ')' : '删除' + '(' + this.nums + ')';
  194. },
  195. getshoplist() {
  196. this.loading = true;
  197. this.request({
  198. url: '/v2/order/cart',
  199. method: 'post',
  200. data: {
  201. store_id: this.store_id,
  202. page: this.page
  203. },
  204. success: res => {
  205. let { data } = res.data;
  206. this.store = data.store;
  207. data.cart_list.forEach(item => {
  208. let arr = item.goods_name.split(' ');
  209. item.desarray = arr[0];
  210. item.descire = item.goods_name.replace(arr[0], '');
  211. });
  212. this.list =data.cart_list;
  213. console.log(this.list);
  214. this.loading = false;
  215. }
  216. });
  217. },
  218. // 返回上层
  219. goback() {
  220. uni.navigateBack();
  221. },
  222. // 数量累加
  223. bindChange(e) {
  224. this.list[e.index].goods_num = e.number;
  225. this.checkoutprice();
  226. },
  227. // 单选
  228. checkboxChange(e) {
  229. this.checkboxList = [];
  230. e.detail.forEach((item, idx) => {
  231. this.$set(this.checkboxList, idx, item);
  232. });
  233. let array = this.list;
  234. let price = [];
  235. this.checkboxList.forEach(item => {
  236. price.push(array[item]);
  237. });
  238. this.pricelist = price;
  239. let num = 0;
  240. this.pricelist.forEach(item => {
  241. num += item.goods_num;
  242. });
  243. this.nums = num;
  244. this.lengthnumber = this.pricelist.length;
  245. this.listip = this.isdelete ? '结算' + '(' + this.nums + ')' : '删除' + '(' + this.nums + ')';
  246. this.checkoutprice();
  247. },
  248. // 计算价格
  249. checkoutprice() {
  250. let total = 0;
  251. let num = 0;
  252. this.pricelist.forEach(item => {
  253. total += item.goods_price * item.goods_num;
  254. num += item.goods_num;
  255. });
  256. this.nums = num;
  257. this.prices = Number(total.toFixed(2)) * 100;
  258. this.listip = this.isdelete ? '结算' + '(' + this.nums + ')' : '删除' + '(' + this.nums + ')';
  259. },
  260. // 全选
  261. onChange(a) {
  262. this.isAll = !this.isAll;
  263. let items = this.list;
  264. if (this.isAll) {
  265. items.forEach((item, idx) => {
  266. this.$set(this.checkboxList, idx, idx.toString());
  267. });
  268. this.pricelist = this.list;
  269. let num = 0;
  270. this.pricelist.forEach(item => {
  271. num += item.goods_num;
  272. });
  273. this.nums = num;
  274. this.listip = this.isdelete ? '结算' + '(' + this.nums + ')' : '删除' + '(' + this.nums + ')';
  275. this.checkoutprice();
  276. } else {
  277. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  278. this.checkboxList = [];
  279. this.pricelist = [];
  280. this.nums = 0;
  281. this.listip = this.isdelete ? '结算' + '(' + this.nums + ')' : '删除' + '(' + this.nums + ')';
  282. this.checkoutprice();
  283. }
  284. }
  285. }
  286. }
  287. };
  288. </script>
  289. <style>
  290. .betweenclass {
  291. display: flex;
  292. justify-content: space-between !important;
  293. }
  294. .uniNavBar {
  295. height: 100%;
  296. }
  297. .cart-list {
  298. background-color: #fff;
  299. /* padding: 0 36upx; */
  300. }
  301. .titleright {
  302. text-align: right;
  303. padding: 10upx 30upx;
  304. background: #fff;
  305. border-bottom: 1px solid #eee;
  306. height: 4vh;
  307. }
  308. .changewidth {
  309. color: #303133;
  310. font-size: 32upx;
  311. font-weight: 100 !important;
  312. width: 100%;
  313. white-space: nowrap;
  314. overflow: hidden;
  315. text-overflow: ellipsis;
  316. }
  317. .van-card {
  318. background-color: #fff !important;
  319. overflow: hidden;
  320. }
  321. .shoplist {
  322. display: flex;
  323. padding: 0 35upx;
  324. justify-content: space-between;
  325. align-items: center;
  326. padding-bottom: 25upx;
  327. }
  328. .checklabel {
  329. transform: scale(0.7);
  330. border-radius: 100%;
  331. }
  332. .checkboxteil {
  333. transform: scale(0.7);
  334. border-radius: 100%;
  335. }
  336. .uni-numbox {
  337. left: auto !important;
  338. right: 0 !important;
  339. }
  340. .isdiaplay .van-submit-bar__text {
  341. visibility: hidden;
  342. }
  343. .isdiaplay {
  344. visibility: hidden;
  345. }
  346. </style>