UserRecharge.php 7.4 KB

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