SubsiteController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\CategoryDistrict;
  5. use App\Models\Subsite;
  6. use App\Models\Tpl;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. use Illuminate\Support\Facades\Cache;
  14. class SubsiteController extends Controller
  15. {
  16. use HasResourceActions;
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('分站管理')
  27. ->description('')
  28. ->body($this->grid());
  29. }
  30. /**
  31. * Show interface.
  32. *
  33. * @param mixed $id
  34. * @param Content $content
  35. * @return Content
  36. */
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->header('分站管理')
  41. ->description('详细')
  42. ->body($this->detail($id));
  43. }
  44. /**
  45. * Edit interface.
  46. *
  47. * @param mixed $id
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function edit($id, Content $content)
  52. {
  53. return $content
  54. ->header('分站管理')
  55. ->description('编辑')
  56. ->body($this->editForm($id)->edit($id));
  57. }
  58. /**
  59. * Create interface.
  60. *
  61. * @param Content $content
  62. * @return Content
  63. */
  64. public function create(Content $content)
  65. {
  66. return $content
  67. ->header('分站管理')
  68. ->description('创建')
  69. ->body($this->createForm());
  70. }
  71. /**
  72. * Make a grid builder.
  73. *
  74. * @return Grid
  75. */
  76. protected function grid()
  77. {
  78. $grid = new Grid(new Subsite);
  79. $grid->id('ID');
  80. $grid->sitename('分站名称')->width(150);
  81. $grid->domain('分站域名')->width(150);
  82. /*$grid->m_domain('触屏版域名');*/
  83. $grid->districtname('地区')->width(150);
  84. $states = [
  85. 'on' => ['value' => 1, 'text' => '可用', 'color' => 'primary'],
  86. 'off' => ['value' => 0, 'text' => '禁用', 'color' => 'default'],
  87. ];
  88. $grid->effective('状态')->switch($states);
  89. $grid->created_at('添加时间');
  90. $grid->updated_at('更新时间');
  91. $grid->actions(function ($actions) use ($grid) {
  92. if (Admin::user()->can('system_subsite_edit')) {
  93. $actions->disableEdit(false);
  94. }
  95. if (Admin::user()->can('system_subsite_delete')) {
  96. $actions->disableDelete(false);
  97. }
  98. });
  99. if (Admin::user()->can('system_subsite_delete')) {
  100. $grid->tools(function ($tools) {
  101. $tools->batch(function ($batch) {
  102. $batch->disableDelete(false);
  103. });
  104. });
  105. $grid->disableRowSelector(false);
  106. }
  107. if (Admin::user()->can('system_subsite_create')) {
  108. $grid->disableCreateButton(false);
  109. }
  110. return $grid;
  111. }
  112. /**
  113. * Make a show builder.
  114. *
  115. * @param mixed $id
  116. * @return Show
  117. */
  118. protected function detail($id)
  119. {
  120. $show = new Show(Subsite::findOrFail($id));
  121. $show->id('ID');
  122. $show->sitename('分站名称');
  123. $show->domain('分站域名');
  124. $show->districtname('地区');
  125. $show->effective('状态')->as(function ($effective) {
  126. return $effective ? '可用' : '禁用';
  127. });
  128. $show->created_at('添加时间');
  129. $show->updated_at('更新时间');
  130. return $show;
  131. }
  132. /**
  133. * Make a form builder.
  134. *
  135. * @return Form
  136. */
  137. protected function form()
  138. {
  139. $form = new Form(new Subsite);
  140. $form->display('ID');
  141. $form->display('添加时间');
  142. $form->display('更新时间');
  143. return $form;
  144. }
  145. /**
  146. * Make a form builder.
  147. *
  148. * @return Form
  149. */
  150. protected function editForm($id)
  151. {
  152. $form = new Form(new Subsite);
  153. $subsite = Subsite::find($id);
  154. $form->text('sitename', '分站名称')->rules([
  155. 'required',
  156. ])->setWidth(2)->setMustMark();
  157. $form->radio('effective', '分站状态')->options([0=>'禁用',1=>'可用'])->default(1);
  158. $province_ids = CategoryDistrict::where(['parent_id'=>0])->pluck('id')->toArray();
  159. $citys = CategoryDistrict::whereIn('parent_id', $province_ids)->selectRaw("name,concat_ws('.',parent_id, id) as id")->pluck('name', 'id');
  160. $form->select('district', '所在地区')->options($citys)->rules(['required',])->setWidth(2)->setMustMark();
  161. $form->hidden('districtname');
  162. $form->text('domain', '域名绑定')->rules([
  163. 'required',
  164. ])->setWidth(2)->setMustMark();
  165. $defaultSubsite = Tpl::List()->where('tpl_type', 4)->pluck('name', 'id')->toArray();
  166. $defaultSubsite[0] = '默认';
  167. $form->select('tpl', 'PC端风格模版')->options($defaultSubsite)->rules([
  168. 'required',
  169. ])->setWidth(2)->setMustMark()->default($subsite->tpl);
  170. $form->number('order', '排序')->min(0)->default(0);
  171. $form->image('logo', '网站logo')->uniqueName()->setWidth(4);
  172. $form->image('logo_white', '网站Logo(白底)')->uniqueName()->setWidth(4);
  173. $form->image('logo_mini', '网站Logo mini')->uniqueName()->setWidth(4);
  174. $form->image('logo_white_mini', '网站Logo mini(白底)')->uniqueName()->setWidth(4);
  175. $form->text('site_name', '网站名称')->setWidth(4);
  176. $form->text('site_keyword', '网站默认关键字')->setWidth(4);
  177. $form->textarea('site_description', '网站默认描述')->setWidth(8);
  178. $form->text('map_ak', '百度地图AK')->setWidth(4);
  179. $form->text('map_x', '默认中心点X坐标')->setWidth(2);
  180. $form->text('map_y', '默认中心点Y坐标')->setWidth(2);
  181. $form->text('max_level', '默认缩放级别')->setWidth(2);
  182. $open_option = [
  183. 'on' => ['value' => 1],
  184. 'off' => ['value' => 0],
  185. ];
  186. $form->switch('is_open', '是否开启微信公众号平台')->states($open_option);
  187. $form->text('app_id', '微信AppID')->setWidth(4);
  188. $form->text('app_secret', 'AppSecret')->setWidth(4);
  189. $form->text('app_token', 'AppToken')->setWidth(4);
  190. $form->text('aes_key', 'EncodingAESKey, 消息加密密钥')->setWidth(4);
  191. $form->text('wechat_name', '公众号名称')->setWidth(4);
  192. $form->text('wechat_id', '微信号')->setWidth(4);
  193. $form->image('wechat_qrcode', '微信二维码')->uniqueName()->setWidth(4);
  194. $form->saving(function (Form $form) {
  195. $district = explode('.', $form->district);
  196. if (array_has($district, 1)) {
  197. $city_id = $district[1];
  198. $city_info = CategoryDistrict::find($city_id);
  199. $province_info = CategoryDistrict::find($city_info->parent_id);
  200. $form->districtname = $province_info->name.'/'.$city_info->name;
  201. }
  202. if ($form->effective=='on') {
  203. $form->effective=1;
  204. } elseif ($form->effective=='off') {
  205. $form->effective=0;
  206. }
  207. });
  208. $form->saved(function (Form $form) {
  209. Cache::forget('subsites_list');
  210. });
  211. return $form;
  212. }
  213. protected function createForm()
  214. {
  215. $form = new Form(new Subsite);
  216. $form->text('sitename', '分站名称')->rules([
  217. 'required',
  218. ])->setWidth(2)->setMustMark();
  219. $form->radio('effective', '分站状态')->options([0=>'禁用',1=>'可用'])->default(1);
  220. /*$form->select('province', '省')->options(
  221. CategoryDistrict::List()->pluck('name', 'id')
  222. )->load('district', '/admin/sys/category/categoryDis')->rules([
  223. 'required',
  224. ]);
  225. $form->select('district', '市')->rules([
  226. 'required',
  227. ]);*/
  228. //获取二级地区(市级)
  229. $province_ids = CategoryDistrict::where(['parent_id'=>0])->pluck('id')->toArray();
  230. //$citys = CategoryDistrict::whereIn('parent_id', $province_ids)->get()->pluck('name', 'id');
  231. $citys = CategoryDistrict::whereIn('parent_id', $province_ids)->selectRaw("name,concat_ws('.',parent_id, id) as id")->pluck('name', 'id');
  232. $form->select('district', '所在地区')->options($citys)->rules([
  233. 'required',
  234. ])->setWidth(2)->setMustMark();
  235. $form->hidden('districtname');
  236. $form->text('domain', '域名绑定')->rules([
  237. 'required',
  238. ])->setWidth(3)->setMustMark();
  239. $defaultSubsite = Tpl::List()->where('tpl_type', 4)->pluck('name', 'id')->toArray();
  240. $defaultSubsite[0] = '默认';
  241. $form->select('tpl', 'PC端风格模版')->options($defaultSubsite)->rules([
  242. 'required',
  243. ])->setWidth(2)->setMustMark()->default(0);
  244. $form->number('order', '排序')->min(0)->default(0);
  245. $form->image('logo', '网站logo')->uniqueName()->setWidth(4);
  246. $form->image('logo_white', '网站Logo(白底)')->uniqueName()->setWidth(4);
  247. $form->image('logo_mini', '网站Logo mini')->uniqueName()->setWidth(4);
  248. $form->image('logo_white_mini', '网站Logo mini(白底)')->uniqueName()->setWidth(4);
  249. $form->text('site_name', '网站名称')->setWidth(4);
  250. $form->text('site_keyword', '网站默认关键字')->setWidth(4);
  251. $form->textarea('site_description', '网站默认描述')->setWidth(8);
  252. $form->text('map_ak', '百度地图AK')->setWidth(4);
  253. $form->text('map_x', '默认中心点X坐标')->setWidth(2);
  254. $form->text('map_y', '默认中心点Y坐标')->setWidth(2);
  255. $form->text('max_level', '默认缩放级别')->setWidth(2);
  256. $open_option = [
  257. 'on' => ['value' => 1],
  258. 'off' => ['value' => 0],
  259. ];
  260. $form->switch('is_open', '是否开启微信公众号平台')->states($open_option)->default('1');
  261. $form->text('app_id', '微信AppID')->setWidth(4);
  262. $form->text('app_secret', 'AppSecret')->setWidth(4);
  263. $form->text('app_token', 'AppToken')->setWidth(4);
  264. $form->text('aes_key', 'EncodingAESKey, 消息加密密钥')->setWidth(4);
  265. $form->text('wechat_name', '公众号名称')->setWidth(4);
  266. $form->text('wechat_id', '微信号')->setWidth(4);
  267. $form->image('wechat_qrcode', '微信二维码')->uniqueName()->setWidth(4);
  268. $form->saving(function (Form $form) {
  269. $district = explode('.', $form->district);
  270. if (array_has($district, 1)) {
  271. $city_id = $district[1];
  272. $city_info = CategoryDistrict::find($city_id);
  273. $province_info = CategoryDistrict::find($city_info->parent_id);
  274. $form->districtname = $province_info->name.'/'.$city_info->name;
  275. }
  276. });
  277. return $form;
  278. }
  279. /**
  280. * Store a newly created resource in storage.
  281. *
  282. * @return mixed
  283. */
  284. public function store()
  285. {
  286. return $this->createForm()->store();
  287. }
  288. /**
  289. * Update the specified resource in storage.
  290. *
  291. * @param int $id
  292. *
  293. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  294. */
  295. public function update($id)
  296. {
  297. return $this->editForm($id)->update($id);
  298. }
  299. }