cart.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. },
  17. onLoad: function (options) {
  18. this.getCartList();
  19. },
  20. onShow: function () {
  21. this.getCartList();
  22. this.setData({submitlock: false});
  23. this.setData({selectedall: false});
  24. this.getCartListAndSelected();
  25. },
  26. getCartList:function(){
  27. console.log('getCartList')
  28. var that = this
  29. var url = 'v1/cart/list'
  30. var params = {}
  31. var success = function (res) {
  32. console.log(res)
  33. if(res.data.list){
  34. that.setData({
  35. cartList: res.data.list,
  36. cartCount: res.data.count,
  37. cartTotal: res.data.total
  38. })
  39. }else{
  40. wx.showToast({
  41. title: '请先授权登录~',
  42. icon: 'none',
  43. duration: 2000
  44. })
  45. }
  46. }
  47. _request.$get(url, params, success)
  48. },
  49. getCartListAndSelected: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. that.setData({
  58. cartList: res.data.list,
  59. cartCount: res.data.count,
  60. cartTotal: res.data.total
  61. })
  62. that.allSelect();
  63. }else{
  64. wx.showToast({
  65. title: '请先授权登录~',
  66. icon: 'none',
  67. duration: 2000
  68. })
  69. }
  70. }
  71. _request.$get(url, params, success)
  72. },
  73. add: function (e) {
  74. console.log('tapadd',e)
  75. var itemid = e.currentTarget.dataset.id;
  76. var itemcount = e.currentTarget.dataset.count;
  77. console.log('itemcount',itemcount)
  78. if (itemcount) {
  79. var index = e.currentTarget.dataset.index;
  80. var count = itemcount + 1;
  81. this.setData({
  82. ['cartList[' + index + '].count']:count
  83. })
  84. this.checkSelected();
  85. // this.changeServercount(count,itemid);
  86. }
  87. },
  88. mius: function (e) {
  89. console.log('tapmius',e)
  90. var itemid = e.currentTarget.dataset.id;
  91. var itemcount = e.currentTarget.dataset.count;
  92. if (itemcount > 1) {
  93. var count = itemcount - 1;
  94. var index = e.currentTarget.dataset.index;
  95. this.setData({
  96. ['cartList[' + index + '].count']:count
  97. })
  98. this.checkSelected();
  99. // this.changeServercount(count,itemid);
  100. }
  101. },
  102. changeCount: function (val) {
  103. var count = val.detail.value
  104. var itemid = val.target.dataset.id;
  105. var index = val.target.dataset.index;
  106. if (count <= 0) {
  107. this.setData({
  108. ['cartList[' + index + '].count']:1
  109. })
  110. this.checkSelected();
  111. // this.changeServercount(1,itemid);
  112. } /*else if (count > this.data.project.left_count) {
  113. this.setData({
  114. count: this.data.project.left_count
  115. })
  116. this.changeCountService(this.data.project.left_count,itemid)
  117. }*/ else {
  118. this.setData({
  119. ['cartList[' + index + '].count']:count
  120. })
  121. this.checkSelected();
  122. // this.changeServercount(count,itemid);
  123. }
  124. },
  125. changeServercount:function(val,itemid){
  126. console.log('changeCountService')
  127. var that = this
  128. var url = "/v1/cart/change_nums/" + itemid + "/" + val
  129. var params = {}
  130. var success = function (res) {
  131. console.log(res)
  132. if(res.result){
  133. //修改成功
  134. }
  135. }
  136. _request.$post(url, params, success);
  137. this.getCartList();
  138. },
  139. itemselected:function(e){
  140. console.log(this)
  141. var nowitems = this.data.selectedItems;
  142. let index = e.currentTarget.dataset.index;
  143. var bool = this.data.cartList[index].IsBuy;
  144. console.log('bool',bool)
  145. this.setData({
  146. ['cartList[' + index + '].IsBuy']: !bool
  147. })
  148. this.checkSelected();
  149. },
  150. allSelect:function(){
  151. if(this.data.selectedall){
  152. for(var i in this.data.cartList){
  153. this.setData({
  154. ['cartList[' + i + '].IsBuy']: false
  155. })
  156. }
  157. this.setData({
  158. selectedall: false
  159. })
  160. }else{
  161. for(var i in this.data.cartList){
  162. this.setData({
  163. ['cartList[' + i + '].IsBuy']: true
  164. })
  165. }
  166. this.setData({
  167. selectedall: true
  168. })
  169. }
  170. this.checkSelected();
  171. },
  172. checkSelected:function(){
  173. var cartList = this.data.cartList;
  174. var checkitem = '';
  175. var checkitemnum = '';
  176. var checktotal = 0;
  177. var checkcount = 0;
  178. for(var i in cartList){
  179. if(cartList[i].IsBuy){
  180. checkitem = checkitem + cartList[i].id+',';
  181. checkitemnum = checkitemnum + cartList[i].count+',';
  182. checktotal = checktotal + (parseFloat(cartList[i].original_price) * parseInt(cartList[i].count));
  183. checkcount = checkcount + parseInt(cartList[i].count);
  184. }
  185. }
  186. checkitem = checkitem.substr(0,checkitem.length - 1);
  187. checkitemnum = checkitemnum.substr(0,checkitemnum.length - 1);
  188. this.setData({
  189. checkeditems:checkitem,
  190. checkeditemsnum:checkitemnum,
  191. cartCount: checkcount,
  192. cartTotal: checktotal
  193. })
  194. // this.mulChangeState();
  195. },
  196. mulChangeState:function(){
  197. var that = this;
  198. var itemid = this.data.checkeditems;
  199. var itemnum = this.data.checkeditemsnum;
  200. var url = "v1/cart/mult_change_state";
  201. var params = {
  202. ids:itemid,
  203. nums:itemnum
  204. }
  205. var success = function (res) {
  206. console.log('mulChangeState',res)
  207. if(res.data.result){
  208. that.getCartList();
  209. }
  210. }
  211. _request.$post(url, params, success);
  212. },
  213. mulChangeStateToPay:function(){
  214. var that = this;
  215. var lock = this.data.submitlock;
  216. if(!lock){
  217. that.setData({submitlock: true})
  218. var itemid = this.data.checkeditems;
  219. var itemnum = this.data.checkeditemsnum;
  220. var url = "v1/cart/order_mul";
  221. var params = {
  222. ids:itemid,
  223. nums:itemnum
  224. }
  225. var success = function (res) {
  226. wx.showToast({
  227. title: '正在跳转到结算页,请稍后~',
  228. icon: 'none',
  229. duration: 2000
  230. })
  231. console.log('mulChangeState',res)
  232. if (res.data.order_id) {
  233. wx.navigateTo({
  234. url: '../pay/pay?orderId=' + res.data.order_id
  235. })
  236. }
  237. }
  238. var err = function(err){
  239. console.log(err)
  240. that.setData({submitlock: false})
  241. }
  242. _request.$post(url, params, success, err);
  243. }
  244. },
  245. toPay:function(){
  246. var that = this;
  247. var url = "/v1/cart/order_mul";
  248. var params = {}
  249. var success = function (res) {
  250. console.log(res)
  251. if (res.data.order_id) {
  252. wx.navigateTo({
  253. url: '../pay/pay?orderId=' + res.data.order_id
  254. })
  255. }
  256. }
  257. _request.$get(url, params, success);
  258. },
  259. delSelected:function(){
  260. var that = this;
  261. var itemid = this.data.checkeditems;
  262. if(itemid== ''){
  263. wx.showToast({
  264. title: '没有选中任何商品~',
  265. icon: 'none',
  266. duration: 2000
  267. })
  268. }else{
  269. wx.showModal({
  270. title: '提示',
  271. content: '确定要删除选中的商品吗?',
  272. success: function (sm) {
  273. if (sm.confirm) {
  274. var url = "/v1/cart/mult_delele";
  275. var params = {
  276. ids:itemid
  277. }
  278. var success = function (res) {
  279. console.log(res)
  280. if(res.data.result){
  281. }
  282. that.getCartList();
  283. }
  284. _request.$post(url, params, success);
  285. } else if (sm.cancel) {
  286. console.log('用户点击取消')
  287. }
  288. }
  289. })
  290. }
  291. }
  292. })