order_addcart.js 10 KB

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