ThinkPHP开发大型B2C商城2

十六、文章模块界面处理

文章表

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>

UEditor编辑器下载

<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('删除链接失败!');
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352