WxCircleController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Admin\Controllers\Circle;
  3. use App\Admin\Actions\Extensions\NavigaterUrlButton;
  4. use App\Admin\Actions\Grid\CircleAction;
  5. use App\Admin\Renderable\IconRender;
  6. use App\Admin\Repositories\Circle\WxCircle;
  7. use App\Jobs\UpdateCircleInfoJob;
  8. use App\Models\User\WxUser;
  9. use App\Models\WxIcon;
  10. use App\Models\WxPlate;
  11. use App\Models\WxTenant;
  12. use App\Wen\Utils\AdminUtils;
  13. use App\Wen\Utils\Settings;
  14. use Dcat\Admin\Form;
  15. use Dcat\Admin\Grid;
  16. use Dcat\Admin\Show;
  17. use Dcat\Admin\Http\Controllers\AdminController;
  18. use Illuminate\Support\Facades\Cache;
  19. class WxCircleController extends AdminController
  20. {
  21. protected $tenants_arr = null;
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. global $__MINI_GLOBAL_TENANT_ID__;
  30. if($this->tenants_arr === null){
  31. $tenant_arr_ = [
  32. '-1' => '全域'
  33. ];
  34. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  35. $tenant_arr = WxTenant::orderBy('order', 'desc')->pluck('name', 'tenant_id')->toArray();
  36. }else{
  37. $tenant_arr = WxTenant::where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->orderBy('order', 'desc')->pluck('name', 'tenant_id')->toArray();
  38. }
  39. foreach ($tenant_arr as $key => $val){
  40. $tenant_arr_[$key.''] = $val;
  41. }
  42. $this->tenants_arr = $tenant_arr_;
  43. }
  44. return Grid::make(new WxCircle(['wxPlate','wxUser']), function (Grid $grid) {
  45. // tenant_id代表在哪个分站创建 tenant_show代表希望展现在哪个分站
  46. global $__MINI_GLOBAL_TENANT_ID__;
  47. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  48. $grid->model()->where('tenant_id', '=', $__MINI_GLOBAL_TENANT_ID__)->where('tenant_show', $__MINI_GLOBAL_TENANT_ID__);
  49. }
  50. $grid->simplePaginate();
  51. $grid->model()->orderBy('id', 'desc');
  52. $grid->quickSearch(['circle_name', 'circle_introduce', 'wxUser.user_name'])->placeholder('搜索圈子名称,介绍,圈主名字...')->width(35);
  53. $grid->column('id');
  54. $grid->column('owers', env('circle_user_call', '圈主'))->display(function ($v) {
  55. if($this->ower['user_avatar'] ?? ''){
  56. return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png')) . '" style="border-radius:50px;width:30px;" />';
  57. }
  58. return '';
  59. })->width('50px');
  60. $grid->column('ower', ' ')->display(function ($v) {
  61. if($v && $v['user_name']){
  62. return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
  63. }
  64. return '';
  65. })->width('100px');
  66. $grid->column('circle_name');
  67. $grid->column('sort')->editable()->sortable();
  68. $grid->column('list_style')->select([0=>'跟随全局', 1=>'列表式', 2=>'瀑布流']);
  69. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  70. $grid->column('tenant_show', '展示分站&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')->select($this->tenants_arr);
  71. }else{
  72. }
  73. $grid->column('circle_introduce')->sortable()->limit(9);
  74. $grid->column('head_portrait')->image('','80');
  75. $grid->column('background_maps')->image('','80');
  76. $grid->column('wxPlate.plate_name')->label('default');
  77. $grid->column('is_top_recommend')->switch()->sortable();
  78. $grid->column('is_hot')->switch()->sortable();
  79. $grid->column('circle_state')->using([0=>'审核中',1=>'审核通过',2=>'驳回'])->label([
  80. 0=>'red',
  81. 1=>'green',
  82. 2=>'default'
  83. ])->sortable();
  84. $grid->column('created_at')->sortable();
  85. $grid->filter(function (Grid\Filter $filter) {
  86. $filter->like('circle_name');
  87. $filter->equal('circle_state')->select([0=>'审核中',1=>'审核通过',2=>'驳回']);
  88. });
  89. // $grid->disableCreateButton();
  90. // 禁用详情按钮
  91. // $grid->disableViewButton();
  92. // $grid->disableBatchDelete();
  93. $grid->actions(function (Grid\Displayers\Actions $actions) {
  94. $actions->append((new CircleAction())->setKey($this->id));
  95. });
  96. $grid->tools([
  97. new NavigaterUrlButton('circle_tag', '', '圈子话题')
  98. ]);
  99. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  100. $grid->header(function ($collection) {
  101. return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block;margin-top: 20px;">圈子分为<b style="color: red">全站圈子</b>和<b style="color: red">分站圈子</b>。全站圈子:可在所有分站展示,有“仅看本站”的开关,在<b>所有圈子</b>里唯一。分站圈子:仅在某个分站展示,没有“仅看本站”的开关,仅在当前分站唯一</div>';
  102. });
  103. }
  104. });
  105. }
  106. /**
  107. * Make a show builder.
  108. *
  109. * @param mixed $id
  110. *
  111. * @return Show
  112. */
  113. protected function detail($id)
  114. {
  115. return Show::make($id, new WxCircle(), function (Show $show) {
  116. $show->field('id');
  117. $show->field('list_style');
  118. $show->field('tenant_show');
  119. $show->field('circle_name');
  120. $show->field('circle_introduce');
  121. $show->field('head_portrait');
  122. $show->field('background_maps');
  123. $show->field('plate_id');
  124. $show->field('user_id');
  125. $show->field('is_top_recommend');
  126. $show->field('is_hot');
  127. $show->field('circle_state');
  128. $show->field('tenant_id');
  129. $show->field('created_at');
  130. $show->field('updated_at');
  131. });
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. protected function form()
  139. {
  140. global $__MINI_GLOBAL_TENANT_ID__;
  141. if($this->tenants_arr === null){
  142. $tenant_arr_ = [
  143. '-1' => '全域'
  144. ];
  145. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  146. $tenant_arr = WxTenant::orderBy('order', 'desc')->pluck('name', 'tenant_id')->toArray();
  147. }else{
  148. $tenant_arr = WxTenant::where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->orderBy('order', 'desc')->pluck('name', 'tenant_id')->toArray();
  149. }
  150. foreach ($tenant_arr as $key => $val){
  151. $tenant_arr_[$key.''] = $val;
  152. }
  153. $this->tenants_arr = $tenant_arr_;
  154. }
  155. return Form::make(new WxCircle(), function (Form $form) {
  156. global $__MINI_GLOBAL_TENANT_ID__;
  157. $old_circle_name = $form->model()->circle_name;
  158. $form->display('id');
  159. $form->text('circle_name')->required();
  160. $form->number('sort');
  161. $form->radio('list_style')->options([0=>'跟随全局', 1=>'列表式', 2=>'瀑布流'])->default(Settings::get('app_circle_list_style', 0, true));
  162. if($__MINI_GLOBAL_TENANT_ID__ === 0){
  163. $form->select('tenant_show')->options($this->tenants_arr)->default(-1)->required();
  164. }
  165. $form->text('circle_introduce')->required();
  166. $form->image('head_portrait')->url('files/uploads')->uniqueName()->autoUpload()->required();
  167. $form->image('background_maps')->help('<a href="https://img.mini.minisns.cn/images/topci/bg.jpg" target="_blank">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->required()->default(Settings::get('img_default_circle_bg', ''));
  168. $form->select('plate_id')->options(WxPlate::pluck('plate_name', 'id'))->required();
  169. $form->select('user_id', env('circle_user_call', '圈主'))->options(function ($id) {
  170. if($id){
  171. $user = WxUser::find($id);
  172. if ($user) {
  173. return [$user->id => $user->user_name];
  174. }
  175. }
  176. })->ajax('select/users');
  177. $form->multipleSelectTable('square_list', '格子菜单')
  178. ->title('图标库')
  179. ->max(100)
  180. ->from(IconRender::make())
  181. ->model(WxIcon::class, 'id', 'name', 'selectTable:WxCircleController:circle_square_list')
  182. ->help('各个圈子自己额外的格子菜单, <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">(ps:如何控制顺序?)&nbsp;</a>')
  183. ->saveAsJson();
  184. $form->switch('is_top_recommend');
  185. $form->switch('is_hot');
  186. $form->switch('is_publish_admin');
  187. $form->radio('is_city_select')->options([0=>'关闭',1=>'开启'])->default(0);
  188. $form->radio('is_tenant_select')->help('仅[展示分站为全域]时起作用,也就是只有全站圈子会显示这个按钮,自然也只有全站圈子可以关闭')->options([0=>'关闭',1=>'开启'])->default(0);
  189. $form->radio('circle_state')->options([0=>'审核中',1=>'审核通过',2=>'驳回'])->default(1);
  190. global $__MINI_GLOBAL_TENANT_ID__;
  191. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  192. $form->text('tenant_id', '本站分应用id')->help('忽略这个配置')->default($__MINI_GLOBAL_TENANT_ID__);
  193. }
  194. $form->saving(function (Form $form){
  195. global $__MINI_GLOBAL_TENANT_ID__;
  196. if($__MINI_GLOBAL_TENANT_ID__ > 0 && $form->getKey() > 0){
  197. if(\App\Models\Circle\WxCircle::where('id', $form->getKey())->value('tenant_id') != $__MINI_GLOBAL_TENANT_ID__){
  198. return $form->response()->error('权限不足,不可以修改其他分站对象');
  199. }
  200. }
  201. if($form->input('user_id') > 0){
  202. $form->user_id = (int)($form->input('user_id'));
  203. }
  204. $form->tenant_id = $__MINI_GLOBAL_TENANT_ID__;
  205. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  206. $form->deleteInput('tenant_show');
  207. }
  208. $form->input('square_list', AdminUtils::_multipleSelectTable_order_process('selectTable:WxCircleController:circle_square_list', $form->input('square_list')));
  209. });
  210. $form->saved(function (Form $form){
  211. Cache::forget('circle:list:style:'.$form->getKey());
  212. });
  213. $form->deleting(function (Form $form){
  214. global $__MINI_GLOBAL_TENANT_ID__;
  215. if($__MINI_GLOBAL_TENANT_ID__ > 0){
  216. if(\App\Models\Circle\WxCircle::where('id', $form->getKey())->value('tenant_id') != $__MINI_GLOBAL_TENANT_ID__){
  217. return $form->response()->error('权限不足,不可以删除其他分站对象');
  218. }
  219. }
  220. });
  221. });
  222. }
  223. }