TransactionPage.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. var that = this;
  139. api.request_wechatPay(this.data.order.orderId).then(res => {
  140. console.log("支付结果", res)
  141. if(res.data.statusCode != 200) {
  142. wx.hideLoading()
  143. wx.showModal({
  144. title: '提示',
  145. content: '支付失败',
  146. })
  147. return
  148. }
  149. this.subMessage()
  150. // this.pay()
  151. }).catch(err => {
  152. wx.hideLoading()
  153. console.log("支付失败", err)
  154. wx.showModal({
  155. title: '提示',
  156. content: '支付失败',
  157. })
  158. })
  159. },
  160. /** 订阅消息模板 */
  161. subMessage() {
  162. const that = this;
  163. wx.requestSubscribeMessage({
  164. tmplIds: ['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'], // 最多支持3条
  165. success(res) {
  166. that.pay()
  167. // 'accept'表示用户同意订阅该条id对应的模板消息
  168. if (res['V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks'] === 'accept') {
  169. // 用户同意订阅,调用云函数或服务器接口发送订阅消息
  170. // wx.cloud.callFunction({
  171. // name: 'sendSubscribeMessage',
  172. // data: {
  173. // templateId: '配置好的模板ID',
  174. // openid: 'o8pFb5cWH1KkBDvGls2X7yMiFkGA',
  175. // data: {
  176. // thing1: {
  177. // value: '活动名称'
  178. // },
  179. // // 其他参数...
  180. // }
  181. // },
  182. // success(res) {
  183. // console.log('订阅消息发送成功', res)
  184. // },
  185. // fail(err) {
  186. // console.error('订阅消息发送失败', err)
  187. // }
  188. // })
  189. }
  190. },
  191. fail(err) {
  192. that.pay()
  193. wx.showModal({
  194. title: '提示',
  195. content: '目前暂未获取到未支付订单',
  196. })
  197. }
  198. })
  199. },
  200. pay() {
  201. const that = this;
  202. api.request_wechatPay(this.data.order.orderId)
  203. .then(res => {
  204. wx.requestPayment({
  205. timeStamp: res.data.data.unifiedOrderResult.timeStamp,
  206. nonceStr: res.data.data.unifiedOrderResult.nonceStr,
  207. package: res.data.data.unifiedOrderResult.package,
  208. signType: res.data.data.unifiedOrderResult.signType,
  209. paySign: res.data.data.unifiedOrderResult.paySign,
  210. success: res => {
  211. wx.hideLoading()
  212. that.sendMessage()
  213. that.toPayResult()
  214. },
  215. fail: res => {
  216. wx.hideLoading()
  217. wx.showModal({
  218. title: '提示',
  219. content: '支付失败',
  220. })
  221. }
  222. })
  223. });
  224. },
  225. /** 发送消息模板 */
  226. sendMessage() {
  227. const message = {
  228. trxid: this.data.order.orderId,
  229. orderType: '支付'
  230. }
  231. api.request_sendMessage(message).then(res => {
  232. console.log("发送消息模板结果", res)
  233. }).catch(err => {
  234. console.log("发送消息模板失败", err)
  235. })
  236. },
  237. /** 跳转到支付结果页 */
  238. toPayResult() {
  239. wx.redirectTo({
  240. url: '../payResult/payResult',
  241. })
  242. },
  243. /** 跳转到支付结果页(不用这个) */
  244. toPayResult_back() {
  245. const that = this;
  246. setTimeout(function () {
  247. wx.navigateTo({
  248. url: '../payResult/payResult?nozzle=' + that.data.nozzleId,
  249. events: {
  250. acceptOrderFromPayResult: function (data) {
  251. console.log("获取到来自result页面传送过来的订单", data)
  252. that.setData({
  253. order: data
  254. })
  255. }
  256. }
  257. })
  258. }, 500)
  259. }
  260. })