index.vue 11 KB

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