瀏覽代碼

添加上传附件、查看附件功能

zhanghe 5 月之前
父節點
當前提交
421643383d

+ 99 - 0
src/main/java/com/jeesite/modules/mes/web/MesProductRecordController.java

@@ -426,11 +426,110 @@ public class MesProductRecordController extends BaseController {
 
 		mesProductRecordService.update(mesProductRecord);
 
+		// 保存上传的图片和附件
+		FileUploadUtils.saveFileUpload(mesProductRecord, mesProductRecord.getId(), "mesProductRecord_image");
+		FileUploadUtils.saveFileUpload(mesProductRecord, mesProductRecord.getId(), "mesProductRecord_file");
 
 		return renderResult(Global.TRUE, text("保存生产记录成功!"));
 	}
 
 	/**
+	 * 查询某条记录关联的附件列表(用于详情页"查看"按钮)
+	 */
+	@RequiresPermissions("mes:mesProductRecord:view")
+	@RequestMapping(value = "fileList")
+	@ResponseBody
+	public List<Map<String, Object>> fileList(String bizKey, String bizType) {
+		List<Map<String, Object>> result = ListUtils.newArrayList();
+		if (StringUtils.isBlank(bizKey) || StringUtils.isBlank(bizType)) {
+			return result;
+		}
+		FileUpload fu = new FileUpload();
+		fu.setBizKey(bizKey);
+		fu.setBizType(bizType);
+		List<FileUpload> list = fileUploadService.findList(fu);
+		if (ListUtils.isEmpty(list)) {
+			return result;
+		}
+		for (FileUpload fi : list) {
+			Map<String, Object> map = MapUtils.newHashMap();
+			map.put("fileName", fi.getFileName());
+			map.put("fileUrl", fi.getFileUrl());
+			if (fi.getFileEntity() != null) {
+				map.put("fileId", fi.getFileEntity().getFileId());
+			}
+			result.add(map);
+		}
+		return result;
+	}
+
+	/**
+	 * 内联预览附件(在线查看)。
+	 * 根据 bizKey + bizType + fileUrl 找到对应文件,从磁盘读取文件流输出,
+	 * Content-Disposition 设为 inline 让浏览器尽量预览而不是下载。
+	 */
+	@RequiresPermissions("mes:mesProductRecord:view")
+	@RequestMapping(value = "viewFile")
+	public void viewFile(String bizKey, String bizType, String fileUrl,
+						 javax.servlet.http.HttpServletResponse response) {
+		try {
+			if (StringUtils.isBlank(bizKey) || StringUtils.isBlank(bizType) || StringUtils.isBlank(fileUrl)) {
+				response.sendError(404, "params missing");
+				return;
+			}
+			FileUpload q = new FileUpload();
+			q.setBizKey(bizKey);
+			q.setBizType(bizType);
+			List<FileUpload> list = fileUploadService.findList(q);
+			if (ListUtils.isEmpty(list)) {
+				response.sendError(404, "file not found");
+				return;
+			}
+			FileUpload fu = null;
+			for (FileUpload f : list) {
+				if (fileUrl.equals(f.getFileUrl())) {
+					fu = f;
+					break;
+				}
+			}
+			if (fu == null) {
+				response.sendError(404, "file not found");
+				return;
+			}
+			String url = fu.getFileUrl();
+			String baseDir = Global.getConfig("file.baseDir");
+			if (StringUtils.isBlank(baseDir)) {
+				baseDir = System.getProperty("user.home");
+			}
+			java.io.File file = new java.io.File(baseDir, url);
+			if (!file.exists() || !file.isFile()) {
+				response.sendError(404, "file not exist on disk: " + file.getAbsolutePath());
+				return;
+			}
+			String contentType = java.net.URLConnection.guessContentTypeFromName(file.getName());
+			if (StringUtils.isBlank(contentType)) {
+				contentType = "application/octet-stream";
+			}
+			response.setContentType(contentType);
+			response.setContentLengthLong(file.length());
+			String encodedName = java.net.URLEncoder.encode(fu.getFileName(), "UTF-8").replace("+", "%20");
+			response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + encodedName);
+
+			try (java.io.InputStream in = new java.io.FileInputStream(file);
+				 java.io.OutputStream out = response.getOutputStream()) {
+				byte[] buf = new byte[8192];
+				int len;
+				while ((len = in.read(buf)) != -1) {
+					out.write(buf, 0, len);
+				}
+				out.flush();
+			}
+		} catch (Exception e) {
+			logger.error("内联预览文件失败", e);
+		}
+	}
+
+	/**
 	 * 保存数据  创建生产订单,都可以创建----
 	 */
 //	@RequiresPermissions("mes:mesProductRecord:edit")

