pay.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. define(['$', 'template', 'api', 'native', 'config', 'user'], function ($, template, api, native, config, user) {
  2. var backHash; // 回退页面
  3. var orderID; // 订单ID
  4. var orderType; // 订单类型
  5. var orderPrice; // 订单价格
  6. var isRecharge = false; // 是否是充值
  7. function back() {
  8. $(document).trigger('spa:navigate', {
  9. hash: backHash,
  10. pushData: {
  11. dontNeedReload: true,
  12. animate: 'slideOutDown' //强制设置动画为落下
  13. }
  14. });
  15. }
  16. return {
  17. title: '壹管家上门服务',
  18. body: '',
  19. init: function (pageData, dtd) {
  20. var $view = this;
  21. var $doc = $(document);
  22. // 充值的回调
  23. function payResult(res) {
  24. $doc.trigger('spa:closeloader');
  25. if (res.success) {
  26. var orderInfo = user.getOrderInfo(orderID);
  27. var params = {
  28. orderInfo: orderInfo,
  29. charge: res.data
  30. };
  31. native.pay(params, function (resA) {
  32. if (resA.success) {
  33. $doc.trigger('spa:navigate', {
  34. hash: 'paySuccess'
  35. });
  36. } else {
  37. $doc.trigger('spa:openpanel', ['simpleAlert', {
  38. message: resA.message
  39. }]);
  40. }
  41. });
  42. } else {
  43. $doc.trigger('spa:openpanel', ['simpleAlert', {
  44. message: res.message
  45. }]);
  46. }
  47. }
  48. $.newTouch('.pay-option', function (event) {
  49. event.preventDefault();
  50. var payChannel = $(this).data('value');
  51. $('.btn-pay', $view).attr('data-pay-channel', payChannel);
  52. $(this).addClass('checked').parent().siblings().find('a').removeClass('checked');
  53. }, $view);
  54. $.newTouch('.btn-pay', function (event) {
  55. event.preventDefault();
  56. var payChannel = $(this).attr('data-pay-channel');
  57. if (config.isWX) {
  58. payChannel = 'wx_pub';
  59. }
  60. if (!payChannel) return;
  61. $doc.trigger('spa:openloader');
  62. var params = {
  63. user_id: user.id,
  64. order_id: orderID,
  65. pay_channel: payChannel
  66. };
  67. if (isRecharge){
  68. api.payRecharge(params, function (res) {
  69. payResult(res)
  70. });
  71. }else {
  72. api.getPayCharge(params, function (res) {
  73. payResult(res)
  74. });
  75. }
  76. }, $view);
  77. //返回
  78. $.newTouch('.cancel-button', function (event) {
  79. event.preventDefault();
  80. back.call(this, event);
  81. }, $view);
  82. $view.on('tapBackButton', function (event) {
  83. event.preventDefault();
  84. back.call(this, event);
  85. });
  86. dtd.resolve();
  87. },
  88. beforeopen: function (pageData, dtd) {
  89. var $view = this;
  90. var pushData = pageData.pushData;
  91. orderID = pushData.orderID;
  92. backHash = pushData.backHash ? pushData.backHash : '';
  93. isRecharge = pushData.isRecharge;
  94. // 在用户的所有订单中找出这个订单
  95. var orders = user.orders[1].data;
  96. for (var i = 0, len = orders.length; i < len; i++) {
  97. if (orders[i].id == orderID) {
  98. orderType = orders[i].products[0].name;
  99. orderPrice = orders[i].final_price;
  100. }
  101. }
  102. var tpl = template('pay/index', {
  103. showNavBar: !config.isWX && !config.isChubao,
  104. wxPub: true,
  105. orderType: orderType,
  106. orderPrice: orderPrice
  107. });
  108. $('.spa-page-body', $view).html(tpl);
  109. dtd.resolve();
  110. // if (config.isAndroid || config.isIOS) {
  111. // native.isWxAppInstalled(function(res){
  112. // var showWX = true;
  113. // if (res.success && !res.isInstalled) {
  114. // showWX = false;
  115. // }
  116. // if (config.appVersion < '2.2') {
  117. // //微信支付2.2版本以下有问题 隐藏按钮
  118. // showWX = false;
  119. // }
  120. // var tpl = template('pay/index', {
  121. // showWX: showWX,
  122. // showNavBar: !config.isWX && !config.isChubao,
  123. // orderType: orderType,
  124. // orderprice: orderprice
  125. // });
  126. // $('.spa-page-body', $view).html(tpl);
  127. //
  128. // dtd.resolve();
  129. // })
  130. // } else if (config.isChubao) {
  131. // var tpl = template('pay/index', {
  132. // showNavBar: !config.isWX && !config.isChubao,
  133. // orderType: orderType,
  134. // orderprice: orderprice,
  135. // chubao: true
  136. // });
  137. // $('.spa-page-body', $view).html(tpl);
  138. // dtd.resolve();
  139. // } else {
  140. //
  141. // }
  142. }
  143. }
  144. })