DynamicController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\DynamicModel;
  4. use App\Models\DynamicTag;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. use Illuminate\Support\Facades\DB;
  10. class DynamicController extends AdminController
  11. {
  12. /**
  13. * Make a grid builder.
  14. *
  15. * @return Grid
  16. */
  17. protected function grid()
  18. {
  19. $grid = new Grid(new DynamicModel());
  20. $grid->model()->with(['user','user_info'])->orderBy('id','desc');
  21. $grid->column('id')->sortable();
  22. $grid->column('user_info.avatar','头像')->image("",50);;
  23. $grid->column('user_id','用户')->display(function (){
  24. return $this->user->mobile.'<br>'.$this->user_info->nickname;
  25. });
  26. $grid->column('img_url','图片内容')->display(function ($v){
  27. if($v){
  28. $v = json_decode($v,true);
  29. $str = '';
  30. if(count($v)>0){
  31. foreach ($v as $item){
  32. $str.='<img data-action="preview-img" src="'.$item.'" style="max-width:50px;max-height:200px;cursor:pointer" class="img img-thumbnail">';
  33. }
  34. }
  35. return $str;
  36. }else{
  37. return "";
  38. }
  39. });
  40. $grid->tableCollapse(false);
  41. $grid->column('content','动态内容')->limit(30);
  42. $grid->column('zan_num');
  43. $grid->column('city','所在位置');
  44. $grid->column('status','状态')->switch();
  45. $grid->column('tag')->display(function ($res){
  46. $tag = DB::table("dynamic_tag")->whereIn('id',explode(',',$res))->get();
  47. $str = '<div>';
  48. foreach ($tag as $k=>$v){
  49. $str .= '<span class="label" style="background:#21b978;margin-right: 5px">'.$v->title.'</span>';
  50. }
  51. $str .= '</div>';
  52. return $str;
  53. });
  54. $grid->column('created_at','发布时间');
  55. $grid->filter(function (Grid\Filter $filter) {
  56. $filter->equal('id');
  57. });
  58. $grid->disableCreateButton();
  59. $grid->filter(function (Grid\Filter $filter) {
  60. $filter->equal('user_id','用户ID');
  61. $filter->like('user_info.nickname','用户昵称');
  62. $filter->like('user.mobile','手机号');
  63. $filter->like('content','内容');
  64. $filter->between('created_at', '发布时间')->datetime();
  65. $tag = DynamicTag::query()->get();
  66. $tag_arr = array();
  67. foreach ($tag as $v){
  68. $tag_arr[$v['id']] = $v['title'];
  69. }
  70. $filter->like('tag', '标签')->select($tag_arr);
  71. });
  72. return $grid;
  73. }
  74. /**
  75. * Make a show builder.
  76. *
  77. * @param mixed $id
  78. *
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make($id, new DynamicModel(), function (Show $show) {
  84. $show->field('id');
  85. $show->field('user_id','用户ID');
  86. $show->html(function ($res){
  87. $str = "";
  88. if($res['img_url']){
  89. $res = json_decode($res['img_url'],true);
  90. if(count($res)>0){
  91. foreach ($res as $k=>$v){
  92. $str.='<img data-action="preview-img" src="'.$v.'" style="max-width:100px;max-height:100px;cursor:pointer" class="img img-thumbnail">';
  93. }
  94. }else{
  95. $str = "无";
  96. }
  97. }else{
  98. $str = "无";
  99. }
  100. $html = '<div class="show-field form-group row">
  101. <div class="col-sm-2 control-label">
  102. <span>图片</span>
  103. </div>
  104. <div class="col-sm-8">
  105. <div class="box box-solid box-default no-margin box-show">
  106. <div class="box-body">
  107. '.$str.'
  108. </div>
  109. </div>
  110. </div>
  111. </div>';
  112. return $html;
  113. });
  114. $show->field('content')->unescape();
  115. // $show->field('status');
  116. $show->field('zan_num');
  117. $show->html(function ($res){
  118. $str = "";
  119. $tag = DB::table("dynamic_tag")->whereIn('id',[$res['tag']])->get();
  120. if(count($tag)>0){
  121. foreach ($tag as $k=>$v){
  122. $str .= '<span class="label" style="background:#21b978;margin-right: 5px">'.$v->title.'</span>';
  123. }
  124. }else{
  125. $str = "无";
  126. }
  127. $html = '<div class="show-field form-group row">
  128. <div class="col-sm-2 control-label">
  129. <span>标签</span>
  130. </div>
  131. <div class="col-sm-8">
  132. <div class="box box-solid box-default no-margin box-show">
  133. <div class="box-body">
  134. '.$str.'
  135. </div>
  136. </div>
  137. </div>
  138. </div>';
  139. return $html;
  140. });
  141. $show->field('city','城市');
  142. // $show->field('latitude','纬度');
  143. // $show->field('longitude','经度');
  144. });
  145. }
  146. /**
  147. * Make a form builder.
  148. *
  149. * @return Form
  150. */
  151. protected function form()
  152. {
  153. return Form::make(new DynamicModel(), function (Form $form) {
  154. $form->display('id');
  155. $form->text('user_id');
  156. $form->text('img_url');
  157. $form->switch('status')->default(1);
  158. $form->text('zan_num');
  159. $form->text('tag');
  160. });
  161. }
  162. }