centcart.js 10.0 KB

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