+ 11 - 0
src/main/resources/views/modules/mes/mesProductRecordEdit.html

@@ -72,6 +72,17 @@
 							</div>
 						</div>
 					</div>
+
+					<div class="col-xs-12">
+						<div class="form-group">
+							<label class="control-label col-sm-2">
+								<span class="required hide">*</span> ${text('附件上传')}:</label>
+							<div class="col-sm-10">
+								<#form:fileupload id="uploadFile" bizKey="${mesProductRecord.id}" bizType="mesProductRecord_file"
+								uploadType="all" class="" readonly="false" preview="true"/>
+							</div>
+						</div>
+					</div>
 				</div>
 			</div>
 			<div class="box-footer">

+ 58 - 0
src/main/resources/views/modules/mes/mesProductRecordInfo.html

@@ -48,6 +48,13 @@
 					</td>
 				</tr>
 				<tr>
+					<th class="mpc-header">附件</th>
+					<td>
+						<#form:fileupload id="uploadFile" bizKey="${mesProductRecord.id}" bizType="mesProductRecord_file"
+						uploadType="all" class="" readonly="true" preview="true"/>
+					</td>
+				</tr>
+				<tr>
 					<th class="mpc-header">备注</th>
 					<td>${mesProductRecord.remark}</td>
 				</tr>
@@ -72,5 +79,56 @@
 </div>
 <% } %>
 <script>
+// 在每行附件的"下载"按钮前注入"查看"按钮,链接走后端 viewFile 接口(inline 输出)
+$(function(){
+    var bizKey = '${mesProductRecord.id}';
+    var bizType = 'mesProductRecord_file';
+
+    function buildViewUrl(fileUrl){
+        return '${ctx}/mes/mesProductRecord/viewFile'
+            + '?bizKey=' + encodeURIComponent(bizKey)
+            + '&bizType=' + encodeURIComponent(bizType)
+            + '&fileUrl=' + encodeURIComponent(fileUrl);
+    }
+
+    function injectButtons(fileList){
+        // 找到附件控件容器内所有"下载"按钮(class 含 btn 的 a 标签且文本含"下载"或图标 fa-download)
+        var $downloads = $('#uploadFile').closest('td').find('a').filter(function(){
+            var $a = $(this);
+            if($a.data('view-injected')) return false;
+            var text = $.trim($a.text());
+            return text.indexOf('下载') !== -1 || $a.find('i.fa-download').length > 0;
+        });
+
+        if(!$downloads.length) return false;
+
+        // 按 DOM 顺序与 fileList 一一对应
+        $downloads.each(function(idx){
+            var $a = $(this);
+            var f = fileList[idx];
+            if(!f) return;
+            $a.data('view-injected', true);
+            var $view = $('<a class="btn btn-info btn-xs" target="_blank" style="margin-right:4px;">'
+                        + '<i class="fa fa-eye"></i> 查看</a>');
+            $view.attr('href', buildViewUrl(f.fileUrl));
+            $a.before($view);
+        });
+        return true;
+    }
 
+    $.get('${ctx}/mes/mesProductRecord/fileList', {
+        bizKey: bizKey,
+        bizType: bizType
+    }, function(list){
+        if(!list || !list.length) return;
+        // 控件渲染是异步的,多次重试直到注入成功
+        var tries = 0;
+        var timer = setInterval(function(){
+            tries++;
+            if(injectButtons(list) || tries >= 10){
+                clearInterval(timer);
+            }
+        }, 300);
+    });
+});
 </script>