pay.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. orderBizType10: null
  11. },
  12. onLoad: function (options) {
  13. // 页面初始化 options为页面跳转所带来的参数
  14. this.setData({
  15. orderIds: options.orderIds,
  16. actualPrice: options.actualPrice,
  17. isMergePay: options.isMergePay
  18. })
  19. if (options.orderBizType10){
  20. this.setData({
  21. orderBizType10: options.orderBizType10
  22. })
  23. }
  24. },
  25. onReady: function () {
  26. },
  27. onShow: function () {
  28. let that = this;
  29. // 页面显示
  30. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  31. if (wx.getStorageSync('storeId')) {
  32. util.request(api.ChooseStoreId, {
  33. storeId: wx.getStorageSync('storeId'),
  34. merchSn: wx.getStorageSync('merchSn'),
  35. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  36. }, 'POST').then(function (res) {
  37. if (res.errno === 0) {
  38. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  39. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  40. that.reLoad();
  41. }
  42. });
  43. }
  44. } else {
  45. wx.navigateTo({
  46. url: '/pages/auth/btnAuth/btnAuth',
  47. })
  48. }
  49. },
  50. onHide: function () {
  51. // 页面隐藏
  52. console.log('页面隐藏');
  53. },
  54. onUnload: function () {
  55. console.log('页面关闭');
  56. // 页面关闭
  57. // wx.switchTab({
  58. // url: "/pages/cart/cart"
  59. // });
  60. },
  61. reLoad: function () {
  62. let that = this;
  63. if (wx.getStorageSync('storeId')) {
  64. if (wx.getStorageSync('userId')) {
  65. wx.request({
  66. url: api.updateLoginUser,
  67. data: {
  68. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  69. },
  70. method: 'POST',
  71. header: {
  72. 'Content-Type': 'application/json'
  73. },
  74. success: function (wxRes) {
  75. if (wxRes.data.errno === 0) {
  76. // console.log("用户信息更新成功");
  77. }
  78. },
  79. fail: function (err) {
  80. console.log("failed");
  81. }
  82. });
  83. }
  84. }
  85. },
  86. //改变支付方式
  87. changePayType: function (e) {
  88. console.log(e.currentTarget.id);
  89. this.setData({
  90. payType: e.currentTarget.id
  91. });
  92. },
  93. //向服务请求支付参数
  94. requestPayParam(formId) {
  95. let that = this;
  96. let orderBizType10 = that.data.orderBizType10;
  97. // console.log(orderBizType10)
  98. if(orderBizType10){
  99. //校验获取用户id上次活体人脸核身时间是否在可调用范围内
  100. util.request(api.CheckFaceTimeByUserId, {
  101. userId: wx.getStorageSync('userId'),
  102. storeId: wx.getStorageSync('storeId')
  103. }, 'POST').then(function (checkRes) {
  104. if (checkRes.errno === 0) {
  105. //根据用户上次活体人脸核身时间判断出,该用户可以调起人脸接口
  106. if (checkRes.data === true) {
  107. //根据订单号获取是否认证成功,认证成功则调起支付,否则提示
  108. that.checkFace(formId);
  109. } else {
  110. //调起支付
  111. wx.showLoading({
  112. title: '加载中...',
  113. });
  114. that.requestPay(formId);
  115. }
  116. }
  117. });
  118. } else {
  119. //调起支付
  120. wx.showLoading({
  121. title: '加载中...',
  122. });
  123. that.requestPay(formId);
  124. }
  125. },
  126. /**
  127. * 根据订单号获取是否认证成功,认证成功则调起支付,否则提示
  128. */
  129. checkFace(formId){
  130. var that = this;
  131. util.request(api.GetIsFaceCheckByOrderId, {
  132. orderIds: that.data.orderIds
  133. }).then(function (res) {
  134. if (res.errno === 0) {
  135. if (res.data.isFaceCheck === '1' || res.data.isFaceCheck === '2') {
  136. wx.showLoading({
  137. title: '加载中...',
  138. });
  139. that.requestPay(formId);
  140. } else {
  141. wx.showToast({
  142. title: '现场速递商品购买需先认证身份信息再进行支付',
  143. icon: 'none',
  144. duration: 1500
  145. })
  146. }
  147. }
  148. });
  149. },
  150. /**
  151. * 调起支付
  152. */
  153. requestPay(formId) {
  154. let that = this;
  155. let url = '';
  156. if (this.data.payType == 'wx') {
  157. url = api.PayPrepayId;
  158. } else if (this.data.payType == 'pingan') {
  159. url = api.Payorder;
  160. } else if (this.data.payType == 'wxGlobal') {
  161. url = api.GlobalPayorder;
  162. }
  163. // 测试领取优惠券
  164. // wx.redirectTo({
  165. // url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId,
  166. // })
  167. // todo
  168. util.request(url, {
  169. orderIds: that.data.orderIds, payType: 1,
  170. isMergePay: that.data.isMergePay
  171. }).then(function (res) {
  172. wx.hideLoading();
  173. if (res.errno === 0) {
  174. let payParam = res.data;
  175. wx.requestPayment({
  176. 'timeStamp': payParam.timeStamp,
  177. 'nonceStr': payParam.nonceStr,
  178. 'package': payParam.package,
  179. 'signType': payParam.signType,
  180. 'paySign': payParam.paySign,
  181. 'success': function (res) {
  182. if (that.data.orderBizType10) {
  183. wx.redirectTo({
  184. url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds + '&orderBizType10=' + that.data.orderBizType10,
  185. })
  186. }else{
  187. wx.redirectTo({
  188. url: '/pages/payResult/payResult?status=1&orderIds=' + that.data.orderIds
  189. })
  190. }
  191. },
  192. 'fail': function (res) {
  193. console.log('支付失败')
  194. if (that.data.orderBizType10) {
  195. wx.redirectTo({
  196. url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds + '&orderBizType10=' + that.data.orderBizType10,
  197. })
  198. } else {
  199. wx.redirectTo({
  200. url: '/pages/payResult/payResult?status=0&orderIds=' + that.data.orderIds
  201. })
  202. }
  203. }
  204. })
  205. } else {
  206. // util.showErrorToast(res.errmsg)
  207. wx.showModal({
  208. title: '',
  209. content: res.errmsg,
  210. showCancel: false
  211. });
  212. }
  213. });
  214. },
  215. startPay(e) {
  216. console.log(e.detail.formId);
  217. this.requestPayParam(e.detail.formId);
  218. }
  219. })