DistAdminDistributorController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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->paginate(config('admin.per_page'));
  38. //指定视图,去掉删除按钮
  39. $grid->view('admin.grid.table');
  40. $grid->column('id')->sortable();
  41. $grid->column('company_name')->width('30%');
  42. $grid->column('site_name');
  43. $grid->column('domain_type',admin_trans_label('domain'))->display(function ($domainType) {
  44. $title = "";
  45. if ($domainType == 0) {
  46. $title = $this->secondary_domain;
  47. } else {
  48. $title = $this->custom_domain;
  49. }
  50. return "<span style='color:#586cb1'>$title</span>";
  51. });
  52. $grid->column('country',);
  53. $grid->column('appearance.title' ,admin_trans_field('appearance'));
  54. $grid->column('remark');
  55. $grid->column('enabled')->switch();
  56. $grid->column('updated_at')->sortable();
  57. // 过滤
  58. $grid->filter(function (Grid\Filter $filter) {
  59. $filter->panel();
  60. $filter->expand();
  61. $filter->equal('company_name', )->width(2);
  62. $filter->equal('site_name')->width(2);
  63. $filter->equal('domain')->width(2);
  64. $filter->equal('enabled', )->select(admin_trans_array(config('dictionary.enabled')))->width(2);
  65. });
  66. // 排序
  67. $grid->model()->orderBy("id",'desc');
  68. //按钮
  69. $grid->showQuickEditButton();
  70. $grid->enableDialogCreate();
  71. $grid->disableEditButton();
  72. $grid->disableDeleteButton();
  73. //$grid->disableBatchDelete();
  74. //批量操作
  75. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  76. $batch->disableDelete();
  77. $batch->add(new InitAppearance());
  78. });
  79. });
  80. }
  81. /**
  82. * Make a show builder.
  83. *
  84. * @param mixed $id
  85. *
  86. * @return Show
  87. */
  88. protected function detail($id)
  89. {
  90. return Show::make($id, DistAdminDistributor::with(['appearance']), function (Show $show) {
  91. $show->field('id');
  92. $show->field('company_name');
  93. $show->field('company_address');
  94. $show->field('site_name');
  95. $show->field('logo')->as(function ($image) {
  96. // 开始生成 HTML
  97. $dataImages = [$image];
  98. return CommonHelper::displayImage($dataImages,100);
  99. })->unescape();
  100. $show->field('domain_type',admin_trans_label('domain'))->as(function ($domainType) {
  101. $title = "";
  102. if ($domainType == 0) {
  103. $title = $this->secondary_domain;
  104. } else {
  105. $title = $this->custom_domain;
  106. }
  107. return "<span style='color:#586cb1'>$title</span>";
  108. })->unescape();
  109. $show->field('appearance.title' ,admin_trans_field('appearance'));
  110. $show->field('country');
  111. $show->field('contact_number');
  112. $show->field('service_hotline');
  113. $show->field('whats_app');
  114. $show->field('facebook');
  115. $show->field('instagram');
  116. $show->field('youtube');
  117. $show->field('linkedin');
  118. $show->field('tiktok');
  119. $show->field('remark');
  120. $show->field('enabled')->using(admin_trans_array(config('dictionary.enabled')));
  121. $show->field('created_at');
  122. $show->field('updated_at');
  123. $show->field('copy_right');
  124. $show->field('seo_title');
  125. $show->field('seo_keywords');
  126. $show->field('seo_description');
  127. // 按钮
  128. $show->disableDeleteButton();
  129. });
  130. }
  131. /**
  132. * Make a form builder.
  133. *
  134. * @return Form
  135. */
  136. protected function form()
  137. {
  138. return Form::make(new DistAdminDistributor(), function (Form $form) {
  139. $form->text('company_name')->width(9,3)->required();
  140. $form->text('company_address',)->width(9,3);
  141. $form->text('site_name')->width(9,3)->required();
  142. $form->image("logo")
  143. ->autoUpload()
  144. ->uniqueName()
  145. ->accept(config('distributor.upload.oss_image.accept'))
  146. ->maxSize(config('distributor.upload.oss_image.max_size'))
  147. ->dir(config("distributor.upload.directory.image").'/logo')
  148. ->width(9,3);
  149. $form->radio('domain_type')
  150. ->width(9,3)
  151. ->when(0, function (Form $form) {
  152. $form->text('secondary_domain')->width(9,3)->help('Please enter the domain name, such as demo.'.env('TOP_DOMAIN'));
  153. })
  154. ->when(1, function (Form $form) {
  155. $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'));
  156. })
  157. ->default(0)
  158. ->options([0=>'second-level domain',1=>'custom domain'])
  159. ->required();
  160. $form->select('appearance_id',admin_trans_field('appearance'))->width(9,3)->options(DistAppearance::selectOptions())->required();
  161. $form->text('country')->width(9,3)->required();
  162. $form->text('contact_number')->width(9,3);
  163. $form->text('service_hotline')->width(9,3);
  164. $form->text('whats_app')->width(9,3);
  165. $form->text('facebook')->width(9,3);
  166. $form->text('instagram')->width(9,3);
  167. $form->text('youtube')->width(9,3);
  168. $form->text('linkedin')->width(9,3);
  169. $form->text('tiktok')->width(9,3);
  170. $form->textarea('remark')->width(9,3);
  171. $form->switch('enabled')->width(9,3)->default(1);
  172. $form->text('copy_right')->width(9,3);
  173. $form->text('seo_title')->width(9,3)->required();
  174. $form->text('seo_keywords')->width(9,3);
  175. $form->textarea('seo_description')->width(9,3);
  176. //保存前回调
  177. $form->saving(function (Form $form) {
  178. if (!$form->isCreating()) {
  179. //如果appearance_id有变化,则更新模版与变量
  180. if ($form->model()->appearance_id != $form->input('appearance_id')) {
  181. $id = $form->model()->id;
  182. //更新模版与变量
  183. DistAppearance::switchTheme($form->input('appearance_id'), $id);
  184. }
  185. }
  186. });
  187. //保存后回调
  188. $form->saved(function (Form $form, $result) {
  189. if ($form->isCreating()) {
  190. //创建后创建默认分类
  191. $newId = $form->getKey();
  192. $appearanceId = $form->input('appearance_id');
  193. //初始化分销商
  194. DistAdminDistributor::initDist($appearanceId,$newId);
  195. //更新模版与变量
  196. DistAppearance::switchTheme($appearanceId, $newId);
  197. }
  198. });
  199. });
  200. }
  201. /*
  202. * 新增事件事务
  203. */
  204. public function store()
  205. {
  206. try {
  207. DB::beginTransaction();
  208. $result = parent::store();
  209. DB::commit();
  210. return $result;
  211. } catch (\Exception $e) {
  212. DB::rollBack();
  213. throw $e;
  214. }
  215. }
  216. }