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. let datas = res.datas
  72. let { goods_list, summary, bundling, payinfo } = res.datas
  73. let { freight_hash, goods_amount, freight, available_pred = 0, user_bonus = 0, room_bonus = 0, full_discount = 0, opgoods_discount = 0 } = payinfo
  74. let totalPrice = parseFloat((goods_amount + freight - user_bonus - room_bonus - full_discount - opgoods_discount).toFixed(2))
  75. let n_goods_list = self.getNewGoodsList(goods_list, summary, bundling)
  76. if (datas.address) {
  77. let { true_name, mob_phone, area_info, address, address_id, area_id, city_id } = datas.address;
  78. app.checkDefaultAddress({
  79. true_name,
  80. mob_phone,
  81. area_info,
  82. address,
  83. address_id,
  84. area_id,
  85. city_id
  86. })
  87. }
  88. self.setData({
  89. datas,
  90. freight_hash,
  91. n_goods_list,
  92. freight,
  93. totalPrice,
  94. firstLoad: false,
  95. defaultAddress: datas.address,
  96. available_pred,
  97. room_bonus,
  98. full_discount,
  99. opgoods_discount
  100. })
  101. }
  102. else {
  103. wx.showToast({
  104. icon: 'none',
  105. title: res.message,
  106. duration: 2000
  107. })
  108. app.globalData.fcodeErr = res.message
  109. wx.navigateBack()
  110. }
  111. })
  112. },
  113. toPay() {
  114. if (!this.data.defaultAddress) {
  115. wx.showToast({
  116. icon: 'none',
  117. title: '请填写收货地址',
  118. duration: 1500
  119. })
  120. return
  121. }
  122. let goods_id = this.data.goods_id
  123. let goods_num = this.data.num
  124. let iscart = this.data.ifcart
  125. let cart_id = this.data.cart_id
  126. let vat_hash = this.data.datas['payinfo'].vat_hash
  127. let offpay_hash = this.data.datas['payinfo'].offpay_hash
  128. let offpay_hash_batch = this.data.datas['payinfo'].offpay_hash_batch
  129. let address_id = this.data.defaultAddress.address_id ? this.data.defaultAddress.address_id : this.data.datas['address'].address_id
  130. let ifcart = cart_id ? 1 : 0
  131. let cartids = cart_id ? cart_id : (goods_id + '|' + goods_num)
  132. getReq({
  133. act: 'member_buy',
  134. op: 'step_second',
  135. // payment: 'jspay',
  136. payment: 'minipay',
  137. usebonus: 1,
  138. ifcart,
  139. cart_id: cartids,
  140. address_id,
  141. invoice_id: 0,
  142. vat_hash,
  143. offpay_hash,
  144. offpay_hash_batch
  145. }, function (res) {
  146. wx.hideLoading()
  147. if (res.code == 200) {
  148. let param = res.datas.param.data
  149. let pay_sn = res.datas.pay_sn
  150. wx.requestPayment({
  151. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  152. nonceStr: param.nonceStr, //随机串
  153. package: param.package,
  154. signType: param.signType, //微信签名方式:
  155. paySign: param.paySign, //微信签名
  156. success: function (res) {
  157. wx.reLaunch({
  158. url: "/pages/index/index"
  159. })
  160. },
  161. fail: function (res) {
  162. wx.redirectTo({
  163. url: `/pages/orderPaySn/orderPaySn?pay_sn=${pay_sn}`
  164. })
  165. }
  166. });
  167. }
  168. else {
  169. wx.showToast({
  170. icon: 'none',
  171. title: res.message,
  172. duration: 2000
  173. })
  174. app.globalData.fcodeErr = res.message
  175. setTimeout(() => {
  176. wx.navigateBack()
  177. }, 2000);
  178. }
  179. })
  180. },
  181. getNewGoodsList(goods_list, summary, bundling) {
  182. let summaryMap = new Map()
  183. let bundlingMap = new Map()
  184. summary.map(item => {
  185. summaryMap.set(item['goods_id'], item)
  186. })
  187. if (bundling.length) {
  188. bundling.map(item => {
  189. bundlingMap.set(item['bl_id'], item)
  190. })
  191. }
  192. let n_goods_list = new Bundle(goods_list, summaryMap, bundlingMap).createOrders()
  193. return n_goods_list
  194. },
  195. /**
  196. * 生命周期函数--监听页面初次渲染完成
  197. */
  198. onReady: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面显示
  202. */
  203. onShow: function () {
  204. if (!this.data.firstLoad) {
  205. app.setFromSource(this.data.fromSource)
  206. let self = this
  207. let defaultAddress = app.globalData.defaultAddress
  208. let { city_id, area_id } = defaultAddress
  209. let { freight_hash } = self.data
  210. let params = {
  211. act: 'member_buy',
  212. op: 'change_addrex',
  213. city_id,
  214. area_id,
  215. freight_hash
  216. }
  217. getReq(params, function(res) {
  218. if (res.code == 200) {
  219. let { freight } = res.datas
  220. let prev_freight = self.data.freight
  221. let totalPrice = parseFloat((self.data.totalPrice - prev_freight + freight).toFixed(2))
  222. self.setData({
  223. totalPrice,
  224. freight,
  225. defaultAddress
  226. })
  227. }
  228. })
  229. }
  230. },
  231. /**
  232. * 生命周期函数--监听页面隐藏
  233. */
  234. onHide: function () {
  235. },
  236. /**
  237. * 生命周期函数--监听页面卸载
  238. */
  239. onUnload: function () {
  240. },
  241. /**
  242. * 页面相关事件处理函数--监听用户下拉动作
  243. */
  244. onPullDownRefresh: function () {
  245. },
  246. /**
  247. * 页面上拉触底事件的处理函数
  248. */
  249. onReachBottom: function () {
  250. }
  251. })