index.vue 11 KB

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