recharge.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var app = new Vue({
  2. el: '#app',
  3. data: {
  4. submitting: false,
  5. // 详情
  6. info: {},
  7. type: '',
  8. orderId: 0,
  9. // 提交参数
  10. params: {
  11. money: '',
  12. score: '',
  13. },
  14. },
  15. created: function () {
  16. this.getInfo();
  17. this.type = getParam('type');
  18. this.orderId = getParam('id');
  19. },
  20. updated: function(){
  21. var _this = this;
  22. $(".weui-money").blur(function(){
  23. var money = $.trim($(this).val());
  24. var patt = /^(\d+\.\d{1,2}|\d+)$/gi;
  25. if(!patt.test(money)){
  26. money = !isNaN(parseFloat(money))? moneyFormat(parseFloat(money), 2) : '0.00';
  27. }
  28. $(this).val(money);
  29. });
  30. $(".weui-money").keyup(function(){
  31. var money = $.trim($(this).val());
  32. var patt = /^(\d+\.\d{1,2}|\d+)$/gi;
  33. console.log(money)
  34. if(!patt.test(money)){
  35. money = !isNaN(parseFloat(money))? moneyFormat(parseFloat(money), 2) : '0.00';
  36. }
  37. var score = parseFloat(money) * _this.info.score_rate;
  38. score = score>0? score : 0;
  39. _this.params.score = parseInt(score);
  40. })
  41. },
  42. methods: {
  43. // 获取用户信息
  44. getInfo: function(){
  45. var _this = this;
  46. var id = getParam('id');
  47. $.post('/weixin/member/getMemberInfo', {id: id, type: 2}, function (res) {
  48. if (res.code == 'success') {
  49. _this.info = res.data
  50. }else if(res.code == 'login'){
  51. login(res.data.url);
  52. }else if(res.code == 'exception'){
  53. showAlert(res.message);
  54. }else{
  55. $.toast(res.message, 'text');
  56. }
  57. }, "json");
  58. },
  59. // 提交
  60. doSubmit: function(){
  61. var _this = this;
  62. if (_this.submitting) {
  63. return false;
  64. }
  65. if(_this.params.money <=0 || _this.params.money == null){
  66. $.toast('请输入提现金额','text');
  67. return false;
  68. }
  69. if(confirm('确认充值?')){
  70. $.showLoading('数据提交中...');
  71. $.post('/weixin/member/doRecharge', _this.params, function (res) {
  72. $.hideLoading();
  73. if (res.code == 'success') {
  74. // TODO 2.调起微信支付
  75. if (typeof WeixinJSBridge == "undefined") {
  76. $.toast('请在微信环境中使用...', "text");
  77. if (document.addEventListener) {
  78. document.addEventListener('WeixinJSBridgeReady', function(){
  79. _this.weixinPay(res.data);
  80. }, false);
  81. } else if (document.attachEvent) {
  82. document.attachEvent('WeixinJSBridgeReady', function(){
  83. _this.weixinPay(res.data);
  84. });
  85. document.attachEvent('onWeixinJSBridgeReady', function(){
  86. _this.weixinPay(res.data);
  87. });
  88. }
  89. } else {
  90. _this.weixinPay(res.data);
  91. }
  92. } else if (res.code == 'login') {
  93. login(res.data.url);
  94. }else{
  95. $.toast(res.message,'text');
  96. }
  97. },'json');
  98. }
  99. },
  100. // 发起微信支付
  101. weixinPay: function (params) {
  102. var _this = this;
  103. WeixinJSBridge.invoke(
  104. 'getBrandWCPayRequest', {
  105. "appId": params.appId, //公众号名称,由商户传入
  106. "timeStamp": params.timeStamp, //时间戳,自1970年以来的秒数
  107. "nonceStr": params.nonceStr, //随机串
  108. "package": params.package,
  109. "signType": params.signType, //微信签名方式:
  110. "paySign": params.sign, //微信签名
  111. },
  112. function (res) {
  113. if (res.err_msg == "get_brand_wcpay_request:ok") {
  114. $.toast('充值成功');
  115. setTimeout(function () {
  116. _this.params.money = '';
  117. _this.getInfo();
  118. if(_this.type == 'pay'){
  119. location.href = '/weixin/order/pay?id='+_this.orderId;
  120. }
  121. }, 800)
  122. }
  123. }
  124. );
  125. }
  126. }
  127. });