TaskListController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\TaskList;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Http\Controllers\AdminController;
  8. class TaskListController extends AdminController
  9. {
  10. /**
  11. * Make a grid builder.
  12. *
  13. * @return Grid
  14. */
  15. protected function grid()
  16. {
  17. return Grid::make(new TaskList(), function (Grid $grid) {
  18. $grid->model()->with('userData')->where('is_handpick', 0)->orderByDesc('id');
  19. $grid->column('id')->sortable();
  20. $grid->column('userData.name', '昵称');
  21. $grid->column('userData.mobile', '手机号');
  22. $grid->column('userData.avatar', '头像')->image('', 40, 40);
  23. $grid->column('title')->display(function ($initContent) {
  24. $maxLength = 10; // 你希望显示的最大字符数
  25. $ellipsis = '...'; // 省略号
  26. // 使用 mb_substr 截取字符串
  27. $displayContent = mb_substr($initContent, 0, $maxLength);
  28. // 如果原始字符串长度超过最大长度,添加省略号
  29. if (mb_strlen($initContent) > $maxLength) {
  30. $displayContent .= $ellipsis;
  31. }
  32. return $displayContent;
  33. });
  34. // $grid->column('role_id');
  35. // $grid->column('content');
  36. $grid->column('state')->using(TaskList::$state)
  37. ->dot(
  38. [
  39. 0 => 'primary',
  40. 1 => 'secondary',
  41. 2 => 'danger',
  42. 3 => 'warning',
  43. 4 => 'info',
  44. 5 => 'light',
  45. 6 => 'success',
  46. ],
  47. 'danger' // 第二个参数为默认值
  48. );
  49. // $grid->column('image')->image('',80,80);
  50. // $grid->column('init_content')->width('300px')->display(function ($initContent) {
  51. // $maxLength = 10; // 你希望显示的最大字符数
  52. // $ellipsis = '...'; // 省略号
  53. //
  54. // // 使用 mb_substr 截取字符串
  55. // $displayContent = mb_substr($initContent, 0, $maxLength);
  56. //
  57. // // 如果原始字符串长度超过最大长度,添加省略号
  58. // if (mb_strlen($initContent) > $maxLength) {
  59. // $displayContent .= $ellipsis;
  60. // }
  61. //
  62. // return $displayContent;
  63. // });
  64. // $grid->column('keyword');
  65. // $grid->column('sd_image');
  66. // $grid->column('desc');
  67. // $grid->column('sd_id')->image('',80,80);
  68. $grid->column('surplus_diamond');
  69. // $grid->column('nickname');
  70. // $grid->column('plot');
  71. $grid->column('is_handpick')->switch();
  72. $grid->column('sort')->sortable();
  73. $grid->column('pdf_path')->display(function ($pdfPath) {
  74. // $downloadUrl = url($pdfPath);
  75. if (!empty($pdfPath)) {
  76. return "<a target='_blank' href='" . $pdfPath . "'>下载PDF</a>";
  77. }
  78. return '还未生成PDF';
  79. });
  80. $grid->column('image_path', '图片地址')->display(function ($image_path) {
  81. // $downloadUrl = url($pdfPath);
  82. if (!empty($image_path)) {
  83. return "<a target='_blank' href='" . $image_path . "'>浏览图片</a>";
  84. }
  85. return '还未生成图片';
  86. });
  87. // $grid->column('pinyin_content');
  88. $grid->column('created_at');
  89. $grid->column('updated_at')->sortable();
  90. $grid->disableEditButton();
  91. $grid->disableDeleteButton();
  92. $grid->disableViewButton();
  93. $grid->disableCreateButton();
  94. $grid->filter(function (Grid\Filter $filter) {
  95. $filter->panel();
  96. $filter->between('created_at')->datetime();
  97. $filter->equal('is_handpick', '是否精选')->select([0 => '不是精选', 1 => '精选']);
  98. $filter->where('mobile', function ($query) {
  99. $query->whereHas('userData', function ($query) {
  100. $query->where('mobile', 'like', "%{$this->input}%");
  101. });
  102. }, '用户手机号');
  103. $filter->where('name', function ($query) {
  104. $query->whereHas('userData', function ($query) {
  105. $query->where('name', 'like', "%{$this->input}%");
  106. });
  107. }, '用户昵称');
  108. });
  109. });
  110. }
  111. /**
  112. * Make a show builder.
  113. *
  114. * @return Show
  115. */
  116. protected function detail($id)
  117. {
  118. return Show::make($id, new TaskList(), function (Show $show) {
  119. $show->field('id');
  120. $show->field('user_id');
  121. $show->field('title');
  122. $show->field('role_id');
  123. $show->field('content');
  124. $show->field('state');
  125. $show->field('image');
  126. $show->field('init_content');
  127. $show->field('keyword');
  128. $show->field('sd_image');
  129. $show->field('desc');
  130. $show->field('sd_id');
  131. $show->field('surplus_diamond');
  132. $show->field('nickname');
  133. $show->field('plot');
  134. $show->field('is_handpick');
  135. $show->field('sort');
  136. $show->field('pinyin_content');
  137. $show->field('pdf_path');
  138. $show->field('created_at');
  139. $show->field('updated_at');
  140. });
  141. }
  142. /**
  143. * Make a form builder.
  144. *
  145. * @return Form
  146. */
  147. protected function form()
  148. {
  149. return Form::make(new TaskList(), function (Form $form) {
  150. $form->display('id');
  151. $form->text('user_id');
  152. $form->text('title');
  153. $form->text('role_id');
  154. $form->text('content');
  155. $form->text('state');
  156. $form->text('image');
  157. $form->text('init_content');
  158. $form->text('keyword');
  159. $form->text('sd_image');
  160. $form->text('desc');
  161. $form->text('sd_id');
  162. $form->text('surplus_diamond');
  163. $form->text('nickname');
  164. $form->text('plot');
  165. $form->text('is_handpick');
  166. $form->text('sort');
  167. $form->text('pinyin_content');
  168. $form->text('pdf_path');
  169. $form->display('created_at');
  170. $form->display('updated_at');
  171. });
  172. }
  173. }