centcart.js 11 KB

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