centcart.js 11 KB

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