accounts.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. let app = getApp();
  2. let host = app.globalData.servsers; // 请求的url
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. goodsList: [],
  9. host: app.globalData.servsers,
  10. amount: '',
  11. address: {}, // 地址为空
  12. },
  13. onLoad() {
  14. let that = this;
  15. wx.request({
  16. url: host + '/adressapi/getdefaustAdress',
  17. method: 'get',
  18. data: {
  19. user_id: app.globalData.user_id
  20. },
  21. header: {
  22. 'content-type': 'application/x-www-form-urlencoded'
  23. },
  24. success(res) {
  25. console.log(res, "获取默认地址")
  26. that.setData({
  27. address: res.data
  28. })
  29. }
  30. })
  31. },
  32. /**
  33. * 生命周期函数--监听页面显示
  34. */
  35. onShow: function () {
  36. let goodsList = [],
  37. that = this;
  38. let address = wx.getStorageSync('accountAddress');
  39. console.log(address, "addressaddress")
  40. if (address) {
  41. that.setData({
  42. address: address
  43. })
  44. }
  45. goodsList = [...wx.getStorageSync('goodsList')];
  46. that.setData({
  47. goodsList: goodsList,
  48. amount: wx.getStorageSync('amount')
  49. })
  50. },
  51. /**
  52. * 生命周期函数--监听页面隐藏
  53. */
  54. onHide: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面卸载
  58. */
  59. onUnload: function () {
  60. },
  61. /**
  62. * 页面相关事件处理函数--监听用户下拉动作
  63. */
  64. onPullDownRefresh: function () {
  65. },
  66. /**
  67. * 页面上拉触底事件的处理函数
  68. */
  69. onReachBottom: function () {
  70. },
  71. /**
  72. * 用户点击右上角分享
  73. */
  74. onShareAppMessage: function () {
  75. },
  76. submitOrder() {
  77. if (!this.data.address.consignee) {
  78. wx.showToast({
  79. title: '您还没有添加收货地址',
  80. icon: 'none'
  81. })
  82. return;
  83. }
  84. wx.showLoading({
  85. title: '结算中...'
  86. })
  87. let that = this,
  88. goodsList = that.data.goodsList,
  89. address = that.data.address;
  90. //生成订单
  91. goodsList.forEach(res => {
  92. res['userId'] = app.globalData.user_id; //'oovpNwjlsY6xx8ceCebFa1dOLd9E',
  93. res['adress'] = address.adress;
  94. res['area'] = address.area;
  95. res['city'] = address.city;
  96. res['province'] = address.province;
  97. res['consignee'] = address.consignee;
  98. res['phone'] = address.phone;
  99. res['openId'] = app.globalData.openid;
  100. delete res.imgArray
  101. });
  102. wx.request({
  103. url: host + '/createNewOrderCustomer',
  104. method: 'post',
  105. data: {
  106. shopCarList: JSON.stringify(goodsList)
  107. },
  108. success(el) {
  109. let res = el.data;
  110. console.log(res)
  111. wx.requestPayment({
  112. timeStamp: res.timeStamp,
  113. nonceStr: res.nonceStr,
  114. package: res.package,
  115. signType: res.signType,
  116. paySign: res.paySign,
  117. success(res) {
  118. console.log(res, "结算结果")
  119. if (res.errMsg === 'requestPayment:ok') {
  120. // wx.showToast({
  121. // title: '支付成功',
  122. // })
  123. wx.redirectTo({
  124. url: '/customized/pages/result/result?state=true',
  125. })
  126. } else {
  127. wx.redirectTo({
  128. url: '/customized/pages/result/result?state=false',
  129. })
  130. }
  131. },
  132. fail(err) {
  133. wx.showToast({
  134. title: err.errMsg,
  135. icon: 'error'
  136. })
  137. console.log(err)
  138. },
  139. complete() {
  140. wx.hideLoading()
  141. }
  142. })
  143. }
  144. })
  145. },
  146. addressBtn() {
  147. wx.navigateTo({
  148. url: '/customized/pages/address/address',
  149. })
  150. }
  151. })