submitBuy.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const cityData = require('../../utils/picker.city.all.js')
  2. const util = require('../../utils/util')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. selectList: cityData, //接口返回picker数据,此处就直接使用本地测试数据
  10. multiArray: [], //picker数据
  11. values:{
  12. receiver:'',
  13. receiverPhone:'',
  14. receiverArea:'',
  15. recevierAddress:'',
  16. storeId:'',
  17. productOrderDetailList:[]
  18. },
  19. area: [0, 0, 0],
  20. totalPrice:0,
  21. region: [],
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. this.data.values.storeId = options.storeId
  28. if(options && options.strJson){
  29. var list = JSON.parse(decodeURIComponent(options.strJson))
  30. var totalPrice = 0
  31. list.forEach((e)=>{
  32. totalPrice += e.productPrice * e.productNum
  33. })
  34. this.setData({
  35. totalPrice: totalPrice,
  36. ["values.productOrderDetailList"]: list
  37. })
  38. }
  39. this.setData({
  40. multiArray: [
  41. this.toArr(this.data.selectList),
  42. this.toArr(this.data.selectList[0].children),
  43. this.toArr(this.data.selectList[0].children[0].children)
  44. ]
  45. })
  46. },
  47. picker: function(e) {
  48. let value = e.detail.value;
  49. if (this.data.selectList.length > 0) {
  50. let provice = this.data.selectList[value[0]].text
  51. let city = this.data.selectList[value[0]].children[value[1]].text
  52. let district = this.data.selectList[value[0]].children[value[1]].children[value[2]].text
  53. this.setData({
  54. ["values.receiverArea"]: provice + " " + city + " " + district,
  55. id: this.data.selectList[value[0]].children[value[1]].children[value[2]].value
  56. })
  57. }
  58. },
  59. toArr(object) {
  60. let arr = [];
  61. for (let i in object) {
  62. arr.push(object[i].text);
  63. }
  64. return arr;
  65. },
  66. columnPicker: function(e) {
  67. //第几列 下标从0开始
  68. let column = e.detail.column;
  69. //第几行 下标从0开始
  70. let value = e.detail.value;
  71. if (column === 0) {
  72. this.setData({
  73. multiArray: [
  74. this.data.multiArray[0],
  75. this.toArr(this.data.selectList[value].children),
  76. this.toArr(this.data.selectList[value].children[0].children)
  77. ],
  78. area: [value, 0, 0]
  79. })
  80. } else if (column === 1) {
  81. this.setData({
  82. multiArray: [
  83. this.data.multiArray[0],
  84. this.data.multiArray[1],
  85. this.toArr(this.data.selectList[this.data.value[0]].children[value].children)
  86. ],
  87. area: [this.data.value[0], value, 0]
  88. })
  89. }
  90. },
  91. productNumChange(e){
  92. let that = this;
  93. let dataset = e.currentTarget.dataset;
  94. let value = e.detail.value;
  95. var list = that.data.values.productOrderDetailList
  96. var index = dataset.index;
  97. var itemProduct = dataset.item
  98. itemProduct.productNum = value
  99. list.splice(index,1,itemProduct)
  100. that.setData({
  101. ["values.productOrderDetailList"]:list
  102. })
  103. var list = this.data.values.productOrderDetailList.filter((e)=>e.productNum > 0)
  104. var totalPrice = 0
  105. list.forEach((e)=>{
  106. totalPrice += e.productPrice * e.productNum
  107. })
  108. this.setData({
  109. totalPrice: totalPrice
  110. })
  111. },
  112. bindRegionChange: function (e) {
  113. console.log(e.detail.value)
  114. console.log(e.detail.code)
  115. this.setData({
  116. region: e.detail.value
  117. })
  118. this.data.values.receiverArea = this.data.region.join(" ")
  119. },
  120. submitPay(){
  121. var that = this
  122. var list = that.data.values.productOrderDetailList.filter((e)=>e.productNum > 0)
  123. if(list.length < 1){
  124. util.toast('请选择下单商品,并且订货数量必须大于0')
  125. return
  126. }
  127. var that = this
  128. // util.request(util.api.generateOrder, {openId:app.globalData.openId,deviceId:app.globalData.deviceId,gridNum:that.data.productGrid}, "POST", true, true,app.globalData.token).then((res)=>{
  129. // if (res.code == 200) {
  130. // wx.requestPayment({
  131. // 'timeStamp': res.result.result.timeStamp + '',
  132. // 'nonceStr': res.result.result.nonceStr,
  133. // 'package': res.result.result.packageValue,
  134. // 'signType': 'MD5',
  135. // 'paySign': res.result.result.paySign,
  136. // 'success': function (res) {
  137. // that.openGrate(1)
  138. // wx.redirectTo({
  139. // url: 'success'
  140. // })
  141. // },
  142. // 'fail': function (res) {
  143. // }
  144. // })
  145. // }else if(res.code == 500){
  146. // util.toast(res.message)
  147. // }
  148. // })
  149. // },
  150. util.request(util.api.addMerchantOrder, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
  151. if (res.code == 200) {
  152. wx.requestPayment({
  153. 'timeStamp': res.result.result.timeStamp + '',
  154. 'nonceStr': res.result.result.nonceStr,
  155. 'package': res.result.result.packageValue,
  156. 'signType': 'MD5',
  157. 'paySign': res.result.result.paySign,
  158. 'success': function (res) {
  159. wx.redirectTo({
  160. url: 'success'
  161. })
  162. },
  163. 'fail': function (res) {
  164. }
  165. })
  166. }else if(res.code == 500){
  167. util.toast(res.message)
  168. }
  169. // if (res.code == 200) {
  170. // util.toast("下单成功")
  171. // wx.redirectTo({
  172. // url: '/pages/orderGoods/order',
  173. // })
  174. // }
  175. }).catch((res) => {})
  176. },
  177. inputedit: function (e) {
  178. let _this = this;
  179. let dataset = e.currentTarget.dataset;
  180. let value = e.detail.value;
  181. let name = dataset.name;
  182. if (dataset.type && dataset.type == 'float') {
  183. _this.data[name] = parseFloat(value * 100);
  184. } else {
  185. _this.data[name] = value;
  186. }
  187. _this.setData({
  188. [name]: _this.data[name]
  189. });
  190. }
  191. })