فهرست منبع

文章及公共附件上传

sugangqiang 2 سال پیش
والد
کامیت
73c43c0e94

+ 72 - 0
app/admin/controller/CommonFile.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace app\admin\controller;
+
+use app\admin\common\AdminController;
+use app\common\api\CommonFileApi;
+
+/**
+ * Description of System
+ *
+ * @author sgq
+ */
+class CommonFile extends AdminController {
+
+    /**
+     * @auth {{/commonFile/}}
+     * @return type
+     */
+    public function index() {
+        return view();
+    }
+
+    public function list() {
+        $params = $this->request->param();
+        $res = CommonFileApi::getList($params);
+        return json($res);
+    }
+
+    public function add() {
+        $params = $this->request->param();
+        $returnObj = new \stdClass();
+        $returnObj->obj = $params["index"];
+        try {
+            if (!$this->request->file())
+                throw new \think\Exception("请上传附件");
+
+            $file = $this->request->file("fileUrl");
+            $upload = new \app\common\api\UploadApi();
+            $result = $upload->uploadOne($file, "file", "common/file");
+            if ($result->code != 200)
+                throw new \think\Exception($result->msg);
+
+            $data["url"] = $result->filepath;
+            $data["originalName"] = $file->getOriginalName();
+            $data["description"] = $params["description"];
+            if (CommonFileApi::edit($data)) {
+                $returnObj->msg = "上传附件成功";
+                $returnObj->code = 200;
+                echo sprintf("<script>parent.CommonFile.callback(%s);</script>", json_encode($returnObj));
+                exit();
+            }
+            throw new \think\Exception("上传附件失败");
+        } catch (\think\exception $e) {
+            $returnObj->msg = $e->getMessage();
+            $returnObj->code = 500;
+            echo sprintf("<script>parent.CommonFile.callback(%s);</script>", json_encode($returnObj));
+            exit();
+        }
+    }
+
+    public function delete() {
+        $id = $this->request->param("id");
+        $file = CommonFileApi::getOne($id);
+        if ($file && CommonFileApi::delete($id)) {
+            $url = "storage/" . $file["url"];
+            @unlink($url);
+            return json(["code" => 200, "msg" => "删除成功"]);
+        }
+        return json(["msg" => "找不到对应附件"]);
+    }
+
+}

+ 29 - 9
app/admin/controller/Notice.php

