centcart.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. },
  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. },
  48. onShow: function () {
  49. this.setData({
  50. count:1,
  51. cartList:[],
  52. cartCount:0,
  53. cartTotal:0,
  54. checkeditems:'',
  55. checkeditemsnum:'',
  56. checkedtotal:0,
  57. selectedall:false,
  58. submitlock:false,
  59. x:0,
  60. currentX: 0,
  61. perpage:10,
  62. nowpage:1,
  63. cartMore:true
  64. })
  65. this.getCartList();
  66. this.getBalanceInfo();
  67. this.getCentAct();
  68. this.setData({
  69. submitlock: false,
  70. selectedall: false
  71. });
  72. // this.getCartListAndSelected();
  73. },
  74. getCentAct(){
  75. var that = this
  76. var url = 'v1/task_sign/daily/check'
  77. var params = {
  78. }
  79. var success = function (res) {
  80. var result = res.data
  81. that.setData({
  82. centlist: result
  83. })
  84. console.log('getCentAct',result)
  85. }
  86. _request.$get(url, params, success)
  87. },
  88. getBalanceInfo () {
  89. var that = this
  90. var url = 'v1/user/cent/info'
  91. var params = {
  92. }
  93. var success = function (res) {
  94. var result = res.data.total
  95. that.setData({
  96. cent: result
  97. })
  98. console.log('getBalanceInfo',result)
  99. }
  100. _request.$get(url, params, success)
  101. },
  102. getCartList:function(){
  103. console.log('getCartList')
  104. var that = this
  105. var url = 'v1/products?ptype=cent_sale&&page='+that.data.nowpage+'&&per_page='+that.data.perpage
  106. var params = {}
  107. var success = function (res) {
  108. console.log(res)
  109. if(res.data.list){
  110. for(let i in res.data.list){
  111. res.data.list[i]['movex'] = 0;
  112. res.data.list[i]['IsBuy'] = false;
  113. res.data.list[i]['count'] = 1;
  114. }
  115. that.setData({
  116. cartMore: res.data.list_count > that.data.cartList.length,
  117. cartList: that.data.cartList.concat(res.data.list || []),
  118. cartCount: res.data.count,
  119. cartTotal: res.data.total
  120. })
  121. }else{
  122. wx.showToast({
  123. title: '请先授权登录~',
  124. icon: 'none',
  125. duration: 2000
  126. })
  127. }
  128. }
  129. _request.$get(url, params, success)
  130. },
  131. onReachBottom: function () {
  132. console.log('onreachbottom')
  133. if (this.data.cartMore) {
  134. var page = this.data.nowpage + 1
  135. this.setData({
  136. nowpage: page
  137. })
  138. this.getCartList()
  139. }
  140. },
  141. getCartListAndSelected:function(){
  142. console.log('getCartList')
  143. var that = this
  144. var url = 'v1/products?ptype=cent_sale&&page=1&&per_page=3'
  145. var params = {}
  146. var success = function (res) {
  147. console.log(res)
  148. for(let i in res.data.list){
  149. res.data.list[i]['movex'] = 0;
  150. res.data.list[i]['IsBuy'] = false;
  151. res.data.list[i]['count'] = 1;
  152. }
  153. if(res.data.list){
  154. that.setData({
  155. cartList: res.data.list,
  156. cartCount: res.data.count,
  157. cartTotal: res.data.total
  158. })
  159. // that.allSelect();
  160. }else{
  161. wx.showToast({
  162. title: '请先授权登录~',
  163. icon: 'none',
  164. duration: 2000
  165. })
  166. }
  167. }
  168. _request.$get(url, params, success)
  169. },
  170. add: function (e) {
  171. console.log('tapadd',e)
  172. var itemid = e.currentTarget.dataset.id;
  173. var itemcount = e.currentTarget.dataset.count;
  174. console.log('itemcount',itemcount)
  175. if (itemcount) {
  176. var index = e.currentTarget.dataset.index;
  177. var count = itemcount + 1;
  178. this.setData({
  179. ['cartList[' + index + '].count']:count
  180. })
  181. this.checkSelected();
  182. // this.changeServercount(count,itemid);
  183. }
  184. },
  185. mius: function (e) {
  186. console.log('tapmius',e)
  187. var that = this;
  188. var itemid = e.currentTarget.dataset.id;
  189. var itemcount = e.currentTarget.dataset.count;
  190. if (itemcount > 1) {
  191. var count = itemcount - 1;
  192. var index = e.currentTarget.dataset.index;
  193. this.setData({
  194. ['cartList[' + index + '].count']:count
  195. })
  196. this.checkSelected();
  197. // this.changeServercount(count,itemid);
  198. }/*else{
  199. wx.showModal({
  200. title: '提示',
  201. content: '需要删除改商品吗?',
  202. success: function (sm) {
  203. if (sm.confirm) {
  204. var url = "/v1/cart/mult_delele";
  205. var params = {
  206. ids:itemid
  207. }
  208. var success = function (res) {
  209. console.log(res)
  210. if(res.data.result){
  211. }
  212. that.getCartList();
  213. }
  214. _request.$post(url, params, success);
  215. } else if (sm.cancel) {
  216. console.log('用户点击取消')
  217. }
  218. }
  219. })
  220. }*/
  221. },
  222. delitem:function(e){
  223. var that = this;
  224. var itemid = e.currentTarget.dataset.id;
  225. console.log('itemid',itemid);
  226. // wx.showModal({
  227. // title: '提示',
  228. // content: '需要删除改商品吗?',
  229. // success: function (sm) {
  230. // if (sm.confirm) {
  231. // var url = "/v1/cart/mult_delele";
  232. // var params = {
  233. // ids:itemid
  234. // }
  235. // var success = function (res) {
  236. // console.log(res)
  237. // if(res.data.result){
  238. //
  239. // }
  240. // that.getCartList();
  241. // }
  242. // _request.$post(url, params, success);
  243. // } else if (sm.cancel) {
  244. // console.log('用户点击取消')
  245. // }
  246. // }
  247. // })
  248. },
  249. changeCount: function (val) {
  250. var count = val.detail.value
  251. var itemid = val.target.dataset.id;
  252. var index = val.target.dataset.index;
  253. if (count <= 0) {
  254. this.setData({
  255. ['cartList[' + index + '].count']:1
  256. })
  257. this.checkSelected();
  258. // this.changeServercount(1,itemid);
  259. } /*else if (count > this.data.project.left_count) {
  260. this.setData({
  261. count: this.data.project.left_count
  262. })
  263. this.changeCountService(this.data.project.left_count,itemid)
  264. }*/ else {
  265. this.setData({
  266. ['cartList[' + index + '].count']:count
  267. })
  268. this.checkSelected();
  269. // this.changeServercount(count,itemid);
  270. }
  271. },
  272. changeServercount:function(val,itemid){
  273. console.log('changeCountService')
  274. var that = this
  275. var url = "/v1/cart/change_nums/" + itemid + "/" + val
  276. var params = {}
  277. var success = function (res) {
  278. console.log(res)
  279. if(res.result){
  280. //修改成功
  281. }
  282. }
  283. _request.$post(url, params, success);
  284. this.getCartList();
  285. },
  286. itemselected:function(e){
  287. console.log(this)
  288. var nowitems = this.data.selectedItems;
  289. let index = e.currentTarget.dataset.index;
  290. var bool = this.data.cartList[index].IsBuy;
  291. console.log('bool',bool)
  292. this.setData({
  293. ['cartList[' + index + '].IsBuy']: !bool
  294. })
  295. this.checkSelected();
  296. },
  297. allSelect:function(){
  298. if(this.data.selectedall){
  299. for(var i in this.data.cartList){
  300. this.setData({
  301. ['cartList[' + i + '].IsBuy']: false
  302. })
  303. }
  304. this.setData({
  305. selectedall: false
  306. })
  307. }else{
  308. for(var i in this.data.cartList){
  309. this.setData({
  310. ['cartList[' + i + '].IsBuy']: true
  311. })
  312. }
  313. this.setData({
  314. selectedall: true
  315. })
  316. }
  317. this.checkSelected();
  318. },
  319. checkSelected:function(){
  320. var cartList = this.data.cartList;
  321. var checkitem = '';
  322. var checkitemnum = '';
  323. var checktotal = 0;
  324. var checkcount = 0;
  325. // console.log('cartList',cartList)
  326. for(var i in cartList){
  327. if(cartList[i].IsBuy){
  328. checkitem = checkitem + cartList[i].id+',';
  329. checkitemnum = checkitemnum + cartList[i].count+',';
  330. checktotal = checktotal + (parseFloat(cartList[i].price) * parseInt(cartList[i].count));
  331. checkcount = checkcount + parseInt(cartList[i].count);
  332. }
  333. }
  334. checkitem = checkitem.substr(0,checkitem.length - 1);
  335. checkitemnum = checkitemnum.substr(0,checkitemnum.length - 1);
  336. this.setData({
  337. checkeditems:checkitem,
  338. checkeditemsnum:checkitemnum,
  339. cartCount: checkcount,
  340. cartTotal: checktotal
  341. })
  342. // this.mulChangeState();
  343. },
  344. mulChangeState:function(){
  345. var that = this;
  346. var itemid = this.data.checkeditems;
  347. var itemnum = this.data.checkeditemsnum;
  348. var url = "v1/cart/mult_change_state";
  349. var params = {
  350. ids:itemid,
  351. nums:itemnum
  352. }
  353. var success = function (res) {
  354. console.log('mulChangeState',res)
  355. if(res.data.result){
  356. that.getCartList();
  357. }
  358. }
  359. _request.$post(url, params, success);
  360. },
  361. mulChangeStateToPay:function(){
  362. var that = this;
  363. var lock = this.data.submitlock;
  364. if(!lock){
  365. that.setData({submitlock: true})
  366. var itemid = this.data.checkeditems;
  367. var itemnum = this.data.checkeditemsnum;
  368. var url = "v1/cent/order_mul";
  369. var params = {
  370. ids:itemid,
  371. nums:itemnum
  372. }
  373. var success = function (res) {
  374. wx.showToast({
  375. title: '正在跳转到结算页,请稍后~',
  376. icon: 'none',
  377. duration: 2000
  378. })
  379. console.log('mulChangeState',res)
  380. if (res.data.order_id) {
  381. wx.navigateTo({
  382. url: '../pay/pay?orderId=' + res.data.order_id
  383. })
  384. }
  385. }
  386. var err = function(err){
  387. console.log(err)
  388. that.setData({submitlock: false})
  389. }
  390. _request.$post(url, params, success, err);
  391. }
  392. },
  393. toPay:function(){
  394. var that = this;
  395. var url = "/v1/cent/order_mul";
  396. var params = {}
  397. var success = function (res) {
  398. console.log(res)
  399. if (res.data.order_id) {
  400. wx.navigateTo({
  401. url: '../pay/pay?orderId=' + res.data.order_id
  402. })
  403. }
  404. }
  405. _request.$get(url, params, success);
  406. },
  407. delSelected:function(){
  408. var that = this;
  409. var itemid = this.data.checkeditems;
  410. if(itemid== ''){
  411. wx.showToast({
  412. title: '没有选中任何商品~',
  413. icon: 'none',
  414. duration: 2000
  415. })
  416. }else{
  417. wx.showModal({
  418. title: '提示',
  419. content: '确定要删除选中的商品吗?',
  420. success: function (sm) {
  421. if (sm.confirm) {
  422. var url = "/v1/cart/mult_delele";
  423. var params = {
  424. ids:itemid
  425. }
  426. var success = function (res) {
  427. console.log(res)
  428. if(res.data.result){
  429. }
  430. that.getCartList();
  431. }
  432. _request.$post(url, params, success);
  433. } else if (sm.cancel) {
  434. console.log('用户点击取消')
  435. }
  436. }
  437. })
  438. }
  439. },
  440. toProject:function(val){
  441. var id = val.currentTarget.dataset.val
  442. wx.navigateTo({
  443. url: '/pages/projects/project-detail/project-detail?id=' + id + '&read=1'
  444. })
  445. }
  446. })