作者 何书鹏
1 个管道 的构建 通过 耗费 3 秒

APP首页轮播图跳转

... ... @@ -3,6 +3,10 @@
namespace app\admin\controller\mobile;
use app\common\controller\Backend;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
/**
* 首页轮播图管理
... ... @@ -22,6 +26,7 @@ class IndexBanner extends Backend
{
parent::_initialize();
$this->model = new \app\admin\model\mobile\IndexBanner;
$this->view->assign("typeList", $this->model->getTypeList());
}
... ... @@ -63,7 +68,7 @@ class IndexBanner extends Backend
->select();
foreach ($list as $row) {
$row->visible(['id','image','url','updatetime']);
$row->visible(['id','image','weigh']);
}
$list = collection($list)->toArray();
... ... @@ -73,4 +78,133 @@ class IndexBanner extends Backend
}
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
switch ($params['type']){
case 1:
$params['course_id'] = 0;
$params['news_id'] = 0;
break;
case 2:
!$params['course_id'] && $this->error('请选择精品课程');
$params['news_id'] = 0;
break;
case 3:
!$params['news_id'] && $this->error('请选择报考咨询');
$params['course_id'] = 0;
break;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
if (!in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
switch ($params['type']){
case 1:
$params['course_id'] = 0;
$params['news_id'] = 0;
break;
case 2:
!$params['course_id'] && $this->error('请选择精品课程');
$params['news_id'] = 0;
break;
case 3:
!$params['news_id'] && $this->error('请选择报考咨询');
$params['course_id'] = 0;
break;
}
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
$row->validateFailException(true)->validate($validate);
}
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row);
return $this->view->fetch();
}
}
... ...
... ... @@ -17,6 +17,7 @@ class News extends Backend
* @var \app\admin\model\mobile\News
*/
protected $model = null;
protected $noNeedRight = ['selectpage'];
public function _initialize()
{
... ... @@ -73,4 +74,9 @@ class News extends Backend
}
return $this->view->fetch();
}
public function selectpage()
{
return parent::selectpage(); // TODO: Change the autogenerated stub
}
}
... ...
... ... @@ -3,7 +3,13 @@
return [
'Id' => 'ID',
'Image' => '图片地址',
'Url' => '链接地址',
'Type' => '跳转类型',
'Type 1' => '无跳转',
'Type 2' => '精品课程',
'Type 3' => '报考咨询',
'Course_id' => '精品课程',
'News_id' => '报考咨询',
'Weigh' => '权重',
'Createtime' => '创建时间',
'Updatetime' => '修改时间'
];
... ...
... ... @@ -25,16 +25,29 @@ class IndexBanner extends Model
// 追加属性
protected $append = [
'type_text',
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
public function getTypeList()
{
return [1 => __('Type 1'),2 => __('Type 2'),3 => __('Type 3')];
}
public function getTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
$list = $this->getTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
}
... ...
... ... @@ -16,9 +16,33 @@
<div class="col-sm-3" style="padding-top: 7px;">建议宽高:343*128</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Url')}:</label>
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-url" data-rule="required" class="form-control" name="row[url]" type="text" value="">
<div class="radio">
{foreach name="typeList" item="vo"}
<label for="row[type]-{$key}"><input id="row[type]-{$key}" name="row[type]" type="radio" value="{$key}" {in name="key" value="1"}checked{/in} /> {$vo}</label>
{/foreach}
</div>
</div>
</div>
<div class="form-group course_id hide">
<label class="control-label col-xs-12 col-sm-2">{:__('Course_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-course_id" data-source="mobile/course/course/selectpage" data-field="title" class="form-control selectpage" name="row[course_id]" type="text" value="">
</div>
</div>
<div class="form-group news_id hide">
<label class="control-label col-xs-12 col-sm-2">{:__('News_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-news_id" data-source="mobile/news/selectpage" data-field="title" class="form-control selectpage" name="row[news_id]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Weigh')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-weigh" data-rule="required" class="form-control" name="row[weigh]" type="number" value="0">
</div>
</div>
<div class="form-group layer-footer">
... ...
... ... @@ -16,9 +16,33 @@
<div class="col-sm-3" style="padding-top: 7px;">建议宽高:343*128</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Url')}:</label>
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-url" data-rule="required" class="form-control" name="row[url]" type="text" value="{$row.url|htmlentities}">
<div class="radio">
{foreach name="typeList" item="vo"}
<label for="row[type]-{$key}"><input id="row[type]-{$key}" name="row[type]" type="radio" value="{$key}" {in name="key" value="$row.type"}checked{/in} /> {$vo}</label>
{/foreach}
</div>
</div>
</div>
<div class="form-group course_id hide">
<label class="control-label col-xs-12 col-sm-2">{:__('Course_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-course_id" data-source="mobile/course/course/selectpage" data-field="title" class="form-control selectpage" name="row[course_id]" type="text" value="{$row.course_id|htmlentities}">
</div>
</div>
<div class="form-group news_id hide">
<label class="control-label col-xs-12 col-sm-2">{:__('News_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-news_id" data-source="mobile/news/selectpage" data-field="title" class="form-control selectpage" name="row[news_id]" type="text" value="{$row.news_id|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Weigh')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-weigh" data-rule="required" class="form-control" name="row[weigh]" type="number" value="{$row.weigh|htmlentities}">
</div>
</div>
<div class="form-group layer-footer">
... ...
... ... @@ -46,7 +46,9 @@ class Index extends Api
"banner_list": [{ //轮播图列表
"id": 1, //ID
"image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png", //图片地址
"link": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png" //链接地址
"type": 1, //跳转类型:1=无跳转,2=精品课程,3=报考咨询
"course_id": 1, //精品课程ID,当type为 2 时,跳转到指定课程
"news_id": 1, //报考咨询ID,当type为 3 时,跳转到指定报考咨询
}],
"exam": { //考试
"id": 3, //ID
... ... @@ -86,7 +88,7 @@ class Index extends Api
$exam['countdown'] = $countdown > 0 ? $countdown : 0;
}
// 轮播图
$banner_list = IndexBanner::order('createtime desc')->field('id,image,url')->select();
$banner_list = IndexBanner::order('createtime desc')->field('id,image,type,course_id,news_id')->select();
// 消息
$message = Message::where('user_id',$this->auth->id)->where('is_read','0')->field('id,title')->find();
$message = $message ?: (Object)[];
... ...
... ... @@ -20,14 +20,15 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
sortName: 'weigh',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
{field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
// {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
// {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'weigh', title: __('Weigh'), operate: false},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
... ... @@ -41,10 +42,28 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
},
edit: function () {
Controller.api.bindevent();
setTimeout(function () {
$('input[name="row[type]"]').trigger("change");
}, 100);
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
// 链接类型
$('input[name="row[type]"]').change(function () {
var type = $('input[name="row[type]"]:checked').val();
console.log(type);
if(type == 1) {
$('.course_id').addClass('hide');
$('.news_id').addClass('hide');
} else if(type == 2) {
$('.course_id').removeClass('hide');
$('.news_id').addClass('hide');
} else {
$('.course_id').addClass('hide');
$('.news_id').removeClass('hide');
}
})
}
}
};
... ...
... ... @@ -581,7 +581,9 @@
"banner_list": [{ //轮播图列表
"id": 1, //ID
"image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png", //图片地址
"link": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png" //链接地址
"type": 1, //跳转类型:1=无跳转,2=精品课程,3=报考咨询
"course_id": 1, //精品课程ID,当type为 2 时,跳转到指定课程
"news_id": 1, //报考咨询ID,当type为 3 时,跳转到指定报考咨询
}],
"exam": { //考试
"id": 3, //ID
... ... @@ -23636,7 +23638,7 @@
<div class="row mt0 footer">
<div class="col-md-6" align="left">
Generated on 2021-02-27 20:01:32 </div>
Generated on 2021-03-05 13:17:19 </div>
<div class="col-md-6" align="right">
<a href="./" target="_blank">企智帮</a>
</div>
... ...