Withdrawal.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace app\admin\controller\greenroom;
  3. use app\admin\controller\auth\Admin;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Env;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\response\Json;
  11. use Udun\Dispatch\UdunDispatch;
  12. /**
  13. *
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Withdrawal extends Backend
  18. {
  19. /**
  20. * Withdrawal模型对象
  21. * @var \app\admin\model\greenroom\Withdrawal
  22. */
  23. protected $model = null;
  24. protected $udunDispatch = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->udunDispatch = new UdunDispatch([
  29. 'merchant_no' => Env::get("udun.mech_no"), //商户号
  30. 'api_key' => Env::get("udun.api_key"),//apikey
  31. 'gateway_address'=>Env::get("udun.host","https://sig10.udun.io"), //节点
  32. 'callUrl'=>Env::get("udun.callback_url", "https://www.advcover.xyz/admin/index/udun_callback"), //回调地址
  33. 'debug' => false //调试模式
  34. ]);
  35. $this->model = new \app\admin\model\greenroom\Withdrawal;
  36. $this->view->assign("statusList", $this->model->getStatusList());
  37. }
  38. /**
  39. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  40. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  41. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  42. */
  43. /**
  44. * 查看
  45. */
  46. public function index()
  47. {
  48. //当前是否为关联查询
  49. $this->relationSearch = true;
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags', 'trim']);
  52. if ($this->request->isAjax()) {
  53. //如果发送的来源是Selectpage,则转发到Selectpage
  54. if ($this->request->request('keyField')) {
  55. return $this->selectpage();
  56. }
  57. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  58. $list = $this->model
  59. ->with(['admin'])
  60. ->where($where)
  61. ->order($sort, $order)
  62. ->paginate($limit);
  63. foreach ($list as $row) {
  64. $row->getRelation('admin')->visible(['username','nickname','appname']);
  65. }
  66. $result = array("total" => $list->total(), "rows" => $list->items());
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 编辑
  73. *
  74. * @param $ids
  75. * @return string
  76. * @throws DbException
  77. * @throws \think\Exception
  78. */
  79. public function edit($ids = null)
  80. {
  81. $row = $this->model->get($ids);
  82. if (!$row) {
  83. $this->error(__('No Results were found'));
  84. }
  85. $adminIds = $this->getDataLimitAdminIds();
  86. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  87. $this->error(__('You have no permission'));
  88. }
  89. if (false === $this->request->isPost()) {
  90. $this->view->assign('row', $row);
  91. return $this->view->fetch();
  92. }
  93. $params = $this->request->post('row/a');
  94. if (empty($params)) {
  95. $this->error(__('Parameter %s can not be empty', ''));
  96. }
  97. $params = $this->preExcludeFields($params);
  98. $result = false;
  99. Db::startTrans();
  100. try {
  101. //是否采用模型验证
  102. if ($this->modelValidate) {
  103. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  105. $row->validateFailException()->validate($validate);
  106. }
  107. if ($params['status'] == 2) {
  108. $accountResult = $this->udunDispatch->checkAddress(195, $row->account);
  109. if ($accountResult['code'] != 200) {
  110. // 转账地址不合法
  111. $row->status = 0; // 自动拒绝 转账地址不合法
  112. Admin::where("id", $row->uid)->setInc("money", $row->amount);
  113. Db::commit();
  114. return $this->error('提现地址不正确');
  115. }
  116. $result = $this->udunDispatch->withdraw($row->id, "195", "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",$row->account, bcsub($row->amount, "3", 4), "提现");
  117. if ($result['code'] != 200) {
  118. Db::rollback();
  119. return $this->error($result['message']);
  120. }
  121. }
  122. $result = $row->allowField("status")->save($params);
  123. Db::commit();
  124. } catch (ValidateException|PDOException|Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if (false === $result) {
  129. $this->error(__('No rows were updated'));
  130. }
  131. $this->success();
  132. }
  133. // 驳回
  134. public function reject($ids = null)
  135. {
  136. Db::startTrans();
  137. try {
  138. if(!$ids) throw new \Exception('参数错误');
  139. $withdraw = $this->model->find($ids);
  140. if (!$withdraw) throw new \Exception('提现记录不存在');
  141. if ($withdraw->status != 1) throw new \Exception('状态错误');
  142. $withdraw->status = -1;
  143. $withdraw->save();
  144. \app\admin\model\Admin::money($withdraw->amount, $this->auth->id,'Withdrawal');
  145. \app\admin\model\Admin::where("id", $withdraw->uid)->setInc("money", $withdraw->amount);
  146. // \app\admin\model\Admin::update([
  147. // 'money' => bcadd($withdraw->amount, 3, 4)
  148. // ], [
  149. // 'id' => $withdraw->uid
  150. // ]);
  151. Db::commit();
  152. return \json(['code' => 1, 'msg' => '驳回成功']);
  153. } catch (\Exception $e) {
  154. Db::rollback();
  155. return \json(['code' => 0, 'msg' => $e->getMessage()]);
  156. }
  157. }
  158. }