Browse Source

上传附件功能

star7th 6 years ago
parent
commit
2a33da4eda

BIN
Sqlite/showdoc.db.php


+ 98 - 0
server/Application/Api/Controller/PageController.class.php

@@ -21,6 +21,8 @@ class PageController extends BaseController {
         if ($page) {
            //unset($page['page_content']);
            $page['addtime'] = date("Y-m-d H:i:s",$page['addtime']);
+           //判断是否包含附件信息
+           $page['attachment_count'] = D("UploadFile")->where("page_id = '$page_id' ")->count();
         }
         $this->sendResult($page);
     }
@@ -240,4 +242,100 @@ class PageController extends BaseController {
 
     }
 
+    //上传附件
+    public function upload(){
+        $login_user = $this->checkLogin();
+        $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
+        $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
+        $uploadFile = $_FILES['file'] ;
+ 
+        if (!$page_id) {
+            $this->sendError(10103,"请至少先保存一次页面内容");
+            return;
+        }
+        if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
+            $this->sendError(10103);
+            return;
+        }
+
+        if (strstr(strtolower($uploadFile['name']), ".php") ) {
+            return false;
+        }
+
+        $upload = new \Think\Upload();// 实例化上传类
+        $upload->maxSize  = 4145728 ;// 设置附件上传大小
+        $upload->rootPath = './../Public/Uploads/';// 设置附件上传目录
+        $upload->savePath = '';// 设置附件上传子目录
+        $info = $upload->upload() ;
+        if(!$info) {// 上传错误提示错误信息
+          $this->error($upload->getError());
+          return;
+        }else{// 上传成功 获取上传文件信息
+          $url = get_domain().__ROOT__.substr($upload->rootPath,1).$info['file']['savepath'].$info['file']['savename'] ;
+          $insert = array(
+            "uid" => $login_user['uid'],
+            "item_id" => $item_id,
+            "page_id" => $page_id,
+            "display_name" => $uploadFile['name'],
+            "file_type" => $uploadFile['type'],
+            "file_size" => $uploadFile['size'],
+            "real_url" => $url,
+            "addtime" => time(),
+            );
+          $ret = D("UploadFile")->add($insert);
+
+          echo json_encode(array("url"=>$url,"success"=>1));
+        }
+
+    }
+
+    public function uploadList(){
+        $login_user = $this->checkLogin();
+        $item_id = I("item_id/d") ? I("item_id/d") : 0 ;
+        $page_id = I("page_id/d") ? I("page_id/d") : 0 ;
+        if (!$page_id) {
+            $this->sendError(10103,"请至少先保存一次页面内容");
+            return;
+        }
+        $return = array() ;
+        $files = D("UploadFile")->where("page_id = '$page_id' ")->order("addtime desc")->select();
+        if ($files) {
+            $item_id = $files[0]['item_id'] ;
+            if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
+                $this->sendError(10103);
+                return;
+            }
+            foreach ($files as $key => $value) {
+                $return[] = array(
+                    "file_id"=>$value['file_id'],
+                    "display_name"=>$value['display_name'],
+                    "url"=>$value['real_url'],
+                    "addtime"=> date("Y-m-d H:i:s" , $value['addtime'] ),
+                    );
+            }
+
+        }
+        $this->sendResult($return);
+
+    }
+
+    //删除已上传文件
+    public function deleteUploadFile(){
+        $login_user = $this->checkLogin();
+        $file_id = I("file_id/d") ? I("file_id/d") : 0 ;
+
+        $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
+        $item_id = $file['item_id'] ;
+        if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
+            $this->sendError(10103);
+            return;
+        }
+        $ret = D("Page")->deleteFile($file_id);
+        if ($ret) {
+            $this->sendResult(array());
+        }else{
+            $this->sendError(10101,"删除失败");
+        }
+    }
+
 }

+ 16 - 0
server/Application/Api/Model/PageModel.class.php

@@ -136,4 +136,20 @@ class PageModel extends BaseModel {
       return $ret;
    }
 
+   public function deleteFile($file_id){
+      $file = D("UploadFile")->where("file_id = '$file_id' ")->find();
+      $real_url = $file['real_url'] ;
+      $array = explode("/Public/Uploads/", $real_url) ;
+      $file_path = "../Public/Uploads/".$array[1] ;
+      $ret = unlink($file_path);
+      if ($ret) {
+          D("UploadFile")->where(" file_id = '$file_id' ")->delete();
+          return true ;
+      }else{
+          return false;
+      }
+
+
+    }
+
 }

+ 1 - 0
server/Application/Home/Controller/UpdateController.class.php

@@ -195,6 +195,7 @@ class UpdateController extends BaseController {
         `item_id` int(11) NOT NULL DEFAULT '0' ,
         `visit_times` int(11) NOT NULL DEFAULT '0' ,
         `addtime` int(11) NOT NULL DEFAULT '0' ,
+        `real_url` CHAR(200) NOT NULL DEFAULT '',
         `last_update_time` int(11) NOT NULL DEFAULT '0' 
         )";
         D("User")->execute($sql);

