confirmOrder.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // pages/confirmOrder/confirmOrder.js
  2. const getReq = require('./../../config.js').getReq
  3. import recordSource from '../../utils/recordSource';
  4. import Bundle from '../../utils/Bundle'
  5. let app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. goods_id: '',
  12. ifcart: '',
  13. num: '',
  14. datas: {},
  15. goods_list: [],
  16. firstLoad: true,
  17. defaultAddress: null,
  18. cart_id: '',
  19. n_goods_list: [],
  20. freight: 0,
  21. freight_hash: '',
  22. fromSource: '',
  23. available_pred: 0,
  24. room_bonus: 0,
  25. full_discount: 0,
  26. opgoods_discount: 0
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. let goods_id = options.goods_id || ''
  33. let iscart = options.iscart || ''
  34. let num = options.num || ''
  35. let cart_id = options.cart_id || ''
  36. this.setData({
  37. goods_id,
  38. ifcart: iscart,
  39. num,
  40. cart_id
  41. })
  42. this.getDatas(goods_id, iscart, num, cart_id)
  43. },
  44. getDatas(goods_id, ifcart, num, cart_id) {
  45. wx.showLoading({
  46. title: '加载中',
  47. })
  48. var self = this
  49. let goods_datas = '';
  50. if (ifcart == 0) {
  51. goods_datas = goods_id + '|' + num;
  52. }
  53. else {
  54. goods_datas = cart_id;
  55. }
  56. let fromSource = recordSource(app, `act=member_buy&op=step_first&curpage=1&cart_id=${goods_datas}&ifcart=${ifcart}&client_type=mini`)
  57. let params = {
  58. act: 'member_buy',
  59. op: 'step_first',
  60. curpage: 1,
  61. cart_id: goods_datas,
  62. ifcart,
  63. from: fromSource
  64. }
  65. this.setData({
  66. fromSource: app.globalData.fromSource
  67. })
  68. getReq(params, function (res) {
  69. wx.hideLoading()
  70. if (res.code == 200) {
  71. console.log('confirmOrder:', res);
  72. let datas = res.datas
  73. let { goods_list, summary, bundling, payinfo } = res.datas
  74. let { freight_hash, goods_amount, freight, available_pred = 0, user_bonus = 0, room_bonus = 0, full_discount = 0, opgoods_discount = 0 } = payinfo
  75. let totalPrice = (goods_amount + freight - available_pred - user_bonus - room_bonus - full_discount - opgoods_discount).toFixed(2)
  76. let n_goods_list = self.getNewGoodsList(goods_list, summary, bundling)
  77. if (datas.address) {
  78. let { true_name, mob_phone, area_info, address, address_id, area_id, city_id } = datas.address;
  79. app.checkDefaultAddress({
  80. true_name,
  81. mob_phone,
  82. area_info,
  83. address,
  84. address_id,
  85. area_id,
  86. city_id
  87. })
  88. }
  89. self.setData({
  90. datas,
  91. freight_hash,
  92. n_goods_list,
  93. freight,
  94. totalPrice,
  95. firstLoad: false,
  96. defaultAddress: datas.address,
  97. available_pred,
  98. room_bonus,
  99. full_discount,
  100. opgoods_discount
  101. })
  102. }
  103. else {
  104. wx.showToast({
  105. icon: 'none',
  106. title: res.message,
  107. duration: 2000
  108. })
  109. app.globalData.fcodeErr = res.message
  110. wx.navigateBack()
  111. }
  112. })
  113. },
  114. toPay() {
  115. if (!this.data.defaultAddress) {
  116. wx.showToast({
  117. icon: 'none',
  118. title: '请填写收货地址',
  119. duration: 1500
  120. })
  121. return
  122. }
  123. let goods_id = this.data.goods_id
  124. let goods_num = this.data.num
  125. let iscart = this.data.ifcart
  126. let cart_id = this.data.cart_id
  127. let vat_hash = this.data.datas['payinfo'].vat_hash
  128. let offpay_hash = this.data.datas['payinfo'].offpay_hash
  129. let offpay_hash_batch = this.data.datas['payinfo'].offpay_hash_batch
  130. let address_id = this.data.defaultAddress.address_id ? this.data.defaultAddress.address_id : this.data.datas['address'].address_id
  131. let ifcart = cart_id ? 1 : 0
  132. let cartids = cart_id ? cart_id : (goods_id + '|' + goods_num)
  133. getReq({
  134. act: 'member_buy',
  135. op: 'step_second',
  136. // payment: 'jspay',
  137. payment: 'minipay',
  138. usebonus: 1,
  139. ifcart,
  140. cart_id: cartids,
  141. address_id,
  142. invoice_id: 0,
  143. vat_hash,
  144. offpay_hash,
  145. offpay_hash_batch
  146. }, function (res) {
  147. wx.hideLoading()
  148. if (res.code == 200) {
  149. let param = res.datas.param.data
  150. let pay_sn = res.datas.pay_sn
  151. wx.requestPayment({
  152. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  153. nonceStr: param.nonceStr, //随机串
  154. package: param.package,
  155. signType: param.signType, //微信签名方式:
  156. paySign: param.paySign, //微信签名
  157. success: function (res) {
  158. wx.reLaunch({
  159. url: "/pages/index/index"
  160. })
  161. },
  162. fail: function (res) {
  163. wx.redirectTo({
  164. url: `/pages/orderPaySn/orderPaySn?pay_sn=${pay_sn}`
  165. })
  166. }
  167. });
  168. }
  169. else {
  170. wx.showToast({
  171. icon: 'none',
  172. title: res.message,
  173. duration: 2000
  174. })
  175. app.globalData.fcodeErr = res.message
  176. wx.navigateBack()
  177. }
  178. })
  179. },
  180. getNewGoodsList(goods_list, summary, bundling) {
  181. let summaryMap = new Map()
  182. let bundlingMap = new Map()
  183. summary.map(item => {
  184. summaryMap.set(item['goods_id'], item)
  185. })
  186. if (bundling.length) {
  187. bundling.map(item => {
  188. bundlingMap.set(item['bl_id'], item)
  189. })
  190. }
  191. let n_goods_list = new Bundle(goods_list, summaryMap, bundlingMap).createOrders()
  192. return n_goods_list
  193. },
  194. /**
  195. * 生命周期函数--监听页面初次渲染完成
  196. */
  197. onReady: function () {
  198. },
  199. /**
  200. * 生命周期函数--监听页面显示
  201. */
  202. onShow: function () {
  203. if (!this.data.firstLoad) {
  204. app.setFromSource(this.data.fromSource)
  205. let self = this
  206. let defaultAddress = app.globalData.defaultAddress
  207. let { city_id, area_id } = defaultAddress
  208. let { freight_hash } = self.data
  209. let params = {
  210. act: 'member_buy',
  211. op: 'change_addrex',
  212. city_id,
  213. area_id,
  214. freight_hash
  215. }
  216. getReq(params, function(res) {
  217. if (res.code == 200) {
  218. let { freight } = res.datas
  219. let prev_freight = self.data.freight
  220. let totalPrice = (self.data.totalPrice - prev_freight + freight).toFixed(2)
  221. self.setData({
  222. totalPrice,
  223. freight,
  224. defaultAddress
  225. })
  226. }
  227. })
  228. }
  229. },
  230. /**
  231. * 生命周期函数--监听页面隐藏
  232. */
  233. onHide: function () {
  234. },
  235. /**
  236. * 生命周期函数--监听页面卸载
  237. */
  238. onUnload: function () {
  239. },
  240. /**
  241. * 页面相关事件处理函数--监听用户下拉动作
  242. */
  243. onPullDownRefresh: function () {
  244. },
  245. /**
  246. * 页面上拉触底事件的处理函数
  247. */
  248. onReachBottom: function () {
  249. }
  250. })