DynamicController.php 6.1 KB

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