areacart.js 10 KB

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