TransactionPage.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. const {
  2. default: api
  3. } = require("../../js/api");
  4. const util = require('../../utils/util.js')
  5. // pages/orderConfirm/orderConfirm.js
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. nozzleId: 0,
  12. type: '后支付',
  13. order: null,
  14. // order: {
  15. // oilName: '好名字',
  16. // nozzle: "1",
  17. // volume: "1",
  18. // amount: "1",
  19. // payAmount: "1",
  20. // discount: "1-1",
  21. // orderId: "1",
  22. // time: "2025-4-3"
  23. // },
  24. oprationBtn1: '',
  25. oprationBtn2: '对此订单有疑问?'
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad(options) {
  31. const link = decodeURIComponent(options.q) // 获取到二维码原始链接
  32. var id = link.split('wxapp?id=')[1];
  33. if (id == undefined) {
  34. id = options.id;
  35. }
  36. this.setData({
  37. nozzleId: id
  38. })
  39. this.getOrder()
  40. },
  41. /**
  42. * 生命周期函数--监听页面初次渲染完成
  43. */
  44. onReady() {
  45. },
  46. /**
  47. * 生命周期函数--监听页面显示
  48. */
  49. onShow() {
  50. },
  51. /**
  52. * 生命周期函数--监听页面隐藏
  53. */
  54. onHide() {
  55. },
  56. /**
  57. * 生命周期函数--监听页面卸载
  58. */
  59. onUnload() {
  60. },
  61. /**
  62. * 页面相关事件处理函数--监听用户下拉动作
  63. */
  64. onPullDownRefresh() {
  65. },
  66. /**
  67. * 页面上拉触底事件的处理函数
  68. */
  69. onReachBottom() {
  70. },
  71. /**
  72. * 用户点击右上角分享
  73. */
  74. onShareAppMessage() {
  75. },
  76. /** 获取订单信息 */
  77. getOrder() {
  78. wx.showLoading({
  79. title: '正在获取订单',
  80. })
  81. api.request_GetMiniProgramTransactionsUnpaidNozzle(Number(this.data.nozzleId))
  82. .then(res => {
  83. wx.hideLoading()
  84. if (res.data.statusCode == 203) {
  85. //若为203,证明还未登录,跳转到登录页,这里可能刚从主页跳转过来,频繁的跳转可能会跳转页面超时,故而加上延时
  86. setTimeout(() => {
  87. wx.navigateTo({
  88. url: '../login/login'
  89. })
  90. }, 500)
  91. return
  92. }
  93. console.log("获取到未支付订单", res)
  94. var order = res.data.data[0];
  95. var time = util.formatDateNotSecond(order.fuelItemTransactionEndTime);
  96. this.setData({
  97. order: {
  98. oilName: order.productName,
  99. nozzle: order.nozzleId,
  100. volume: order.originalQty,
  101. amount: order.originalAmount,
  102. payAmount: order.actualPaymentAmount,
  103. discount: order.originalAmount - order.actualPaymentAmount,
  104. orderId: order.id,
  105. time: time,
  106. price:order.price
  107. }
  108. })
  109. }).catch(err => {
  110. wx.hideLoading()
  111. console.log("未获取到未支付订单")
  112. })
  113. },
  114. /** 后支付查看更多交易 */
  115. toMordOrderPage() {
  116. wx.showLoading({
  117. title: '正在跳转',
  118. })
  119. var that = this;
  120. wx.navigateTo({
  121. url: '../moreOrder/moreOrder?nozzle=' + this.data.nozzleId,
  122. events: {
  123. acceptOrderFromMoreOrder: function (data) {
  124. console.log("获取到传送过来的订单", data)
  125. that.setData({
  126. order: data
  127. })
  128. }
  129. }
  130. })
  131. wx.hideLoading()
  132. },
  133. /** 支付按钮点击事件 */
  134. toPay() {
  135. wx.showLoading({
  136. title: '正在跳转',
  137. })
  138. this.subMessage()
  139. // var that = this;
  140. // api.request_wechatPay(this.data.order.orderId).then(res => {
  141. // console.log("支付结果", res)
  142. // if(res.data.statusCode != 200) {
  143. // wx.hideLoading()
  144. // wx.showModal({
  145. // title: '提示',
  146. // content: '支付失败',
  147. // })
  148. // return
  149. // }
  150. // this.subMessage()
  151. // // this.pay()
  152. // }).catch(err => {
  153. // wx.hideLoading()
  154. // console.log("支付失败", err)
  155. // wx.showModal({
  156. // title: '提示',
  157. // content: '支付失败',
  158. // })
  159. // })
  160. },
  161. /** 订阅消息模板 */
  162. subMessage() {
  163. const that = this;
  164. wx.requestSubscribeMessage({
  165. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  166. success(res) {
  167. that.pay()
  168. // 'accept'表示用户同意订阅该条id对应的模板消息
  169. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  170. // 用户同意订阅,调用云函数或服务器接口发送订阅消息
  171. // wx.cloud.callFunction({
  172. // name: 'sendSubscribeMessage',
  173. // data: {
  174. // templateId: '配置好的模板ID',
  175. // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA',
  176. // data: {
  177. // thing1: {
  178. // value: '活动名称'
  179. // },
  180. // // 其他参数...
  181. // }
  182. // },
  183. // success(res) {
  184. // console.log('订阅消息发送成功', res)
  185. // },
  186. // fail(err) {
  187. // console.error('订阅消息发送失败', err)
  188. // }
  189. // })
  190. }
  191. },
  192. fail(err) {
  193. that.pay()
  194. wx.showModal({
  195. title: '提示',
  196. content: '目前暂未获取到未支付订单',
  197. })
  198. }
  199. })
  200. },
  201. pay() {
  202. const that = this;
  203. api.request_wechatPay(this.data.order.orderId)
  204. .then(res => {
  205. wx.requestPayment({
  206. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  207. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  208. package: res.data.data.unifiedOrderResult.package,
  209. signType: res.data.data.unifiedOrderResult.signType,
  210. paySign: res.data.data.unifiedOrderResult.paySign,
  211. success: res => {
  212. wx.hideLoading()
  213. that.sendMessage()
  214. that.toPayResult()
  215. },
  216. fail: res => {
  217. wx.hideLoading()
  218. wx.showModal({
  219. title: '提示',
  220. content: '支付失败',
  221. })
  222. }
  223. })
  224. });
  225. },
  226. /** 发送消息模板 */
  227. sendMessage() {
  228. const message = {
  229. trxid: this.data.order.orderId,
  230. orderType: '支付'
  231. }
  232. api.request_sendMessage(message).then(res => {
  233. console.log("发送消息模板结果", res)
  234. }).catch(err => {
  235. console.log("发送消息模板失败", err)
  236. })
  237. },
  238. /** 跳转到支付结果页 */
  239. toPayResult() {
  240. wx.redirectTo({
  241. url: '../payResult/payResult',
  242. })
  243. },
  244. /** 跳转到支付结果页(不用这个) */
  245. toPayResult_back() {
  246. const that = this;
  247. setTimeout(function () {
  248. wx.navigateTo({
  249. url: '../payResult/payResult?nozzle=' + that.data.nozzleId,
  250. events: {
  251. acceptOrderFromPayResult: function (data) {
  252. console.log("获取到来自result页面传送过来的订单", data)
  253. that.setData({
  254. order: data
  255. })
  256. }
  257. }
  258. })
  259. }, 500)
  260. }
  261. })