pay.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. console.log('页面关闭');
  49. // 页面关闭
  50. wx.switchTab({
  51. url: "/pages/cart/cart"
  52. });
  53. },
  54. reLoad: function () {
  55. let that = this;
  56. if (wx.getStorageSync('storeId')) {
  57. if (wx.getStorageSync('userId')) {
  58. wx.request({
  59. url: api.updateLoginUser,
  60. data: {
  61. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  62. },
  63. method: 'POST',
  64. header: {
  65. 'Content-Type': 'application/json'
  66. },
  67. success: function (wxRes) {
  68. if (wxRes.data.errno === 0) {
  69. // console.log("用户信息更新成功");
  70. }
  71. },
  72. fail: function (err) {
  73. console.log("failed");
  74. }
  75. });
  76. }
  77. }
  78. },
  79. //改变支付方式
  80. changePayType: function (e) {
  81. console.log(e.currentTarget.id);
  82. this.setData({
  83. payType: e.currentTarget.id
  84. });
  85. },
  86. //向服务请求支付参数
  87. requestPayParam() {
  88. let that = this;
  89. wx.showLoading({
  90. title: '加载中...',
  91. });
  92. console.log(this.data.payType);
  93. let url = '';
  94. if (this.data.payType == 'wx') {
  95. url = api.PayPrepayId;
  96. } else if (this.data.payType == 'pingan') {
  97. url = api.Payorder;
  98. } else if (this.data.payType == 'wxGlobal') {
  99. url = api.GlobalPayorder;
  100. }
  101. // 测试领取优惠券
  102. // wx.redirectTo({
  103. // url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId,
  104. // })
  105. // todo
  106. util.request(url, {
  107. orderIds: that.data.orderIds, payType: 1,
  108. isMergePay: that.data.isMergePay
  109. }).then(function (res) {
  110. wx.hideLoading();
  111. if (res.errno === 0) {
  112. let payParam = res.data;
  113. wx.requestPayment({
  114. 'timeStamp': payParam.timeStamp,
  115. 'nonceStr': payParam.nonceStr,
  116. 'package': payParam.package,
  117. 'signType': payParam.signType,
  118. 'paySign': payParam.paySign,
  119. 'success': function (res) {
  120. wx.redirectTo({
  121. url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds,
  122. })
  123. },
  124. 'fail': function (res) {
  125. wx.redirectTo({
  126. url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds,
  127. })
  128. }
  129. })
  130. }else {
  131. // util.showErrorToast(res.errmsg)
  132. wx.showModal({
  133. title: '',
  134. content: res.errmsg,
  135. showCancel: false
  136. });
  137. }
  138. });
  139. },
  140. startPay(e) {
  141. this.requestPayParam();
  142. }
  143. })