centcart.js 18 KB

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