index.vue 11 KB

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