+ 28 - 2
web_src/src/components/item/show/show_regular_item/Index.vue

@@ -47,6 +47,9 @@
              <div class="doc-title-box"  v-if="page_id">
                 <span id="doc-title-span" class="dn"></span>
                 <h2 id="doc-title">{{page_title}}</h2>
+                <el-badge :value="attachment_count" class="item"  id="attachment" v-if="attachment_count"   @click.native="ShowAttachment" >
+                  <i class="el-icon-upload"></i> 
+                </el-badge>
             </div>
               <Editormd v-bind:content="content" type="html"  v-if="page_id" ></Editormd>
 
@@ -79,6 +82,11 @@
     </span>
   </el-dialog>
 
+    <!-- 附件列表 -->
+    <AttachmentList callback="" :item_id="page_info.item_id" :manage="false" :page_id="page_info.page_id" ref="AttachmentList"></AttachmentList>
+
+
+
     <Footer> </Footer>
     
   </div>
@@ -91,6 +99,9 @@
   import BackToTop from '@/components/common/BackToTop'
   import LeftMenu from '@/components/item/show/show_regular_item/LeftMenu'
   import PageBar from '@/components/item/show/show_regular_item/PageBar'
+  import AttachmentList from '@/components/page/edit/AttachmentList'
+
+
   export default {
     props:{
       item_info:'',
@@ -107,13 +118,15 @@
         page_info:'',
         show_page_bar:true,
         copyText:"",
+        attachment_count:'',
       }
     },
   components:{
     Editormd,
     LeftMenu,
     PageBar,
-    BackToTop
+    BackToTop,
+    AttachmentList
   },
   methods:{
     //获取页面内容
@@ -138,6 +151,7 @@
               
               that.page_title = response.data.data.page_title ;
               that.page_info = response.data.data ;
+              that.attachment_count = response.data.data.attachment_count > 0 ?  response.data.data.attachment_count  :'' ;
               //切换变量让它重新加载、渲染子组件
               that.page_id = 0 ;
               that.$nextTick(() => {
@@ -196,6 +210,10 @@
     onCopy(){
       this.$message(this.$t("copy_success"));
     },
+    ShowAttachment(){
+        let childRef = this.$refs.AttachmentList ;//获取子组件
+        childRef.show() ; 
+    },
     
   },
   mounted () {
@@ -272,5 +290,13 @@
     cursor: pointer;
     position: fixed;
   }
-
+  #attachment{
+    float: right;
+    font-size: 25px;
+    margin-top: -40px;
+    margin-right: 5px;
+    cursor:pointer;
+    color: #abd1f1;
+  }
+  
 </style>

+ 145 - 0
web_src/src/components/page/edit/AttachmentList.vue

@@ -0,0 +1,145 @@
+<!-- 附件 -->
+<template>
+  <div class="hello">
+    <Header> </Header>
+
+    <el-container class="container-narrow">
+
+      <el-dialog :title="$t('attachment')" :visible.sync="dialogTableVisible" >
+        <el-upload
+          class="upload-file"
+          :action="uploadUrl"
+          :on-success="clearFiles"
+          :on-error="clearFiles"
+          :data="uploadData"
+          ref="uploadFile"
+          v-if="manage"
+          >
+          <el-button size="small" type="primary">{{$t('upload_file')}}</el-button><small>&nbsp;&nbsp;&nbsp;{{$t('file_size_tips')}}</small>
+        </el-upload>
+
+        <el-table :data="content">
+          <el-table-column property="addtime" :label="$t('add_time')" width="170"></el-table-column>
+          <el-table-column property="display_name" :label="$t('file_name')" ></el-table-column>
+          <el-table-column
+            :label="$t('operation')"
+            width="150">
+            <template slot-scope="scope">
+              <el-button @click="downloadFile(scope.row)" type="text" size="small">{{$t('download')}}</el-button>
+              <el-button type="text" size="small" @click="deleteFile(scope.row)" v-if="manage">{{$t('delete')}}</el-button>
+            </template>
+          </el-table-column>
+        </el-table>
+      </el-dialog>
+
+      </el-container>
+    <Footer> </Footer>
+    <div class=""></div>
+  </div>
+</template>
+
+<style>
+
+
+</style>
+
+<script>
+
+export default {
+  props:{
+    callback:'',
+    page_id:'',
+    item_id:'',
+    manage:true,
+  },
+  data () {
+    return {
+      currentDate: new Date(),
+      content: [],
+      dialogTableVisible: false,
+      uploadUrl:DocConfig.server+'/api/page/upload',
+    };
+  },
+  components:{
+    
+  },
+  computed:{
+    uploadData:function(){
+      return {
+        page_id:this.page_id,
+        item_id:this.item_id,
+      }
+    }
+  },
+  methods:{
+    get_content(){
+        var that = this ;
+        var url = DocConfig.server+'/api/page/uploadList';
+        var params = new URLSearchParams();
+        params.append('page_id',  this.page_id);
+        that.axios.post(url, params)
+          .then(function (response) {
+            if (response.data.error_code === 0 ) {
+              that.dialogTableVisible = true ;
+              //that.$message.success("加载成功");
+              var json = response.data.data ;
+              that.content = response.data.data ;
+              
+            }else{
+              that.dialogTableVisible = false;
+              that.$alert(response.data.error_message);
+            }
+            
+          });
+    },
+    show(){
+      this.get_content();
+
+    },
+    downloadFile(row){
+      var url  = row.url ;
+      window.open(url);
+    },
+
+    deleteFile(row){
+      var that = this ;
+      this.$confirm(that.$t('comfirm_delete'), ' ', {
+        confirmButtonText: that.$t('confirm'),
+        cancelButtonText: that.$t('cancel'),
+        type: 'warning'
+      }).then(() => {
+
+        var file_id = row['file_id'] ;
+        var that = this ;
+        var url = DocConfig.server+'/api/page/deleteUploadFile';
+        var params = new URLSearchParams();
+        params.append('file_id',  file_id);
+        that.axios.post(url, params)
+          .then(function (response) {
+            if (response.data.error_code === 0 ) {
+                that.get_content();
+              
+            }else{
+              that.$alert(response.data.error_message);
+            }
+            
+          })
+
+      });
+
+
+
+    },
+    clearFiles(){
+        let childRef = this.$refs.uploadFile ;//获取子组件
+        childRef.clearFiles() ; 
+        this.get_content();
+    }
+
+  },
+  mounted () {
+    
+
+  }
+}
+</script>

+ 20 - 2
web_src/src/components/page/edit/Index.vue

@@ -48,6 +48,10 @@
             </el-dropdown>
           <el-button type="" size="medium" @click="ShowRunApi">{{$t('http_test_api')}}</el-button>
 
+          <el-badge :value="attachment_count" class="item">
+            <el-button type="" size="medium" @click="ShowAttachment">{{$t('attachment')}}</el-button>
+          </el-badge>
+
           </el-row>
 
       <Editormd v-bind:content="content" v-if="content" ref="Editormd"  type="editor" ></Editormd>
@@ -67,6 +71,11 @@
         <!-- Json格式化 -->
         <JsonBeautify :callback="insertValue" ref="JsonBeautify"></JsonBeautify>
 
+
+        <!-- 附件列表 -->
+        <AttachmentList :callback="insertValue" :item_id="item_id" :manage="true" :page_id="page_id" ref="AttachmentList"></AttachmentList>
+
+
       </el-container>
     <Footer> </Footer>
     <div class=""></div>
@@ -173,6 +182,7 @@ import JsonToTable from '@/components/common/JsonToTable'
 import JsonBeautify from '@/components/common/JsonBeautify'
 import TemplateList from '@/components/page/edit/TemplateList'
 import HistoryVersion from '@/components/page/edit/HistoryVersion'
+import AttachmentList from '@/components/page/edit/AttachmentList'
 
 export default {
   data () {
@@ -186,6 +196,7 @@ export default {
       s_number:'',
       page_id:'',
       copy_page_id:'',
+      attachment_count:'',
 
     };
   },
@@ -229,7 +240,8 @@ export default {
     JsonToTable,
     JsonBeautify,
     TemplateList,
-    HistoryVersion
+    HistoryVersion,
+    AttachmentList
   },
   methods:{
     //获取页面内容
@@ -260,6 +272,7 @@ export default {
               that.title = response.data.data.page_title ;
               that.item_id = response.data.data.item_id ;
               that.s_number = response.data.data.s_number ;
+              that.attachment_count = response.data.data.attachment_count > 0 ? "..." :'' ;
             }else{
               that.$alert(response.data.error_message);
             }
@@ -408,6 +421,7 @@ export default {
             localStorage.removeItem("page_content");
             if (page_id <= 0 ) {
               that.$router.push({path:'/page/edit/'+item_id+'/'+response.data.data.page_id}) ;
+              that.page_id = response.data.data.page_id ;
             };
           }else{
             that.$alert(response.data.error_message);
@@ -451,7 +465,11 @@ export default {
             });
        });
     },
-
+    //附件
+    ShowAttachment(){
+        let childRef = this.$refs.AttachmentList ;//获取子组件
+        childRef.show() ; 
+    },
     /** 粘贴上传图片 **/
     upload_paste_img(e){
       var that = this;

+ 8 - 0
web_src/static/lang/en.js

@@ -251,4 +251,12 @@ exports.default = {
 
     "name":"name",
     "name_tips":"your real name",
+
+    "attachment":"attachment",
+    "upload":"upload",
+    "upload_file":"upload file",
+    "file_name":"file name",
+    "download":"download",
+    "file_size_tips":"less than 4M",
+
 };

+ 7 - 0
web_src/static/lang/zh-CN.js

@@ -258,4 +258,11 @@ exports.default = {
     "name":"姓名",
     "name_tips":"推荐使用真实姓名",
 
+    "attachment":"附件",
+    "upload":"上传",
+    "upload_file":"上传文件",
+    "file_name":"文件名",
+    "download":"下载",
+    "file_size_tips":"文件大小在4M内",
+    
 };