17 changed files with 84 additions and 402 deletions
-
23app/Admin/Extensions/Grid/AuditAgent.php
-
12app/AdminAgent/Controllers/AgentProductController.php
-
127app/AdminAgent/Controllers/ChannelController.php
-
16app/AdminAgent/Repositories/Channel.php
-
1app/AdminAgent/routes.php
-
22app/AdminGuide/Controllers/MyDemandProductController.php
-
25app/Http/Controllers/Api/CategoryController.php
-
66app/Http/Controllers/Api/ChannelController.php
-
22app/Http/Controllers/Api/IndexController.php
-
36app/Http/Controllers/Api/UserCategoryController.php
-
35app/Http/Controllers/Api/UserChannelController.php
-
5app/Models/AgentProduct.php
-
34app/Models/Channel.php
-
13app/Models/UserCategory.php
-
26app/Models/UserChannel.php
-
16resources/lang/zh_CN/channel.php
-
7routes/api.php
@ -1,127 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\AdminAgent\Controllers; |
|
||||
|
|
||||
use App\AdminAgent\Repositories\Channel; |
|
||||
use App\Models\Channel as ChannelModel; |
|
||||
use Dcat\Admin\Admin; |
|
||||
use Dcat\Admin\Form; |
|
||||
use Dcat\Admin\Grid; |
|
||||
use Dcat\Admin\Layout\Content; |
|
||||
use Dcat\Admin\Layout\Row; |
|
||||
use Dcat\Admin\Show; |
|
||||
use Dcat\Admin\Http\Controllers\AdminController; |
|
||||
use Dcat\Admin\Tree; |
|
||||
use Illuminate\Support\Facades\Storage; |
|
||||
|
|
||||
class ChannelController extends AdminController |
|
||||
{ |
|
||||
public function index(Content $content) |
|
||||
{ |
|
||||
return $content->header('产品频道') |
|
||||
->body(function (Row $row) { |
|
||||
$tree = new Tree(new ChannelModel()); |
|
||||
$tree->query(function ($model) { |
|
||||
//agent_id为0是系统分类,其它是代理商分类
|
|
||||
return $model->where('agent_id', Admin::user()->id)->orderBy('sort')->orderBy('id'); |
|
||||
}); |
|
||||
|
|
||||
/*$prefix = Storage::disk('public')->url(''); |
|
||||
$tree->branch(function ($branch) use ($prefix) { |
|
||||
$src = $prefix . $branch['icon'] ; |
|
||||
$logo = '<img data-action="preview-img" src="'.$src.'" style="max-width:30px;max-height:30px;cursor:pointer;margin-left:20px;" class="img">'; |
|
||||
|
|
||||
return "{$branch['id']} - {$branch['name']} $logo"; |
|
||||
});*/ |
|
||||
$row->column(12, $tree); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Make a grid builder. |
|
||||
* |
|
||||
* @return Grid |
|
||||
*/ |
|
||||
protected function grid() |
|
||||
{ |
|
||||
return Grid::make(new Channel(['parent']), function (Grid $grid) { |
|
||||
$grid->model()->where('agent_id', Admin::user()->id) |
|
||||
->orderBy('sort')->orderBy('id', 'desc'); |
|
||||
|
|
||||
$grid->column('id')->sortable(); |
|
||||
$grid->column('icon')->image('', 60, 60); |
|
||||
$grid->column('name'); |
|
||||
$grid->column('parent.name'); |
|
||||
$grid->column('sort')->editable()->width(120); |
|
||||
|
|
||||
$grid->filter(function (Grid\Filter $filter) { |
|
||||
$filter->panel(); |
|
||||
|
|
||||
$filter->equal('id')->width(2); |
|
||||
$filter->like('name')->width(3); |
|
||||
}); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Make a show builder. |
|
||||
* |
|
||||
* @param mixed $id |
|
||||
* |
|
||||
* @return Show |
|
||||
*/ |
|
||||
protected function detail($id) |
|
||||
{ |
|
||||
return Show::make($id, new Channel(['parent']), function (Show $show) { |
|
||||
//不允许查看非自己的数据
|
|
||||
if ($show->model()->agent_id != Admin::user()->id) { |
|
||||
Admin::exit('数据不存在'); |
|
||||
} |
|
||||
|
|
||||
$show->field('id'); |
|
||||
$show->field('icon')->image('', 80, 80); |
|
||||
$show->field('name'); |
|
||||
$show->field('parent.name'); |
|
||||
$show->field('sort'); |
|
||||
}); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Make a form builder. |
|
||||
* |
|
||||
* @return Form |
|
||||
*/ |
|
||||
protected function form() |
|
||||
{ |
|
||||
return Form::make(new Channel(), function (Form $form) { |
|
||||
//不允许修改非自己的数据
|
|
||||
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { |
|
||||
return $form->response()->error('数据不存在'); |
|
||||
} |
|
||||
|
|
||||
$form->display('id'); |
|
||||
$form->image('icon')->uniqueName()->removable(false); |
|
||||
$form->text('name')->required(); |
|
||||
$form->select('pid')->options(\App\Models\Channel::selectOptions())->default(0)->required(); |
|
||||
$form->text('sort')->default(255); |
|
||||
})->saving(function (Form $form) { |
|
||||
//不允许修改非自己的数据
|
|
||||
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { |
|
||||
return $form->response()->error('数据不存在'); |
|
||||
} |
|
||||
|
|
||||
//处理特殊字段
|
|
||||
$form->hidden(['agent_id']); |
|
||||
$form->agent_id = Admin::user()->id; |
|
||||
$form->sort = $form->sort ?? 255; |
|
||||
|
|
||||
//不允许编辑的字段
|
|
||||
$form->ignore(['id', 'deleted_at']); |
|
||||
})->deleting(function (Form $form) { |
|
||||
//不允许删除非自己的数据
|
|
||||
if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) { |
|
||||
return $form->response()->error('数据不存在'); |
|
||||
} |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
@ -1,16 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\AdminAgent\Repositories; |
|
||||
|
|
||||
use App\Models\Channel as Model; |
|
||||
use Dcat\Admin\Repositories\EloquentRepository; |
|
||||
|
|
||||
class Channel extends EloquentRepository |
|
||||
{ |
|
||||
/** |
|
||||
* Model. |
|
||||
* |
|
||||
* @var string |
|
||||
*/ |
|
||||
protected $eloquentClass = Model::class; |
|
||||
} |
|
||||
@ -1,66 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\Http\Controllers\Api; |
|
||||
use App\Http\Controllers\Controller; |
|
||||
use App\Models\AgentProduct; |
|
||||
use App\Models\Channel; |
|
||||
use App\Models\UserChannel; |
|
||||
use Illuminate\Support\Facades\Storage; |
|
||||
|
|
||||
/** |
|
||||
* 频道列表 |
|
||||
* Class Channel |
|
||||
* @package App\Http\Controllers\Api |
|
||||
*/ |
|
||||
class ChannelController extends Controller |
|
||||
{ |
|
||||
// 所有频道列表
|
|
||||
public function index() |
|
||||
{ |
|
||||
$list = Channel::query() |
|
||||
->where('agent_id', $this->agent_id) |
|
||||
->orderBy('sort') |
|
||||
->orderBy('id') |
|
||||
->get(['id', 'pid', 'name', 'icon']); |
|
||||
$prefix = Storage::disk('public')->url(''); |
|
||||
foreach ($list as $k => &$v) { |
|
||||
$v->icon = $prefix . $v->icon; |
|
||||
} |
|
||||
return $this->success($list); |
|
||||
} |
|
||||
|
|
||||
// 我的频道
|
|
||||
public function my() |
|
||||
{ |
|
||||
$channel_ids = UserChannel::where('user_id', $this->user_id)->value('channels'); |
|
||||
$list = Channel::whereIn('id', $channel_ids)->get(['id', 'pid', 'name', 'icon']); |
|
||||
return $this->success($list); |
|
||||
} |
|
||||
|
|
||||
//根据频道ID获取产品
|
|
||||
public function product() |
|
||||
{ |
|
||||
$channel_id = (int)request()->input('channel_id'); |
|
||||
|
|
||||
$list = AgentProduct::list($this->agent_id) |
|
||||
->whereRaw("FIND_IN_SET($channel_id, `channel_id`)") |
|
||||
->orderBy('id', 'DESC') |
|
||||
->simplePaginate(); |
|
||||
$list = $this->paginatePicAddHost($list); |
|
||||
return $this->success($list); |
|
||||
} |
|
||||
|
|
||||
//分页列表产品图片加域名
|
|
||||
private function paginatePicAddHost($list) |
|
||||
{ |
|
||||
if (!$list->isEmpty()) { |
|
||||
$prefix = Storage::disk('public')->url(''); |
|
||||
foreach ($list->items() as $k=>&$v) { |
|
||||
$v->pictures = array_map(function($item) use ($prefix) { |
|
||||
return strpos($item, $prefix) === false ? $prefix . $item : $item; |
|
||||
}, $v->pictures); |
|
||||
} |
|
||||
} |
|
||||
return $list; |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,36 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers\Api; |
||||
|
|
||||
|
use App\Http\Controllers\Controller; |
||||
|
use App\Models\UserCategory; |
||||
|
use Illuminate\Http\JsonResponse; |
||||
|
use Illuminate\Http\Request; |
||||
|
|
||||
|
class UserCategoryController extends Controller |
||||
|
{ |
||||
|
# 我的频道列表
|
||||
|
public function index(): JsonResponse |
||||
|
{ |
||||
|
return $this->success(UserCategory::where('user_id', $this->user_id)->value('categories') ?? []); |
||||
|
} |
||||
|
|
||||
|
# 我的频道编辑
|
||||
|
public function update(Request $request): JsonResponse |
||||
|
{ |
||||
|
$formData = $request->only(['channels']); |
||||
|
$request->validate([ |
||||
|
'channels' => 'nullable|array', |
||||
|
], [ |
||||
|
'channels.array' => 'channels必须是数组', |
||||
|
]); |
||||
|
|
||||
|
$cats = array_unique(array_filter($formData['channels'] ?? [])); |
||||
|
UserCategory::updateOrCreate(['user_id' => $this->user_id], [ |
||||
|
'user_id' => $this->user_id, |
||||
|
'categories' => $cats, |
||||
|
]); |
||||
|
|
||||
|
return $this->success(); |
||||
|
} |
||||
|
} |
||||
@ -1,35 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\Http\Controllers\Api; |
|
||||
use App\Http\Controllers\Controller; |
|
||||
use App\Models\UserChannel; |
|
||||
use Illuminate\Http\Request; |
|
||||
|
|
||||
class UserChannelController extends Controller |
|
||||
{ |
|
||||
//我的频道列表
|
|
||||
public function index() |
|
||||
{ |
|
||||
$channels = UserChannel::where('user_id', $this->user_id)->value('channels'); |
|
||||
return $this->success($channels); |
|
||||
} |
|
||||
|
|
||||
// 我的频道编辑
|
|
||||
public function update(Request $request) |
|
||||
{ |
|
||||
$formData = $request->only(['channels']); |
|
||||
$request->validate([ |
|
||||
'channels' => 'nullable|array', |
|
||||
], [ |
|
||||
'channels.array' => 'channels必须是数组', |
|
||||
]); |
|
||||
|
|
||||
$channels = array_unique(array_filter($formData['channels'])); |
|
||||
|
|
||||
$UserChannel = UserChannel::where('user_id', $this->user_id)->first(); |
|
||||
$UserChannel->channels = $channels; |
|
||||
$UserChannel->save(); |
|
||||
|
|
||||
return $this->success(); |
|
||||
} |
|
||||
} |
|
||||
@ -1,34 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\Models; |
|
||||
|
|
||||
use Dcat\Admin\Traits\ModelTree; |
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
||||
use Illuminate\Database\Eloquent\SoftDeletes; |
|
||||
|
|
||||
/** |
|
||||
* 频道列表 |
|
||||
* Class Channel |
|
||||
* @package App\Models |
|
||||
*/ |
|
||||
class Channel extends BaseModel |
|
||||
{ |
|
||||
use HasFactory, SoftDeletes; |
|
||||
use ModelTree; |
|
||||
|
|
||||
// 模型树需要的三个属性
|
|
||||
protected $parentColumn = 'pid'; |
|
||||
protected $orderColumn = 'sort'; |
|
||||
protected $titleColumn = 'name'; |
|
||||
|
|
||||
public function __construct(array $attributes = []) |
|
||||
{ |
|
||||
parent::__construct($attributes); |
|
||||
$this->timestamps = false; |
|
||||
} |
|
||||
|
|
||||
public function parent() |
|
||||
{ |
|
||||
return $this->hasOne(self::class, 'id', 'pid'); |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,13 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class UserCategory extends Model |
||||
|
{ |
||||
|
protected $primaryKey = 'user_id'; |
||||
|
public $timestamps = false; |
||||
|
protected $guarded = []; |
||||
|
protected $casts = ['categories' => 'json']; |
||||
|
} |
||||
@ -1,26 +0,0 @@ |
|||||
<?php |
|
||||
|
|
||||
namespace App\Models; |
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory; |
|
||||
|
|
||||
/** |
|
||||
* 我的频道 |
|
||||
* Class UserChannel |
|
||||
* @package App\Models |
|
||||
*/ |
|
||||
class UserChannel extends BaseModel |
|
||||
{ |
|
||||
use HasFactory; |
|
||||
|
|
||||
public function __construct(array $attributes = []) |
|
||||
{ |
|
||||
parent::__construct($attributes); |
|
||||
$this->timestamps = false; |
|
||||
} |
|
||||
|
|
||||
// 获取时需要转换,写入时框架可自动将数组转换后保存
|
|
||||
public function getChannelsAttribute($value) |
|
||||
{ |
|
||||
return json_decode($value, true); |
|
||||
} |
|
||||
} |
|
||||
@ -1,16 +0,0 @@ |
|||||
<?php |
|
||||
return [ |
|
||||
'labels' => [ |
|
||||
'Channel' => '频道', |
|
||||
'channel' => '频道', |
|
||||
], |
|
||||
'fields' => [ |
|
||||
'agent_id' => '代理商ID', |
|
||||
'icon' => '频道图标', |
|
||||
'name' => '频道名称', |
|
||||
'pid' => '父级ID', |
|
||||
'sort' => '排序', |
|
||||
], |
|
||||
'options' => [ |
|
||||
], |
|
||||
]; |
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue