海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
10 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\Product;
  4. use App\Common\ProductStatus;
  5. use App\Models\AgentProduct;
  6. use App\Models\AgentProductItem;
  7. use App\Models\Category;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Form\NestedForm;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Dcat\Admin\Http\Controllers\AdminController;
  14. use Dcat\Admin\Widgets\Card;
  15. use Dcat\Admin\Widgets\Table;
  16. use Illuminate\Support\Facades\DB;
  17. class ProductController extends AdminController
  18. {
  19. protected $title = '产品';
  20. /**
  21. * Make a grid builder.
  22. *
  23. * @return Grid
  24. */
  25. protected function grid()
  26. {
  27. return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
  28. $grid->model()->where('supplier_id', Admin::user()->id);
  29. $grid->column('id')->sortable();
  30. $grid->column('type')->using(admin_trans('product.options.publish_type'));
  31. $grid->column('category.name', '产品分类');
  32. $grid->column('title');
  33. $grid->column('picture')->image('', 60, 60);
  34. $grid->column('price');
  35. $grid->column('original_price');
  36. $grid->column('stock');
  37. $grid->column('sale');
  38. $grid->column('status')->using(ProductStatus::array());
  39. $grid->column('verify_mobile','核销员手机');
  40. $grid->column('created_at');
  41. $grid->column('updated_at');
  42. $grid->filter(function (Grid\Filter $filter) {
  43. $filter->panel();
  44. $filter->equal('id')->width(2);
  45. $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2);
  46. });
  47. });
  48. }
  49. /**
  50. * Make a show builder.
  51. *
  52. * @param mixed $id
  53. *
  54. * @return Show
  55. */
  56. protected function detail($id)
  57. {
  58. return Show::make($id, new Product(), function (Show $show) {
  59. $show->field('id');
  60. $show->field('supplier_id');
  61. $show->field('category_id');
  62. $show->field('title');
  63. $show->field('price');
  64. $show->field('original_price');
  65. $show->field('pictures')->image('', 80, 80);
  66. $show->field('stock');
  67. $show->field('sale');
  68. $show->field('status');
  69. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  70. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  71. $show->field('verify_mobile','核销员手机');
  72. $show->field('extends', '附加信息')
  73. ->unescape()
  74. ->as(function ($v) {
  75. $html = '';
  76. $arr = [
  77. 'project' => '服务项目',
  78. 'date' => '时间',
  79. 'tags' => '包含项目',
  80. 'open_time' => '开放时间',
  81. 'package' => '包含套餐',
  82. ];
  83. foreach ($v as $k => $item) {
  84. $html .= new Card(Table::make([$arr[$k] ?? '项目'], $item));
  85. }
  86. return $html;
  87. });
  88. $show->field('created_at');
  89. $show->field('updated_at');
  90. });
  91. }
  92. /**
  93. * Make a form builder.
  94. *
  95. * @return Form
  96. */
  97. protected function form()
  98. {
  99. Form\Field\Map::requireAssets(); //地图
  100. Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true);
  101. return Form::make(new Product(), function (Form $form) {
  102. //不允许编辑非自己数据
  103. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  104. return $form->response()->error('数据不存在');
  105. }
  106. $form->display('id');
  107. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  108. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  109. $form->text('title')->required();
  110. $form->text('price')->required();
  111. $form->text('original_price')->required();
  112. $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
  113. $form->text('stock')->default(99999)->required();
  114. $form->editor('know');
  115. $form->editor('content')->required();
  116. $form->text('verify_mobile','核销员手机');
  117. //扩展字段
  118. $publish_type = array_intersect_key(
  119. admin_trans('product.options.publish_type'),
  120. array_flip(Admin::user()->publish_type)
  121. );
  122. $form->radio('type', '产品类型')
  123. ->options($publish_type)
  124. ->default(current(Admin::user()->publish_type))
  125. ->when(0, function (Form $form) { //旅游线路
  126. $form->table('extends.field_0.project', '包含项目', function (NestedForm $table) {
  127. $table->text('name', '');
  128. $table->text('num', '');
  129. $table->text('price', '');
  130. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  131. $form->dateRange('extends.field_0.date.start', 'extends.field_0.date.end', '行程时间');
  132. })->when(1, function (Form $form) { //酒店
  133. $default = [
  134. ['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
  135. ['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
  136. ['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
  137. ['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
  138. ];
  139. $form->table('extends.field_1.tags', '酒店设施', function (NestedForm $table) {
  140. $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
  141. })->value($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
  142. $form->text('extends.field_1.name', '酒店名');
  143. $form->text('extends.field_1.address', '地址');
  144. $form->map('extends.field_1.latitude', 'extends.field_1.longitude', '位置');
  145. })->when(2, function (Form $form) { //景区
  146. $form->table('extends.field_2.open_time', '开放时间', function (NestedForm $table) {
  147. $table->text('node', '')->placeholder('如:周一至周五');
  148. $table->text('summer', '')->placeholder('如:08:00~19:00');
  149. $table->text('winter', '')->placeholder('如:08:00~18:00');
  150. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  151. $form->table('extends.field_2.project', '包含项目', function (NestedForm $table) {
  152. $table->text('name', '');
  153. $table->text('num', '');
  154. $table->text('price', '');
  155. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  156. $form->text('extends.field_2.name', '景区名');
  157. $form->text('extends.field_2.address', '地址');
  158. $form->map('extends.field_2.latitude', 'extends.field_2.longitude', '位置');
  159. })->when(3, function (Form $form) { //餐厅
  160. $form->table('extends.field_3.open_time', '开放时间', function (NestedForm $table) {
  161. $table->text('week', '')->placeholder('如:周一至周五');
  162. $table->text('section', '')->placeholder('如:上午/下午');
  163. $table->text('time', '')->placeholder('如:08:00~18:00');
  164. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  165. $form->table('extends.field_3.package', '包含套餐', function (NestedForm $table) {
  166. $table->text('name', '')->placeholder('如:清蒸鱿鱼');
  167. $table->text('num', '')->placeholder('如:1条');
  168. $table->text('price', '')->placeholder('如:99元');
  169. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  170. $form->text('extends.field_3.name', '餐厅名');
  171. $form->text('extends.field_3.address', '地址');
  172. $form->map('extends.field_3.latitude', 'extends.field_3.longitude', '位置');
  173. });
  174. if ($form->isEditing()) {
  175. $form->confirm('提示', '修改标题、价格、产品图片、旅游须知、产品详情需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
  176. }
  177. })->saving(function (Form $form) {
  178. //不允许编辑非自己数据
  179. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  180. return $form->response()->error('数据不存在');
  181. }
  182. if (!Admin::user()->publish_type || !in_array($form->type, Admin::user()->publish_type)) {
  183. return $form->response()->error('对不起,你没有此类产品的发布、编辑权限');
  184. }
  185. //不允许编辑的字段,忽略字段不起作用?
  186. $form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
  187. //null字段转为''
  188. foreach ($form->input() as $k => $v) {
  189. if (is_null($v)) {
  190. $form->$k = '';
  191. }
  192. }
  193. //特殊字段处理
  194. if ($form->isCreating()) {
  195. $form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
  196. $form->supplier_id = Admin::user()->id;
  197. $form->status = ProductStatus::UNAUDITED;
  198. }
  199. })->saved(function (Form $form, $result) {
  200. if ($form->isEditing() && $result) {
  201. $ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
  202. DB::beginTransaction();
  203. try {
  204. //如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核
  205. if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) {
  206. $form->model()->update(['status' => ProductStatus::UNAUDITED]);
  207. //下架所有代理商产品,未审核的产品,不能同步信息到代理商产品
  208. AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update(['status' => ProductStatus::SOLD_OUT]);
  209. } else {
  210. //不需要审核的修改信息同步信息到代理商产品,注:组合产品不同步
  211. AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
  212. 'title' => $form->title,
  213. 'pictures' => explode(',', $form->pictures),
  214. 'know' => $form->know,
  215. 'content' => $form->content,
  216. ]);
  217. }
  218. DB::commit();
  219. return $form->response()->success('更新成功!')->script('history.go(-1)');
  220. } catch (\Exception $exception) {
  221. DB::rollBack();
  222. return $form->response()->error($exception->getMessage());
  223. }
  224. }
  225. })->deleting(function (Form $form) {
  226. //不允许删除非自己的数据
  227. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  228. return $form->response()->error('数据不存在');
  229. }
  230. });
  231. }
  232. }