UserRecharge.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\user\{User, UserRecharge as UserRechargeModel};
  5. use app\models\routine\RoutineTemplate;
  6. use app\models\user\{UserBill, WechatUser as WechatUserWap};
  7. use crmeb\services\{JsonService,
  8. MiniProgramService,
  9. WechatService,
  10. UtilService as Util,
  11. FormBuilder as Form,
  12. WechatTemplateService};
  13. use think\facade\Route as Url;
  14. /**
  15. * 微信充值记录
  16. * Class UserRecharge
  17. * @package app\admin\controller\user
  18. */
  19. class UserRecharge extends AuthController
  20. {
  21. /**
  22. * 显示操作记录
  23. */
  24. public function index()
  25. {
  26. $this->assign('year', get_month());
  27. return $this->fetch();
  28. }
  29. public function get_user_recharge_list()
  30. {
  31. $where = Util::getMore([
  32. ['data', ''],
  33. ['paid', ''],
  34. ['page', 1],
  35. ['limit', 20],
  36. ['nickname', ''],
  37. ['excel', ''],
  38. ]);
  39. return JsonService::successlayui(UserRechargeModel::getUserRechargeList($where));
  40. }
  41. public function delect($id = 0)
  42. {
  43. if (!$id) return JsonService::fail('缺少参数');
  44. $rechargInfo = UserRechargeModel::get($id);
  45. if ($rechargInfo->paid) return JsonService::fail('已支付的订单记录无法删除');
  46. if (UserRechargeModel::del($id))
  47. return JsonService::successful('删除成功');
  48. else
  49. return JsonService::fail('删除失败');
  50. }
  51. public function get_badge()
  52. {
  53. $where = Util::getMore([
  54. ['data', ''],
  55. ['paid', ''],
  56. ['nickname', ''],
  57. ]);
  58. return JsonService::successful(UserRechargeModel::getDataList($where));
  59. }
  60. /**退款
  61. * @param $id
  62. * @return mixed|void
  63. */
  64. public function edit($id)
  65. {
  66. if (!$id) return $this->failed('数据不存在');
  67. $UserRecharge = UserRechargeModel::get($id);
  68. if (!$UserRecharge) return JsonService::fail('数据不存在!');
  69. if ($UserRecharge['paid'] == 1) {
  70. $f = array();
  71. $f[] = Form::input('order_id', '退款单号', $UserRecharge->getData('order_id'))->disabled(1);
  72. $f[] = Form::number('refund_price', '退款金额', $UserRecharge->getData('price'))->precision(2)->min(0)->max($UserRecharge->getData('price'));
  73. $jsContent = <<<SCRIPT
  74. parent.SuccessFun();
  75. parent.layer.close(parent.layer.getFrameIndex(window.name));
  76. SCRIPT;
  77. $form = Form::make_post_form('编辑', $f, Url::buildUrl('updateRefundY', array('id' => $id)), $jsContent);
  78. $this->assign(compact('form'));
  79. return $this->fetch('public/form-builder');
  80. } else return JsonService::fail('数据不存在!');
  81. }
  82. /**
  83. * 退款更新
  84. * @param $id
  85. */
  86. public function updateRefundY($id)
  87. {
  88. $data = Util::postMore([
  89. 'refund_price',
  90. ]);
  91. if (!$id) return $this->failed('数据不存在');
  92. $UserRecharge = UserRechargeModel::get($id);
  93. if (!$UserRecharge) return JsonService::fail('数据不存在!');
  94. if ($UserRecharge['price'] == $UserRecharge['refund_price']) return JsonService::fail('已退完支付金额!不能再退款了');
  95. if (!$data['refund_price']) return JsonService::fail('请输入退款金额');
  96. $refund_price = $data['refund_price'];
  97. $data['refund_price'] = bcadd($data['refund_price'], $UserRecharge['refund_price'], 2);
  98. $bj = bccomp((float)$UserRecharge['price'], (float)$data['refund_price'], 2);
  99. if ($bj < 0) return JsonService::fail('退款金额大于支付金额,请修改退款金额');
  100. $refund_data['pay_price'] = $UserRecharge['price'];
  101. $refund_data['refund_price'] = $refund_price;
  102. // $refund_data['refund_account']='REFUND_SOURCE_RECHARGE_FUNDS';
  103. try {
  104. $recharge_type = UserRechargeModel::where('order_id', $UserRecharge['order_id'])->value('recharge_type');
  105. if ($recharge_type == 'weixin') {
  106. WechatService::payOrderRefund($UserRecharge['order_id'], $refund_data);
  107. } else {
  108. MiniProgramService::payOrderRefund($UserRecharge['order_id'], $refund_data);
  109. }
  110. } catch (\Exception $e) {
  111. return JsonService::fail($e->getMessage());
  112. }
  113. UserRechargeModel::edit($data, $id);
  114. User::bcDec($UserRecharge['uid'], 'now_money', $refund_price, 'uid');
  115. switch (strtolower($UserRecharge['recharge_type'])) {
  116. case 'weixin':
  117. WechatTemplateService::sendTemplate(WechatUserWap::where('uid', $UserRecharge['uid'])->value('openid'), WechatTemplateService::ORDER_REFUND_STATUS, [
  118. 'first' => '亲,您充值的金额已退款,本次退款' .
  119. $data['refund_price'] . '金额',
  120. 'keyword1' => $UserRecharge['order_id'],
  121. 'keyword2' => $UserRecharge['price'],
  122. 'keyword3' => date('Y-m-d H:i:s', $UserRecharge['add_time']),
  123. 'remark' => '点击查看订单详情'
  124. ], Url::buildUrl('/user/bill/2')->suffix(false)->build());
  125. break;
  126. case 'routine':
  127. RoutineTemplate::sendRechargeSuccess($UserRecharge, $data['refund_price']);
  128. break;
  129. }
  130. UserBill::expend('系统退款', $UserRecharge['uid'], 'now_money', 'user_recharge_refund', $refund_price, $id, $UserRecharge['price'], '退款给用户' . $refund_price . '元');
  131. return JsonService::successful('退款成功!');
  132. }
  133. }