index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <view>
  3. <view class="to_bottom" v-if="!cartList.length"> -----还没有产品啦-----</view>
  4. <view class="car_list">
  5. <view class="car_item" @longpress="deleteCar(index)" v-for="(item,index) in cartList" :key="index">
  6. <view class="check_label" @click="checkedItem(index)" >
  7. <image class="checkbox" :src="item.checked?'../../static/icon/checked.png':'../../static/icon/checkbox.png'"></image>
  8. </view>
  9. <view class="box_left">
  10. <image class="car_image" :src="item.thumb" mode=""></image>
  11. </view>
  12. <view class="box_center">
  13. <view class="car_name">{{item.name}}</view>
  14. <view class="car_spec">{{item.spec}}</view>
  15. <view class="car_price">
  16. <text class="price">¥{{item.price}}</text>
  17. <text class="market_price">¥{{item.market_price}}</text>
  18. </view>
  19. </view>
  20. <view class="box_right">
  21. <view class="buy_num_box">
  22. <button class="buy_num_sub" @click="changeQuantity(index,-1)" data-eventsync="true">
  23. <image class="sub_icon" src="../../static/icon/sub_icon.png" mode=""></image>
  24. </button>
  25. <input type="number" class="buy_num" placeholder="数量" v-model="item.buy_num" @blur="changeQuantity(index,0)" ></input>
  26. <button class="buy_num_add" @click="changeQuantity(index,+1)" data-eventsync="true">
  27. <image class="add_icon" src="../../static/icon/add_icon.png" mode=""></image>
  28. </button>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="to_bottom" > -----到底啦-----</view>
  34. <view class="bottom_box">
  35. <view class="check_all_label" @click="checkAll()">
  36. <image class="checkbox" :src="checkedAll?'../../static/icon/checked.png':'../../static/icon/checkbox.png'" ></image>
  37. <text class="checkall">全选</text>
  38. </view>
  39. <view class="price_box">
  40. 合计:<text class="price_total">¥{{priceTotal}}</text>
  41. </view>
  42. <view class="to_order" @click="toOrder()" >下单</view>
  43. </view>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. // 购物车列表
  51. cartList:[],
  52. // 请求参数
  53. requestParam:{},
  54. // 是否全选
  55. checkedAll:false,
  56. // 总价
  57. priceTotal:'0.00',
  58. // 是否请求中
  59. isReqing:false,
  60. }
  61. },
  62. onLoad() {
  63. },
  64. onShow() {
  65. // 登录提示
  66. if( !this.$checkAccess.alterLogin() ) return ;
  67. // 请求中,不允许刷新
  68. if( this.isReqing ) return ;
  69. // 设置请求中
  70. this.isReqing = true;
  71. // 非全选
  72. this.checkedAll = 0;
  73. // 请求列表
  74. this.$http.request('api/shop_cart/get_list',this.requestParam).then((re)=>{
  75. // 设置非请求中
  76. this.isReqing = false;
  77. // 成功结果
  78. if( re.code == 'success' ){
  79. // 赋值
  80. this.cartList = re.data;
  81. // 计算价格
  82. this.priceHandler();
  83. }
  84. });
  85. },
  86. onPullDownRefresh() {
  87. // 登录提示
  88. if( !this.$checkAccess.alterLogin() ) return ;
  89. // 请求列表
  90. this.$http.request('api/shop_cart/get_list',this.requestParam).then((re)=>{
  91. if( re.code == 'success' ){
  92. // 赋值
  93. this.cartList = re.data;
  94. // 计算价格
  95. this.priceHandler();
  96. }
  97. });
  98. uni.stopPullDownRefresh();
  99. },
  100. onReachBottom() {
  101. },
  102. methods: {
  103. // 数量调整
  104. changeQuantity(index,number){
  105. // 如果不是0.表示两侧按钮点击,0表示输入的修改
  106. if( number != 0 ) {
  107. // 计算个数
  108. this.cartList[index].buy_num = this.cartList[index].buy_num + number;
  109. }
  110. // 如果大于库存
  111. if( this.cartList[index].buy_num > this.cartList[index].stock ) {
  112. // 设置为库存
  113. this.cartList[index].buy_num = this.cartList[index].stock;
  114. // 提示
  115. uni.showToast({
  116. title:"购买数量不能大于库存",
  117. icon:"none",
  118. })
  119. return ;
  120. }
  121. // 如果小于1.设置为1
  122. if( this.cartList[index].buy_num < 1 ) {
  123. // 恢复1
  124. this.cartList[index].buy_num = 1;
  125. // 提示
  126. uni.showToast({
  127. title:"数量不可小于1",
  128. icon:"none",
  129. })
  130. return ;
  131. }
  132. // 请求列表
  133. this.$http.request('api/shop_cart/edit',{id:this.cartList[index].id,buy_num:this.cartList[index].buy_num}).then((re)=>{
  134. if( re.code == 'success' ){
  135. // 计算价格
  136. this.priceHandler();
  137. }else{
  138. uni.showToast({
  139. title: re.msg,
  140. icon:"none"
  141. })
  142. }
  143. });
  144. },
  145. // 删除购物车
  146. deleteCar(index){
  147. uni.showModal({
  148. title:"是否删除?",
  149. success: (re) => {
  150. if(re.confirm){
  151. // 请求列表
  152. this.$http.request('api/shop_cart/del',{id:this.cartList[index].id}).then((re)=>{
  153. // 如果删除成功的话
  154. if( re.code == 'success' ){
  155. this.cartList.splice(index,1);
  156. // 计算价格
  157. this.priceHandler();
  158. }
  159. });
  160. }
  161. }
  162. })
  163. },
  164. checkedItem(index){
  165. // 默认全选
  166. let checkedAll = 1;
  167. // 单个设置选中/未选
  168. this.cartList[index].checked = this.cartList[index].checked ? 0 : 1;
  169. // 循环处理
  170. for (let i in this.cartList) {
  171. // 有未选的就不做全选
  172. if( !this.cartList[i].checked ) checkedAll = 0;
  173. }
  174. // 全选赋值
  175. this.checkedAll = checkedAll;
  176. // 计算价格
  177. this.priceHandler();
  178. },
  179. checkAll(){
  180. // 设置全选/单选
  181. this.checkedAll = this.checkedAll ? 0 : 1;
  182. // 循环处理
  183. for (let index in this.cartList) {
  184. this.cartList[index].checked = this.checkedAll;
  185. }
  186. // 计算价格
  187. this.priceHandler();
  188. },
  189. priceHandler(){
  190. // 总价格
  191. let priceTotal = 0;
  192. // 循环处理
  193. for (let index in this.cartList) {
  194. // 如果选中的
  195. if( this.cartList[index].checked ){
  196. priceTotal = this.$decimal.add(priceTotal,this.$decimal.mul(this.cartList[index].price,this.cartList[index].buy_num));
  197. }
  198. }
  199. // 小数点处理
  200. this.priceTotal = priceTotal.toFixed(2);
  201. },
  202. toOrder(){
  203. // 等待支付的信息
  204. let waitList = [];
  205. // 循环处理
  206. for (let index in this.cartList) {
  207. // 如果选中的
  208. if( this.cartList[index].checked ){
  209. // 如果库存不足
  210. if( this.cartList[index].buy_num < 1 ){
  211. uni.showToast({icon:"none",title:"选择的产品至少需要1个"})
  212. return ;
  213. }
  214. // 如果库存不足
  215. if( this.cartList[index].buy_num > this.cartList[index].stock ){
  216. uni.showToast({icon:"none",title:"产品库存不足"})
  217. return ;
  218. }
  219. waitList.push(this.cartList[index].id);
  220. }
  221. }
  222. // 如果没有选择
  223. if( !waitList.length ) {
  224. uni.showToast({icon:"none",title:"请选择需要结算的产品"})
  225. return ;
  226. }
  227. // 如果没有选择
  228. if( waitList.length > 99 ) {
  229. uni.showToast({icon:"none",title:"这么多产品一个预约单写不下哦"})
  230. return ;
  231. }
  232. uni.navigateTo({
  233. url:"/pages/car/order?cart_ids="+waitList.join(',')
  234. })
  235. }
  236. }
  237. }
  238. </script>
  239. <style lang="less">
  240. .car_list{
  241. display: block;
  242. overflow: hidden;
  243. margin: 0rpx auto;
  244. margin-top: 20rpx;
  245. .car_item{
  246. height: 180rpx;
  247. display: block;
  248. background: #FFFFFF;
  249. margin: 0rpx auto;
  250. margin-bottom: 20rpx;
  251. padding: 20rpx 0rpx;
  252. .check_label{
  253. float: left;
  254. width: 40rpx;
  255. height: 40rpx;
  256. display: block;
  257. margin-top: 10rpx;
  258. padding: 50rpx 20rpx;
  259. .checkbox{
  260. float: left;
  261. width: 40rpx;
  262. height: 40rpx;
  263. }
  264. }
  265. .box_left{
  266. float: left;
  267. width: 140rpx;
  268. height: 200rpx;
  269. margin-top: 10rpx;
  270. .car_image{
  271. width: 140rpx;
  272. height: 140rpx;
  273. border-radius: 5rpx;
  274. }
  275. }
  276. .box_center{
  277. float: left;
  278. width: 300rpx;
  279. margin-left: 25rpx;
  280. .car_name{
  281. max-height: 80rpx;
  282. font-size: 30rpx;
  283. line-height: 50rpx;
  284. overflow: hidden;
  285. }
  286. .car_spec{
  287. color: #999999;
  288. font-size: 24rpx;
  289. line-height: 60rpx;
  290. }
  291. .car_price{
  292. font-size: 30rpx;
  293. line-height: 60rpx;
  294. .price{
  295. color: red;
  296. }
  297. .market_price{
  298. font-size: 24rpx;
  299. color: #999999;
  300. margin-left: 10rpx;
  301. padding-left: 10rpx;
  302. text-decoration: line-through;
  303. border-left: 2rpx solid #DDDDDD;
  304. }
  305. }
  306. }
  307. .box_right{
  308. float: right;
  309. padding-right: 25rpx;
  310. width: 185rpx;
  311. .buy_num_box{
  312. float: right;
  313. color: #333333;
  314. overflow: hidden;
  315. font-size: 24rpx;
  316. margin-top: 70rpx;
  317. text-align: center;
  318. .buy_num_sub{
  319. float: left;
  320. border: none;
  321. height: 36rpx;
  322. background: none;
  323. text-align: center;
  324. line-height: 36rpx;
  325. padding: 10rpx 10rpx;
  326. padding-right: 15rpx;
  327. .sub_icon{
  328. width: 22rpx;
  329. height: 22rpx;
  330. display: block;
  331. }
  332. }
  333. .buy_num_sub::after{
  334. border: none;
  335. background: none;
  336. }
  337. .buy_num{
  338. float: left;
  339. width: 90rpx;
  340. height: 36rpx;
  341. font-size: 24rpx;
  342. min-height: 36rpx;
  343. line-height: 36rpx;
  344. padding: 0rpx 0rpx;
  345. border-radius: 8rpx;
  346. border: 2rpx solid #dddddd;
  347. }
  348. .buy_num_add{
  349. float: left;
  350. border: none;
  351. height: 36rpx;
  352. background: none;
  353. text-align: center;
  354. padding: 10rpx 10rpx;
  355. padding-left: 15rpx;
  356. line-height: 36rpx;
  357. .add_icon{
  358. width: 22rpx;
  359. height: 22rpx;
  360. display: block;
  361. }
  362. }
  363. .buy_num_add::after{
  364. border: none;
  365. background: none;
  366. }
  367. }
  368. }
  369. }
  370. }
  371. .bottom_box{
  372. left: 0rpx;
  373. width: 100%;
  374. height: 100rpx;
  375. display: block;
  376. position: fixed;
  377. overflow: hidden;
  378. background: #FFFFFF;
  379. padding: 0rpx 35rpx;
  380. bottom: var(--window-bottom);
  381. .check_all_label{
  382. float: left;
  383. width: 120rpx;
  384. height: 40rpx;
  385. font-size: 24rpx;
  386. line-height: 40rpx;
  387. padding: 30rpx 0rpx;
  388. .checkbox{
  389. float: left;
  390. width: 40rpx;
  391. height: 40rpx;
  392. }
  393. .checkbox.active{
  394. border: 2rpx solid red;
  395. .checkbox_active{
  396. background-color: #E03519;
  397. }
  398. }
  399. .checkall{
  400. float: left;
  401. height: 40rpx;
  402. display: block;
  403. margin-left: 10rpx;
  404. line-height: 40rpx;
  405. }
  406. }
  407. .price_box{
  408. float: left;
  409. width: 400rpx;
  410. display: block;
  411. color: #666666;
  412. font-size: 26rpx;
  413. text-align: right;
  414. line-height: 100rpx;
  415. margin-right: 20rpx;
  416. .price_total{
  417. color: red;
  418. font-size: 30rpx;
  419. }
  420. }
  421. .to_order{
  422. float: left;
  423. width: 140rpx;
  424. height: 60rpx;
  425. display: block;
  426. color: #FFFFFF;
  427. font-size: 28rpx;
  428. margin-top: 20rpx;
  429. line-height: 60rpx;
  430. padding: 0rpx 0rpx;
  431. text-align: center;
  432. border-radius: 30rpx;
  433. background-color: #E03519;
  434. }
  435. }
  436. </style>