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

小游戏科普知识

<?php
namespace app\admin\controller\game;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;
/**
* 科普知识管理
*
* @icon fa fa-circle-o
*/
class Science extends Backend
{
/**
* Science模型对象
* @var \app\admin\model\game\Science
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\game\Science;
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['id','title','image','weigh','createtime','updatetime']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
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;
}
$result = false;
$params['admin_id'] = Session::get('admin.id');
$params['last_change_id'] = Session::get('admin.id');
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);
$result = false;
$params['last_change_id'] = Session::get('admin.id');
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();
}
}
... ...
<?php
return [
'Admin_id' => '管理员id',
'Last_change_id' => '最新一次修改管理员id',
'Title' => '文章标题',
'Author' => '作者',
'Image' => '图片',
'Description' => '描述',
'Content' => '内容',
'Video' => '腾讯视频地址',
'Weigh' => '权重',
'Createtime' => '创建时间',
'Updatetime' => '更新时间'
];
... ...
<?php
namespace app\admin\model\game;
use think\Model;
class Science extends Model
{
// 表名
protected $name = 'game_science';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
];
protected static function init()
{
self::afterInsert(function ($row) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
});
}
}
... ...
<?php
namespace app\admin\validate\game;
use think\Validate;
class Science extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
... ...
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Last_change_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-last_change_id" data-rule="required" data-source="last/change/index" class="form-control selectpage" name="row[last_change_id]" type="text" value="">
</div>
</div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-title" data-rule="required" class="form-control" name="row[title]" type="text" value="">
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Author')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-author" data-rule="required" class="form-control" name="row[author]" type="text" value="">
</div>
</div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Image')}:</label>
<div class="col-xs-12 col-sm-8">
<div class="input-group">
<input id="c-image" data-rule="required" class="form-control" size="50" name="row[image]" type="text" value="">
<div class="input-group-addon no-border no-padding">
<span><button type="button" id="plupload-image" class="btn btn-danger plupload" data-input-id="c-image" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-image"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
<span><button type="button" id="fachoose-image" class="btn btn-primary fachoose" data-input-id="c-image" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
</div>
<span class="msg-box n-right" for="c-image"></span>
</div>
<ul class="row list-inline plupload-preview" id="p-image"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Description')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-description" data-rule="required" class="form-control" name="row[description]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Content')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-content" data-rule="required" class="form-control editor" rows="5" name="row[content]" cols="50"></textarea>
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Video')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-video" class="form-control" name="row[video]" 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">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Last_change_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-last_change_id" data-rule="required" data-source="last/change/index" class="form-control selectpage" name="row[last_change_id]" type="text" value="{$row.last_change_id|htmlentities}">
</div>
</div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Title')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-title" data-rule="required" class="form-control" name="row[title]" type="text" value="{$row.title|htmlentities}">
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Author')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-author" data-rule="required" class="form-control" name="row[author]" type="text" value="{$row.author|htmlentities}">
</div>
</div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Image')}:</label>
<div class="col-xs-12 col-sm-8">
<div class="input-group">
<input id="c-image" data-rule="required" class="form-control" size="50" name="row[image]" type="text" value="{$row.image|htmlentities}">
<div class="input-group-addon no-border no-padding">
<span><button type="button" id="plupload-image" class="btn btn-danger plupload" data-input-id="c-image" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-image"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
<span><button type="button" id="fachoose-image" class="btn btn-primary fachoose" data-input-id="c-image" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
</div>
<span class="msg-box n-right" for="c-image"></span>
</div>
<ul class="row list-inline plupload-preview" id="p-image"></ul>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Description')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-description" data-rule="required" class="form-control" name="row[description]" type="text" value="{$row.description|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Content')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-content" data-rule="required" class="form-control editor" rows="5" name="row[content]" cols="50">{$row.content|htmlentities}</textarea>
</div>
</div>
<!--<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Video')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-video" class="form-control" name="row[video]" type="text" value="{$row.video|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">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<div class="panel panel-default panel-intro">
{:build_heading()}
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('game/science/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('game/science/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('game/science/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
<!--<a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('game/science/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>
<div class="dropdown btn-group {:$auth->check('game/science/multi')?'':'hide'}">
<a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>
<ul class="dropdown-menu text-left" role="menu">
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
</ul>
</div>-->
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="{:$auth->check('game/science/edit')}"
data-operate-del="{:$auth->check('game/science/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
... ... @@ -2,6 +2,7 @@
namespace app\api\controller;
use app\common\model\GameScience;
use app\common\model\Rubbish;
use app\common\controller\Api;
use app\common\model\User;
... ... @@ -22,7 +23,7 @@ use think\exception\PDOException;
*/
class Game extends Api
{
protected $noNeedLogin = ['get_session_key','authority','getPhoneNumber'];
protected $noNeedLogin = ['get_session_key','authority','getPhoneNumber','scienceList'];
protected $noNeedRight = ['*'];
/**
... ... @@ -819,4 +820,43 @@ class Game extends Api
}
$this->success('授权成功',$data);
}
/**
* @ApiWeigh (73)
* @ApiTitle (科普知识)
* @ApiSummary (科普知识)
* @ApiMethod (POST)
* @ApiParams (name="page", type="integer", required=true, description="页数")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
"list"文章列表: [{
"id"文章id: 1,
"title"标题: "test",,
"image"图片地址: "http://ljfl.w.brotop.cn/assets/img/qrcode.png"
"description"描述: "123",
"updatetime"日期: "2019-11-18"
}],
"total"数据总数: 1,
"this_page"当前页数: 1,
"total_page"总页数: 1
}
})
*/
public function scienceList()
{
$article_model = new GameScience();
$num = config('miniprogram.page_num');
$page = empty($param['page']) ? 1 : $param['page'];
// 获取文章列表
$article = $article_model->getList([],$num,['page'=>$page]);
$return = [
'list' => $article->items(),
'total' => $article->total(),
'this_page' => $article->currentPage(),
'total_page' => $article->lastPage()
];
$this->success('请求成功',$return);
}
}
... ...
<?php
namespace app\common\model;
use think\Model;
class GameScience extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
public function getImageAttr($data) {
return cdnurl($data,true);
}
public function getUpdatetimeAttr($data) {
return date('Y-m-d',$data);
}
/**
* @param $where 查询条件
*/
public function getWeighList($where) {
return $this->field('id,title,image,description,updatetime')
->where($where)
->order(['weigh'=>'ASC','updatetime'=>'DESC'])
->limit(0,3)
->select();
}
/**
* @param $where 查询条件
*/
public function getList($where,$num,$page) {
return $this->field('id,title,image,description,updatetime')
->where($where)
->order(['weigh'=>'ASC','updatetime'=>'DESC'])
->paginate($num,false,$page);
}
/**
* @param $id 文章id
*/
public function getInfo($id) {
return $this->field('id,title,author,description,content,video,updatetime')
->where('id',$id)
->find();
}
}
\ No newline at end of file
... ...
此 diff 太大无法显示。
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'game/science/index' + location.search,
add_url: 'game/science/add',
edit_url: 'game/science/edit',
del_url: 'game/science/del',
multi_url: 'game/science/multi',
table: 'game_science',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'updatetime',
showExport: false,
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'title', title: __('Title')},
{field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
{field: 'weigh', title: __('Weigh')},
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file
... ...