1
0

pay.js 3.9 KB

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