order_addcart.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import {
  2. getCartList,
  3. getCartCounts,
  4. changeCartNum,
  5. cartDel
  6. } from '../../api/order.js';
  7. import {
  8. getProductHot,
  9. collectAll
  10. } from '../../api/store.js';
  11. const app = getApp();
  12. const util = require('../../utils/util.js');
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. parameter: {
  19. 'navbar': '1',
  20. 'return': '0',
  21. 'title': '购物车',
  22. 'color': false
  23. },
  24. navH: 0,
  25. cartCount: 0,
  26. goodsHidden: true,
  27. footerswitch: true,
  28. host_product: [],
  29. cartList: [],
  30. isAllSelect: false, //全选
  31. selectValue: [], //选中的数据
  32. selectCountPrice: 0.00,
  33. isGoIndex: true,
  34. isHidden: false,
  35. },
  36. /**
  37. * 生命周期函数--监听页面加载
  38. */
  39. onLoad: function (options) {
  40. var that = this;
  41. that.setData({
  42. navH: app.globalData.navHeight
  43. });
  44. if (app.globalData.token) that.setData({
  45. isHidden: true
  46. });
  47. },
  48. /**
  49. * input阻止冒泡
  50. */
  51. proventD: function () {},
  52. /**
  53. * 手动输入数量失焦事件
  54. */
  55. inputBlur(e) {
  56. if (e.detail.value < 1) {
  57. let index = e.currentTarget.dataset.index;
  58. let item = this.data.cartList.valid[index];
  59. item.cart_num = 1;
  60. if (item.cart_num) this.setCartNum(item.id, item.cart_num);
  61. let itemData = "cartList.valid[" + index + "]";
  62. this.setData({
  63. [itemData]: item
  64. });
  65. this.switchSelect();
  66. }
  67. },
  68. /**
  69. * 关闭授权
  70. */
  71. onCloseAuto: function () {
  72. this.setData({
  73. isHidden: true
  74. });
  75. },
  76. subDel: function (event) {
  77. let that = this,
  78. selectValue = that.data.selectValue;
  79. if (selectValue.length > 0)
  80. cartDel(selectValue).then(res => {
  81. that.getCartList();
  82. that.getCartNum();
  83. });
  84. else
  85. return app.Tips({
  86. title: '请选择产品'
  87. });
  88. },
  89. getSelectValueProductId: function () {
  90. var that = this;
  91. var validList = that.data.cartList.valid;
  92. var selectValue = that.data.selectValue;
  93. var productId = [];
  94. if (selectValue.length > 0) {
  95. for (var index in validList) {
  96. if (that.inArray(validList[index].id, selectValue)) {
  97. productId.push(validList[index].product_id);
  98. }
  99. }
  100. };
  101. return productId;
  102. },
  103. subCollect: function (event) {
  104. let that = this,
  105. selectValue = that.data.selectValue;
  106. if (selectValue.length > 0) {
  107. var selectValueProductId = that.getSelectValueProductId();
  108. collectAll(that.getSelectValueProductId().join(',')).then(res => {
  109. return app.Tips({
  110. title: res.msg,
  111. icon: 'success'
  112. });
  113. }).catch(err => {
  114. return app.Tips({
  115. title: err
  116. });
  117. });
  118. } else {
  119. return app.Tips({
  120. title: '请选择产品'
  121. });
  122. }
  123. },
  124. subOrder: function (event) {
  125. let that = this,
  126. selectValue = that.data.selectValue;
  127. if (selectValue.length > 0) {
  128. wx.navigateTo({
  129. url: '/pages/order_confirm/index?cartId=' + selectValue.join(',')
  130. });
  131. } else {
  132. return app.Tips({
  133. title: '请选择产品'
  134. });
  135. }
  136. },
  137. checkboxAllChange: function (event) {
  138. var value = event.detail.value;
  139. if (value.length > 0) {
  140. this.setAllSelectValue(1)
  141. } else {
  142. this.setAllSelectValue(0)
  143. }
  144. },
  145. setAllSelectValue: function (status) {
  146. var that = this;
  147. var selectValue = [];
  148. var valid = that.data.cartList.valid;
  149. if (valid.length > 0) {
  150. for (var index in valid) {
  151. if (status == 1) {
  152. valid[index].checked = true;
  153. selectValue.push(valid[index].id);
  154. } else valid[index].checked = false;
  155. }
  156. var validData = "cartList.valid";
  157. that.setData({
  158. [validData]: valid,
  159. selectValue: selectValue,
  160. });
  161. that.switchSelect();
  162. }
  163. },
  164. checkboxChange: function (event) {
  165. var that = this;
  166. var value = event.detail.value;
  167. var valid = this.data.cartList.valid;
  168. for (var index in valid) {
  169. if (that.inArray(valid[index].id, value)) valid[index].checked = true;
  170. else valid[index].checked = false;
  171. }
  172. var validData = "cartList.valid";
  173. this.setData({
  174. [validData]: valid,
  175. isAllSelect: value.length == this.data.cartList.valid.length,
  176. selectValue: value,
  177. })
  178. this.switchSelect();
  179. },
  180. inArray: function (search, array) {
  181. for (var i in array) {
  182. if (array[i] == search) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. },
  188. switchSelect: function () {
  189. var that = this;
  190. var validList = that.data.cartList.valid;
  191. var selectValue = that.data.selectValue;
  192. var selectCountPrice = 0.00;
  193. if (selectValue.length < 1) {
  194. that.setData({
  195. selectCountPrice: selectCountPrice
  196. });
  197. } else {
  198. for (var index in validList) {
  199. if (that.inArray(validList[index].id, selectValue)) {
  200. selectCountPrice = Number(selectCountPrice) + Number(validList[index].cart_num) * Number(validList[index].truePrice)
  201. }
  202. }
  203. that.setData({
  204. selectCountPrice: selectCountPrice.toFixed(2)
  205. });
  206. }
  207. },
  208. subCart: function (event) {
  209. var that = this;
  210. var status = false;
  211. var index = event.currentTarget.dataset.index;
  212. var item = that.data.cartList.valid[index];
  213. item.cart_num = item.cart_num - 1;
  214. if (item.cart_num < 1) status = true;
  215. if (item.cart_num <= 1) {
  216. item.cart_num = 1;
  217. item.numSub = true;
  218. } else {
  219. item.numSub = false;
  220. item.numAdd = false;
  221. }
  222. if (false == status) {
  223. that.setCartNum(item.id, item.cart_num, function (data) {
  224. var itemData = "cartList.valid[" + index + "]";
  225. that.setData({
  226. [itemData]: item
  227. });
  228. that.switchSelect();
  229. });
  230. }
  231. },
  232. /**
  233. * 购物车手动填写
  234. */
  235. iptCartNum: function (e) {
  236. let index = e.currentTarget.dataset.index;
  237. let item = this.data.cartList.valid[index];
  238. item.cart_num = e.detail.value;
  239. if (item.cart_num) this.setCartNum(item.id, item.cart_num);
  240. let itemData = "cartList.valid[" + index + "]";
  241. this.setData({
  242. [itemData]: item
  243. });
  244. this.switchSelect();
  245. },
  246. addCart: function (event) {
  247. var that = this;
  248. var index = event.currentTarget.dataset.index;
  249. var item = that.data.cartList.valid[index];
  250. item.cart_num = parseInt(item.cart_num) + 1;
  251. var productInfo = item.productInfo;
  252. if (productInfo.hasOwnProperty('attrInfo') && item.cart_num >= item.productInfo.attrInfo.stock) {
  253. item.cart_num = item.productInfo.attrInfo.stock;
  254. item.numAdd = true;
  255. item.numSub = false;
  256. } else if (item.cart_num >= item.productInfo.stock) {
  257. item.cart_num = item.productInfo.stock;
  258. item.numAdd = true;
  259. item.numSub = false;
  260. } else {
  261. item.numAdd = false;
  262. item.numSub = false;
  263. }
  264. that.setCartNum(item.id, item.cart_num, function (data) {
  265. var itemData = "cartList.valid[" + index + "]";
  266. that.setData({
  267. [itemData]: item
  268. });
  269. that.switchSelect();
  270. });
  271. },
  272. setCartNum(cartId, cartNum, successCallback) {
  273. var that = this;
  274. changeCartNum(cartId, cartNum).then(res => {
  275. successCallback && successCallback(res.data);
  276. });
  277. },
  278. getCartNum: function () {
  279. var that = this;
  280. getCartCounts().then(res => {
  281. that.setData({
  282. cartCount: res.data.count
  283. });
  284. });
  285. },
  286. getCartList: function () {
  287. var that = this;
  288. getCartList().then(res => {
  289. var cartList = res.data;
  290. var valid = cartList.valid;
  291. var numSub = [{
  292. numSub: true
  293. }, {
  294. numSub: false
  295. }];
  296. var numAdd = [{
  297. numAdd: true
  298. }, {
  299. numAdd: false
  300. }],
  301. selectValue = [];;
  302. if (valid.length > 0) {
  303. for (var index in valid) {
  304. if (valid[index].cart_num == 1) {
  305. valid[index].numSub = true;
  306. } else {
  307. valid[index].numSub = false;
  308. }
  309. var productInfo = valid[index].productInfo;
  310. if (productInfo.hasOwnProperty('attrInfo') && valid[index].cart_num == valid[index].productInfo.attrInfo.stock) {
  311. valid[index].numAdd = true;;
  312. } else if (valid[index].cart_num == valid[index].productInfo.stock) {
  313. valid[index].numAdd = true;;
  314. } else {
  315. valid[index].numAdd = false;
  316. }
  317. valid[index].checked = true;
  318. selectValue.push(valid[index].id);
  319. }
  320. }
  321. that.setData({
  322. cartList: cartList,
  323. goodsHidden: cartList.valid.length <= 0 ? false : true,
  324. selectValue: selectValue,
  325. isAllSelect: valid.length == selectValue.length && valid.length
  326. });
  327. that.switchSelect();
  328. });
  329. },
  330. getHostProduct: function () {
  331. var that = this;
  332. getProductHot().then(res => {
  333. that.setData({
  334. host_product: res.data
  335. });
  336. });
  337. },
  338. goodsOpen: function () {
  339. var that = this;
  340. that.setData({
  341. goodsHidden: !that.data.goodsHidden
  342. })
  343. },
  344. manage: function () {
  345. var that = this;
  346. that.setData({
  347. footerswitch: !that.data.footerswitch
  348. })
  349. },
  350. /**
  351. * 生命周期函数--监听页面初次渲染完成
  352. */
  353. onReady: function () {
  354. },
  355. onLoadFun: function () {
  356. this.getHostProduct();
  357. this.getCartList();
  358. this.getCartNum();
  359. },
  360. /**
  361. * 生命周期函数--监听页面显示
  362. */
  363. onShow: function () {
  364. if (app.globalData.isLog == true) {
  365. this.getHostProduct();
  366. this.getCartList();
  367. this.getCartNum();
  368. this.setData({
  369. goodsHidden: true,
  370. footerswitch: true,
  371. host_product: [],
  372. cartList: [],
  373. isAllSelect: false, //全选
  374. selectValue: [], //选中的数据
  375. selectCountPrice: 0.00,
  376. cartCount: 0,
  377. isHidden: true
  378. });
  379. }
  380. },
  381. unsetCart: function () {
  382. let that = this,
  383. ids = [];
  384. for (var i = 0, len = that.data.cartList.invalid.length; i < len; i++) {
  385. ids.push(that.data.cartList.invalid[i].id);
  386. }
  387. cartDel(ids).then(res => {
  388. app.Tips({
  389. title: '清除成功'
  390. });
  391. that.setData({
  392. 'cartList.invalid': []
  393. });
  394. }).catch(res => {
  395. });
  396. },
  397. /**
  398. * 生命周期函数--监听页面隐藏
  399. */
  400. onHide: function () {
  401. },
  402. /**
  403. * 生命周期函数--监听页面卸载
  404. */
  405. onUnload: function () {
  406. },
  407. /**
  408. * 页面相关事件处理函数--监听用户下拉动作
  409. */
  410. onPullDownRefresh: function () {
  411. },
  412. })