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
						
					
					
						
							11 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							257 lines
						
					
					
						
							11 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminSupplier\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminSupplier\Repositories\Product;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\AgentProductItem;
							 | 
						|
								use App\Models\Category;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Form\NestedForm;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Dcat\Admin\Widgets\Card;
							 | 
						|
								use Dcat\Admin\Widgets\Table;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								
							 | 
						|
								class ProductController extends AdminController
							 | 
						|
								{
							 | 
						|
									protected $title = '产品';
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Product(['category:id,name']), function (Grid $grid) {
							 | 
						|
											$grid->model()->where('supplier_id', Admin::user()->id);
							 | 
						|
								
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
											$grid->column('type')->using(admin_trans('product.options.publish_type'));
							 | 
						|
								            $grid->column('category.name', '产品分类');
							 | 
						|
								            $grid->column('title');
							 | 
						|
											$grid->column('picture')->image('', 60, 60);
							 | 
						|
								            $grid->column('price');
							 | 
						|
								            $grid->column('original_price');
							 | 
						|
								            $grid->column('stock');
							 | 
						|
								            $grid->column('sale');
							 | 
						|
								            $grid->column('status')->using(ProductStatus::array());
							 | 
						|
											$grid->column('verify_mobile','核销员手机');
							 | 
						|
								            $grid->column('created_at');
							 | 
						|
								            $grid->column('updated_at');
							 | 
						|
								
							 | 
						|
								            $grid->filter(function (Grid\Filter $filter) {
							 | 
						|
												$filter->panel();
							 | 
						|
								
							 | 
						|
								                $filter->equal('id')->width(2);
							 | 
						|
								                $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2);
							 | 
						|
								            });
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new Product(), function (Show $show) {
							 | 
						|
								            $show->field('id');
							 | 
						|
								            $show->field('supplier_id');
							 | 
						|
								            $show->field('category_id');
							 | 
						|
								            $show->field('title');
							 | 
						|
								            $show->field('price');
							 | 
						|
								            $show->field('original_price');
							 | 
						|
								            $show->field('pictures')->image('', 80, 80);
							 | 
						|
								            $show->field('stock');
							 | 
						|
								            $show->field('sale');
							 | 
						|
								            $show->field('status');
							 | 
						|
								            $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
								            $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
											$show->field('verify_mobile','核销员手机');
							 | 
						|
											$show->field('extends', '附加信息')
							 | 
						|
												->unescape()
							 | 
						|
												->as(function ($v) {
							 | 
						|
													$html = '';
							 | 
						|
													$arr = [
							 | 
						|
														'project' => '服务项目',
							 | 
						|
														'date' => '时间',
							 | 
						|
														'tags' => '包含项目',
							 | 
						|
														'open_time' => '开放时间',
							 | 
						|
														'package' => '包含套餐',
							 | 
						|
													];
							 | 
						|
													foreach ($v as $k => $item) {
							 | 
						|
														$html .= new Card(Table::make([$arr[$k] ?? '项目'], $item));
							 | 
						|
													}
							 | 
						|
													return $html;
							 | 
						|
												});
							 | 
						|
								            $show->field('created_at');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
										Form\Field\Map::requireAssets(); //地图
							 | 
						|
										Admin::user()->publish_type = json_decode(Admin::user()->publish_type, true);
							 | 
						|
								        return Form::make(new Product(), function (Form $form) {
							 | 
						|
											//不允许编辑非自己数据
							 | 
						|
											if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$form->display('id');
							 | 
						|
								
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
							 | 
						|
								            $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
							 | 
						|
								            $form->text('title')->required();
							 | 
						|
								            $form->text('price')->required();
							 | 
						|
								            $form->text('original_price')->required();
							 | 
						|
								            $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
							 | 
						|
								            $form->text('stock')->default(99999)->required();
							 | 
						|
								            $form->editor('know');
							 | 
						|
								            $form->editor('content')->required();
							 | 
						|
											$form->text('verify_mobile','核销员手机');
							 | 
						|
								
							 | 
						|
											//扩展字段
							 | 
						|
											$publish_type = array_intersect_key(
							 | 
						|
												admin_trans('product.options.publish_type'),
							 | 
						|
												array_flip(Admin::user()->publish_type)
							 | 
						|
											);
							 | 
						|
											$form->radio('type', '产品类型')
							 | 
						|
												->options($publish_type)
							 | 
						|
												->default(current(Admin::user()->publish_type))
							 | 
						|
												->when(0, function (Form $form) { //旅游线路
							 | 
						|
													$form->table('extends.field_0.project', '包含项目', function (NestedForm $table) {
							 | 
						|
														$table->text('name', '字段1');
							 | 
						|
														$table->text('num', '字段2');
							 | 
						|
														$table->text('price', '字段3');
							 | 
						|
													})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
							 | 
						|
								
							 | 
						|
													$form->dateRange('extends.field_0.date.start', 'extends.field_0.date.end', '行程时间');
							 | 
						|
												})->when(1, function (Form $form) { //酒店
							 | 
						|
													$default = [
							 | 
						|
														['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
							 | 
						|
														['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
							 | 
						|
														['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
							 | 
						|
														['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
							 | 
						|
													];
							 | 
						|
													$form->table('extends.field_1.tags', '酒店设施', function (NestedForm $table) {
							 | 
						|
														$table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
							 | 
						|
													})->value($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
							 | 
						|
								
							 | 
						|
													$form->text('extends.field_1.name', '酒店名');
							 | 
						|
													$form->text('extends.field_1.address', '地址');
							 | 
						|
													$form->map('extends.field_1.latitude', 'extends.field_1.longitude', '位置');
							 | 
						|
												})->when(2, function (Form $form) { //景区
							 | 
						|
													$form->table('extends.field_2.open_time', '开放时间', function (NestedForm $table) {
							 | 
						|
														$table->text('node', '字段1')->placeholder('如:周一至周五');
							 | 
						|
														$table->text('summer', '字段2')->placeholder('如:08:00~19:00');
							 | 
						|
														$table->text('winter', '字段3')->placeholder('如:08:00~18:00');
							 | 
						|
													})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
							 | 
						|
								
							 | 
						|
													$form->table('extends.field_2.project', '包含项目', function (NestedForm $table) {
							 | 
						|
														$table->text('name', '字段1');
							 | 
						|
														$table->text('num', '字段2');
							 | 
						|
														$table->text('price', '字段3');
							 | 
						|
													})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
							 | 
						|
								
							 | 
						|
													$form->text('extends.field_2.name', '景区名');
							 | 
						|
													$form->text('extends.field_2.address', '地址');
							 | 
						|
													$form->map('extends.field_2.latitude', 'extends.field_2.longitude', '位置');
							 | 
						|
												})->when(3, function (Form $form) { //餐厅
							 | 
						|
													$form->table('extends.field_3.open_time', '开放时间', function (NestedForm $table) {
							 | 
						|
														$table->text('week', '字段1')->placeholder('如:周一至周五');
							 | 
						|
														$table->text('section', '字段2')->placeholder('如:上午/下午');
							 | 
						|
														$table->text('time', '字段3')->placeholder('如:08:00~18:00');
							 | 
						|
													})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
							 | 
						|
								
							 | 
						|
													$form->table('extends.field_3.package', '包含套餐', function (NestedForm $table) {
							 | 
						|
														$table->text('name', '字段1')->placeholder('如:清蒸鱿鱼');
							 | 
						|
														$table->text('num', '字段2')->placeholder('如:1条');
							 | 
						|
														$table->text('price', '字段3')->placeholder('如:99元');
							 | 
						|
													})->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
							 | 
						|
								
							 | 
						|
													$form->text('extends.field_3.name', '餐厅名');
							 | 
						|
													$form->text('extends.field_3.address', '地址');
							 | 
						|
													$form->map('extends.field_3.latitude', 'extends.field_3.longitude', '位置');
							 | 
						|
												});
							 | 
						|
								
							 | 
						|
								            if ($form->isEditing()) {
							 | 
						|
												$form->confirm('提示', '修改标题、价格、产品图片、旅游须知、产品详情需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
							 | 
						|
											}
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
								        	//不允许编辑非自己数据
							 | 
						|
											if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (!Admin::user()->publish_type || !in_array($form->type, Admin::user()->publish_type)) {
							 | 
						|
												return $form->response()->error('对不起,你没有此类产品的发布、编辑权限');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//不允许编辑的字段,忽略字段不起作用?
							 | 
						|
											$form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
							 | 
						|
								
							 | 
						|
											//null字段转为''
							 | 
						|
											foreach ($form->input() as $k => $v) {
							 | 
						|
												if (is_null($v)) {
							 | 
						|
													$form->$k = '';
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//特殊字段处理
							 | 
						|
											if ($form->isCreating()) {
							 | 
						|
												$form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
							 | 
						|
												$form->supplier_id = Admin::user()->id;
							 | 
						|
												$form->status = ProductStatus::UNAUDITED;
							 | 
						|
											}
							 | 
						|
										})->saved(function (Form $form, $result) {
							 | 
						|
											if ($form->isEditing() && $result) {
							 | 
						|
												$ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
							 | 
						|
								
							 | 
						|
												DB::beginTransaction();
							 | 
						|
												try {
							 | 
						|
													//如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核
							 | 
						|
													if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content'])) {
							 | 
						|
														$form->model()->update(['status' => ProductStatus::UNAUDITED]);
							 | 
						|
								
							 | 
						|
														//下架所有代理商产品,未审核的产品,不能同步信息到代理商产品
							 | 
						|
														AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update(['status' => ProductStatus::SOLD_OUT]);
							 | 
						|
													} else {
							 | 
						|
														//不需要审核的修改信息同步信息到代理商产品,注:组合产品不同步
							 | 
						|
														AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
							 | 
						|
															'title' => $form->title,
							 | 
						|
															'pictures' => explode(',', $form->pictures),
							 | 
						|
															'know' => $form->know,
							 | 
						|
															'content' => $form->content,
							 | 
						|
														]);
							 | 
						|
													}
							 | 
						|
								
							 | 
						|
													DB::commit();
							 | 
						|
													return $form->response()->success('更新成功!')->script('history.go(-1)');
							 | 
						|
												} catch (\Exception $exception) {
							 | 
						|
													DB::rollBack();
							 | 
						|
													return $form->response()->error($exception->getMessage());
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
										})->deleting(function (Form $form) {
							 | 
						|
											//不允许删除非自己的数据
							 | 
						|
											if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
							 | 
						|
												return $form->response()->error('数据不存在');
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |