TaskController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Admin\Controllers\Company;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Task;
  5. use Encore\Admin\Auth\Permission;
  6. use Encore\Admin\Controllers\HasResourceActions;
  7. use Encore\Admin\Facades\Admin;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Encore\Admin\Show;
  12. use Illuminate\Support\Facades\Route;
  13. class TaskController extends Controller
  14. {
  15. use HasResourceActions;
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. * @param $type_id
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. $name = Route::currentRouteName();
  26. $arr = explode('.', $name);
  27. $header = '企业任务';
  28. if ($arr[0] == 'task') { //路由参数变化导致。
  29. $type_id =1;
  30. Permission::check('companytask_manager');
  31. } else {
  32. $type_id =2;
  33. $header = '个人任务';
  34. Permission::check('person_task');
  35. }
  36. return $content
  37. ->header($header)
  38. ->description('列表')
  39. ->body($this->grid($type_id));
  40. }
  41. /**
  42. * Show interface.
  43. *
  44. * @param mixed $id
  45. * @param Content $content
  46. * @return Content
  47. */
  48. public function show($id, Content $content)
  49. {
  50. return $content
  51. ->header('企业任务')
  52. ->description('任务详情')
  53. ->body($this->detail($id));
  54. }
  55. /**
  56. * Edit interface.
  57. *
  58. * @param mixed $id
  59. * @param Content $content
  60. * @return Content
  61. */
  62. public function edit($id, Content $content)
  63. {
  64. return $content
  65. ->header('Edit')
  66. ->description('description')
  67. ->body($this->form()->edit($id));
  68. }
  69. /**
  70. * Create interface.
  71. *
  72. * @param Content $content
  73. * @return Content
  74. */
  75. public function create(Content $content)
  76. {
  77. return $content
  78. ->header('Create')
  79. ->description('description')
  80. ->body($this->form());
  81. }
  82. /**
  83. * Make a grid builder.
  84. *
  85. * @param $type_id
  86. * @return Grid
  87. */
  88. protected function grid($type_id)
  89. {
  90. if ($type_id == 1) {
  91. Permission::check('task_manager_company');
  92. } else {
  93. Permission::check('person_task');
  94. }
  95. $grid = new Grid(new Task);
  96. $grid->model()->where('utype', $type_id);
  97. $grid->title('任务名称')->width(200);
  98. if ($type_id == 1) {
  99. if (Admin::user()->can('task_manager_company_edit')) {
  100. $grid->status('是否启用')->switch()->display(function ($status) {
  101. if ($this->title == '每日签到') {
  102. return '不允许关闭';
  103. }
  104. return $status;
  105. });
  106. } else {
  107. $grid->status('是否启用')->display(function ($status) {
  108. if ($this->title == '每日签到') {
  109. return '不允许关闭';
  110. }
  111. if ($status =0) {
  112. return "关闭";
  113. }
  114. return "开启";
  115. });
  116. }
  117. } else {
  118. if (Admin::user()->can('person_task_edit')) {
  119. $grid->status('是否启用')->switch()->display(function ($status) {
  120. if ($this->title == '每日签到') {
  121. return '不允许关闭';
  122. }
  123. return $status;
  124. });
  125. } else {
  126. $grid->status('是否启用')->display(function ($status) {
  127. if ($this->title == '每日签到') {
  128. return '不允许关闭';
  129. }
  130. if ($status =0) {
  131. return "关闭";
  132. }
  133. return "开启";
  134. });
  135. }
  136. }
  137. if ($type_id == 1) {
  138. if (Admin::user()->can('task_manager_company_edit')) {
  139. $grid->points('增加(积分)')->editable();
  140. } else {
  141. $grid->points('增加(积分)');
  142. }
  143. } else {
  144. if (Admin::user()->can('person_task_edit')) {
  145. $grid->points('增加(积分)')->editable();
  146. } else {
  147. $grid->points('增加(积分)');
  148. }
  149. }
  150. if ($type_id == 1) {
  151. if (Admin::user()->can('task_manager_company_edit')) {
  152. $grid->times('每天赠送积分上限')->editable()->display(function ($times) {
  153. if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
  154. return $times . "/次";
  155. } else {
  156. return "--";
  157. }
  158. });
  159. } else {
  160. $grid->times('每天赠送积分上限')->display(function ($times) {
  161. if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
  162. return $times . "/次";
  163. } else {
  164. return "--";
  165. }
  166. });
  167. }
  168. } else {
  169. if (Admin::user()->can('person_task_edit')) {
  170. $grid->times('每天赠送积分上限')->editable()->display(function ($times) {
  171. if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
  172. return $times . "/次";
  173. } else {
  174. return "--";
  175. }
  176. });
  177. } else {
  178. $grid->times('每天赠送积分上限')->display(function ($times) {
  179. if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
  180. return $times . "/次";
  181. } else {
  182. return "--";
  183. }
  184. });
  185. }
  186. }
  187. $grid->disableFilter();
  188. return $grid;
  189. }
  190. /**
  191. * Make a show builder.
  192. *
  193. * @param mixed $id
  194. * @return Show
  195. */
  196. protected function detail($id)
  197. {
  198. $show = new Show(Task::findOrFail($id));
  199. $show->id('ID');
  200. $show->title('任务名称');
  201. $show->status('是否启用')->as(function ($status) {
  202. if ($status == 1) {
  203. return "启用";
  204. } else {
  205. return "关闭";
  206. }
  207. });
  208. $show->points('增加(积分)');
  209. $show->once('是否一次性任务')->as(function ($once) {
  210. switch ($once) {
  211. case 1:
  212. return "是";
  213. break;
  214. case 0:
  215. return '否';
  216. break;
  217. }
  218. });
  219. $show->times('每天赠送积分上限')->as(function ($times) {
  220. if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
  221. return $this->times;
  222. }
  223. });
  224. return $show;
  225. }
  226. /**
  227. * Make a form builder.
  228. *
  229. * @return Form
  230. */
  231. protected function form()
  232. {
  233. $form = new Form(new Task);
  234. $form->display('ID');
  235. $form->switch('status');
  236. $form->text('points', '积分');
  237. $form->text('times', '每天赠送积分上限');
  238. $form->display('Created at');
  239. $form->display('Updated at');
  240. return $form;
  241. }
  242. }