RecharOrder.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. $result = [
  69. 'code' => 1,
  70. 'msg' => '操作成功',
  71. ];
  72. Db::startTrans();
  73. try {
  74. $row = $this->model->get($ids);
  75. if (!$row) {
  76. throw new Exception(__('No Results were found'));
  77. }
  78. $adminIds = $this->getDataLimitAdminIds();
  79. if (is_array($adminIds)) {
  80. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  81. throw new Exception(__('You have no permission'));
  82. }
  83. }
  84. if ($row['status'] != 1) {
  85. throw new Exception('支付成功才能退款');
  86. }
  87. if ($row['refund_money'] != 0) {
  88. throw new Exception('已退款');
  89. }
  90. //验证用户钻石是否足够
  91. $userWhere['id'] = $row['user_id'];
  92. $user = model('User')->where($userWhere)->lock(true)->find();
  93. $userJewel = isset($user['jewel']) ? $user['jewel'] : 0;
  94. if ($userJewel < $row['jewel']) {
  95. throw new Exception('钻石不足,无法退款');
  96. }
  97. //更新退款金额
  98. $data = [
  99. 'refund_money' => $row['money'],//退款金额
  100. 'status' => 2,//订单状态:-1=支付超时,0=支付中,1=支付成功,2=退款
  101. ];
  102. $where['id'] = $ids;
  103. $res = $this->model->update($data,$where);
  104. if (!$res) {
  105. throw new Exception('更新退款金额失败');
  106. }
  107. //记录退款日志
  108. $res = model('Wallet')->lockChangeAccountRemain($row['user_id'],$row['jewel'],'-',$user['jewel'],'充值退款',18,'jewel');
  109. if (!$res['status']) {
  110. throw new Exception($res['msg']);
  111. }
  112. //实际退款
  113. $refundParams = [
  114. 'order' => [
  115. 'pay_out_trade_no' => $row['order_no'],
  116. 'pay_fee' => $row['money'],
  117. 'pay_type' => $row['pay_type'],
  118. ],
  119. 'table' => 'rechar_order',
  120. 'table_id' => $ids,
  121. 'refund_price' => $row['money'],
  122. 'remark' => '充值退款',
  123. ];
  124. /*$refundParams['order']['pay_fee'] = 0.01;
  125. $refundParams['refund_price'] = 0.01;*/
  126. $refundRes = $this->model->old_refund($refundParams);
  127. if ($refundRes['status'] == 0) {
  128. throw new Exception($refundRes['msg']);
  129. }
  130. Db::commit();
  131. } catch (Exception $e) {
  132. Db::rollback();
  133. $result['code'] = 0;
  134. $result['msg'] = $e->getMessage();
  135. }
  136. return json($result);
  137. }
  138. }