index.vue 11 KB

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