提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.modular.rest.controller; |
|
17 |
|
|
18 |
import cn.hutool.core.codec.Base64; |
|
19 |
import cn.stylefeng.guns.base.auth.context.LoginContextHolder; |
|
20 |
import cn.stylefeng.guns.base.auth.model.LoginUser; |
|
21 |
import cn.stylefeng.guns.base.oshi.SystemHardwareInfo; |
|
22 |
import cn.stylefeng.guns.sys.core.constant.DefaultAvatar; |
|
23 |
import cn.stylefeng.guns.sys.modular.rest.service.RestFileInfoService; |
|
24 |
import cn.stylefeng.guns.sys.modular.rest.service.RestUserService; |
|
25 |
import cn.stylefeng.roses.core.base.controller.BaseController; |
|
26 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
27 |
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException; |
|
28 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
29 |
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum; |
|
30 |
import cn.stylefeng.roses.kernel.model.response.ResponseData; |
|
31 |
import cn.stylefeng.roses.kernel.model.response.SuccessResponseData; |
|
32 |
import lombok.extern.slf4j.Slf4j; |
|
33 |
import org.springframework.beans.factory.annotation.Autowired; |
|
34 |
import org.springframework.web.bind.annotation.*; |
|
35 |
import org.springframework.web.multipart.MultipartFile; |
|
36 |
|
|
37 |
import javax.servlet.http.HttpServletResponse; |
|
38 |
import java.io.IOException; |
|
39 |
import java.util.HashMap; |
|
40 |
|
|
41 |
/** |
|
42 |
* 通用控制器 |
|
43 |
* |
|
44 |
* @author fengshuonan |
|
45 |
* @Date 2017年2月17日20:27:22 |
|
46 |
*/ |
|
47 |
@RestController |
|
48 |
@RequestMapping("/rest/system") |
|
49 |
@Slf4j |
|
50 |
public class RestSystemController extends BaseController { |
|
51 |
|
|
52 |
@Autowired |
|
53 |
private RestUserService restUserService; |
|
54 |
|
|
55 |
@Autowired |
|
56 |
private RestFileInfoService restFileInfoService; |
|
57 |
|
|
58 |
/** |
|
59 |
* 系统硬件信息 |
|
60 |
* |
|
61 |
* @author fengshuonan |
|
62 |
* @Date 2018/12/24 22:43 |
|
63 |
*/ |
|
64 |
@RequestMapping("/systemInfo") |
|
65 |
public ResponseData systemInfo() { |
|
66 |
|
|
67 |
SystemHardwareInfo systemHardwareInfo = new SystemHardwareInfo(); |
|
68 |
systemHardwareInfo.copyTo(); |
|
69 |
|
|
70 |
return new SuccessResponseData(systemHardwareInfo); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* 更新头像 |
|
75 |
* |
|
76 |
* @author fengshuonan |
|
77 |
* @Date 2018/11/9 12:45 PM |
|
78 |
*/ |
|
79 |
@RequestMapping("/updateAvatar") |
|
80 |
public Object uploadAvatar(@RequestParam("fileId") String fileId) { |
|
81 |
|
|
82 |
if (ToolUtil.isEmpty(fileId)) { |
|
83 |
throw new RequestEmptyException("请求头像为空"); |
|
84 |
} |
|
85 |
|
|
86 |
restFileInfoService.updateAvatar(fileId); |
|
87 |
|
|
88 |
return SUCCESS_TIP; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* 预览头像 |
|
93 |
* |
|
94 |
* @author fengshuonan |
|
95 |
* @Date 2018/11/9 12:45 PM |
|
96 |
*/ |
|
97 |
@RequestMapping("/previewAvatar") |
|
98 |
public Object previewAvatar(HttpServletResponse response) { |
|
99 |
|
|
100 |
//输出图片的文件流 |
|
101 |
try { |
|
102 |
response.setContentType("image/jpeg"); |
|
103 |
byte[] decode = this.restFileInfoService.previewAvatar(); |
|
104 |
response.getOutputStream().write(decode); |
|
105 |
} catch (IOException e) { |
|
106 |
throw new ServiceException(CoreExceptionEnum.SERVICE_ERROR); |
|
107 |
} |
|
108 |
|
|
109 |
return null; |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 获取当前用户详情 |
|
114 |
* |
|
115 |
* @author fengshuonan |
|
116 |
* @Date 2018/12/23 6:59 PM |
|
117 |
*/ |
|
118 |
@RequestMapping("/currentUserInfo") |
|
119 |
public ResponseData getUserInfo() { |
|
120 |
|
|
121 |
LoginUser currentUser = LoginContextHolder.getContext().getUser(); |
|
122 |
if (currentUser == null) { |
|
123 |
throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER); |
|
124 |
} |
|
125 |
|
|
126 |
return new SuccessResponseData(restUserService.getUserInfo(currentUser.getId())); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* layui上传组件 通用文件上传接口 |
|
131 |
* |
|
132 |
* @author fengshuonan |
|
133 |
* @Date 2019-2-23 10:48:29 |
|
134 |
*/ |
|
135 |
@RequestMapping(method = RequestMethod.POST, path = "/upload") |
|
136 |
public ResponseData layuiUpload(@RequestPart("file") MultipartFile file) { |
|
137 |
|
|
138 |
String fileId = this.restFileInfoService.uploadFile(file); |
|
139 |
|
|
140 |
HashMap<String, Object> map = new HashMap<>(); |
|
141 |
map.put("fileId", fileId); |
|
142 |
|
|
143 |
return ResponseData.success(0, "上传成功", map); |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 |
* 查看图片 |
|
148 |
* |
|
149 |
* @author fengshuonan |
|
150 |
* @Date 2019/7/27 20:33 |
|
151 |
*/ |
|
152 |
@RequestMapping("/preview/{fileId}") |
|
153 |
public Object preview(@PathVariable("fileId") String fileId, HttpServletResponse response) { |
|
154 |
|
|
155 |
byte[] decode = null; |
|
156 |
|
|
157 |
//输出图片的文件流 |
|
158 |
try { |
|
159 |
response.setContentType("image/jpeg"); |
|
160 |
decode = this.restFileInfoService.getFileBytes(fileId); |
|
161 |
} catch (Exception e) { |
|
162 |
//返回默认头像 |
|
163 |
decode = Base64.decode(DefaultAvatar.BASE_64_AVATAR); |
|
164 |
} |
|
165 |
|
|
166 |
try { |
|
167 |
response.getOutputStream().write(decode); |
|
168 |
} catch (IOException e) { |
|
169 |
throw new ServiceException(CoreExceptionEnum.SERVICE_ERROR); |
|
170 |
} |
|
171 |
|
|
172 |
return null; |
|
173 |
} |
|
174 |
|
|
175 |
} |