WxPostController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace App\Admin\Controllers\Posts;
  3. use App\Admin\Actions\Extensions\Posts\CommentManualButton;
  4. use App\Admin\Actions\Grid\PostCommentLotteryAction;
  5. use App\Admin\Actions\Grid\PostMetaAction;
  6. use App\Admin\Actions\Grid\PostPassAction;
  7. use App\Admin\Actions\Grid\PostsAction;
  8. use App\Admin\Actions\Grid\PostTopAction;
  9. use App\Admin\Actions\Grid\PostUnTopAction;
  10. use App\Admin\Actions\Grid\PostPushAction;
  11. use App\Admin\Actions\Grid\PostShelvesAction;
  12. use App\Admin\Repositories\Posts\WxPost;
  13. use App\Jobs\Posts\UpdatePostOnlyTextJob;
  14. use App\Jobs\UpdatePostImgCountJob;
  15. use App\Wen\Utils\PostUtils;
  16. use App\Wen\Utils\Settings;
  17. use App\Wen\Utils\Utils;
  18. use App\Models\Posts\WxPostsAddress;
  19. use App\Models\Posts\WxPostsTag;
  20. use App\Models\Posts\WxPostsVideo;
  21. use App\Models\Posts\WxTag;
  22. use App\Models\Circle\WxCircle;
  23. use Dcat\Admin\Actions\Action;
  24. use Dcat\Admin\Form;
  25. use Dcat\Admin\Grid;
  26. use Dcat\Admin\Show;
  27. use Dcat\Admin\Http\Controllers\AdminController;
  28. use Dcat\Admin\Widgets\Metrics\Card;
  29. use Illuminate\Support\Facades\Cache;
  30. class WxPostController extends AdminController
  31. {
  32. /**
  33. * Make a grid builder.
  34. *
  35. * @return Grid
  36. */
  37. protected function grid()
  38. {
  39. return Grid::make(new WxPost(['tags']), function (Grid $grid) {
  40. global $__MINI_GLOBAL_TENANT_ID__;
  41. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  42. $grid->model()->where('tenant_id', '=', $__MINI_GLOBAL_TENANT_ID__);
  43. }
  44. $grid->simplePaginate();
  45. $grid->quickSearch(['id', 'posts_content','user_id','user.user_name', 'circle_name'])->placeholder('搜索文章ID,内容,用户ID,用户名字,圈子名字...')->width(50);
  46. $grid->model()->orderBy('is_sticky', 'desc');
  47. $grid->model()->orderBy('id', 'desc');
  48. $grid->column('id')->sortable();
  49. $grid->column('is_sticky')->using([0 => '否', 1 => '↑'])->label([
  50. 0 => 'white',
  51. 1 => 'red'
  52. ])->sortable();
  53. $grid->column('users', '头像')->display(function ($v) {
  54. return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png')) . '" style="border-radius:50px;width:30px;" />';
  55. })->width('50px');
  56. $grid->column('user')->display(function ($v) {
  57. if($v && $v['user_name']){
  58. return '<a target="_blank" href="'.admin_url('/users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
  59. }
  60. return '用户已删除';
  61. })->width('100px');
  62. $grid->column('posts_content')->display('更多') // 设置按钮名称
  63. ->expand(function ($a) {
  64. $content = _mini_phone(_mini_aite_replace(_mini_emoji($this->posts_content, true), true), true);
  65. $images = $this->images_url;
  66. if($images){
  67. $images = Utils::imgWithStyle($images, 0, true);
  68. }
  69. $address = $this->address;
  70. $tagIds = WxPostsTag::where('posts_id', $this->id)->pluck('tags_id');
  71. // dump($tagIds);
  72. $tags = WxTag::whereIn('id', $tagIds)->get();
  73. $video = WxPostsVideo::where('posts_id', $this->id)->first();
  74. // 只填充内容,不需要标题
  75. $card = Card::make(view('admin/posts/detail', compact('content', 'images', 'address', 'tags', 'video')));
  76. return $card;
  77. })->width('150px');
  78. $grid->column('pay_content_id')->display(function ($v){
  79. if(_empty_($v)){
  80. return '';
  81. }
  82. return '<a target="_blank" href="'.admin_url('posts/paycontent?id='.$v).'">付费内容</a>';
  83. });
  84. // $grid->column('images_url')->display(function ($pictures) {
  85. //
  86. // return $pictures;
  87. //
  88. // })->image('',200,90);
  89. // $grid->column('circle_id');
  90. $grid->column('circle_name');
  91. $grid->column('is_anonymous')->display(function ($v){
  92. if($v == 1){
  93. return '是';
  94. }else{
  95. return '';
  96. }
  97. });
  98. // $grid->column('tags')->display(function($v){
  99. // return $this->id;
  100. // });
  101. // $grid->column('address_id');
  102. // $grid->column('tags_id');
  103. // $grid->column('is_information')->select([0=>'否',1=>'是']);
  104. $grid->column('is_information')->switch()->sortable();
  105. $grid->column('is_examine')->using([0 => '待审核', 1 => '审核通过', 2 => '驳回'])->label([
  106. 0 => 'red',
  107. 1 => 'green',
  108. 2 => 'default'
  109. ])->sortable();
  110. // $grid->column('overrule_content');
  111. // $grid->column('overrule_is_read');
  112. $grid->column('posts_state')->using([0 => '推送中', 1 => '用户删除', 2 => '已下架'])->label([
  113. 0 => 'primary',
  114. 1 => 'red',
  115. 2 => 'default'
  116. ])->sortable();
  117. $grid->column('created_at');
  118. // $grid->column('updated_at')->sortable();
  119. $grid->filter(function (Grid\Filter $filter) {
  120. $filter->equal('id');
  121. $filter->equal('user_id');
  122. $filter->equal('circle_id')->select(WxCircle::pluck('circle_name', 'id'));
  123. $filter->equal('is_information')->select([0 => '否', 1 => '是']);
  124. $filter->equal('is_examine')->select([0 => '待审核', 1 => '审核通过', 2 => '驳回']);
  125. $filter->between('created_at')->date();
  126. });
  127. // 或
  128. $grid->batchActions(function ($batch) {
  129. $batch->add(new PostPassAction());
  130. $batch->add(new PostTopAction());
  131. $batch->add(new PostUnTopAction());
  132. $batch->add(new PostPushAction());
  133. $batch->add(new PostShelvesAction());
  134. });
  135. $grid->actions(function (Grid\Displayers\Actions $actions) {
  136. $actions->append((new PostsAction())->setKey($this->id));
  137. $actions->append((new PostMetaAction())->setKey($this->id));
  138. $actions->append((new PostCommentLotteryAction())->setKey($this->id));
  139. });
  140. $grid->disableCreateButton();
  141. // $grid->disableActions();
  142. // 禁用编辑按钮
  143. // $grid->disableEditButton();
  144. // 禁用详情按钮
  145. $grid->disableViewButton();
  146. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  147. $grid->tools([
  148. new CommentManualButton(),
  149. ]);
  150. }
  151. });
  152. }
  153. /**
  154. * Make a show builder.
  155. *
  156. * @param mixed $id
  157. *
  158. * @return Show
  159. */
  160. protected function detail($id)
  161. {
  162. return Show::make($id, new WxPost(), function (Show $show) {
  163. $show->field('id');
  164. $show->field('posts_content');
  165. $show->field('user_id');
  166. $show->field('circle_id');
  167. $show->field('circle_name');
  168. $show->field('address_id');
  169. $show->field('tags_id', '话题id(预留都为0)');
  170. $show->field('is_information');
  171. $show->field('is_examine');
  172. $show->field('overrule_content');
  173. $show->field('overrule_is_read');
  174. $show->field('posts_state');
  175. $show->field('tenant_id');
  176. $show->field('created_at');
  177. $show->field('updated_at');
  178. });
  179. }
  180. /**
  181. * Make a form builder.
  182. *
  183. * @return Form
  184. */
  185. protected function form()
  186. {
  187. return Form::make(new WxPost(), function (Form $form) {
  188. $form->display('id');
  189. if(($form->model()->img_count) > 0){
  190. UpdatePostImgCountJob::dispatch(($form->model()->id), 0, 0)->delay(now()->addSeconds(10));
  191. }
  192. if(__system_is_model_enable('laradocs', 'dcat-neditor')){
  193. $form->neditor('posts_content');
  194. }else{
  195. $form->editor('posts_content');
  196. }
  197. // $form->multipleSelectTable('user_id', '用户')
  198. // ->title('用户')
  199. // ->max(2)
  200. // ->from(UsersRender::make())
  201. // ->model(WxUser::class, 'id', 'user_name')->saving(function ($v) {
  202. // if(is_array($v) && count($v) > 0){
  203. // return $v[0];
  204. // }else if(is_numeric($v)){
  205. // return $v;
  206. // }
  207. // return 0;
  208. // })->required();
  209. $form->select('user_id', '用户')->options(function ($id) {
  210. $user = \App\Models\User\WxUser::find($id);
  211. if ($user) {
  212. return [$user->id => $user->user_name];
  213. }
  214. })->ajax('select/users');
  215. // $form->text('circle_id');
  216. $form->select('circle_id','圈子ID')
  217. ->options(WxCircle::pluck('circle_name','id'));
  218. // $form->text('circle_name');
  219. $form->select('circle_name','圈子名称')
  220. ->options(WxCircle::pluck('circle_name','circle_name'));
  221. // $form->text('address_id');
  222. $form->select('address_id','位置')
  223. ->options(WxPostsAddress::pluck('address_name','id'));
  224. // $form->text('tags_id');
  225. $form->display('tags_id','话题id');
  226. $form->text('voter_id')->help('暂时通过修改这里,来和选票活动关联');
  227. $form->switch('is_information');
  228. // $form->text('is_examine');
  229. $form->radio('is_examine', '审核状态')->options([0=>'审核中',
  230. 1=>'审核通过',
  231. 2=>'驳回']);
  232. $form->text('overrule_content');
  233. // $form->text('overrule_is_read');
  234. $form->radio('overrule_is_read', '驳回用户是否已读')->options([0=>'未读',
  235. 1=>'已读']);
  236. // $form->text('posts_state');
  237. $form->radio('posts_state', '状态')->options([0=>'正常',
  238. 1=>'用户删除',2=>'管理员下架']);
  239. global $__MINI_GLOBAL_TENANT_ID__;
  240. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  241. $form->text('tenant_id', '本站分应用id')->help('忽略这个配置')->default($__MINI_GLOBAL_TENANT_ID__);
  242. }
  243. $form->saving(function (Form $form){
  244. global $__MINI_GLOBAL_TENANT_ID__;
  245. $form->tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  246. if(!_empty_($form->input('posts_content'))){
  247. $form->posts_content = PostUtils::post_icon_inline_process($form->input('posts_content'));
  248. }
  249. });
  250. $form->saved(function (Form $form){
  251. Cache::forget('posts:post_type:'.($form->model()->id));
  252. Cache::forget('get_the_title:'.($form->model()->id).':30');
  253. Cache::forget('get_the_title:'.($form->model()->id).':15');
  254. Cache::forget('get_the_title:'.($form->model()->id).':100');
  255. Cache::forget('get:post:goods:'.($form->model()->id));
  256. Cache::forget('get:post:tags:'.($form->model()->id));
  257. Cache::forget('get:post:files:'.($form->model()->id));
  258. Cache::forget('get:post:sounds:'.($form->model()->id));
  259. \App\Models\Posts\WxPost::withoutSyncingToSearch(function () use (&$form){
  260. \App\Models\Posts\WxPost::where('id', $form->model()->id )->update(['is_only_text'=>null]);
  261. });
  262. UpdatePostOnlyTextJob::dispatch($form->model()->id);
  263. });
  264. $form->deleting(function (Form $form){
  265. global $__MINI_GLOBAL_TENANT_ID__;
  266. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  267. if(\App\Models\Posts\WxPost::where('id', $form->getKey())->value('tenant_id') != $__MINI_GLOBAL_TENANT_ID__){
  268. return $form->response()->error('权限不足,不可以删除其他分站对象');
  269. }
  270. }
  271. });
  272. });
  273. }
  274. }