Browse Source

轮播图增加链接功能

dev
李可松 4 years ago
parent
commit
fbb1406774
  1. 6
      MySQL_change.sql
  2. 73
      app/AdminAgent/Controllers/SlideController.php
  3. 5
      app/Http/Controllers/Api/IndexController.php
  4. 5
      app/Models/Slide.php
  5. 4
      resources/lang/zh_CN/slide.php

6
MySQL_change.sql

@ -92,3 +92,9 @@ ALTER TABLE `orders`
ALTER TABLE `orders` ALTER TABLE `orders`
ADD COLUMN verify_code CHAR(13) NOT NULL DEFAULT '' COMMENT '核销码' AFTER `guide_id`; ADD COLUMN verify_code CHAR(13) NOT NULL DEFAULT '' COMMENT '核销码' AFTER `guide_id`;
# 10:35 ‎2021/‎08/‎22
ALTER TABLE `slides`
CHANGE COLUMN `url` `picture` VARCHAR(255) NOT NULL COMMENT '轮播图地址' COLLATE 'utf8_general_ci' AFTER `title`,
ADD COLUMN `type` TINYINT NOT NULL DEFAULT '0' COMMENT '链接类型,0:链接产品详情;1:链接到webview url' AFTER `sort`,
ADD COLUMN `url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '链接地址' AFTER `type`;

73
app/AdminAgent/Controllers/SlideController.php

@ -2,7 +2,11 @@
namespace App\AdminAgent\Controllers; namespace App\AdminAgent\Controllers;
use App\AdminAgent\Renderable\SelectAgentProduct;
use App\AdminAgent\Renderable\SelectProduct;
use App\AdminAgent\Repositories\Slide; use App\AdminAgent\Repositories\Slide;
use App\Models\AgentProduct;
use App\Models\Product;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
use Dcat\Admin\Form; use Dcat\Admin\Form;
use Dcat\Admin\Grid; use Dcat\Admin\Grid;
@ -18,12 +22,17 @@ class SlideController extends AdminController
*/ */
protected function grid() protected function grid()
{ {
return Grid::make(new Slide(), function (Grid $grid) {
return Grid::make(new Slide(['agentProduct.product:id,title,pictures']), function (Grid $grid) {
$grid->model()->where('agent_id', Admin::user()->id)->orderBy('sort'); $grid->model()->where('agent_id', Admin::user()->id)->orderBy('sort');
$grid->column('id')->sortable(); $grid->column('id')->sortable();
$grid->column('title'); $grid->column('title');
$grid->column('url')->image('', 60, 60);
$grid->column('picture')->image('', 60, 60);
$grid->column('type')->using(['链接产品', '链接网址']);
$grid->column('url')
->if(fn($column) => $this->type == 0)
->then(fn($column) => $column->display($this->agentProduct->product->title ?? ''))
->else(fn($column) => $column->display($this->url ?? ''));
$grid->column('status')->switch(); $grid->column('status')->switch();
$grid->column('sort')->editable()->sortable()->width(120); $grid->column('sort')->editable()->sortable()->width(120);
@ -43,10 +52,18 @@ class SlideController extends AdminController
*/ */
protected function detail($id) protected function detail($id)
{ {
return Show::make($id, new Slide(), function (Show $show) {
return Show::make($id, new Slide(['agentProduct.product:id,title,pictures']), function (Show $show) {
$show->field('id'); $show->field('id');
$show->field('title'); $show->field('title');
$show->field('url')->image('', 80, 80);
$show->field('picture')->image('', 80, 80);
$show->field('type')->using(['链接到产品详情', '链接到网址']);
$show->field('url')
->as(function ($v) {
if($this->type == 0) {
return $this->agentProduct->product->title;
}
return $v;
});
$show->field('status')->using(['禁用', '启用']); $show->field('status')->using(['禁用', '启用']);
$show->field('sort'); $show->field('sort');
$show->field('created_at'); $show->field('created_at');
@ -63,15 +80,53 @@ class SlideController extends AdminController
{ {
return Form::make(new Slide(), function (Form $form) { return Form::make(new Slide(), function (Form $form) {
$form->display('id'); $form->display('id');
$form->text('title');
$form->image('url')->required()->removable(false);
$form->switch('status')->required();
$form->text('sort')->required();
$form->text('title')
->help('主要用于后台显示,方便管理');
$form->image('picture')
->required()->removable(false)->uniqueName()
->help('图片大小:750*360');
$form->select('status')->options(['禁用', '启用'])->default(1)->required();
$form->radio('type', '链接类型')
->options(['链接到产品详情', '链接到网址'])
->value(0)->default(0)
->when(0, function (Form $form) {
$form->selectTable('url-0', '链接到产品')
->help('请选择要链接到的产品')
->title('选择在售产品')
->dialogWidth('80%;min-width:825px;')
->from(SelectAgentProduct::make(['id' => $form->url]))
->options(function ($v) {
if (!$v) return [];
$agent_product = AgentProduct::with('product:id,title')
->select(['id', 'product_id'])
->firstWhere(['id' => $v]);
return [$agent_product->id => $agent_product->product->title];
})
->pluck('product.title')
->value($form->model()->url);
})
->when(1, function (Form $form) {
$form->url('url-1', '链接到网址')
->placeholder('如:https://www.baidu.com/')
->customFormat(fn() => $this->type == 1 ? $this->url : '');
});
$form->text('sort')->default(255)->required();
})->saving(function (Form $form) { })->saving(function (Form $form) {
//不允许编辑的字段
$form->ignore(['id']);
foreach ($form->input() as $k => $v) {
if (is_null($v)) {
$form->$k = '';
}
}
//处理特殊字段 //处理特殊字段
$form->hidden(['agent_id']);
$form->hidden(['agent_id', 'url']);
$form->agent_id = Admin::user()->id; $form->agent_id = Admin::user()->id;
$form->status = $form->status ? 1 : 0; $form->status = $form->status ? 1 : 0;
$form->url = $form->{'url-' . $form->type};
$form->deleteInput(['url-0', 'url-1']);
}); });
} }
} }

5
app/Http/Controllers/Api/IndexController.php

@ -20,8 +20,9 @@ class IndexController extends Controller
public function index() public function index()
{ {
# 轮播图 # 轮播图
$slide = Slide::where('agent_id', $this->agent_id)->where('status', 1)
->orderBy('sort')->orderBy('id', 'DESC')->limit(10)->get(['title','url']);
$slide = Slide::where(['agent_id' => $this->agent_id, 'status' => 1])
->orderBy('sort')->orderBy('id', 'DESC')->limit(10)
->get(['title', 'picture', 'type', 'url']);
# 公告 # 公告
$notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']); $notice = Notice::where('agent_id', $this->agent_id)->limit(10)->get(['id', 'title', 'updated_at']);

5
app/Models/Slide.php

@ -12,4 +12,9 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
class Slide extends BaseModel class Slide extends BaseModel
{ {
use HasFactory; use HasFactory;
public function agentProduct()
{
return $this->belongsTo(AgentProduct::class, 'url', 'id');
}
} }

4
resources/lang/zh_CN/slide.php

@ -6,10 +6,12 @@ return [
], ],
'fields' => [ 'fields' => [
'title' => '图片说明', 'title' => '图片说明',
'url' => '图片地址',
'picture' => '图片地址',
'status' => '状态', 'status' => '状态',
'agent_id' => '代理商ID', 'agent_id' => '代理商ID',
'sort' => '排序', 'sort' => '排序',
'type' => '链接类型',
'url' => '链接到',
], ],
'options' => [ 'options' => [
], ],

Loading…
Cancel
Save