areacart.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. var _request = require('../../utils/request.js')
  2. var _handle = require('../../utils/handle.js')
  3. var validator = require('../../utils/validator.js')
  4. Page({
  5. data: {
  6. orderId: 0,
  7. count:1,
  8. cartList:[],
  9. cartCount:0,
  10. cartTotal:0,
  11. checkeditems:'',
  12. checkeditemsnum:'',
  13. checkedtotal:0,
  14. selectedall:false,
  15. submitlock:false,
  16. x:0,
  17. currentX: 0,
  18. perpage:10,
  19. nowpage:1,
  20. cartMore:true,
  21. showDialog: false,
  22. checkbox:'',
  23. },
  24. handleMovableChange: function(e) {
  25. // this.data.currentX = e.detail.x;
  26. this.data.currentX = e.detail.x;
  27. },
  28. handleTouchend: function(e) {
  29. this.isMove = true;
  30. let index = e.currentTarget.id;
  31. let clist = this.data.cartList;
  32. // if (this.data.currentX < -46) {
  33. //// this.data.x = -92;
  34. // clist[index]['movex'] = -100;
  35. // this.setData({
  36. // cartList: clist
  37. // });
  38. // } else {
  39. clist[index]['movex'] = 0;
  40. this.setData({
  41. cartList: clist
  42. });
  43. // }
  44. },
  45. onLoad: function (options) {
  46. // this.getCartList();
  47. this.getCartList();
  48. this.setData({submitlock: false});
  49. this.setData({selectedall: false});
  50. this.getIphoneX()
  51. },
  52. onShow: function () {
  53. // this.getCartListAndSelected();
  54. this.setData({submitlock: false})
  55. },
  56. getCartList:function(){
  57. console.log('getCartList')
  58. var that = this
  59. var url = 'v1/products?ptype=shop_sale&&page='+that.data.nowpage+'&&per_page='+that.data.perpage
  60. var params = {}
  61. var success = function (res) {
  62. console.log(res)
  63. if(res.data.list){
  64. for(let i in res.data.list){
  65. res.data.list[i]['movex'] = 0;
  66. res.data.list[i]['IsBuy'] = false;
  67. res.data.list[i]['count'] = 1;
  68. res.data.list[i]['overtype'] = res.data.list[i]['have_size'] ? false:true;
  69. }
  70. that.setData({
  71. cartMore: res.data.list_count > that.data.cartList.length,
  72. cartList: that.data.cartList.concat(res.data.list || []),
  73. cartCount: res.data.count,
  74. cartTotal: res.data.total
  75. })
  76. }else{
  77. wx.showToast({
  78. title: '请先授权登录~',
  79. icon: 'none',
  80. duration: 2000
  81. })
  82. }
  83. }
  84. _request.$get(url, params, success)
  85. },
  86. onReachBottom: function () {
  87. console.log('onreachbottom')
  88. if (this.data.cartMore) {
  89. var page = this.data.nowpage + 1
  90. this.setData({
  91. nowpage: page
  92. })
  93. this.getCartList()
  94. }
  95. },
  96. getCartListAndSelected:function(){
  97. console.log('getCartList')
  98. var that = this
  99. var url = 'v1/products?ptype=shop_sale&&page=1&&per_page=3'
  100. var params = {}
  101. var success = function (res) {
  102. console.log(res)
  103. for(let i in res.data.list){
  104. res.data.list[i]['movex'] = 0;
  105. res.data.list[i]['IsBuy'] = false;
  106. res.data.list[i]['count'] = 1;
  107. }
  108. if(res.data.list){
  109. that.setData({
  110. cartList: res.data.list,
  111. cartCount: res.data.count,
  112. cartTotal: res.data.total
  113. })
  114. // that.allSelect();
  115. }else{
  116. wx.showToast({
  117. title: '请先授权登录~',
  118. icon: 'none',
  119. duration: 2000
  120. })
  121. }
  122. }
  123. _request.$get(url, params, success)
  124. },
  125. add: function (e) {
  126. console.log('tapadd',e)
  127. var itemid = e.currentTarget.dataset.id;
  128. var itemcount = e.currentTarget.dataset.count;
  129. console.log('itemcount',itemcount)
  130. if (itemcount) {
  131. var index = e.currentTarget.dataset.index;
  132. var count = itemcount + 1;
  133. this.setData({
  134. ['cartList[' + index + '].count']:count
  135. })
  136. this.checkSelected();
  137. // this.changeServercount(count,itemid);
  138. }
  139. },
  140. mius: function (e) {
  141. console.log('tapmius',e)
  142. var that = this;
  143. var itemid = e.currentTarget.dataset.id;
  144. var itemcount = e.currentTarget.dataset.count;
  145. if (itemcount > 1) {
  146. var count = itemcount - 1;
  147. var index = e.currentTarget.dataset.index;
  148. this.setData({
  149. ['cartList[' + index + '].count']:count
  150. })
  151. this.checkSelected();
  152. // this.changeServercount(count,itemid);
  153. }/*else{
  154. wx.showModal({
  155. title: '提示',
  156. content: '需要删除改商品吗?',
  157. success: function (sm) {
  158. if (sm.confirm) {
  159. var url = "/v1/cart/mult_delele";
  160. var params = {
  161. ids:itemid
  162. }
  163. var success = function (res) {
  164. console.log(res)
  165. if(res.data.result){
  166. }
  167. that.getCartList();
  168. }
  169. _request.$post(url, params, success);
  170. } else if (sm.cancel) {
  171. console.log('用户点击取消')
  172. }
  173. }
  174. })
  175. }*/
  176. },
  177. delitem:function(e){
  178. var that = this;
  179. var itemid = e.currentTarget.dataset.id;
  180. console.log('itemid',itemid);
  181. // wx.showModal({
  182. // title: '提示',
  183. // content: '需要删除改商品吗?',
  184. // success: function (sm) {
  185. // if (sm.confirm) {
  186. // var url = "/v1/cart/mult_delele";
  187. // var params = {
  188. // ids:itemid
  189. // }
  190. // var success = function (res) {
  191. // console.log(res)
  192. // if(res.data.result){
  193. //
  194. // }
  195. // that.getCartList();
  196. // }
  197. // _request.$post(url, params, success);
  198. // } else if (sm.cancel) {
  199. // console.log('用户点击取消')
  200. // }
  201. // }
  202. // })
  203. },
  204. changeCount: function (val) {
  205. var count = val.detail.value
  206. var itemid = val.target.dataset.id;
  207. var index = val.target.dataset.index;
  208. if (count <= 0) {
  209. this.setData({
  210. ['cartList[' + index + '].count']:1
  211. })
  212. this.checkSelected();
  213. // this.changeServercount(1,itemid);
  214. } /*else if (count > this.data.project.left_count) {
  215. this.setData({
  216. count: this.data.project.left_count
  217. })
  218. this.changeCountService(this.data.project.left_count,itemid)
  219. }*/ else {
  220. this.setData({
  221. ['cartList[' + index + '].count']:count
  222. })
  223. this.checkSelected();
  224. // this.changeServercount(count,itemid);
  225. }
  226. },
  227. changeServercount:function(val,itemid){
  228. console.log('changeCountService')
  229. var that = this
  230. var url = "/v1/cart/change_nums/" + itemid + "/" + val
  231. var params = {}
  232. var success = function (res) {
  233. console.log(res)
  234. if(res.result){
  235. //修改成功
  236. }
  237. }
  238. _request.$post(url, params, success);
  239. this.getCartList();
  240. },
  241. itemselected:function(e){
  242. console.log(this)
  243. var nowitems = this.data.selectedItems;
  244. let index = e.currentTarget.dataset.index;
  245. var bool = this.data.cartList[index].IsBuy;
  246. var overtype = this.data.cartList[index].overtype;
  247. console.log('bool',bool)
  248. if(overtype){
  249. if(this.data.cartList[index].is_deliver && (this.data.cartList[index].deliver_state == 1 || this.data.cartList[index].deliver_state == 3)){
  250. var txt = this.data.cartList[index].deliver_state == 1 ? '商品正在预售中暂未开放购买~' : '商品已售罄~';
  251. wx.showToast({
  252. title: txt,
  253. icon: 'none',
  254. duration: 3000
  255. })
  256. }else{
  257. this.setData({
  258. ['cartList[' + index + '].IsBuy']: !bool
  259. })
  260. this.checkSelected();
  261. }
  262. }else{
  263. wx.showToast({
  264. title: '商品 '+this.data.cartList[index].name+' 请先选择规格~',
  265. icon: 'none',
  266. duration: 3000
  267. })
  268. }
  269. },
  270. allSelect:function(){
  271. if(this.data.selectedall){
  272. for(var i in this.data.cartList){
  273. this.setData({
  274. ['cartList[' + i + '].IsBuy']: false
  275. })
  276. }
  277. this.setData({
  278. selectedall: false
  279. })
  280. }else{
  281. for(var i in this.data.cartList){
  282. if(this.data.cartList[i].overtype){
  283. if(this.data.cartList[i].is_deliver && (this.data.cartList[i].deliver_state == 1 || this.data.cartList[i].deliver_state == 3)){
  284. var txt = this.data.cartList[i].deliver_state == 1 ? '商品正在预售中暂未开放购买~' : '商品已售罄~';
  285. wx.showToast({
  286. title: txt,
  287. icon: 'none',
  288. duration: 3000
  289. })
  290. }else{
  291. this.setData({
  292. ['cartList[' + i + '].IsBuy']: true
  293. })
  294. }
  295. }else{
  296. wx.showToast({
  297. title: '商品 '+this.data.cartList[i].name+' 请先选择规格~',
  298. icon: 'none',
  299. duration: 3000
  300. })
  301. }
  302. }
  303. this.setData({
  304. selectedall: true
  305. })
  306. }
  307. this.checkSelected();
  308. },
  309. checkSelected:function(){
  310. var cartList = this.data.cartList;
  311. var checkitem = '';
  312. var checkitemnum = '';
  313. var checktotal = 0;
  314. var checkcount = 0;
  315. // console.log('cartList',cartList)
  316. for(var i in cartList){
  317. if(cartList[i].IsBuy){
  318. checkitem = checkitem + cartList[i].id+',';
  319. checkitemnum = checkitemnum + cartList[i].count+',';
  320. checktotal = checktotal + (parseFloat(cartList[i].price) * parseInt(cartList[i].count));
  321. checkcount = checkcount + parseInt(cartList[i].count);
  322. }
  323. }
  324. checkitem = checkitem.substr(0,checkitem.length - 1);
  325. checkitemnum = checkitemnum.substr(0,checkitemnum.length - 1);
  326. this.setData({
  327. checkeditems:checkitem,
  328. checkeditemsnum:checkitemnum,
  329. cartCount: checkcount,
  330. cartTotal: checktotal
  331. })
  332. // this.mulChangeState();
  333. },
  334. mulChangeState:function(){
  335. var that = this;
  336. var itemid = this.data.checkeditems;
  337. var itemnum = this.data.checkeditemsnum;
  338. var url = "v1/cart/mult_change_state";
  339. var params = {
  340. ids:itemid,
  341. nums:itemnum
  342. }
  343. var success = function (res) {
  344. console.log('mulChangeState',res)
  345. if(res.data.result){
  346. that.getCartList();
  347. }
  348. }
  349. _request.$post(url, params, success);
  350. },
  351. mulChangeStateToPay:function(){
  352. var that = this;
  353. var lock = this.data.submitlock;
  354. if(!lock){
  355. that.setData({submitlock: true})
  356. var itemid = this.data.checkeditems;
  357. var itemnum = this.data.checkeditemsnum;
  358. var url = "v1/shop/order_mul";
  359. var params = {
  360. ids:itemid,
  361. nums:itemnum
  362. }
  363. var success = function (res) {
  364. wx.showToast({
  365. title: '正在跳转到结算页,请稍后~',
  366. icon: 'none',
  367. duration: 2000
  368. })
  369. console.log('mulChangeState',res)
  370. if (res.data.order_id) {
  371. wx.navigateTo({
  372. url: '../pay/pay?orderId=' + res.data.order_id
  373. })
  374. }
  375. }
  376. var err = function(err){
  377. console.log(err)
  378. that.setData({submitlock: false})
  379. }
  380. _request.$post(url, params, success, err);
  381. }
  382. },
  383. toPay:function(){
  384. var that = this;
  385. var url = "/v1/cart/order_mul";
  386. var params = {}
  387. var success = function (res) {
  388. console.log(res)
  389. if (res.data.order_id) {
  390. wx.navigateTo({
  391. url: '../pay/pay?orderId=' + res.data.order_id
  392. })
  393. }
  394. }
  395. _request.$get(url, params, success);
  396. },
  397. delSelected:function(){
  398. var that = this;
  399. var itemid = this.data.checkeditems;
  400. if(itemid== ''){
  401. wx.showToast({
  402. title: '没有选中任何商品~',
  403. icon: 'none',
  404. duration: 2000
  405. })
  406. }else{
  407. wx.showModal({
  408. title: '提示',
  409. content: '确定要删除选中的商品吗?',
  410. success: function (sm) {
  411. if (sm.confirm) {
  412. var url = "/v1/cart/mult_delele";
  413. var params = {
  414. ids:itemid
  415. }
  416. var success = function (res) {
  417. console.log(res)
  418. if(res.data.result){
  419. }
  420. that.getCartList();
  421. }
  422. _request.$post(url, params, success);
  423. } else if (sm.cancel) {
  424. console.log('用户点击取消')
  425. }
  426. }
  427. })
  428. }
  429. },
  430. toProject:function(val){
  431. var id = val.currentTarget.dataset.val
  432. wx.navigateTo({
  433. url: '/pages/projects/project-detail-noshare/project-detail-noshare?id=' + id + '&read=1'
  434. })
  435. },
  436. getIphoneX () {
  437. var that = this
  438. wx.getSystemInfo({
  439. success: function (res) {
  440. if (res.model.toLowerCase().indexOf('iphone x') > -1) {
  441. that.setData({
  442. value: 34
  443. })
  444. }
  445. }
  446. })
  447. },
  448. selectSize:function(val){
  449. var that = this
  450. var id = val.currentTarget.dataset.val;
  451. var item = val.currentTarget.dataset.item;
  452. that.setData({
  453. selectProjectid: id,
  454. selectProjectItem: item
  455. });
  456. console.log('id',id);
  457. that.getProjectDetail();
  458. that.getProductSize();
  459. var animation = wx.createAnimation({
  460. duration: 200,
  461. timingFunction: "ease-out"
  462. })
  463. this.animation = animation
  464. animation.translateY(500).step()
  465. this.setData({
  466. animationData: animation.export(),
  467. showDialog: true
  468. })
  469. setTimeout(function () {
  470. animation.translateY(0).step()
  471. this.setData({
  472. animationData: animation.export()
  473. })
  474. }.bind(this), 200)
  475. },
  476. getProjectDetail() {
  477. var that = this
  478. var url = "v1/product/" + that.data.selectProjectid;
  479. var params = {}
  480. var success = function (val) {
  481. var p = val.data.detail
  482. that.setData({
  483. selectProject: val.data
  484. });
  485. if(val.data.have_size){
  486. // that.getProductSize();
  487. }
  488. }
  489. _request.$get(url, params, success)
  490. },
  491. getProductSize(){
  492. var that = this
  493. var url = "v1/product_size/" + that.data.selectProjectid;
  494. var params = {}
  495. var success = function (val) {
  496. let cbox = [];
  497. for(let i in val.data){
  498. if(i != 'product' && val.data[i]) cbox.push(i);
  499. }
  500. console.log('cbox',cbox)
  501. that.setData({
  502. selectProjectSize: val.data,
  503. checkbox:cbox
  504. });
  505. }
  506. _request.$get(url, params, success)
  507. },
  508. hideDialog:function(e){
  509. var that = this
  510. that.data.showDialog = !that.data.showDialog
  511. if (that.data.showDialog) {
  512. that.showModal(1)
  513. } else {
  514. that.hideModal(1)
  515. }
  516. },
  517. // 显示弹框
  518. showModal: function (type) {
  519. var animation = wx.createAnimation({
  520. duration: 200,
  521. timingFunction: "ease-out"
  522. })
  523. this.animation = animation
  524. animation.translateY(500).step()
  525. if(type == 1){
  526. this.setData({
  527. animationData: animation.export(),
  528. showDialog: true
  529. })
  530. }else{
  531. this.setData({
  532. animationData: animation.export(),
  533. showCartDialog: true
  534. })
  535. }
  536. setTimeout(function () {
  537. animation.translateY(0).step()
  538. this.setData({
  539. animationData: animation.export()
  540. })
  541. }.bind(this), 200)
  542. },
  543. // 隐藏弹框
  544. hideModal: function (type = 1) {
  545. var animation = wx.createAnimation({
  546. duration: 200,
  547. timingFunction: "ease-out"
  548. })
  549. this.animation = animation
  550. animation.translateY(500).step()
  551. this.setData({
  552. animationData: animation.export(),
  553. })
  554. setTimeout(function () {
  555. animation.translateY(0).step()
  556. if(type == 1){
  557. this.setData({
  558. animationData: animation.export(),
  559. showDialog: false
  560. })
  561. }else{
  562. this.setData({
  563. animationData: animation.export(),
  564. showCartDialog: false
  565. })
  566. }
  567. }.bind(this), 200)
  568. },
  569. checkSize(event){
  570. var type = event.currentTarget.dataset.type;
  571. var id = event.currentTarget.dataset.id;
  572. var list = event.currentTarget.dataset.list;
  573. var tname = event.currentTarget.dataset.tname;
  574. var that = this;
  575. var tkey = type=='color_list'?'cname':'sname';
  576. var reproduct = this.data.selectProjectSize;
  577. // console.log('reproduct',reproduct)
  578. for(var i in reproduct[type]['attr_values']){
  579. // console.log('reproduct[type][i].is_select',reproduct[type]['attr_values'][i].is_select)
  580. reproduct[type]['attr_values'][i].is_select = false;
  581. if(reproduct[type]['attr_values'][i].id == id) reproduct[type]['attr_values'][i].is_select = true;
  582. }
  583. // console.log('reproduct',reproduct)
  584. that.setData({
  585. selectProjectSize :reproduct,
  586. [type]:id,
  587. [tkey]:tname
  588. })
  589. console.log('that.data[type]',that.data[type])
  590. let cflag = true;
  591. for(let i in that.data.checkbox){
  592. if(that.data[that.data.checkbox[i]] == 0) cflag = false;
  593. }
  594. if(cflag){
  595. that.getProductDetail();
  596. }
  597. },
  598. getProductDetail () {
  599. var that = this;
  600. console.log(that.data.size_list);
  601. console.log(that.data.color_list);
  602. var sizelist = that.data.size_list ? that.data.size_list : 0;
  603. var colorlist = that.data.color_list ? that.data.color_list : 0;
  604. var url = "v1/product_detail/" + that.data.selectProjectid + "/" + sizelist + "/" + colorlist;
  605. var params = {}
  606. var success = function (val) {
  607. console.log('val',val)
  608. if(val.data.product){
  609. that.setData({
  610. canpay:true,
  611. selectProject:val.data.product,
  612. selectProjectid:val.data.product.id,
  613. noproduct:false
  614. })
  615. that.setData({
  616. canpay:true,
  617. selectProject:val.data.product,
  618. selectProjectid:val.data.product.id,
  619. noproduct:false
  620. })
  621. }else{
  622. that.setData({
  623. canpay:false,
  624. noproduct:true
  625. })
  626. }
  627. }
  628. var fail = function(err){
  629. }
  630. _request.$get(url, params, success, fail)
  631. },
  632. selectType:function(){
  633. var that = this;
  634. var itemindex = this.data.selectProjectItem;
  635. var itemdata = this.data.selectProject;
  636. console.log('xuanzzzzz')
  637. console.log(this.data.cartList)
  638. console.log(this.data.selectProject)
  639. console.log(this.data.selectProjectItem)
  640. itemdata['count']=1;
  641. itemdata['IsBuy']=this.data.cartList[itemindex].IsBuy;
  642. itemdata['movex']=0;
  643. itemdata['overtype']=true;
  644. that.setData({
  645. ['cartList[' + itemindex + ']']:itemdata
  646. })
  647. that.hideModal(1);
  648. },
  649. selectTypeno:function(){
  650. wx.showToast({
  651. title: '选中的规格暂无库存,请选择其他规格~',
  652. icon: 'none',
  653. duration: 2000
  654. })
  655. }
  656. })