RecharOrder.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 充值订单管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class RecharOrder extends Backend
  12. {
  13. /**
  14. * RecharOrder模型对象
  15. * @var \app\admin\model\RecharOrder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\RecharOrder;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  24. $this->view->assign("platformList", $this->model->getPlatformList());
  25. }
  26. public function import()
  27. {
  28. parent::import();
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['nickname']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 退款
  65. */
  66. public function refund($ids = null)
  67. {
  68. Db::startTrans();
  69. try {
  70. $row = $this->model->get($ids);
  71. if (!$row) {
  72. throw new Exception(__('No Results were found'));
  73. }
  74. $adminIds = $this->getDataLimitAdminIds();
  75. if (is_array($adminIds)) {
  76. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  77. throw new Exception(__('You have no permission'));
  78. }
  79. }
  80. if ($row['status'] != 1) {
  81. throw new Exception('支付成功才能退款');
  82. }
  83. if ($row['refund_money'] != 0) {
  84. throw new Exception('已退款');
  85. }
  86. //验证用户钻石是否足够
  87. $userWhere['id'] = $row['user_id'];
  88. $user = model('User')->where($userWhere)->lock(true)->find();
  89. $userJewel = isset($user['jewel']) ? $user['jewel'] : 0;
  90. if ($userJewel < $row['jewel']) {
  91. throw new Exception('钻石不足,无法退款');
  92. }
  93. //更新退款金额
  94. $data = [
  95. 'refund_money' => $row['money'],//退款金额
  96. ];
  97. $where['id'] = $ids;
  98. $res = $this->model->update($data,$where);
  99. if (!$res) {
  100. throw new Exception('更新退款金额失败');
  101. }
  102. //记录退款日志
  103. $res = model('Wallet')->lockChangeAccountRemain($row['user_id'],$row['jewel'],'-',$user['jewel'],'充值退款',18,'jewel');
  104. if (!$res['status']) {
  105. throw new Exception($res['msg']);
  106. }
  107. //实际退款
  108. $refundParams = [
  109. 'order' => [
  110. 'pay_out_trade_no' => $row['order_no'],
  111. 'pay_fee' => $row['money'],
  112. 'pay_type' => $row['pay_type'],
  113. ],
  114. 'table' => 'rechar_order',
  115. 'table_id' => $ids,
  116. 'refund_price' => $row['money'],
  117. 'remark' => '充值退款',
  118. ];
  119. $refundParams['order']['pay_fee'] = 0.01;
  120. $refundParams['refund_price'] = 0.01;
  121. $refundRes = $this->model->old_refund($refundParams);
  122. if ($refundRes['status'] == 0) {
  123. throw new Exception($refundRes['msg']);
  124. }
  125. Db::commit();
  126. $this->success('操作成功');
  127. } catch (Exception $e) {
  128. Db::rollback();
  129. $this->error($e->getMessage());
  130. }
  131. }
  132. }