areacart.js 9.6 KB

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