DistAdminDistributorController.php 7.3 KB

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