@@ -29,10 +29,11 @@ class Notice extends AdminController {
     public function add() {
         if ($this->request->isPost()) {
             try {
-                $param = $this->request->param();
-                validate(NoticeValidate::class)->check($param);
-                NoticeApi::update($this->request->param());
-                exit();
+                $params = $this->request->param();
+                validate(NoticeValidate::class)->check($params);
+                if (NoticeApi::edit($params))
+                    return json(["msg" => "文章发布成功"]);
+                throw new \think\exception("文章发布失败");
             } catch (\think\exception $e) {
                 return json(["msg" => $e->getMessage()]);
             }
@@ -45,17 +46,19 @@ class Notice extends AdminController {
      * @return type
      */
     public function edit() {
+        $params = $this->request->param();
         if ($this->request->isPost()) {
             try {
-                $param = $this->request->param();
-                validate(NoticeValidate::class)->check($param);
-                NoticeApi::update($this->request->param());
-                exit();
+                validate(NoticeValidate::class)->check($params);
+                if (NoticeApi::edit($params))
+                    return json(["msg" => "文章编辑成功"]);
+                throw new \think\exception("文章编辑失败");
             } catch (\think\exception $e) {
                 return json(["msg" => $e->getMessage()]);
             }
         }
-        return view();
+        $row = NoticeApi::getOne($params["id"]);
+        return view("", ["row" => $row]);
     }
 
     /**
@@ -66,6 +69,23 @@ class Notice extends AdminController {
         if ($this->request->isPost()) {
             $params = $this->request->param();
             $id = $params["id"];
+            if (NoticeApi::delete($id))
+                return json(["msg" => "删除成功"]);
+            return json(["msg" => "删除失败"]);
+        }
+    }
+
+    /**
+     * @auth {{/notice/delete}}
+     * @return type
+     */
+    public function top() {
+        if ($this->request->isPost()) {
+            $params = $this->request->param();
+            $id = $params["id"];
+            if (NoticeApi::setTop($id))
+                return json(["msg" => "置顶成功"]);
+            return json(["msg" => "置顶失败"]);
         }
     }
 

+ 37 - 0
app/admin/view/common_file/index.html

@@ -0,0 +1,37 @@
+{extend name="layout/content"}
+{block name="content"}
+<div class="row">
+    <div class="col-sm-12">
+        <div class="ibox float-e-margins">
+            <div class="ibox-title">
+                <h5>公共附件管理</h5>
+            </div>
+            <div class="ibox-content">
+                <div class="row row-lg">
+                    <div class="col-sm-12">
+                        <div class="hidden-xs" id="CommonFileTableToolbar" role="group">                            
+                            <button type="button" class="btn btn-sm btn-primary " onclick="CommonFile.openAddCommonFile()">
+                                <i class="fa fa-plus"></i>添加
+                            </button>
+                            <button type="button" class="btn btn-sm btn-primary " onclick="CommonFile.delete()">
+                                <i class="fa fa-remove"></i>删除
+                            </button>
+                        </div>
+                        <table id="CommonFileTable" class="table-condensed" style="font-size: 10px;table-layout: fixed!important;" data-mobile-responsive="true" data-click-to-select="true">
+                            <thead>
+                                <tr>
+                                    <th data-field="selectItem" data-checkbox="true"></th>
+                                </tr>
+                            </thead>
+                        </table>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+</div>
+<iframe id="hiddenIframe" name="hiddenIframe" style="display: none;"></iframe>
+<script type="text/javascript">
+    document.write('<script src="/static/modular/system/commonFile/commonFile.js?v=' + (new Date()).getTime() + '"><\/script>');
+</script>
+{/block}

+ 7 - 1
app/admin/view/notice/add.html

@@ -8,10 +8,16 @@
                 <div class="col-sm-12">
                     <div class="form-group">
                         <label class="col-sm-1 control-label">标题</label>
-                        <div class="col-sm-11">
+                        <div class="col-sm-5">
                             <input class="form-control" id="title" name="title" type="text">
                         </div>
                     </div>
+                    <div class="form-group">
+                        <label class="col-sm-1 control-label">批次号</label>
+                        <div class="col-sm-5">
+                            <input class="form-control" id="batch" name="batch" type="text">
+                        </div>
+                    </div>
                     <div class="form-group">
                         <label class="col-sm-1 control-label">内容</label>
                         <div class="col-sm-11">

+ 10 - 5
app/admin/view/notice/edit.html

@@ -3,15 +3,20 @@
 <div class="ibox float-e-margins">
     <div class="ibox-content">
         <div class="form-horizontal" id="noticeInfoForm">
-            <input type="hidden" id="id" value="{$notice.id}">
-            <input type="hidden" id="contentVal" value='{$notice.content}'>
+            <input type="hidden" id="id" value="{$row.id}">
+            <input type="hidden" id="contentVal" value='{$row.content}'>
             <div class="row">
                 <div class="col-sm-12">
                     <div class="form-group">
                         <label class="col-sm-1 control-label">标题</label>
-                        <div class="col-sm-11">
-                            <input class="form-control" id="title" name="title" type="text" value="{$notice.title}">
-
+                        <div class="col-sm-5">
+                            <input class="form-control" id="title" name="title" type="text" value="{$row.title}">
+                        </div>
+                    </div>
+                    <div class="form-group">
+                        <label class="col-sm-1 control-label">批次号</label>
+                        <div class="col-sm-5">
+                            <input class="form-control" id="batch" name="batch" type="text" value="{$row.batch}">
                         </div>
                     </div>
                     <div class="form-group">

+ 3 - 2
app/common.php

@@ -274,9 +274,10 @@ function getHostWithProtocol() {
 /**
  * 获得上传文件的路径
  * @param $path
+ * @param $stillOriginalPath 如果是pdf这类文件会生成fileview格式的url,此参数为真是则生成原始url
  * @return string
  */
-function getStoragePath($path) {
+function getStoragePath($path, $stillOriginalPath = false) {
     if (!$path)
         return "";
     if (strpos($path, "jjrcw") === 0) {
@@ -284,7 +285,7 @@ function getStoragePath($path) {
     } else {
         $path = getHostWithProtocol() . "/storage/{$path}";
     }
-    if (isImage($path))
+    if (isImage($path) || $stillOriginalPath)
         return $path;
     return getFileView($path);
 }

+ 56 - 0
app/common/api/CommonFileApi.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace app\common\api;
+
+use app\common\model\CommonFile;
+
+/**
+ * Description of CommonFileApi
+ *
+ * @author sgq
+ */
+class CommonFileApi {
+
+    public static function getOne($id) {
+        return CommonFile::findOrEmpty($id);
+    }
+
+    public static function getList($params) {
+        $order = $params["order"] ?: "desc";
+        $offset = $params["offset"] ?: 0;
+        $limit = $params["limit"] ?: 10;
+        $count = CommonFile::where($where)->count();
+        $list = CommonFile::alias("c")->leftJoin("sys_user u", "c.createUser=u.id")->field("c.*,u.name as createUser")->where($where)->limit($offset, $limit)->order("updateTime desc,createTime desc")->select()->toArray();
+        foreach ($list as $key => $item) {
+            $list[$key]["url"] = getStoragePath($item["url"], true);
+        }
+        return ["total" => $count, "rows" => $list];
+    }
+
+    public static function getAll($where = []) {
+        $list = CommonFile::where($where)->order("updateTime desc,createTime desc")->select()->toArray();
+        return $list;
+    }
+
+    public static function edit($params) {
+        $data["originalName"] = $params["originalName"];
+        $data["url"] = $params["url"];
+        $data["description"] = $params["description"];
+        if ($params["id"]) {
+            $data["id"] = $params["id"];
+            $data["updateTime"] = date("Y-m-d H:i:s");
+            $data["updateUser"] = session("user")["uid"];
+            return CommonFile::update($data);
+        } else {
+            $data["id"] = getStringId();
+            $data["createTime"] = date("Y-m-d H:i:s");
+            $data["createUser"] = session("user")["uid"];
+            return CommonFile::insert($data);
+        }
+    }
+
+    public static function delete($id) {
+        return CommonFile::where(["id" => $id])->delete();
+    }
+
+}

+ 30 - 1
app/common/api/NoticeApi.php

@@ -11,6 +11,10 @@ use app\admin\model\Notice;
  */
 class NoticeApi {
 
+    public static function getOne($id) {
+        return Notice::findOrEmpty($id);
+    }
+
     public static function getList($params = []) {
         $where = [];
         $condition = $params["condition"];
@@ -25,11 +29,36 @@ class NoticeApi {
         $list = Notice::alias("n")->whereOr($where)
                         ->leftJoin("sys_user u", "u.id=n.creater")
                         ->leftJoin("sys_company c", "c.id=u.companyId")
-                        ->order(["n.isTop" => $order, "n.topTime" => $order, "n.createTime" => $order])
+                        ->order(["n.isTop" => "asc", "n.topTime" => $order, "n.createtime" => $order])
                         ->limit($offset, $limit)
                         ->field("n.*,concat(u.name,'(',c.name,')') as createrName")
                         ->select()->toArray();
         return ["total" => $count, "rows" => $list];
     }
 
+    public static function edit($params) {
+        $data["title"] = $params["title"];
+        $data["content"] = $params["content"];
+        $data["batch"] = $params["batch"];
+        if ($params["id"]) {
+            $data["id"] = $params["id"];
+            return Notice::update($data);
+        } else {
+            $data["createtime"] = date("Y-m-d H:i:s");
+            $data["creater"] = session("user")["uid"];
+            return Notice::insert($data);
+        }
+    }
+
+    public static function delete($id) {
+        return Notice::where(["id" => $id])->delete();
+    }
+
+    public static function setTop($id) {
+        $upd["id"] = $id;
+        $upd["isTop"] = 1;
+        $upd["topTime"] = date("Y-m-d H:i:s");
+        return Notice::update($upd);
+    }
+
 }

+ 5 - 0
app/common/controller/Api.php

@@ -388,6 +388,11 @@ class Api extends BaseController {
                 $filename = $fileinfo["templateUrl"];
                 $url = $fileinfo["templateUrl"];
                 break;
+            case 4:
+                $fileinfo = Db::table("sys_common_file")->where($where)->findOrEmpty();
+                $filename = $fileinfo["orignName"];
+                $url = $fileinfo["url"];
+                break;
         }
         $filepath = "storage/" . $url;     // 下载文件名
         if (!file_exists($filepath)) {

+ 16 - 0
app/common/model/CommonFile.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+/**
+ * Description of CommonFile
+ *
+ * @author sgq
+ */
+class CommonFile extends Model {
+
+    protected $table = "sys_common_file";
+
+}

+ 39 - 39
public/static/modular/system/commonFile/commonFile.js

@@ -2,8 +2,8 @@
  * 公共附件管理初始化
  */
 var CommonFile = {
-    id: "CommonFileTable",	//表格id
-    seItem: null,		//选中的条目
+    id: "CommonFileTable", //表格id
+    seItem: null, //选中的条目
     table: null,
     layerIndex: -1
 };
@@ -14,20 +14,20 @@ var CommonFile = {
 CommonFile.initColumn = function () {
     return [
         {field: 'selectItem', radio: true},
-        {title: '附件原名', field: 'originalName', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip',width:"20%"},
-        {title: '下载地址', field: 'url', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip',width:"40%",
-            formatter : function(value,row,index){
-                return "<a href=\'javascript:void(0)\' onclick=\"CommonFile.downloadFileByUrl('"+value+"')\">"+value+"</a>";
+        {title: '附件原名', field: 'originalName', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "20%"},
+        {title: '下载地址', field: 'id', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "40%",
+            formatter: function (value, row, index) {
+                return "<a href=\'javascript:void(0)\' onclick=\"CommonFile.downloadFileById('" + value + "')\">" + row.url + "</a>";
             }
         },
-        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip',width:"20%"},
-        {title: '上传时间', field: 'createTime', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip',width:"10%"},
-        {title: '创建用户', field: 'createUser', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip',width:"10%"},
+        {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "20%"},
+        {title: '上传时间', field: 'createTime', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "10%"},
+        {title: '创建用户', field: 'createUser', visible: true, align: 'center', valign: 'middle', 'class': 'uitd_showTip', width: "10%"},
     ];
 };
 
-CommonFile.downloadFileByUrl = function(url){
-    window.location.href = Feng.ctxPath + "/api/commonDownload/downloadByUrl?url=" + url;
+CommonFile.downloadFileById = function (id) {
+    window.location.href = Feng.ctxPath + "/common/api/downloadFile/type/4/id/" + id;
 }
 
 
@@ -36,10 +36,10 @@ CommonFile.downloadFileByUrl = function(url){
  */
 CommonFile.check = function () {
     var selected = $('#' + this.id).bootstrapTable('getSelections');
-    if(selected.length == 0){
+    if (selected.length == 0) {
         Feng.info("请先选中表格中的某一记录!");
         return false;
-    }else{
+    } else {
         CommonFile.seItem = selected[0];
         return true;
     }
@@ -55,21 +55,21 @@ CommonFile.openAddCommonFile = function () {
         area: ['800px', '420px'], //宽高
         fix: false, //不固定
         maxmin: true,
-        content: "<form id=\"commonFileForm\" action=\""+Feng.ctxPath+"/commonFile/add\" method=\"post\" enctype=\"multipart/form-data\" target=\"hiddenIframe\">\n" +
-            "                    <div  class=\"panel panel-default\">\n" +
-            "                        <div class=\"panel-heading\" style=\"font-weight: bold\">附件材料</div>\n" +
-            "                        <div class=\"panel-body\">\n" +
-            "                            <input type=\"text\" style=\"display: none\" id=\"index\" name=\"index\" >\n" +
-            "                            <input type=\"file\" style=\"display: none\" id=\"fileUrl\" name=\"fileUrl\" onchange=\"CommonFile.fileChange(this)\">\n" +
-            "                            <input class=\"form-control\"  id=\"fileInput\" onclick=\"$('#fileUrl').click()\">\n" +
-            "                        </div>\n" +
-            "                        <div class=\"panel-heading\" style=\"font-weight: bold\">备注</div>\n" +
-            "                        <div class=\"panel-body\">\n" +
-            "                            <textarea class=\"form-control\" id=\"description\" name=\"description\"></textarea>\n" +
-            "                        </div>\n" +
-            "                    </div>\n" +
-            "                </form>",
-        btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;提交' ,'<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
+        content: "<form id=\"commonFileForm\" action=\"" + Feng.ctxPath + "/admin/common_file/add\" method=\"post\" enctype=\"multipart/form-data\" target=\"hiddenIframe\">\n" +
+                "                    <div  class=\"panel panel-default\">\n" +
+                "                        <div class=\"panel-heading\" style=\"font-weight: bold\">附件材料</div>\n" +
+                "                        <div class=\"panel-body\">\n" +
+                "                            <input type=\"text\" style=\"display: none\" id=\"index\" name=\"index\" >\n" +
+                "                            <input type=\"file\" style=\"display: none\" id=\"fileUrl\" name=\"fileUrl\" onchange=\"CommonFile.fileChange(this)\">\n" +
+                "                            <input class=\"form-control\"  id=\"fileInput\" onclick=\"$('#fileUrl').click()\">\n" +
+                "                        </div>\n" +
+                "                        <div class=\"panel-heading\" style=\"font-weight: bold\">备注</div>\n" +
+                "                        <div class=\"panel-body\">\n" +
+                "                            <textarea class=\"form-control\" id=\"description\" name=\"description\"></textarea>\n" +
+                "                        </div>\n" +
+                "                    </div>\n" +
+                "                </form>",
+        btn: ['<i class="fa fa-eye"></i>&nbsp;&nbsp;提交', '<i class="fa fa-eraser"></i>&nbsp;&nbsp;关闭'],
         btnAlign: 'c',
         yes: function (index, layero) {
             $("#index").val(index);
@@ -86,35 +86,35 @@ CommonFile.openAddCommonFile = function () {
 CommonFile.delete = function () {
     if (this.check()) {
         var operation = function () {
-            var ajax = new $ax(Feng.ctxPath + "/commonFile/delete", function (data) {
-                if(data.code == 200){
+            var ajax = new $ax(Feng.ctxPath + "/admin/common_file/delete", function (data) {
+                if (data.code == 200) {
                     Feng.success(data.msg);
                     CommonFile.table.refresh();
-                }else{
+                } else {
                     Feng.info(data.msg);
                 }
             }, function (data) {
                 Feng.error("删除失败!" + data.responseJSON.message + "!");
             });
-            ajax.set("commonFileId",CommonFile.seItem.id);
+            ajax.set("id", CommonFile.seItem.id);
             ajax.start();
         }
         Feng.confirm("确认删除吗?", operation);
     }
 };
 
-CommonFile.fileChange = function(context){
-    var file  = $(context).val();
+CommonFile.fileChange = function (context) {
+    var file = $(context).val();
     var pos = file.lastIndexOf("\\");
-    $("#fileInput").val(file.substring(pos+1));
+    $("#fileInput").val(file.substring(pos + 1));
 }
 
-CommonFile.callback = function(data){
-    if(data.code == 200){
+CommonFile.callback = function (data) {
+    if (data.code == 200) {
         Feng.success(data.msg);
         layer.close(data.obj);
         CommonFile.table.refresh();
-    }else{
+    } else {
         Feng.info(data.msg);
     }
 }
@@ -122,7 +122,7 @@ CommonFile.callback = function(data){
 
 $(function () {
     var defaultColunms = CommonFile.initColumn();
-    var table = new BSTable(CommonFile.id, "/commonFile/list", defaultColunms);
+    var table = new BSTable(CommonFile.id, "/admin/common_file/list", defaultColunms);
     table.setPaginationType("server");
     CommonFile.table = table.init();
 });

+ 3 - 3
public/static/modular/system/notice/notice_info.js

@@ -55,7 +55,7 @@ NoticeInfoDlg.close = function () {
  */
 NoticeInfoDlg.collectData = function () {
     this.noticeInfoData['content'] = NoticeInfoDlg.editor.txt.html();
-    this.set('id').set('title');
+    this.set('id').set('title').set("batch");
 }
 
 /**
@@ -80,7 +80,7 @@ NoticeInfoDlg.addSubmit = function () {
     }
 
     //提交信息
-    var ajax = new $ax(Feng.ctxPath + "/notice/add", function (data) {
+    var ajax = new $ax(Feng.ctxPath + "/admin/notice/add", function (data) {
         Feng.success("添加成功!");
         window.parent.Notice.table.refresh();
         NoticeInfoDlg.close();
@@ -104,7 +104,7 @@ NoticeInfoDlg.editSubmit = function () {
     }
 
     //提交信息
-    var ajax = new $ax(Feng.ctxPath + "/notice/update", function (data) {
+    var ajax = new $ax(Feng.ctxPath + "/admin/notice/edit", function (data) {
         Feng.success("修改成功!");
         window.parent.Notice.table.refresh();
         NoticeInfoDlg.close();