DistAdminDistributorController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\InitAppearance;
  4. use App\Admin\Repositories\DistAdminDistributor;
  5. use App\Admin\Repositories\DistAppearance;
  6. use App\Libraries\CommonHelper;
  7. use App\Models\DistProductCategory;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Http\Controllers\AdminController;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Admin;
  14. use Illuminate\Support\Facades\DB;
  15. class DistAdminDistributorController extends AdminController
  16. {
  17. /**
  18. * page index
  19. */
  20. public function index(Content $content)
  21. {
  22. return $content
  23. ->header(admin_trans( 'admin.distro_management'))
  24. ->description('')
  25. ->breadcrumb(['text'=>'list','url'=>''])
  26. ->body($this->grid());
  27. }
  28. /**
  29. * Make a grid builder.
  30. *
  31. * @return Grid
  32. */
  33. protected function grid()
  34. {
  35. return Grid::make(DistAdminDistributor::with(['appearance']), function (Grid $grid) {
  36. //指定视图,去掉删除按钮
  37. $grid->view('admin.grid.table');
  38. $grid->column('id')->sortable();
  39. $grid->column('company_name');
  40. $grid->column('site_name');
  41. $grid->column('domain_type',admin_trans_label('domain'))->display(function ($domainType) {
  42. $title = "";
  43. if ($domainType == 0) {
  44. $title = $this->secondary_domain;
  45. } else {
  46. $title = $this->custom_domain;
  47. }
  48. return "<span style='color:#586cb1'>$title</span>";
  49. });
  50. $grid->column('country',);
  51. $grid->column('appearance.title' ,admin_trans_field('appearance'));
  52. $grid->column('remark');
  53. $grid->column('enabled')->switch();
  54. $grid->column('updated_at')->sortable();
  55. // 过滤
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $filter->panel();
  58. $filter->expand();
  59. $filter->equal('company_name', )->width(2);
  60. $filter->equal('site_name')->width(2);
  61. $filter->equal('domain')->width(2);
  62. $filter->equal('enabled', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
  63. });
  64. // 排序
  65. $grid->model()->orderBy("id",'desc');
  66. //按钮
  67. $grid->showQuickEditButton();
  68. $grid->enableDialogCreate();
  69. $grid->disableEditButton();
  70. $grid->disableDeleteButton();
  71. //$grid->disableBatchDelete();
  72. //批量操作
  73. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  74. $batch->disableDelete();
  75. $batch->add(new InitAppearance());
  76. });
  77. });
  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, DistAdminDistributor::with(['appearance']), function (Show $show) {
  89. $show->field('id');
  90. $show->field('company_name');
  91. $show->field('company_address');
  92. $show->field('site_name');
  93. $show->field('logo')->as(function ($image) {
  94. // 开始生成 HTML
  95. $dataImages = [$image];
  96. return CommonHelper::displayImage($dataImages,100);
  97. })->unescape();
  98. $show->field('domain_type',admin_trans_label('domain'))->as(function ($domainType) {
  99. $title = "";
  100. if ($domainType == 0) {
  101. $title = $this->secondary_domain;
  102. } else {
  103. $title = $this->custom_domain;
  104. }
  105. return "<span style='color:#586cb1'>$title</span>";
  106. })->unescape();
  107. $show->field('appearance.title' ,admin_trans_field('appearance'));
  108. $show->field('country');
  109. $show->field('contact_number');
  110. $show->field('service_hotline');
  111. $show->field('whats_app');
  112. $show->field('facebook');
  113. $show->field('instagram');
  114. $show->field('youtube');
  115. $show->field('linkedin');
  116. $show->field('tiktok');
  117. $show->field('remark');
  118. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  119. $show->field('created_at');
  120. $show->field('updated_at');
  121. $show->field('seo_title');
  122. $show->field('seo_keywords');
  123. $show->field('seo_description');
  124. // 按钮
  125. $show->disableDeleteButton();
  126. });
  127. }
  128. /**
  129. * Make a form builder.
  130. *
  131. * @return Form
  132. */
  133. protected function form()
  134. {
  135. return Form::make(new DistAdminDistributor(), function (Form $form) {
  136. $form->text('company_name')->width(9,3)->required();
  137. $form->text('company_address',)->width(9,3);
  138. $form->text('site_name')->width(9,3)->required();
  139. $form->image("logo")
  140. ->autoUpload()
  141. ->uniqueName()
  142. ->accept(config('distributor.upload.oss_image.accept'))
  143. ->maxSize(config('distributor.upload.oss_image.max_size'))
  144. ->dir(config("distributor.upload.directory.image").'/logo')
  145. ->width(9,3);
  146. $form->radio('domain_type')
  147. ->width(9,3)
  148. ->when(0, function (Form $form) {
  149. $form->text('secondary_domain')->width(9,3)->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
  150. })
  151. ->when(1, function (Form $form) {
  152. $form->text('custom_domain')->width(9,3)->help('Please enter the domain name, such as www.example.com,and bind the domain name to the IP address '.env('DIST_SITE_IP'));
  153. })
  154. ->default(0)
  155. ->options([0=>'second-level domain',1=>'custom domain'])
  156. ->required();
  157. $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
  158. $form->text('country')->width(9,3)->required();
  159. $form->text('contact_number')->width(9,3);
  160. $form->text('service_hotline')->width(9,3);
  161. $form->text('whats_app')->width(9,3);
  162. $form->text('facebook')->width(9,3);
  163. $form->text('instagram')->width(9,3);
  164. $form->text('youtube')->width(9,3);
  165. $form->text('linkedin')->width(9,3);
  166. $form->text('tiktok')->width(9,3);
  167. $form->textarea('remark')->width(9,3);
  168. $form->switch('enabled')->width(9,3)->default(1);
  169. $form->text('seo_title')->width(9,3)->required();
  170. $form->text('seo_keywords')->width(9,3);
  171. $form->textarea('seo_description')->width(9,3);
  172. //保存前回调
  173. $form->saving(function (Form $form) {
  174. if (!$form->isCreating()) {
  175. //如果appearance_id有变化,则更新模版与变量
  176. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  177. $id = $form->model()->id;
  178. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  179. }
  180. }
  181. });
  182. //保存后回调
  183. $form->saved(function (Form $form, $result) {
  184. if ($form->isCreating()) {
  185. //创建后创建默认分类
  186. $newId = $form->getKey();
  187. //初始化分销商
  188. DistAdminDistributor::initDist($form->input('appearance_id'),$newId);
  189. //更新模版与变量
  190. DistAppearance::switchTheme($form->input('appearance_id'), $newId);
  191. }
  192. });
  193. });
  194. }
  195. /*
  196. * 新增事件事务
  197. */
  198. public function store()
  199. {
  200. try {
  201. DB::beginTransaction();
  202. $result = parent::store();
  203. DB::commit();
  204. return $result;
  205. } catch (\Exception $e) {
  206. DB::rollBack();
  207. throw $e;
  208. }
  209. }
  210. }