十六、文章模块界面处理
文章表
CREATE TABLE `tp_article` (
`id` smallint(5) PRIMARY KEY AUTO_INCREMENT COMMENT '文章id',
`title` varchar(30) NOT NULL COMMENT '标题',
`keywords` varchar(60) NOT NULL COMMENT '关键词',
`description` varchar(150) NOT NULL COMMENT '描述',
`author` varchar(10) NOT NULL COMMENT '作者',
`email` varchar(20) NOT NULL COMMENT '电子邮箱',
`link_url` varchar(100) NOT NULL COMMENT '外链',
`thumb` varchar(100) NOT NULL COMMENT '缩略图',
`content` longtext NOT NULL COMMENT '内容',
`show_top` tinyint(1) NOT NULL DEFAULT '0' COMMENT '置顶 1:是 0:否',
`show_status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '显示 1:是 0:否',
`cate_id` smallint(6) NOT NULL COMMENT '所属栏目',
`addtime` int(10) NOT NULL COMMENT '发布时间'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
文章添加页面
<div class="page-body">
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-header bordered-bottom bordered-blue">
<span class="widget-caption">新增文章</span>
</div>
<div class="widget-body">
<div id="horizontal-form">
<form class="form-horizontal" role="form" action="" method="post"
enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right">所属栏目</label>
<div class="col-sm-6">
<select name="cate_id">
<option value="">选择栏目</option>
{volist name="cateRes" id="cate"}
<option {in name="$cate['id']" value="1,2" } disabled="disabled"
{/in} value="{$cate.id}">
<?php echo str_repeat('-', $cate['level']*8)?>{$cate.cate_name}
</option>
{/volist}
</select>
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">标题</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="title" required=""
type="text">
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">置顶</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="show_top" value="1" class="colored-blue"
type="radio">
<span class="text">是</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="show_top" value="0" checked="checked"
class="colored-blue" type="radio">
<span class="text">否</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">关键词</label>
<div class="col-sm-6">
<textarea class="form-control" name="keywords"></textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">描述</label>
<div class="col-sm-6">
<textarea class="form-control" name="description"></textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">作者</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="author"
type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">邮箱</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="email" type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">外链</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="link_url"
type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">缩略图</label>
<div class="col-sm-6">
<input placeholder="" name="thumb" type="file">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">文章内容</label>
<div class="col-sm-6">
<textarea id="content" name="content"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="{__PLUS__} /ueditor/ueditor.config.js"></script>
<script src="{__PLUS__} /ueditor/ueditor.all.min.js"></script>
<script src="{__PLUS__} /ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例,如果在某个闭包下引用该编辑器,直接调用UE.getEditor('editor')就能拿到相关的实例
UE.getEditor('content', { initialFrameWidth: 770, initialFrameHeight: 400,
</script>
模板输出字符串替换
'tpl_replace_string' => [ '{__PLUS__}' => '/static/admin/plus', ]
十七、文章添加及编辑功能
文章添加
public function add()
{
$cate=new Catetree();
if(request()->isPost()){
$data=input('post.');
$data['addtime']=time();
// $data['article_url']; http://
if($data['link_url'] && stripos($data['link_url'],'http://') === false){
$data['link_url']='http://'.$data['link_url'];
}
//处理图片上传
if($_FILES['thumb']['tmp_name']){
$file = request()->file('thumb');
// 上传到本地服务器
$data['thumb'] = \think\facade\Filesystem::putFile( 'topic', $file);
}
//验证
$validate = validate('article');
if(!$validate->check($data)){
$this->error($validate->getError());
}
$add=Db::table('tp_article')->insert($data);
if($add){
$this->success('添加文章成功!','lst');
}else{
$this->error('添加文章失败!');
}
return;
}
$cateRes = Db::table('tp_cate')->select();
$cateRes=$cate->catetree($cateRes);
View::assign([
'cateRes'=>$cateRes,
]);
return view();
}
文章列表页面
<div class="page-body">
<button type="button" tooltip="添加文章" class="btn btn-sm btn-azure btn-addon"
onClick="javascript:window.location.href = 'add.html'"> <i class="fa fa-plus"></i> Add
</button>
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-body">
<div class="flip-scroll">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th class="text-center" width="6%">ID</th>
<th>标题</th>
<th class="text-center" width="12%">缩略图</th>
<th class="text-center" width="12%">所属栏目</th>
<th class="text-center" width="6%">跳转</th>
<th class="text-center" width="6%">置顶</th>
<th class="text-center" width="6%">显示状态</th>
<th class="text-center" width="10%">发布时间</th>
<th class="text-center" width="10%">操作</th>
</tr>
</thead>
<tbody>
{volist name="artRes" id="art"}
<tr>
<td align="center">{$art.id}</td>
<td>{$art.title|cut_str=###,10}</td>
<td align="center">
{if condition="$art['thumb'] neq ''"}
<img height="30" src="{__UPLOAD_IMG__}/{$art.thumb}">
{else/}
暂无缩略图
{/if}
</td>
<td align="center">{$art.cate_name}</td>
<td align="center">{if condition="$art['link_url'] neq ''"} 是
{else/} 否 {/if}</td>
<td align="center">{if condition="$art['show_top'] eq 1"} 是 {else/}
否 {/if}</td>
<td align="center">{if condition="$art['show_status'] eq 1"} 是
{else/} 否 {/if}</td>
<td align="center">{$art.addtime|date="Y-m-d H:i:s"}</td>
<td align="center">
<a href="{:url('edit',array('id'=>$art['id']))}"
class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a href="{:url('del',array('id'=>$art['id']))}"
onClick="warning('确实要删除吗')"
class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
{/volist}
</tbody>
</table>
</div>
<div style="padding-top:10px; text-align:right;">
<!-- 分页 -->
{$artRes|raw}
</div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
文章列表
public function lst()
{
$artRes=Db::table('tp_article')->field('a.*,c.cate_name')->alias('a')->join('tp_cate c',"a.cate_id=c.id")->order('a.id DESC')->paginate(2);
View::assign([
'artRes'=>$artRes,
]);
return view('list');
}
文章编辑页面
<div class="page-body">
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-header bordered-bottom bordered-blue">
<span class="widget-caption">修改文章</span>
</div>
<div class="widget-body">
<div id="horizontal-form">
<form class="form-horizontal" role="form" action="" method="post"
enctype="multipart/form-data">
<input type="hidden" name="id" value="{$arts.id}">
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right">所属栏目</label>
<div class="col-sm-6">
<select name="cate_id">
<option value="">选择栏目</option>
{volist name="cateRes" id="cate"}
<option {if condition="$arts['cate_id'] eq $cate['id']" }
selected="selected" {/if} {in name="$cate['id']" value="1,2"
} disabled="disabled" {/in} value="{$cate.id}">
<?php echo str_repeat('-', $cate['level']*8)?>{$cate.cate_name}
</option>
{/volist}
</select>
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">标题</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="title"
value="{$arts.title}" required="" type="text">
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">置顶</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="show_top" value="1" {if
condition="$arts['show_top'] eq 1" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">是</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="show_top" value="0" {if
condition="$arts['show_top'] eq 0" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">否</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">关键词</label>
<div class="col-sm-6">
<textarea class="form-control"
name="keywords">{$arts.keywords}</textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">描述</label>
<div class="col-sm-6">
<textarea class="form-control"
name="description">{$arts.description}</textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">作者</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="author"
value="{$arts.author}" type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">邮箱</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="email"
value="{$arts.email}" type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">外链</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="link_url"
value="{$arts.link_url}" type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">缩略图</label>
<div class="col-sm-6">
<input placeholder="" name="thumb" type="file">
{if condition="$arts['thumb']"}
<img src="{__UPLOAD_IMG__}/{$arts.thumb}" height="100">
{else /}
暂无缩略图
{/if}
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">文章内容</label>
<div class="col-sm-6">
<textarea id="content" name="content">{$arts.content}</textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
文章编辑
public function edit()
{
if(request()->isPost()){
$data=input('post.');
// $data['brand_url']; http://
if($data['link_url'] && stripos($data['link_url'],'http://') === false){
$data['link_url']='http://'.$data['link_url'];
}
//处理图片上传
if($_FILES['thumb']['tmp_name']){
$oldarticles=Db::table('tp_article')->field('thumb')->find($data['id']);
$oldarticleImg=IMG_UPLOADS. $oldarticles['thumb'];
if(file_exists($oldarticleImg)){
@unlink($oldarticleImg);
}
$file = request()->file('thumb');
// 上传到本地服务器
$data['thumb'] = \think\facade\Filesystem::putFile( 'topic', $file);
}
//验证
$validate = validate('article');
if(!$validate->check($data)){
$this->error($validate->getError());
}
$save=Db::table('tp_article')->update($data);
if($save !== false){
$this->success('修改品牌成功!','lst');
}else{
$this->error('修改品牌失败!');
}
return;
}
$id=input('id');
$arts=DB::table('tp_article')->find($id);
$cate=new Catetree();
$cateRes=DB::table('tp_cate')->order('sort DESC')->select();
$cateRes=$cate->catetree($cateRes);
View::assign([
'arts'=>$arts,
'cateRes'=>$cateRes,
]);
return view();
}
十八、完成文章模块功能
删除文章
public function del($id)
{
$arts=Db::table('tp_article')->field('thumb')->find($id);
$thumbSrc=IMG_UPLOADS.$arts['thumb'];
if(file_exists($thumbSrc)){
@unlink($thumbSrc);
}
$del=Db::table('tp_article')->delete($id);
if( $del){
$this->success('删除品牌成功!','lst');
}else{
$this->error('删除品牌失败!');
}
return view();
}
删除分类前判断该分类下的文章和文章相关缩略图
$article=Db::table('tp_article');
foreach ($sonids as $k => $v) {
$artRes=$article->field('id,thumb')->where(array('cate_id'=>$v))->select();
foreach ($artRes as $k1 => $v1) {
$thumbSrc=IMG_UPLOADS.$v1['thumb'];
if(file_exists($thumbSrc)){
@unlink($thumbSrc);
}
$article->delete($v1['id']);
}
}
十九、管理ueditor上传的图片
上传保存路径,可以自定义保存路径和文件名格式
"imagePathFormat": "/ueditor/{yyyy}{mm}{dd}/{time}{rand:6}"
图片列表页面
<div class="page-body">
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-body">
<div class="flip-scroll">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th>图片</th>
<th class="text-center" width="10%">操作</th>
</tr>
</thead>
<tbody>
{volist name="imgRes" id="img"}
<tr>
<th><img height="40" src="{$img}"></th>
<td align="center">
<a href="#" onclick="return delimg(this);" id="{$img}"
class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
{/volist}
</tbody>
</table>
<script type="text/javascript">
function delimg(o) {
if (!confirm('确定删除本图片吗?')) {
return false;
}
var url = "{:url('delimg')}";
var imgsrc = $(o).attr('id');
$.ajax({
url: url,
type: "post",
dataType: "json",
data: { imgsrc: imgsrc },
success: function (data) {
if (data == 1) {
$(o).parent().parent().remove();
alert('删除图片成功!');
} else if (data == 2) {
alert('删除图片失败!');
} else {
alert('图片不存在!');
}
}
});
}
</script>
</div>
<div style="padding-top:10px; text-align:right;">
</div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
二十、图片列表展示
定义上传图片路径
define('UEDITOR', __DIR__ . '/ueditor');
define('HTTP_UEDITOR','/ueditor');
define('DEL_UEDITOR',__DIR__ . '');
图片资源处理函数(应用公共文件)
function my_scandir($dir=UEDITOR){
$files=array();
$dir_list=scandir($dir);
foreach ($dir_list as $file) {
if($file != '.' && $file != '..'){
if(is_dir($dir.'/'.$file)){
$files[$file]=my_scandir($dir.'/'.$file);
}else{
$files[]=$dir.'/'.$file;
}
}
}
return $files;
}
ueditor图片管理
public function imglist(){
$_files=my_scandir();
$files=array();
foreach ($_files as $k => $v) {
if(is_array($v)){
foreach ($v as $k1 => $v1) {
$v1=str_replace(UEDITOR, HTTP_UEDITOR, $v1);
$files[]=$v1;
}
}else{
$v=str_replace(UEDITOR, HTTP_UEDITOR, $v);
$files[]=$v;
}
}
View::assign([
'imgRes'=>$files,
]);
return view();
}
ajax异步删除图片
public function delimg(){
$imgsrc=input('imgsrc');
$imgsrc=DEL_UEDITOR.$imgsrc;
if(file_exists($imgsrc)){
if(@unlink($imgsrc)){
echo 1;
}else{
echo 2;
}
}else{
echo 3;
}
}
二十一、友情链接功能完成
CREATE TABLE `tp_link` (
`id` smallint(6) PRIMARY KEY AUTO_INCREMENT COMMENT '友情链接id',
`title` varchar(30) NOT NULL COMMENT '标题',
`link_url` varchar(60) NOT NULL COMMENT '链接地址',
`logo` varchar(100) NOT NULL COMMENT 'logo',
`description` varchar(255) NOT NULL COMMENT '链接描述',
`type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '链接类型 1:文字 2:图片',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '启用状态1:启用 0:禁用'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
添加页面
<div class="page-body">
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-header bordered-bottom bordered-blue">
<span class="widget-caption">新增链接</span>
</div>
<div class="widget-body">
<div id="horizontal-form">
<form class="form-horizontal" role="form" action="" method="post"
enctype="multipart/form-data">
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接标题</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="title" required=""
type="text">
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接网址</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="link_url"
type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接logo</label>
<div class="col-sm-6">
<input placeholder="" name="logo" type="file">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接描述</label>
<div class="col-sm-6">
<textarea class="form-control" name="description"></textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接状态</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="status" value="1" class="colored-blue"
checked="checked" type="radio">
<span class="text">显示</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="status" value="0" class="colored-blue"
type="radio">
<span class="text">隐藏</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接类型</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="type" value="1" class="colored-blue"
checked="checked" type="radio">
<span class="text">文字</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="type" value="2" class="colored-blue"
type="radio">
<span class="text">图片</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
列表页面
<div class="page-body">
<button type="button" tooltip="添加链接" class="btn btn-sm btn-azure btn-addon"
onClick="javascript:window.location.href = 'add.html'"> <i class="fa fa-plus"></i> Add
</button>
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-body">
<div class="flip-scroll">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th class="text-center" width="8%">ID</th>
<th class="text-center">链接名称</th>
<th>链接地址</th>
<th class="text-center">链接logo</th>
<th>链接描述</th>
<th class="text-center" width="8%">链接类型</th>
<th class="text-center" width="8%">链接状态</th>
<th class="text-center" width="14%">操作</th>
</tr>
</thead>
<tbody>
{volist name="linkRes" id="link"}
<tr>
<td align="center">{$link.id}</td>
<td align="center">{$link.title}</td>
<td><a href="{$link.link_url}" target="_blank">{$link.link_url}</a>
</td>
<td align="center">
{if condition="$link['logo'] neq ''"}
<img src="{__UPLOAD_IMG__}/{$link.logo}" height="30">
{else /}
暂无图片
{/if}
</td>
<td>{$link.description|cut_str=###,10}</td>
<td>{$link.description|cut_str=###,10}</td>
<td align="center">
{if condition="$link['status'] eq 1"}
<img src="{__ADMIN__}/images/right.png" height="30">
{else /}
<img src="{__ADMIN__}/images/wrong.png" height="25">
{/if}
</td>
<td align="center">
<a href="{:url('edit',array('id'=>$link['id']))}"
class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a href="{:url('del',array('id'=>$link['id']))}"
onClick="warning('确实要删除吗')"
class="btn btn-danger btn-sm shiny">
<i class="fa fa-trash-o"></i> 删除
</a>
</td>
</tr>
{/volist}
</tbody>
</table>
</div>
<div style="padding-top:10px; text-align:right;">
{$linkRes->render()}
</div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
编辑页面
<div class="page-body">
<div class="row">
<div class="col-lg-12 col-sm-12 col-xs-12">
<div class="widget">
<div class="widget-header bordered-bottom bordered-blue">
<span class="widget-caption">新增链接</span>
</div>
<div class="widget-body">
<div id="horizontal-form">
<form class="form-horizontal" role="form" action="" method="post"
enctype="multipart/form-data">
<input type="hidden" name="id" value="{$links.id}">
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接标题</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" value="{$links.title}"
name="title" required="" type="text">
</div>
<p class="help-block col-sm-4 red">* 必填</p>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接网址</label>
<div class="col-sm-6">
<input class="form-control" placeholder="" name="link_url"
value="{$links.link_url}" type="text">
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接logo</label>
<div class="col-sm-6">
<input placeholder="" name="logo" type="file">
{if condition="$links['logo']"}
<img src="__uploads__/{$links.logo}" height="30">
{else /}
暂无缩略图
{/if}
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接描述</label>
<div class="col-sm-6">
<textarea class="form-control"
name="description">{$links.description}</textarea>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接状态</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="status" value="1" {if
condition="$links['status'] eq 1" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">显示</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="status" value="0" {if
condition="$links['status'] eq 0" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">隐藏</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="username"
class="col-sm-2 control-label no-padding-right">链接类型</label>
<div class="col-sm-6">
<div class="radio" style="float:left; padding-right:10px;">
<label>
<input name="type" value="1" {if
condition="$links['type'] eq 1" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">文字</span>
</label>
</div>
<div class="radio" style="float:left;">
<label>
<input name="type" value="2" {if
condition="$links['type'] eq 2" } checked="checked"
{/if} class="colored-blue" type="radio">
<span class="text">图片</span>
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
业务逻辑处理
<?php
namespace app\admin\controller;
use think\facade\Db;
use think\facade\View;
use app\BaseController;
class Link extends BaseController
{
public function lst()
{
$linkRes=Db::table('tp_link')->order('id DESC')->paginate(6);
View::assign([
'linkRes'=>$linkRes,
]);
return view('list');
}
public function add()
{
if(request()->isPost()){
$data=input('post.');
// $data['link_url']; http://
if($data['link_url'] && stripos($data['link_url'],'http://') === false){
$data['link_url']='http://'.$data['link_url'];
}
//处理图片上传
if($_FILES['logo']['tmp_name']){
$file = request()->file('logo');
// 上传到本地服务器
$data['logo'] = \think\facade\Filesystem::putFile( 'topic', $file);
}
//验证
// $validate = validate('link');
// if(!$validate->check($data)){
// $this->error($validate->getError());
// }
$add=Db::table('tp_link')->insert($data);
if($add){
$this->success('添加链接成功!','lst');
}else{
$this->error('添加链接失败!');
}
return;
}
return view();
}
public function edit()
{
if(request()->isPost()){
$data=input('post.');
// $data['link_url']; http://
if($data['link_url'] && stripos($data['link_url'],'http://') === false){
$data['link_url']='http://'.$data['link_url'];
}
//处理图片上传
if($_FILES['logo']['tmp_name']){
$oldlinks=Db::table('tp_link')->field('logo')->find($data['id']);
$oldlinkImg=IMG_UPLOADS.$oldlinks['logo'];
if(file_exists($oldlinkImg)){
@unlink($oldlinkImg);
}
$file = request()->file('logo');
// 上传到本地服务器
$data['logo'] = \think\facade\Filesystem::putFile( 'topic', $file);
}
//验证
// $validate = validate('link');
// if(!$validate->check($data)){
// $this->error($validate->getError());
// }
$save=db('link')->update($data);
if($save !== false){
$this->success('修改链接成功!','lst');
}else{
$this->error('修改链接失败!');
}
return;
}
$id=input('id');
$links=Db::table('tp_link')->find($id);
View::assign([
'links'=>$links,
]);
return view();
}
public function del($id)
{
$linkObj=Db::table('tp_link');
$links=$linkObj->field('logo')->find($id);
if($links['logo']){
$linkImg=IMG_UPLOADS.$links['logo'];
if(file_exists($linkImg)){
@unlink($linkImg);
}
}
$del=$linkObj->delete($id);
if($del){
$this->success('删除链接成功!','lst');
}else{
$this->error('删除链接失败!');
}
}
}