cart.js 9.2 KB

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