地质所 沉降监测网建设项目
suerwei
2024-05-16 560e679c239af089e2ba79bdbec4bac331c01b4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.javaweb.cms.controller;
 
import com.javaweb.cms.domain.Attachment;
import com.javaweb.cms.domain.Pv;
import com.javaweb.cms.service.CmsService;
import com.javaweb.cms.service.IAttachmentService;
import com.javaweb.cms.service.IPvService;
import com.javaweb.cms.service.PVQueueService;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.config.Global;
import com.javaweb.common.config.ServerConfig;
import com.javaweb.common.core.controller.BaseController;
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.core.text.Convert;
import com.javaweb.common.enums.BusinessType;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.common.utils.file.FileUploadUtils;
import com.javaweb.common.utils.file.MimeTypeUtils;
import com.javaweb.framework.util.ShiroUtils;
import com.javaweb.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * cms模块的通用操作
 */
@Controller
public class CmsController extends BaseController {
 
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private IAttachmentService attachmentService;
 
    @Autowired
    private IPvService pvService;
    @Autowired
    CmsService cms;
    /**
     * 上传附件请求
     */
    @PostMapping("/cms/uploadAttach")
    @ResponseBody
    public AjaxResult uploadAttach(@RequestParam("zid") String zid,String sort, MultipartFile file) throws Exception
    {
        try
        {
            if(StringUtils.isEmpty(zid)){
                return AjaxResult.error("参数[zid]不能为空!");
            }
            Attachment attachment=new Attachment();
            if(StringUtils.isEmpty(sort)){
                sort="1";
            }
            int sortNum=1;
            try{
                sortNum=Integer.valueOf(sort);
            }catch (Exception ex){}
            attachment.setSort(sortNum);
            String suffix=FileUploadUtils.getExtension(file);
            String fileType= MimeTypeUtils.MATERIAL_TYPE_OTHER;//文件类型-其它
            if(FileUploadUtils.isImage(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_IMG;
            }else if(FileUploadUtils.isText(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_TEXT;
            }else if(FileUploadUtils.isAudio(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_AUDIO;
            }else if(FileUploadUtils.isVideo(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_VIDEO;
            }else if(FileUploadUtils.isZip(suffix)){
                fileType=MimeTypeUtils.MATERIAL_TYPE_ZIP;
            }else{
                fileType= MimeTypeUtils.MATERIAL_TYPE_OTHER;
            }
            // 上传并返回新文件名称
            String path = FileUploadUtils.upload(Global.getAttachPath(), file);
            String url = serverConfig.getUrl() + path;
            attachment.setFileType(fileType);
            attachment.setFileUrl(url);
            attachment.setFilePath(path);
            attachment.setSize(file.getSize());
            attachment.setFileName(file.getOriginalFilename());
            attachment.setZid(zid);
 
            attachmentService.insertAttachment(attachment);
            return AjaxResult.success(attachment);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
 
    /**
     * 根据组ID查询附件列表
     */
    @PostMapping("/cms/selectAttachmentsByZid")
    @ResponseBody
    public Object selectAttachmentsByZid(@RequestParam("zid")String  zid)
    {
        List<Attachment> list = attachmentService.selectAttachmentsByZid(zid);
        return AjaxResult.success(list);
    }
 
    /**
     * 删除附件
     */
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @PostMapping( "/cms/deleteAttachments")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        SysUser user= ShiroUtils.getSysUser();
        boolean checkOkFlag=true;
        if(!user.isAdmin()){
            //非管理员用户调用该接口删除只能删除自己的附件
            String[] arr= Convert.toStrArray(ids);
            for(String id:arr){
                if(StringUtils.isNotEmpty(id)){
                    Attachment attachment=attachmentService.selectAttachmentById(id);
                    if(attachment!=null){
                        if(!attachment.getUserId().equals(user.getUserId().toString())){
                            checkOkFlag=false;
                            break;
                        }
                    }
                }
            }
        }
        if(!checkOkFlag){
            return AjaxResult.error("您只能删除自己上传的附件!");
        }
        return toAjax(attachmentService.deleteAttachmentByIds(ids));
    }
 
 
    /**
     * 上传素材
     */
    @PostMapping("/cms/uploadMaterial")
    @ResponseBody
    public AjaxResult uploadMaterial(MultipartFile file) throws Exception
    {
        try
        {
            // 上传并返回新文件名称
            String path = FileUploadUtils.upload(Global.getMaterialPath(), file);
            String url = serverConfig.getUrl() + path;
            Map<String,Object> data=new HashMap();
            int width=FileUploadUtils.getImgWidth(file);
            int height=FileUploadUtils.getImgHeight(file);
            data.put("path",path);
            data.put("url",url);
            data.put("width",width);
            data.put("suffix",FileUploadUtils.getExtension(file));
            data.put("height",height);
            data.put("size",file.getSize());
            data.put("name",file.getOriginalFilename());
            return AjaxResult.success(data);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
 
    @Autowired
    PVQueueService pvQueueService;
 
    @PostMapping( "/page/view")
    @ResponseBody
    public AjaxResult pv(Pv pv, HttpServletRequest request)
    {
        pvQueueService.pushPvQueue(request,pv);
        return success();
    }
 
    @PostMapping( "/cms/clearSiteInfoCache")
    @ResponseBody
    public AjaxResult clearSiteInfoCache(Pv pv, HttpServletRequest request)
    {
        cms.clearCache();
        return success();
    }
}