Withdraw.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\ddrive;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 提现管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Withdraw extends Backend
  11. {
  12. /**
  13. * Withdraw模型对象
  14. * @var \addons\ddrive\model\Withdraw
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \addons\ddrive\model\Withdraw;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //当前是否为关联查询
  34. $this->relationSearch = true;
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags', 'trim']);
  37. if ($this->request->isAjax()) {
  38. //如果发送的来源是Selectpage,则转发到Selectpage
  39. if ($this->request->request('keyField')) {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $total = $this->model
  44. ->with(['user'])
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->with(['user'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->limit($offset, $limit)
  53. ->select();
  54. foreach ($list as $row) {
  55. }
  56. $list = collection($list)->toArray();
  57. $result = array("total" => $total, "rows" => $list);
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 批量更新
  64. */
  65. public function multi($ids = "")
  66. {
  67. $ids = $ids ? $ids : $this->request->param("ids");
  68. if ($ids) {
  69. if ($this->request->has('params')) {
  70. parse_str($this->request->post("params"), $values);
  71. $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  72. if ($values || $this->auth->isSuperAdmin()) {
  73. $adminIds = $this->getDataLimitAdminIds();
  74. if (is_array($adminIds)) {
  75. $this->model->where($this->dataLimitField, 'in', $adminIds);
  76. }
  77. $count = 0;
  78. Db::startTrans();
  79. try {
  80. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  81. foreach ($list as $index => $item) {
  82. $count += $item->allowField(true)->isUpdate(true)->save($values);
  83. // 拒绝提现,需要返还用户余额
  84. if (isset($values['status']) && $values['status'] == -1) {
  85. $this->returndRefuse($item);
  86. }
  87. // 提现成功,企业付款到零钱
  88. if (isset($values['status']) && $values['status'] == 1) {
  89. $this->returndSuccess($item);
  90. }
  91. }
  92. Db::commit();
  93. } catch (\PDOException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (\Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. if ($count) {
  101. $this->success();
  102. } else {
  103. $this->error(__('No rows were updated'));
  104. }
  105. } else {
  106. $this->error(__('You have no permission'));
  107. }
  108. }
  109. }
  110. $this->error(__('Parameter %s can not be empty', 'ids'));
  111. }
  112. /**
  113. * 拒绝提现,返还用户余额
  114. *
  115. * @param [type] $item
  116. * @return void
  117. */
  118. public function returndRefuse($item)
  119. {
  120. $user = Db::name('user')->where('id', $item['user_id'])->find();
  121. $log = [
  122. 'user_id' => $user['id'],
  123. 'money' => $item['money'],
  124. 'before' => $user['money'],
  125. 'after' => $user['money'] + $item['money'],
  126. 'memo' => '提现失败返还',
  127. 'createtime' => time(),
  128. ];
  129. // 添加用户余额日志
  130. Db::name('user_money_log')->insert($log);
  131. // 修改用户余额
  132. Db::name('user')->where('id', $item['user_id'])->setInc('money', $item['money']);
  133. return true;
  134. }
  135. /**
  136. * 提现成功,企业付款到零钱
  137. *
  138. * @return void
  139. */
  140. public function returndSuccess($item)
  141. {
  142. $openid = Db::name('ddrive_user_token')->where('user_id', $item['user_id'])->value('openid');
  143. \addons\ddrive\library\Transfer::pay('mini_program', $openid, $item['money'], $item['id'], '微信自助提现');
  144. }
  145. }