buy.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const util = require('../../utils/util')
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. storeList:[],
  9. searchParams:{
  10. storeId:'',
  11. storeName:''
  12. },
  13. productList:[],
  14. totalPrice:0,
  15. sellProductList:[]
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. this.bindStoreList()
  22. this.bindProductStockList()
  23. },
  24. bindStoreList() {
  25. var that = this
  26. util.request(util.api.storeList, {}, "GET", false, true, app.globalData.token).then((res) => {
  27. if (res.code == 200) {
  28. that.setData({
  29. storeList: res.result
  30. })
  31. }
  32. }).catch((res) => {})
  33. },
  34. bindStorePickerChange: function (e) {
  35. var item = this.data.storeList[e.detail.value]
  36. console.log(item)
  37. this.setData({
  38. ['searchParams.storeId']: item.id,
  39. ['searchParams.storeName']: item.storeName
  40. })
  41. this.bindProductStockList()
  42. },
  43. changeNum: function (e) {
  44. this.setData({
  45. productNum: e.detail.value
  46. })
  47. },
  48. bindProductStockList() {
  49. var that = this
  50. util.request(util.api.productStockList, that.data.searchParams, "GET", false, true, app.globalData.token).then((res) => {
  51. if (res.code == 200) {
  52. res.result.forEach(item => {
  53. item.checkout = false
  54. item.productNum = 0
  55. });
  56. that.setData({
  57. productList: res.result
  58. })
  59. }
  60. }).catch((res) => {})
  61. },
  62. productNumChange(e){
  63. let that = this;
  64. let dataset = e.currentTarget.dataset;
  65. let value = e.detail.value;
  66. var list = that.data.productList
  67. var index = dataset.index;
  68. var itemProduct = dataset.item
  69. itemProduct.productNum = value
  70. list.splice(index,1,itemProduct)
  71. that.setData({
  72. productList:list
  73. })
  74. var list = this.data.productList.filter((e)=>e.checkout == true && e.productNum > 0)
  75. var totalPrice = 0
  76. list.forEach((e)=>{
  77. totalPrice += e.productPrice * e.productNum
  78. })
  79. this.setData({
  80. totalPrice: totalPrice,
  81. sellProductList: list
  82. })
  83. },
  84. checkboxChange(e){
  85. let that = this;
  86. let dataset = e.currentTarget.dataset;
  87. let value = e.detail.value;
  88. var list = that.data.productList
  89. var index = dataset.index;
  90. var itemProduct = dataset.item
  91. itemProduct.checkout = value.length>0? true : false
  92. list.splice(index,1,itemProduct)
  93. that.setData({
  94. productList:list
  95. })
  96. var list = this.data.productList.filter((e)=>e.checkout == true && e.productNum > 0)
  97. var totalPrice = 0
  98. list.forEach((e)=>{
  99. totalPrice += e.productPrice * e.productNum
  100. })
  101. this.setData({
  102. totalPrice: totalPrice,
  103. sellProductList: list
  104. })
  105. },
  106. btnPay(){
  107. if(this.data.sellProductList.length<1){
  108. util.toast('请选择下单商品,并且订货数量必须大于0')
  109. return
  110. }
  111. console.log("1111")
  112. console.log(this.data.sellProductList)
  113. console.log("2222")
  114. wx.navigateTo({
  115. url: '/pages/orderGoods/submitBuy?strJson='+encodeURIComponent(JSON.stringify(this.data.sellProductList))+'&storeId='+this.data.searchParams.storeId,
  116. })
  117. }
  118. })