confirmOrder.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // pages/confirmOrder/confirmOrder.js
  2. const getReq = require('./../../config.js').getReq
  3. let appInstance = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. goods_id: '',
  10. ifcart: '',
  11. num: '',
  12. datas: {},
  13. goods_list: [],
  14. firstLoad: true,
  15. defaultAddress: null,
  16. cart_id: ''
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. let goods_id = options.goods_id || ''
  23. let iscart = options.iscart || ''
  24. let num = options.num || ''
  25. let cart_id = options.cart_id || ''
  26. this.setData({
  27. goods_id,
  28. ifcart: iscart,
  29. num,
  30. cart_id
  31. })
  32. this.getDatas(goods_id, iscart, num, cart_id)
  33. },
  34. getDatas(goods_id, ifcart, num, cart_id) {
  35. wx.showLoading({
  36. title: '加载中',
  37. })
  38. var self = this
  39. let goods_datas = '';
  40. if (ifcart == 0) {
  41. goods_datas = goods_id + '|' + num;
  42. }
  43. else {
  44. goods_datas = cart_id;
  45. }
  46. getReq({
  47. act: 'member_buy',
  48. op: 'step_first',
  49. curpage: 1,
  50. cart_id: goods_datas,
  51. ifcart,
  52. }, function (res) {
  53. console.log(res);
  54. wx.hideLoading()
  55. if (res.code == 200) {
  56. let datas = res.datas
  57. let goods_list = self.getGoodsList(datas.summary, datas.goods_list)
  58. if (datas.address) {
  59. let { true_name, mob_phone, area_info, address, address_id } = datas.address;
  60. appInstance.checkDefaultAddress({
  61. true_name,
  62. mob_phone,
  63. area_info,
  64. address,
  65. address_id
  66. })
  67. }
  68. self.setData({
  69. datas,
  70. goods_list,
  71. firstLoad: false,
  72. defaultAddress: datas.address
  73. })
  74. }
  75. else {
  76. wx.showToast({
  77. icon: 'none',
  78. title: res.message,
  79. duration: 2000
  80. })
  81. appInstance.globalData.fcodeErr = res.message
  82. wx.navigateBack()
  83. }
  84. })
  85. },
  86. getGoodsList(summary, goods_list) {
  87. let newGoodsList = []
  88. goods_list.map((item, index) => {
  89. summary.filter((list, key) => {
  90. if (item.goods_id == list.goods_id) {
  91. let goods_item = list
  92. goods_item.goods_num = item.goods_num
  93. newGoodsList.push(goods_item)
  94. return true
  95. }
  96. })
  97. })
  98. return newGoodsList
  99. },
  100. toPay() {
  101. if (!this.data.defaultAddress) {
  102. wx.showToast({
  103. icon: 'none',
  104. title: '请填写收货地址',
  105. duration: 1500
  106. })
  107. return
  108. }
  109. let goods_id = this.data.goods_id
  110. let goods_num = this.data.num
  111. let iscart = this.data.ifcart
  112. let cart_id = this.data.cart_id
  113. let vat_hash = this.data.datas['payinfo'].vat_hash
  114. let offpay_hash = this.data.datas['payinfo'].offpay_hash
  115. let offpay_hash_batch = this.data.datas['payinfo'].offpay_hash_batch
  116. let address_id = this.data.defaultAddress.address_id ? this.data.defaultAddress.address_id : this.data.datas['address'].address_id
  117. let ifcart = cart_id ? 1 : 0
  118. let cartids = cart_id ? cart_id : (goods_id + '|' + goods_num)
  119. getReq({
  120. act: 'member_buy',
  121. op: 'step_second',
  122. payment: 'jspay',
  123. usebonus: 1,
  124. ifcart,
  125. cart_id: cartids,
  126. address_id,
  127. invoice_id: 0,
  128. vat_hash,
  129. offpay_hash,
  130. offpay_hash_batch
  131. }, function (res) {
  132. wx.hideLoading()
  133. if (res.code == 200) {
  134. let param = res.datas.param.data;
  135. wx.requestPayment({
  136. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  137. nonceStr: param.nonceStr, //随机串
  138. package: param.package,
  139. signType: param.signType, //微信签名方式:
  140. paySign: param.paySign, //微信签名
  141. success:function(res){
  142. wx.showToast({
  143. icon: 'none',
  144. title: res,
  145. duration: 2000
  146. })
  147. console.log("成功:"+res);
  148. console.log('支付成功')
  149. },
  150. fail:function(res){
  151. wx.showToast({
  152. icon: 'none',
  153. title: res,
  154. duration: 2000
  155. })
  156. console.log("失败:" + res);
  157. }
  158. });
  159. }
  160. else {
  161. wx.showToast({
  162. icon: 'none',
  163. title: res.message,
  164. duration: 2000
  165. })
  166. appInstance.globalData.fcodeErr = res.message
  167. wx.navigateBack()
  168. }
  169. })
  170. },
  171. /**
  172. * 生命周期函数--监听页面初次渲染完成
  173. */
  174. onReady: function () {
  175. },
  176. /**
  177. * 生命周期函数--监听页面显示
  178. */
  179. onShow: function () {
  180. if (!this.data.firstLoad) {
  181. let defaultAddress = appInstance.globalData.defaultAddress
  182. this.setData({
  183. defaultAddress
  184. })
  185. }
  186. },
  187. /**
  188. * 生命周期函数--监听页面隐藏
  189. */
  190. onHide: function () {
  191. },
  192. /**
  193. * 生命周期函数--监听页面卸载
  194. */
  195. onUnload: function () {
  196. },
  197. /**
  198. * 页面相关事件处理函数--监听用户下拉动作
  199. */
  200. onPullDownRefresh: function () {
  201. },
  202. /**
  203. * 页面上拉触底事件的处理函数
  204. */
  205. onReachBottom: function () {
  206. }
  207. })