zm
2020-08-25 98d16577e2e1a73509a0024eb190fab8ec053398
上传附件功能
4 files modified
316 ■■■■ changed files
src/web/Application/Home/Controller/AttachmentController.class.php 74 ●●●●● patch | view | raw | blame | history
src/web/Application/Home/DAO/AttachmentDAO.class.php 33 ●●●●● patch | view | raw | blame | history
src/web/Application/Home/Service/AttachmentService.class.php 44 ●●●●● patch | view | raw | blame | history
src/web/Public/Scripts/PSI/Attachment/AttachmentList.js 165 ●●●● patch | view | raw | blame | history
src/web/Application/Home/Controller/AttachmentController.class.php
@@ -31,4 +31,78 @@
      $this->ajaxReturn((new AttachmentService())->attachmentList($params));
    }
  }
  /**
   * 上传附件
   */
  public function uploadFile()
  {
    if (IS_POST) {
      $us = new UserService();
      if (!$us->hasPermission(FIdConst::ATTACHMENT)) {
        $this->ajaxReturn($this->noPermission("上传附件"));
        return;
      }
      if(!I("post.linkTableName") || !I("post.linkDataId")){
        $this->ajaxReturn(
          array(
          "success" => false,
          "msg" => "请选择附件关联的业务单据。"
        ));
        return;
      }
      $upload = new \Think\Upload();
      // 允许上传的文件后缀
      $upload->exts = array(
        'xls',
        'xlsx',
        'doc',
        'docx',
        'pdf',
        'png',
        'jpg',
        'jpeg',
        'zip',
        'rar'
      );
      // 保存路径
      $upload->savePath = '/attachment/';
      // 先上传文件
      $fileInfo = $upload->uploadOne($_FILES['data_file']);
      if (!$fileInfo) {
        $this->ajaxReturn(
          array(
            "msg" => $upload->getError(),
            "success" => false
          )
        );
      } else {
        $uploadFileFullPath = './Uploads' . $fileInfo['savepath'] . $fileInfo['savename']; // 获取上传到服务器文件路径
        $uploadFileExt = $fileInfo['ext']; // 上传文件扩展名
        $params = array();
        $params["file_name"] = $fileInfo["name"];
        $params["full_name"] = $uploadFileFullPath;
        $params["file_type"] = $uploadFileExt;
        $params["link_table_name"] = I("post.linkTableName");
        $params["link_data_id"] = I("post.linkDataId");
        $as = new AttachmentService();
        $this->ajaxReturn($as->addUploadInfo($params));
        $this->ajaxReturn(
          array(
            "msg" => $uploadFileFullPath,
            "success" => false
          )
        );
      }
    }
  }
}
src/web/Application/Home/DAO/AttachmentDAO.class.php
@@ -66,4 +66,37 @@
    return $result;
  }
  /**
   * 向附件表中添加上传的附件信息
   *
   * @param array $params
   * @return NULL|array
   */
  public function addUploadInfo(&$params)
  {
    $db = $this->db;
    $file_name = trim($params["file_name"]);
    $full_name = trim($params["full_name"]);
    $file_type = trim($params["file_type"]);
    $link_table_name = trim($params["link_table_name"]);
    $link_data_id = trim($params["link_data_id"]);
    $create_by = trim($params["create_by"]);
    $companyId = $params["companyId"];
    if ($this->companyIdNotExists($companyId)) {
      return $this->badParam("companyId");
    }
    $sql = "insert into t_c_attachment(file_name,full_name,file_type,link_table_name,link_data_id,create_by,create_on,status)
            values ('%s', '%s', '%s', '%s', '%s','%s',now(), 0) ";
    $rc = $db->execute($sql, $file_name,$full_name,$file_type,$link_table_name,$link_data_id,$create_by);
    if ($rc === false) {
      return $this->sqlError(__METHOD__, __LINE__);
    }
    // 操作成功
    return null;
  }
}
src/web/Application/Home/Service/AttachmentService.class.php
@@ -25,5 +25,49 @@
    return $dao->attachmentList($params);
  }
    /**
   * 向附件表中添加上传的附件信息
   *
   * @param array $params
   * @return NULL|array
   */
  public function addUploadInfo($params)
  {
    if ($this->isNotOnline()) {
      return $this->notOnlineError();
    }
    $file_name = $params["file_name"];
    $db = $this->db();
    $db->startTrans();
    $dao = new AttachmentDAO($db);
    $log = null;
    // 新增
    $params["create_by"] = $this->getLoginUserId();
    $params["companyId"] = $this->getCompanyId();
    $rc = $dao->addUploadInfo($params);
    if ($rc) {
      $db->rollback();
      return $rc;
    }
    $id = $params["id"];
    $log = "上传附件: $file_name";
    // 记录业务日志
    $bs = new BizlogService($db);
    $bs->insertBizlog($log, $this->LOG_CATEGORY);
    $db->commit();
    return $this->ok(null);
  }
}
src/web/Public/Scripts/PSI/Attachment/AttachmentList.js
@@ -14,7 +14,7 @@
  width: 800,
  height: 600,
  modal: true,
  layout: "fit",
  // layout: "fit",
  initComponent: function () {
    var me = this;
@@ -42,7 +42,7 @@
      selType: "checkboxmodel",
      viewConfig: {
        deferEmptyText: false,
        emptyText: "请选择附件。"
        emptyText: ""
      },
      store: userStore,
      columnLines: true,
@@ -66,30 +66,107 @@
    me.__grid = grid;
    Ext.apply(me, {
      items: [grid],
      buttons: [{
        text: "添加附件",
        formBind: true,
        iconCls: "PSI-button-add",
        handler: me.onOK,
        scope: me
      }, {
        text: "关闭",
        handler: function () {
          me.close();
        },
        scope: me
      }],
      listeners: {
        show: {
          fn: me.onWndShow,
          scope: me
        }
      }
    });
    var buttons = [];
    me.callParent(arguments);
    buttons.push( {
      text: "关闭",
      handler: function () {
        me.close();
      },
      scope: me
    });
    Ext.apply(me, {
      items: [{
        id: "panelUploadCmp",
        region: "north",
        height: 50,
        layout: "fit",
        border: 0,
        header: false,
        layout: {
          type: "table",
          columns: 2
        },
        items: me.getUploadCmp()
      },
      {
        region: "center",
        layout: "border",
        layout: "fit",
        height: 480,
        border: 0,
        items: [grid]
      },
      {buttons:buttons}
    ],
    listeners: {
      show: {
        fn: me.onWndShow,
        scope: me
      }
    }
  });
  me.callParent(arguments);
},
  getUploadCmp: function () {
    var me = this;
    return [
      {
        id: "importForm",
        border:0,
        margin: "10 0 0 0",
        xtype: "form",
        layout: {
          type: "table",
          columns: 1
        },
        height: "100%",
        bodyPadding: 5,
        fieldDefaults: {
          labelWidth: 60,
          labelAlign: "right",
          labelSeparator: "",
          msgTarget: 'side'
        },
        items: [{
          xtype: 'filefield',
          name: 'data_file',
          afterLabelTextTpl: '<span style="color:red;font-weight:bold" data-qtip="必须选择一个要上传的文件">*</span>',
          fieldLabel: '文件',
          labelWidth: 50,
          width: 400,
          msgTarget: 'side',
          allowBlank: false,
          anchor: '100%',
          buttonText: '选择文件',
        },
        {
          xtype: "hidden",
          id: "hiddenLinkTableName",
          name: "linkTableName",
          value: me.getLinkTableName()
        },
        {
          xtype: "hidden",
          id: "hiddenLinkDataId",
          name: "linkDataId",
          value: me.getLinkDataId()
        }]
      },
      {
      xtype: "container",
      items: [{
        xtype: "button",
        text: "上传",
        width: 100,
        height: 20,
        margin: "10 1 5 30",
        handler: me.onUpload,
        scope: me
      }]
    }];
  },
  onWndShow: function () {
@@ -112,7 +189,7 @@
        if (success) {
          var data = Ext.JSON
            .decode(response.responseText);
          userStore.removeAll();
          for (var i = 0; i < data.length; i++) {
            var item = data[i];
            userStore.add({
@@ -126,26 +203,32 @@
            });
          }
        }
        el.unmask();
      }
    });
  },
  onUpload: function () {
    var me = this;
    var f = Ext.getCmp("importForm");
    var el = f.getEl();
    el && el.mask('正在导入...');
    f.submit({
      url: PSI.Const.BASE_URL + "Home/Attachment/uploadFile",
      method: "POST",
      success: function (form, action) {
        el && el.unmask();
  onOK: function () {
    var grid = this.__grid;
        PSI.MsgBox.showInfo("上传成功");
        me.onWndShow();
    var items = grid.getSelectionModel().getSelection();
    if (items == null || items.length == 0) {
      PSI.MsgBox.showInfo("没有选择用户");
      return;
    }
    if (this.getParentForm()) {
      this.getParentForm().setSelectedUsers(items);
    }
    this.close();
        //me.close();
        //me.getParentForm().freshGoodsGrid();
      },
      failure: function (form, action) {
        el && el.unmask();
        PSI.MsgBox.showInfo(action.result.msg);
      }
    });
  }
});