pay.js 4.2 KB

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