链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.7 KiB

  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Common\WxPay;
  4. use App\Admin\Repositories\LanzuCsWithdraw;
  5. use App\Models\LanzuCsInfo;
  6. use App\Models\LanzuUserBalance;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Controllers\AdminController;
  12. use Illuminate\Support\Facades\Log;
  13. use App\Models\LanzuCsWithdraw as modelCsWithdraw;
  14. class LanzuCsWithdrawController extends AdminController
  15. {
  16. /**
  17. * Make a grid builder.
  18. *
  19. * @return Grid
  20. */
  21. protected function grid()
  22. {
  23. return Grid::make(new LanzuCsWithdraw(), function (Grid $grid) {
  24. $user = Admin::user();
  25. if (!$user->isRole('lanzu_cs')) {//如果不是社区站点的角色登陆,则隐藏提现入口
  26. $grid->disableCreateButton();
  27. $grid->actions(function (Grid\Displayers\Actions $actions) use ($grid) {
  28. if ($actions->row->status != 0) {//状态一但改变,就不能编辑
  29. $actions->disableEdit();
  30. }
  31. });
  32. } else {
  33. $grid->disableEditButton();
  34. }
  35. $grid->disableViewButton();
  36. $grid->disableDeleteButton();
  37. $grid->id->sortable();
  38. $grid->cs_id('提现用户')->display(function () {
  39. return LanzuCsInfo::where('id', $this->cs_id)->first()->name;
  40. });
  41. $grid->money;
  42. $grid->status('状态')->using([1 => '已同意', -1 => '已拒绝', 0 => '待审核'])->label([1 => 'success', -1 => 'danger', 0 => 'default']);
  43. $grid->is_pay('是否到账')->using(['否', '是']);
  44. $grid->created_at;
  45. $grid->filter(function (Grid\Filter $filter) {
  46. $filter->equal('id');
  47. });
  48. });
  49. }
  50. /**
  51. * Make a show builder.
  52. *
  53. * @param mixed $id
  54. *
  55. * @return Show
  56. */
  57. protected function detail($id)
  58. {
  59. return Show::make($id, new LanzuCsWithdraw(), function (Show $show) {
  60. $show->id;
  61. $show->cs_id('提现用户');
  62. $show->money;
  63. $show->status('状态');
  64. $show->is_pay('是否到帐');
  65. $show->created_at;
  66. $show->updated_at;
  67. });
  68. }
  69. /**
  70. * Make a form builder.
  71. *
  72. * @return Form
  73. */
  74. protected function form()
  75. {
  76. return Form::make(new LanzuCsWithdraw('csInfo'), function (Form $form) {
  77. $form->display('id');
  78. $user = Admin::user();
  79. if ($form->isCreating()) {//如果是添加操作
  80. if ($user->isRole('lanzu_cs')) {//如果是社区站点角色
  81. $cs = LanzuCsInfo::where('admin_user_id', $user->id)->first();
  82. if ($cs) {
  83. $form->text('amount', '可提现金额')->value(LanzuUserBalance::getBalance($cs->id, 3))->disable();
  84. $form->hidden('cs_id', '提现用户id')->value($cs->id);
  85. $form->text('csInfo.name', '提现用户')->value($cs->name)->disable();
  86. $form->number('money')->min(0)->max(env('MAX_MONEY'));
  87. $form->saving(function (Form $form) use ($user) {
  88. //保存前校验提现金额是否符合申请条件
  89. if ($form->money < env('MIN_MONEY') || $form->money > env('MAX_MONEY')) {
  90. return $form->error('申请提现金额不得小于 ' . env('MIN_MONEY') . ' 元 或 不得大于 ' . env('MAX_MONEY') . ' 元.');
  91. }
  92. $res = LanzuUserBalance::checkBalance($user->id, $form->money);
  93. if (!$res) {
  94. return $form->error('您可提现金额不足!');
  95. }
  96. });
  97. }
  98. }
  99. $form->saved(function () use ($cs, $form) {//扣减提现金额
  100. LanzuUserBalance::reduceBalance($cs->admin_user_id, 3, $form->money);
  101. });
  102. } else {//编辑操作
  103. $form->display('amount', '可提现金额')->value(LanzuUserBalance::getBalance($form->model()->cs_id, 3));
  104. $form->display('cs_id', '提现用户')->value($form->model()->name);
  105. $form->display('money');
  106. if ($form->model()->status != 0) {//提现审核后 就能再编辑
  107. $form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝'])->disable();
  108. } else {
  109. $form->radio('status', '状态')->options([1 => '同意', -1 => '拒绝'])->default(-1);
  110. }
  111. $form->saved(function () use ($form) {
  112. if ($form->status == -1) {//如何审核被拒绝,退回提现金额
  113. LanzuUserBalance::returnBalance($form->model()->cs_id, 3, $form->model()->money);
  114. } elseif ($form->status == 1) {//调用微信企业付
  115. //获取站点信息
  116. $csInfo = LanzuCsInfo::find($form->model()->cs_id);
  117. $res = WxPay::pay($csInfo, $form);
  118. if ($res['result_code'] == "SUCCESS") {//更新到账状态
  119. $csw = modelCsWithdraw::find($form->model()->id);
  120. $csw->is_pay = 1;
  121. $csw->save();
  122. } else {
  123. //记录失败日志
  124. Log::error('提现失败.', $res);
  125. }
  126. }
  127. });
  128. }
  129. });
  130. }
  131. }