1
0

pay.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var app = getApp();
  2. var util = require('../../utils/util.js');
  3. var api = require('../../config/api.js');
  4. Page({
  5. data: {
  6. orderIds: [],
  7. actualPrice: 0.00,
  8. isMergePay: '',
  9. payType: 'wx'
  10. },
  11. onLoad: function (options) {
  12. // 页面初始化 options为页面跳转所带来的参数
  13. this.setData({
  14. orderIds: options.orderIds,
  15. actualPrice: options.actualPrice,
  16. isMergePay: options.isMergePay
  17. })
  18. console.log(this.data.orderIds);
  19. },
  20. onReady: function () {
  21. },
  22. onShow: function () {
  23. let that = this;
  24. // 页面显示
  25. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  26. if (wx.getStorageSync('storeId')) {
  27. util.request(api.ChooseStoreId, {
  28. storeId: wx.getStorageSync('storeId'),
  29. merchSn: wx.getStorageSync('merchSn')
  30. }, 'POST').then(function (res) {
  31. if (res.errno === 0) {
  32. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  33. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  34. that.reLoad();
  35. }
  36. });
  37. }
  38. } else {
  39. wx.navigateTo({
  40. url: '/pages/auth/btnAuth/btnAuth',
  41. })
  42. }
  43. },
  44. onHide: function () {
  45. // 页面隐藏
  46. },
  47. onUnload: function () {
  48. // 页面关闭
  49. },
  50. reLoad: function () {
  51. let that = this;
  52. if (wx.getStorageSync('storeId')) {
  53. if (wx.getStorageSync('userId')) {
  54. wx.request({
  55. url: api.updateLoginUser,
  56. data: {
  57. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  58. },
  59. method: 'POST',
  60. header: {
  61. 'Content-Type': 'application/json'
  62. },
  63. success: function (wxRes) {
  64. if (wxRes.data.errno === 0) {
  65. // console.log("用户信息更新成功");
  66. }
  67. },
  68. fail: function (err) {
  69. console.log("failed");
  70. }
  71. });
  72. }
  73. }
  74. },
  75. //改变支付方式
  76. changePayType: function (e) {
  77. console.log(e.currentTarget.id);
  78. this.setData({
  79. payType: e.currentTarget.id
  80. });
  81. },
  82. //向服务请求支付参数
  83. requestPayParam() {
  84. let that = this;
  85. wx.showLoading({
  86. title: '加载中...',
  87. });
  88. console.log(this.data.payType);
  89. let url = '';
  90. if (this.data.payType == 'wx') {
  91. url = api.PayPrepayId;
  92. } else if (this.data.payType == 'pingan') {
  93. url = api.Payorder;
  94. } else if (this.data.payType == 'wxGlobal') {
  95. url = api.GlobalPayorder;
  96. }
  97. // 测试领取优惠券
  98. // wx.redirectTo({
  99. // url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId,
  100. // })
  101. // todo
  102. util.request(url, {
  103. orderIds: that.data.orderIds, payType: 1,
  104. isMergePay: that.data.isMergePay
  105. }).then(function (res) {
  106. wx.hideLoading();
  107. if (res.errno === 0) {
  108. let payParam = res.data;
  109. wx.requestPayment({
  110. 'timeStamp': payParam.timeStamp,
  111. 'nonceStr': payParam.nonceStr,
  112. 'package': payParam.package,
  113. 'signType': payParam.signType,
  114. 'paySign': payParam.paySign,
  115. 'success': function (res) {
  116. wx.redirectTo({
  117. url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds,
  118. })
  119. },
  120. 'fail': function (res) {
  121. wx.redirectTo({
  122. url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds,
  123. })
  124. }
  125. })
  126. }else {
  127. // util.showErrorToast(res.errmsg)
  128. wx.showModal({
  129. title: '',
  130. content: res.errmsg,
  131. showCancel: false
  132. });
  133. }
  134. });
  135. },
  136. startPay(e) {
  137. this.requestPayParam();
  138. }
  139. })