Withdraw.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\agent\controller\finance;
  3. use app\common\controller\AgentController;
  4. use app\http\IResponse;
  5. use Lettered\Support\Auth as IAuth;
  6. use think\App;
  7. use think\Exception;
  8. use think\Request;
  9. class Withdraw extends AgentController
  10. {
  11. protected $model;
  12. public function __construct(App $app = null, IAuth $auth)
  13. {
  14. parent::__construct($app, $auth);
  15. $this->model = new \app\agent\model\finance\Withdraw();
  16. }
  17. /**
  18. * 显示资源列表
  19. *
  20. * @return \think\Response
  21. */
  22. public function index()
  23. {
  24. $where = [];
  25. !empty($this->auth->user()['user_id']) && $where[]
  26. = ['user_id', '=', $this->auth->user()['user_id']];
  27. (!empty(input('status')) || input('status') == '0') &&
  28. $where[] = ['status', 'eq', input('status')];
  29. // 时间处理
  30. if (!empty(input('created_at'))){
  31. list($start, $end) = str2arr(input('created_at'),'-');
  32. $where[] = ['created_at', 'between', [strtotime($start), strtotime($end)]];
  33. }
  34. $list = $this->model->where($where)
  35. ->order(['id' => 'desc'])
  36. ->paginate(input('limit'),false);
  37. return IResponse::paginate($list);
  38. }
  39. /**
  40. * 显示创建资源表单页.
  41. *
  42. * @return \think\Response
  43. */
  44. public function create()
  45. {
  46. //
  47. }
  48. /**
  49. * 保存新建的资源
  50. *
  51. * @param \think\Request $request
  52. * @return \think\Response
  53. * @throws \think\exception\PDOException
  54. */
  55. public function save(Request $request)
  56. {
  57. $params = input();
  58. $validate = validate('\app\agent\validate\finance\Withdraw');
  59. if (!$validate->scene('save')->check($params)) {
  60. return IResponse::failure($validate->getError());
  61. }
  62. $admin = $this->auth->user();
  63. $user = model('\app\common\model\Users')->field('id,motor_agent_money')
  64. ->find($admin['user_id']);
  65. if ($user['motor_agent_money'] < $params['withdraw_amount']) {
  66. return IResponse::failure('余额不足');
  67. }
  68. $this->model->startTrans();
  69. try {
  70. $params['user_id'] = $admin['user_id'];
  71. $result = $this->model::create($params, true);
  72. $this->model->commit();
  73. }
  74. catch(Exception $e) {
  75. $this->model->rollback();
  76. return IResponse::failure($e->getMessage());
  77. }
  78. if ($result) {
  79. return IResponse::success();
  80. }
  81. return IResponse::failure();
  82. }
  83. /**
  84. * 显示指定的资源
  85. *
  86. * @param int $id
  87. * @return \think\Response
  88. */
  89. public function read($id)
  90. {
  91. //
  92. }
  93. /**
  94. * 显示编辑资源表单页.
  95. *
  96. * @param int $id
  97. * @return \think\Response
  98. */
  99. public function edit($id)
  100. {
  101. //
  102. }
  103. /**
  104. * 保存更新的资源
  105. *
  106. * @param int $ids
  107. * @return \think\Response
  108. */
  109. public function update($ids)
  110. {
  111. $row = $this->model::get($ids);
  112. if (!$row || $row['status'] > 20) {
  113. return IResponse::failure('不可操作');
  114. }
  115. $result = false;
  116. $this->model->startTrans();
  117. try {
  118. $result = $row->allowField(true)->save(input());
  119. $this->model->commit();
  120. }
  121. catch(Exception $e) {
  122. $this->model->rollback();
  123. }
  124. if ($result === false) {
  125. return IResponse::failure();
  126. }
  127. return IResponse::success();
  128. }
  129. /**
  130. * 删除指定资源
  131. *
  132. * @param int $ids
  133. * @return \think\Response
  134. * @throws \think\exception\PDOException
  135. */
  136. public function delete($ids)
  137. {
  138. $row = $this->model::get($ids);
  139. if (!$row) {
  140. return IResponse::failure('不可操作');
  141. }
  142. $result = false;
  143. $this->model->startTrans();
  144. try {
  145. $result = $row->delete();
  146. $this->model->commit();
  147. }
  148. catch(Exception $e) {
  149. $this->model->rollback();
  150. }
  151. if ($result) {
  152. return IResponse::success();
  153. }
  154. return IResponse::failure();
  155. }
  156. }