DynamicController.php 6.0 KB

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