areacart.js 10 KB

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