Withdrawal.php 5.1 KB

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