链街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.

156 lines
6.3 KiB

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