confirmOrder.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. let params = {
  133. act: 'member_buy',
  134. op: 'step_second',
  135. payment: 'minipay',
  136. usebonus: 1,
  137. ifcart,
  138. cart_id: cartids,
  139. address_id,
  140. invoice_id: 0,
  141. vat_hash,
  142. offpay_hash,
  143. offpay_hash_batch
  144. }
  145. let options = {
  146. room_id: 0
  147. }
  148. if (this.data.datas['rooms']&&this.data.datas['rooms'].length) {
  149. options = {
  150. room_id: this.data.datas['rooms'][0]['room_id']
  151. }
  152. }
  153. params = Object.assign({}, options, params);
  154. getReq(params, function (res) {
  155. wx.hideLoading()
  156. if (res.code == 200) {
  157. let param = res.datas.param.data
  158. let pay_sn = res.datas.pay_sn
  159. wx.requestPayment({
  160. timeStamp: param.timeStamp, //时间戳,自1970年以来的秒数
  161. nonceStr: param.nonceStr, //随机串
  162. package: param.package,
  163. signType: param.signType, //微信签名方式:
  164. paySign: param.paySign, //微信签名
  165. success: function (res) {
  166. wx.reLaunch({
  167. url: "/pages/index/index"
  168. })
  169. },
  170. fail: function (res) {
  171. wx.redirectTo({
  172. url: `/pages/orderPaySn/orderPaySn?pay_sn=${pay_sn}`
  173. })
  174. }
  175. });
  176. }
  177. else {
  178. wx.showToast({
  179. icon: 'none',
  180. title: res.message,
  181. duration: 2000
  182. })
  183. app.globalData.fcodeErr = res.message
  184. setTimeout(() => {
  185. wx.navigateBack()
  186. }, 2000);
  187. }
  188. })
  189. },
  190. getNewGoodsList(goods_list, summary, bundling) {
  191. let summaryMap = new Map()
  192. let bundlingMap = new Map()
  193. summary.map(item => {
  194. summaryMap.set(item['goods_id'], item)
  195. })
  196. if (bundling.length) {
  197. bundling.map(item => {
  198. bundlingMap.set(item['bl_id'], item)
  199. })
  200. }
  201. let n_goods_list = new Bundle(goods_list, summaryMap, bundlingMap).createOrders()
  202. return n_goods_list
  203. },
  204. /**
  205. * 生命周期函数--监听页面初次渲染完成
  206. */
  207. onReady: function () {
  208. },
  209. /**
  210. * 生命周期函数--监听页面显示
  211. */
  212. onShow: function () {
  213. if (!this.data.firstLoad) {
  214. app.setFromSource(this.data.fromSource)
  215. let self = this
  216. let defaultAddress = app.globalData.defaultAddress
  217. let { city_id, area_id } = defaultAddress
  218. let { freight_hash } = self.data
  219. let params = {
  220. act: 'member_buy',
  221. op: 'change_addrex',
  222. city_id,
  223. area_id,
  224. freight_hash
  225. }
  226. getReq(params, function(res) {
  227. if (res.code == 200) {
  228. let { freight } = res.datas
  229. let prev_freight = self.data.freight
  230. let totalPrice = parseFloat((self.data.totalPrice - prev_freight + freight).toFixed(2))
  231. self.setData({
  232. totalPrice,
  233. freight,
  234. defaultAddress
  235. })
  236. }
  237. })
  238. }
  239. },
  240. /**
  241. * 生命周期函数--监听页面隐藏
  242. */
  243. onHide: function () {
  244. },
  245. /**
  246. * 生命周期函数--监听页面卸载
  247. */
  248. onUnload: function () {
  249. },
  250. /**
  251. * 页面相关事件处理函数--监听用户下拉动作
  252. */
  253. onPullDownRefresh: function () {
  254. },
  255. /**
  256. * 页面上拉触底事件的处理函数
  257. */
  258. onReachBottom: function () {
  259. }
  260. })