Withdraw.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\Message;
  4. use app\common\controller\Backend;
  5. use app\common\service\MoneyService;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 提现管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Withdraw extends Backend
  15. {
  16. /**
  17. * Withdraw模型对象
  18. * @var \app\admin\model\Withdraw
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Withdraw;
  25. $typeList = [
  26. 'statusList' => $this->model->getStatusList(),
  27. 'typeList' => $this->model->getTypeList(),
  28. ];
  29. $this->view->assign($typeList);
  30. $this->assignconfig($typeList);
  31. }
  32. public function import()
  33. {
  34. parent::import();
  35. }
  36. /**
  37. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  38. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  39. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  40. */
  41. /**
  42. * 查看
  43. */
  44. public function index()
  45. {
  46. //当前是否为关联查询
  47. $this->relationSearch = true;
  48. //设置过滤方法
  49. $this->request->filter(['strip_tags', 'trim']);
  50. if ($this->request->isAjax()) {
  51. //如果发送的来源是Selectpage,则转发到Selectpage
  52. if ($this->request->request('keyField')) {
  53. return $this->selectpage();
  54. }
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $list = $this->model
  57. ->with(['user'])
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. foreach ($list as $row) {
  62. $row->getRelation('user')->visible(['nickname','money']);
  63. }
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 编辑
  71. */
  72. public function edit($ids = null)
  73. {
  74. $row = $this->model->get($ids);
  75. if (!$row) {
  76. $this->error(__('No Results were found'));
  77. }
  78. $adminIds = $this->getDataLimitAdminIds();
  79. if (is_array($adminIds)) {
  80. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  81. $this->error(__('You have no permission'));
  82. }
  83. }
  84. if ($this->request->isPost()) {
  85. $params = $this->request->post("row/a");
  86. if (!$params) {
  87. $this->error(__('Parameter %s can not be empty', ''));
  88. }
  89. $params = $this->preExcludeFields($params);
  90. $result = false;
  91. try {
  92. //是否采用模型验证
  93. if ($this->modelValidate) {
  94. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  95. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  96. $row->validateFailException(true)->validate($validate);
  97. }
  98. //状态:created=申请中,successed=成功,rejected=已拒绝
  99. $remark = '';
  100. if ($params['status'] == 'rejected') {//返回金额
  101. $user = model('User')->find($row['user_id']);
  102. $before = $user['money'];
  103. $remark = '提现'.$row['money'].'驳回';
  104. $res = model('Wallet')->lockChangeAccountRemain($row['user_id'],$row['money'],'+',$before,$remark,105,'money');
  105. if (!$res['status']) {
  106. throw new Exception($res['status']);
  107. }
  108. } elseif ($params['status'] == 'successed') {
  109. $remark = '提现'.$row['money'].'通过';
  110. //通过提现到用户账户
  111. if (isset($params['type']) && $params['type'] == 'alipay') {
  112. $moneyService = new MoneyService();
  113. $withdrawParams = [
  114. 'money' => $params['real_money'],
  115. 'alipay_account' => $params['account'],
  116. 'name' => $params['name'],
  117. 'out_biz_no' => $row['orderid'],
  118. ];
  119. $withdrawRes = $moneyService->withdrawTransfer($withdrawParams);
  120. if (!$withdrawRes['status']) {
  121. throw new Exception($withdrawRes['msg']);
  122. }
  123. }
  124. }
  125. if (!empty($remark)) {
  126. //审核结果消息通知
  127. $messageModel = new Message();
  128. $messageModel->addMessage($row['user_id'],'提现审核通知',$remark);
  129. }
  130. $result = $row->allowField(true)->save($params);
  131. } catch (ValidateException|PDOException|Exception $e) {
  132. $this->error($e->getMessage());
  133. }
  134. if ($result == false) {
  135. $this->error(__('No rows were updated'));
  136. }
  137. $this->success();
  138. }
  139. $this->view->assign("row", $row);
  140. return $this->view->fetch();
  141. }
  142. }