pay.js 4.1 KB

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