pay.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // pages/pay/pay.js
  2. const app = getApp();
  3. const { axios } = app.globalData;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. loadding: false,//正在支付中 true
  10. disabled: false,//支付完毕 true
  11. message: '',//订单备注
  12. paytype: "1",// 支付方式 单选
  13. balance: null,//余额
  14. result: [],//复选框
  15. },
  16. // 请求余额
  17. Request () {
  18. let that = this;
  19. axios({
  20. url: '/option/balance/',
  21. method: 'get',
  22. success (res) {
  23. let { data } = res.data;
  24. that.setData({
  25. ...data[0]
  26. })
  27. }
  28. })
  29. },
  30. // 复选框
  31. checkboxonchange (e) {
  32. let { detail } = e;
  33. this.setData({
  34. result: detail
  35. })
  36. },
  37. // 提交支付
  38. onSubmit () {
  39. let { commodity, message: notes, useraddress, paytype, result } = this.data, { id: student } = useraddress, that = this, balance = '';
  40. if (!student) {
  41. wx.showToast({
  42. title: '请选择学生',
  43. icon: 'error'
  44. })
  45. return false;
  46. }
  47. if (result.indexOf('1') > -1) {
  48. balance = 2;
  49. }
  50. let { appid, user } = app.globalData, { openid } = user;
  51. function pay () {
  52. return new Promise(resolve => {
  53. axios({
  54. url: `/order/`,
  55. method: 'post',
  56. data: {
  57. appid, openid, notes, commodity, student, balance
  58. },
  59. success (res) {
  60. that.setData({ loadding: true })
  61. let { data } = res.data;
  62. if (!data) {
  63. resolve({ type: 1, value: "支付成功" });
  64. return false;
  65. }
  66. wx.requestPayment({
  67. timeStamp: data.timeStamp,
  68. nonceStr: data.nonceStr,
  69. package: data.package,
  70. signType: data.signType,
  71. paySign: data.paySign,
  72. success: function (res) {
  73. resolve({ type: 1, value: "支付成功" })
  74. },
  75. fail: function () {
  76. // 取消付款
  77. resolve({ type: 2, value: "支付失败" })
  78. },
  79. })
  80. }
  81. })
  82. })
  83. }
  84. pay().then(
  85. res => {
  86. that.setData({ loadding: false, disabled: true });
  87. wx.lxd.Notify({ type: res.type == 1 ? 'success' : '', message: res.value })
  88. const time = setTimeout(() => {
  89. wx.redirectTo({
  90. url: '/pages/order/order',
  91. })
  92. clearTimeout(time)
  93. }, 3000)
  94. }
  95. );
  96. },
  97. // 挑选地址
  98. ChooseAddress () {
  99. wx.navigateTo({
  100. url: `/pages/adddata/adddata?data=${JSON.stringify({ source: 'pay' })}`,
  101. })
  102. },
  103. // 支付方式改变
  104. onChange (e) {
  105. let { detail } = e;
  106. this.setData({ paytype: detail })
  107. },
  108. /**
  109. * 生命周期函数--监听页面加载
  110. */
  111. onLoad: function (options) {
  112. wx.lxd.Ctr({
  113. // nav
  114. title: "支付", //标题
  115. bgc: false,//使用更改后的颜色;配合json使用
  116. })
  117. let { data } = options;
  118. if (!!data) {
  119. data = JSON.parse(data);
  120. this.setData({
  121. ...data,
  122. commodity: data.id
  123. })
  124. }
  125. },
  126. /**
  127. * 生命周期函数--监听页面初次渲染完成
  128. */
  129. onReady: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面显示
  133. */
  134. onShow: function () {
  135. this.Request()
  136. let { useraddress } = app.globalData;
  137. this.setData({ useraddress })
  138. },
  139. /**
  140. * 生命周期函数--监听页面隐藏
  141. */
  142. onHide: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面卸载
  146. */
  147. onUnload: function () {
  148. app.globalData.useraddress = {};
  149. },
  150. /**
  151. * 页面相关事件处理函数--监听用户下拉动作
  152. */
  153. onPullDownRefresh: function () {
  154. },
  155. /**
  156. * 页面上拉触底事件的处理函数
  157. */
  158. onReachBottom: function () {
  159. },
  160. })