企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
控制器 Admin/Controller/ImageController.class.php ~~~ <?php namespace Admin\Controller; use Admin\Controller\AuthController; use Think\Auth; class ImageController extends AuthController { public function _initialize() { parent::_initialize(); $this->db = D('Image'); $this->cate = D('ImageCate'); $this->uploadUrl = C('UPLOAD_IMG_URL'); $this->image = C('ADMINIMGURL'); } public function index(){ $count = $this->db->where($map)->count(); $page = new \Think\Page($count , C('DEFAULT_PAGE_LIMIT')); $list = $this->db->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select(); $this->list = $list; $this->page = $page->show(); $this->display(); } /** * 编辑文章 * @return [type] [description] */ public function update() { if (IS_POST) { $data = I('post.'); $res = $this->db->update($data); if ($res) { $this->ajaxReturn(['code'=>1,'msg'=>'更新成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>$this->db->getError()]); } }else{ $id = I('id'); if ($id) { $this->info = $this->db->where(array('id'=>$id))->find(); } $this->cate = $this->cate->where($map)->order('id desc')->select(); $this->display(); } } public function del(){ $id = I('id'); $res = $this->db->delete($id); if ($res) { $this->ajaxReturn(['code'=>1,'msg'=>'删除成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>'删除失败']); } } } ~~~ 模型 --Admin/Model/ImageModel.class.php ~~~ <?php namespace Admin\Model; use Think\Model; class ImageModel extends Model { /* 数据库设置 */ protected $connection = 'DB_CONFIG2'; /** * 自动验证 * @var array */ protected $_validate = array( array('title', 'require', 'title 不能为空'), array('upload_img', 'require', 'upload_img 不能为空'), ); /** * 自动完成 * @var array */ protected $_auto = array( array('create_time', NOW_TIME, 1), array('update_time', NOW_TIME, 3), array('status',1), ); /** * 更新数据 * @DateTime 2018-05-04 * @return [type] [description] */ public function update($info){ $data = $this->create($info); // dump($this->getError());die; if(!$data){ //数据对象创建错误 return false; } /* 添加或更新数据 */ if(empty($data['id'])){ return $this->add(); }else{ return $this->save(); } } } ~~~ 数据表 ~~~ CREATE TABLE `lxl_image` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(32) NOT NULL, `cid` int(11) NOT NULL, `images` varchar(255) NOT NULL COMMENT '图片路径', `create_time` int(11) NOT NULL, `update_time` int(11) NOT NULL, `status` tinyint(2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ~~~ 页面 Image/index.html ~~~ <include file="Public:header" /> <include file="Public:top" /> <!-- Contents --> <div id="Contents"> <!-- TopMain --> <div id="TopMain"> <!-- btn_box --> <div class="btn_box floatL"> <a href="{:U('Image/update')}" > <input type="button" value="添加图片"> </a> </div> <!-- /btn_box --> </div> <!-- /TopMain --> <!-- MainForm --> <div id="MainForm"> <div class="form_boxA"> <h2>图片列表</h2> <table cellpadding="0" cellspacing="0"> <tr> <th>ID</th> <th>标题</th> <th>分类</th> <th>更新时间</th> <th>状态</th> <th>操作</th> </tr> <volist name="list" id="vo"> <tr> <td>{$vo.id}</td> <td>{$vo.title}</td> <td>{$vo.cid}</td> <td>{$vo.update_time|date="Y-m-d H:i:s",###}</td> <td>{$vo.status}</td> <td> <a name="edit" href="{:U('Image/update',array('id'=>$vo[id]))}">编辑</a> <a class="del" link="{:U('Image/del',array('id'=>$vo['id']))}">删除</a> </td> </tr> </volist> </table> </div> </div> <!-- /MainForm --> <!-- PageNum --> {$page} <!-- /PageNum --> </div> <!-- /Contents --> <include file="Public:footer" /> ~~~ 添加图片- Image/update.html ~~~ <include file="Public:header" /> <include file="Public:top" /> <!-- Contents --> <div id="Contents"> <style type="text/css"> .layui-upload-file{ position: absolute; top: 0; left: 0; right: 0; bottom: 0; opacity: 0; width: 100%; height: 100% } </style> <!-- TopMain --> <div id="TopMain"> <!-- btn_box --> <div class="btn_box floatL"> <a href="{:U('Image/index')}" style="text-decoration: none;"> <input type="button" value="返回" > </a> </div> <!-- /btn_box --> </div> <!-- /TopMain --> <!-- MainForm --> <div id="MainForm" style="padding: 50px;"> <div class="form_boxC"> <form id="form" method="post"> <table cellpadding="0" cellspacing="0"> <tr> <th>标题</th> <td><div class="txtbox floatL"> <input type="text" name="title" id="title" size="100" value="{$info.title}" /> </div></td> </tr> <tr> <th>分类</th> <td> <div class="selectbox floatL mag_r20"> <select name="cid" id="cid"> <option value="0" <if condition="$info['cid'] eq 0">selected="selected"</if>>请选择分类</option> <volist name="cate" id="vo"> <option value="{$vo.id}" <if condition="$info['cid'] eq $vo['id']">selected="selected"</if>>{$vo.title}</option> </volist> </select> </div> </td> </tr> <tr> <th>图片</th> <td><div class="txtbox floatL"> <if condition="$info['images'] neq '' "> <img src="{$image}{$info.images}" id="upload_image" class="file" width="80"/> <else /> <img src="__PUBLIC__/Admin/images/common/add_image.jpg" class="file" id="upload_image" width="80"/> </if> <input type="hidden" name="images" id="image" value="{$info.images}" /> </div><span class="f_cB">(图片不能为空)</span></td> </tr> <tr> <th>&nbsp;</th> <td> <div class="btn_box" style="box-shadow: none;"> <input type="hidden" name="id" value="{$info.id}" /> <input type="submit" style="min-width:160px;" value="修改"> </div> </td> </tr> </table> </form> </div> </div> <!-- /MainForm --> </div> <!-- /Contents --> <script> $(function(){ var img_domain = "{$image}"; // 上传的图片服务器 var host = "{url:'{$uploadUrl}'}"; // 图片服务器地址 $('.file').attr('lay-data',host); //普通图片上传 layui.use('upload', function(){ var upload = layui.upload;//执行实例 var uploadInst = upload.render({ elem: '.file', done: function(res, index, upload){ if(res.code > 0){ // 上传成功 // 写入路径 $("#image").val(res.path); var previewPhoto = this.item.parents(".preview-photo"); $('#upload_image').attr("src",img_domain+res.path); } } }); }); $("#form").submit(function(e){ var res = $(this).serialize(); var url = "{:U('Image/update')}"; $.ajax({ url: url, data: res, type: 'post', success:function(data){ if (data.code == 1) { layer.alert(data.msg,{icon:6},function (index) { layer.close(index); window.location.href = "{:U('Image/index')}"; }); } else{ layer.alert(data.msg,{icon:5},function (index) { layer.close(index); window.location.reload(); }); } } }); return false; // 阻止表单跳转 }); }); </script> <include file="Public:footer" /> ~~~ 远程服务器上传图片代码控制器 ~~~ <?php /** * 上传方法 */ namespace Api\Controller; use Think\Controller; class UploadController extends Controller { public function uploadFile() { header("Access-Control-Allow-Origin: *"); if (IS_POST) { //生成图片前先判断 $path = './Uploads/uploadFile/'; // 检查目录是否存在 如果不存在目录 则创建目录 if(!is_dir($path)){ mkdir ( $path, 750, true ); } $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = $path.'/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 $upload->saveName = array('uniqid',''); // 设置附件上传(子)目录 // 上传文件 $info = $upload->upload(); if(!$info) {// 上传错误提示错误信息 $this->ajaxReturn(['code'=>404,'msg'=>$upload->getError()]); }else{// 上传成功 获取上传文件信息 foreach($info as $file){ $path = '/Uploads/uploadFile/'.$file['savepath'].$file['savename']; } $this->ajaxReturn(['code'=>200,'msg'=>'上传图片成功','path'=>$path]); } } } } ~~~