|
@@ -5,12 +5,27 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
5
|
5
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
6
|
6
|
import com.huiju.estateagents.base.BaseController;
|
7
|
7
|
import com.huiju.estateagents.base.ResponseBean;
|
|
8
|
+import com.huiju.estateagents.common.StringUtils;
|
|
9
|
+import com.huiju.estateagents.common.WxUtils;
|
8
|
10
|
import com.huiju.estateagents.third.entity.TaMpInfo;
|
9
|
11
|
import com.huiju.estateagents.third.service.ITaMpInfoService;
|
|
12
|
+import lombok.extern.slf4j.Slf4j;
|
|
13
|
+import me.chanjar.weixin.common.api.WxConsts;
|
|
14
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
15
|
+import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
|
16
|
+import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
10
|
17
|
import org.slf4j.Logger;
|
11
|
18
|
import org.slf4j.LoggerFactory;
|
12
|
19
|
import org.springframework.beans.factory.annotation.Autowired;
|
13
|
20
|
import org.springframework.web.bind.annotation.*;
|
|
21
|
+import org.springframework.web.multipart.MultipartFile;
|
|
22
|
+
|
|
23
|
+import javax.servlet.http.HttpServletRequest;
|
|
24
|
+import java.io.File;
|
|
25
|
+import java.io.IOException;
|
|
26
|
+import java.time.LocalDateTime;
|
|
27
|
+import java.util.HashMap;
|
|
28
|
+import java.util.Map;
|
14
|
29
|
|
15
|
30
|
/**
|
16
|
31
|
* <p>
|
|
@@ -20,124 +35,182 @@ import org.springframework.web.bind.annotation.*;
|
20
|
35
|
* @author fxf
|
21
|
36
|
* @since 2020-06-02
|
22
|
37
|
*/
|
|
38
|
+@Slf4j
|
23
|
39
|
@RestController
|
24
|
|
-@RequestMapping("/")
|
|
40
|
+@RequestMapping("/api")
|
25
|
41
|
public class TaMpInfoController extends BaseController {
|
26
|
42
|
|
27
|
|
- private final Logger logger = LoggerFactory.getLogger(TaMpInfoController.class);
|
28
|
|
-
|
29
|
43
|
@Autowired
|
30
|
44
|
public ITaMpInfoService iTaMpInfoService;
|
31
|
45
|
|
|
46
|
+ @Autowired
|
|
47
|
+ private WxUtils wxUtils;
|
32
|
48
|
|
|
49
|
+//
|
|
50
|
+// /**
|
|
51
|
+// * 分页查询列表
|
|
52
|
+// * @param pageNum
|
|
53
|
+// * @param pageSize
|
|
54
|
+// * @return
|
|
55
|
+// */
|
|
56
|
+// @RequestMapping(value="/taMpInfo",method= RequestMethod.GET)
|
|
57
|
+// public ResponseBean taMpInfoList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
|
|
58
|
+// @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
|
|
59
|
+// ResponseBean responseBean = new ResponseBean();
|
|
60
|
+// try {
|
|
61
|
+// //使用分页插件
|
|
62
|
+// IPage<TaMpInfo> pg = new Page<>(pageNum, pageSize);
|
|
63
|
+// QueryWrapper<TaMpInfo> queryWrapper = new QueryWrapper<>();
|
|
64
|
+// queryWrapper.orderByDesc("create_date");
|
|
65
|
+//
|
|
66
|
+// IPage<TaMpInfo> result = iTaMpInfoService.page(pg, queryWrapper);
|
|
67
|
+// responseBean.addSuccess(result);
|
|
68
|
+// }catch (Exception e){
|
|
69
|
+// e.printStackTrace();
|
|
70
|
+// logger.error("taMpInfoList -=- {}",e.toString());
|
|
71
|
+// responseBean.addError(e.getMessage());
|
|
72
|
+// }
|
|
73
|
+// return responseBean;
|
|
74
|
+// }
|
|
75
|
+//
|
|
76
|
+// /**
|
|
77
|
+// * 保存对象
|
|
78
|
+// * @param taMpInfo 实体对象
|
|
79
|
+// * @return
|
|
80
|
+// */
|
|
81
|
+// @RequestMapping(value="/taMpInfo",method= RequestMethod.POST)
|
|
82
|
+// public ResponseBean taMpInfoAdd(@RequestBody TaMpInfo taMpInfo){
|
|
83
|
+// ResponseBean responseBean = new ResponseBean();
|
|
84
|
+// try {
|
|
85
|
+// if (iTaMpInfoService.save(taMpInfo)){
|
|
86
|
+// responseBean.addSuccess(taMpInfo);
|
|
87
|
+// }else {
|
|
88
|
+// responseBean.addError("fail");
|
|
89
|
+// }
|
|
90
|
+// }catch (Exception e){
|
|
91
|
+// e.printStackTrace();
|
|
92
|
+// logger.error("taMpInfoAdd -=- {}",e.toString());
|
|
93
|
+// responseBean.addError(e.getMessage());
|
|
94
|
+// }
|
|
95
|
+// return responseBean;
|
|
96
|
+// }
|
|
97
|
+//
|
|
98
|
+// /**
|
|
99
|
+// * 根据id删除对象
|
|
100
|
+// * @param id 实体ID
|
|
101
|
+// */
|
|
102
|
+// @ResponseBody
|
|
103
|
+// @RequestMapping(value="/taMpInfo/{id}", method= RequestMethod.DELETE)
|
|
104
|
+// public ResponseBean taMpInfoDelete(@PathVariable Integer id){
|
|
105
|
+// ResponseBean responseBean = new ResponseBean();
|
|
106
|
+// try {
|
|
107
|
+// if(iTaMpInfoService.removeById(id)){
|
|
108
|
+// responseBean.addSuccess("success");
|
|
109
|
+// }else {
|
|
110
|
+// responseBean.addError("fail");
|
|
111
|
+// }
|
|
112
|
+// }catch (Exception e){
|
|
113
|
+// e.printStackTrace();
|
|
114
|
+// logger.error("taMpInfoDelete -=- {}",e.toString());
|
|
115
|
+// responseBean.addError(e.getMessage());
|
|
116
|
+// }
|
|
117
|
+// return responseBean;
|
|
118
|
+// }
|
|
119
|
+//
|
|
120
|
+// /**
|
|
121
|
+// * 修改对象
|
|
122
|
+// * @param id 实体ID
|
|
123
|
+// * @param taMpInfo 实体对象
|
|
124
|
+// * @return
|
|
125
|
+// */
|
|
126
|
+// @RequestMapping(value="/taMpInfo/{id}",method= RequestMethod.PUT)
|
|
127
|
+// public ResponseBean taMpInfoUpdate(@PathVariable Integer id,
|
|
128
|
+// @RequestBody TaMpInfo taMpInfo){
|
|
129
|
+// ResponseBean responseBean = new ResponseBean();
|
|
130
|
+// try {
|
|
131
|
+// if (iTaMpInfoService.updateById(taMpInfo)){
|
|
132
|
+// responseBean.addSuccess(taMpInfo);
|
|
133
|
+// }else {
|
|
134
|
+// responseBean.addError("fail");
|
|
135
|
+// }
|
|
136
|
+// }catch (Exception e){
|
|
137
|
+// e.printStackTrace();
|
|
138
|
+// logger.error("taMpInfoUpdate -=- {}",e.toString());
|
|
139
|
+// responseBean.addError(e.getMessage());
|
|
140
|
+// }
|
|
141
|
+// return responseBean;
|
|
142
|
+// }
|
|
143
|
+//
|
33
|
144
|
/**
|
34
|
|
- * 分页查询列表
|
35
|
|
- * @param pageNum
|
36
|
|
- * @param pageSize
|
37
|
|
- * @return
|
|
145
|
+ * 根据id查询对象
|
|
146
|
+ * @param id 实体ID
|
38
|
147
|
*/
|
39
|
|
- @RequestMapping(value="/taMpInfo",method= RequestMethod.GET)
|
40
|
|
- public ResponseBean taMpInfoList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
|
41
|
|
- @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
|
42
|
|
- ResponseBean responseBean = new ResponseBean();
|
43
|
|
- try {
|
44
|
|
- //使用分页插件
|
45
|
|
- IPage<TaMpInfo> pg = new Page<>(pageNum, pageSize);
|
46
|
|
- QueryWrapper<TaMpInfo> queryWrapper = new QueryWrapper<>();
|
47
|
|
- queryWrapper.orderByDesc("create_date");
|
48
|
|
-
|
49
|
|
- IPage<TaMpInfo> result = iTaMpInfoService.page(pg, queryWrapper);
|
50
|
|
- responseBean.addSuccess(result);
|
51
|
|
- }catch (Exception e){
|
52
|
|
- e.printStackTrace();
|
53
|
|
- logger.error("taMpInfoList -=- {}",e.toString());
|
54
|
|
- responseBean.addError(e.getMessage());
|
55
|
|
- }
|
56
|
|
- return responseBean;
|
|
148
|
+ @RequestMapping(value="/admin/mp/taMpInfo",method= RequestMethod.GET)
|
|
149
|
+ public ResponseBean taMpInfoGet(HttpServletRequest request){
|
|
150
|
+ Integer orgId = getOrgId(request);
|
|
151
|
+ TaMpInfo taMpInfo = iTaMpInfoService.getByOrgId(orgId);
|
|
152
|
+ return ResponseBean.success(taMpInfo);
|
57
|
153
|
}
|
58
|
154
|
|
59
|
155
|
/**
|
60
|
|
- * 保存对象
|
61
|
|
- * @param taMpInfo 实体对象
|
|
156
|
+ * 暂时仅支持 jpg
|
|
157
|
+ * @param file
|
|
158
|
+ * @param request
|
62
|
159
|
* @return
|
63
|
160
|
*/
|
64
|
|
- @RequestMapping(value="/taMpInfo",method= RequestMethod.POST)
|
65
|
|
- public ResponseBean taMpInfoAdd(@RequestBody TaMpInfo taMpInfo){
|
66
|
|
- ResponseBean responseBean = new ResponseBean();
|
67
|
|
- try {
|
68
|
|
- if (iTaMpInfoService.save(taMpInfo)){
|
69
|
|
- responseBean.addSuccess(taMpInfo);
|
70
|
|
- }else {
|
71
|
|
- responseBean.addError("fail");
|
72
|
|
- }
|
73
|
|
- }catch (Exception e){
|
74
|
|
- e.printStackTrace();
|
75
|
|
- logger.error("taMpInfoAdd -=- {}",e.toString());
|
76
|
|
- responseBean.addError(e.getMessage());
|
|
161
|
+ @PostMapping("/admin/mp/material/miniapp")
|
|
162
|
+ public ResponseBean postMpMiniappMedia(@RequestParam("file") MultipartFile file,
|
|
163
|
+ HttpServletRequest request) {
|
|
164
|
+ if (file.isEmpty()) {
|
|
165
|
+ return ResponseBean.error("请选择文件上传", ResponseBean.ERROR_MISSING_PARAMS);
|
77
|
166
|
}
|
78
|
|
- return responseBean;
|
79
|
|
- }
|
80
|
167
|
|
81
|
|
- /**
|
82
|
|
- * 根据id删除对象
|
83
|
|
- * @param id 实体ID
|
84
|
|
- */
|
85
|
|
- @ResponseBody
|
86
|
|
- @RequestMapping(value="/taMpInfo/{id}", method= RequestMethod.DELETE)
|
87
|
|
- public ResponseBean taMpInfoDelete(@PathVariable Integer id){
|
88
|
|
- ResponseBean responseBean = new ResponseBean();
|
|
168
|
+ Integer orgId = getOrgId(request);
|
|
169
|
+ TaMpInfo taMpInfo = iTaMpInfoService.getByOrgId(orgId);
|
|
170
|
+
|
89
|
171
|
try {
|
90
|
|
- if(iTaMpInfoService.removeById(id)){
|
91
|
|
- responseBean.addSuccess("success");
|
92
|
|
- }else {
|
93
|
|
- responseBean.addError("fail");
|
|
172
|
+ String nwName = "yz-"+LocalDateTime.now().getNano();
|
|
173
|
+ String path = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + nwName;
|
|
174
|
+ File tmpFile = File.createTempFile(path, ".jpg");
|
|
175
|
+ file.transferTo(tmpFile);
|
|
176
|
+
|
|
177
|
+ String fname = null == file.getOriginalFilename() ? nwName : file.getOriginalFilename();
|
|
178
|
+ if (!fname.contains(".jpg")) {
|
|
179
|
+ fname += ".jpg";
|
94
|
180
|
}
|
95
|
|
- }catch (Exception e){
|
96
|
|
- e.printStackTrace();
|
97
|
|
- logger.error("taMpInfoDelete -=- {}",e.toString());
|
98
|
|
- responseBean.addError(e.getMessage());
|
99
|
|
- }
|
100
|
|
- return responseBean;
|
101
|
|
- }
|
102
|
181
|
|
103
|
|
- /**
|
104
|
|
- * 修改对象
|
105
|
|
- * @param id 实体ID
|
106
|
|
- * @param taMpInfo 实体对象
|
107
|
|
- * @return
|
108
|
|
- */
|
109
|
|
- @RequestMapping(value="/taMpInfo/{id}",method= RequestMethod.PUT)
|
110
|
|
- public ResponseBean taMpInfoUpdate(@PathVariable Integer id,
|
111
|
|
- @RequestBody TaMpInfo taMpInfo){
|
112
|
|
- ResponseBean responseBean = new ResponseBean();
|
113
|
|
- try {
|
114
|
|
- if (iTaMpInfoService.updateById(taMpInfo)){
|
115
|
|
- responseBean.addSuccess(taMpInfo);
|
116
|
|
- }else {
|
117
|
|
- responseBean.addError("fail");
|
|
182
|
+ WxMpMaterial wxMaterial = new WxMpMaterial();
|
|
183
|
+ wxMaterial.setFile(tmpFile);
|
|
184
|
+ wxMaterial.setName(fname);
|
|
185
|
+
|
|
186
|
+ WxMpMaterialUploadResult res = wxUtils.getMpService(orgId).getMaterialService().materialFileUpload(WxConsts.MaterialType.IMAGE, wxMaterial);
|
|
187
|
+
|
|
188
|
+ if (StringUtils.isEmpty(res.getMediaId())) {
|
|
189
|
+ log.error("上传图片到微信失败 [{}]: {}", res.getErrCode(), res.getErrMsg());
|
|
190
|
+ return ResponseBean.error("上传图片到微信失败: " + res.getErrMsg(), ResponseBean.ERROR_UNAVAILABLE);
|
118
|
191
|
}
|
119
|
|
- }catch (Exception e){
|
120
|
|
- e.printStackTrace();
|
121
|
|
- logger.error("taMpInfoUpdate -=- {}",e.toString());
|
122
|
|
- responseBean.addError(e.getMessage());
|
123
|
|
- }
|
124
|
|
- return responseBean;
|
125
|
|
- }
|
126
|
192
|
|
127
|
|
- /**
|
128
|
|
- * 根据id查询对象
|
129
|
|
- * @param id 实体ID
|
130
|
|
- */
|
131
|
|
- @RequestMapping(value="/taMpInfo/{id}",method= RequestMethod.GET)
|
132
|
|
- public ResponseBean taMpInfoGet(@PathVariable Integer id){
|
133
|
|
- ResponseBean responseBean = new ResponseBean();
|
134
|
|
- try {
|
135
|
|
- responseBean.addSuccess(iTaMpInfoService.getById(id));
|
136
|
|
- }catch (Exception e){
|
|
193
|
+ taMpInfo.setMiniappMedia(res.getMediaId());
|
|
194
|
+ taMpInfo.setMiniappUrl(res.getUrl());
|
|
195
|
+ if (!iTaMpInfoService.updateById(taMpInfo)) {
|
|
196
|
+ return ResponseBean.error("保存图片媒体ID失败", ResponseBean.ERROR_UNAVAILABLE);
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ if (tmpFile.exists()) {
|
|
200
|
+ tmpFile.delete();
|
|
201
|
+ }
|
|
202
|
+
|
|
203
|
+ return ResponseBean.success(new HashMap<String, String>(){{
|
|
204
|
+ put("mediaId", res.getMediaId());
|
|
205
|
+ put("url", res.getUrl());
|
|
206
|
+ }});
|
|
207
|
+
|
|
208
|
+ } catch (IOException e) {
|
|
209
|
+ e.printStackTrace();
|
|
210
|
+ return ResponseBean.error("接收图片失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
|
|
211
|
+ } catch (WxErrorException e) {
|
137
|
212
|
e.printStackTrace();
|
138
|
|
- logger.error("taMpInfoDelete -=- {}",e.toString());
|
139
|
|
- responseBean.addError(e.getMessage());
|
|
213
|
+ return ResponseBean.error("上传图片到微信失败: " + e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
|
140
|
214
|
}
|
141
|
|
- return responseBean;
|
142
|
215
|
}
|
143
|
216
|
}
|