areacart.js 10.0 KB

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