WxMiniController.java
13.4 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package com.w1hd.zzhnc.controller.wx;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.theyeasy.weixin.model.WxMiniSessionInfo;
import org.theyeasy.weixin.service.WxMiniService;
import org.theyeasy.weixin.util.WxMiniUtil;
import com.beust.jcommander.internal.Maps;
import com.w1hd.zzhnc.controller.pc.BaseController;
import com.w1hd.zzhnc.model.Activity;
import com.w1hd.zzhnc.model.Articles;
import com.w1hd.zzhnc.model.Banner;
import com.w1hd.zzhnc.model.Fans;
import com.w1hd.zzhnc.model.Goods;
import com.w1hd.zzhnc.model.Lotterylog;
import com.w1hd.zzhnc.model.Prize;
import com.w1hd.zzhnc.service.ActivityService;
import com.w1hd.zzhnc.service.ArticleService;
import com.w1hd.zzhnc.service.BannerService;
import com.w1hd.zzhnc.service.FansService;
import com.w1hd.zzhnc.service.GoodsService;
import com.w1hd.zzhnc.service.PrizeService;
import com.w1hd.zzhnc.util.PageResults;
import com.w1hd.zzhnc.util.RedisUtil;
import com.w1hd.zzhnc.vo.Vo_msg;
import com.wordnik.swagger.annotations.ApiOperation;
@Controller
@RequestMapping("/wxmini")
public class WxMiniController extends BaseController {
public static final String PRIZE_KEY_TIME = RedisUtil.PROJECTNAME.concat("_").concat("PRIZE_KEY_TIME_");
@Autowired
FansService fansService;
@Autowired
WxMiniService wxMiniService;
@Autowired
ArticleService articleService;
@Autowired
ActivityService activityService;
@Autowired
PrizeService prizeService;
@RequestMapping(value = "/wxmsg")
public void WxMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
PrintWriter out = response.getWriter();
if (!WxMiniUtil.checkSignature(signature, timestamp, nonce)) {
out.print("sorry,该方法只处理微信推送的消息!");
System.out.println("sorry,该方法只处理微信推送的消息!");
out.close();
return;
}
// 微信消息接入验证echostr
if (null != echostr && echostr.length() > 0) {
System.out.println("check ok,echostr=" + echostr);
out.print(echostr);
out.close();
return;
}
// 处理用户发送来的消息
WxMiniUtil wxMiniUtil = new WxMiniUtil();
String responseMsg = wxMiniService.processRequest(request);
if (null == response || responseMsg.length() <= 0)
responseMsg = "success";
// 响应微信服务器
out.print(responseMsg);
out.close();
System.out.println("回复给微信的消息:" + responseMsg);
}
@RequestMapping(value = "/login", method = RequestMethod.GET) // 购房助手登录 double lng,double lat
public @ResponseBody Vo_msg login(@RequestParam(value = "code") String code,
@RequestParam(value = "shareId") Integer shareId, @RequestParam(value = "fansId") Integer fansId,
@RequestParam(value = "nickname") String nickname, @RequestParam(value = "logo") String logo,
@RequestParam(value = "lng") double lng, @RequestParam(value = "lat") double lat) {
logger.info("小程序登录:code=" + code);
// 取openid
WxMiniSessionInfo sessionInfo = WxMiniUtil.jscode2session(code);
// 添加粉丝记录
Fans fans = fansService.addFans(sessionInfo, shareId, nickname, logo, lng, lat, fansId);
if (fans != null) {
Map<String, Object> map = new HashMap<>();
map.put("miniOpenId", sessionInfo.getOpenid());
map.put("fansId", fans.getId());
return new Vo_msg(0, map);
} else {
return new Vo_msg(-1, "登录失败,服务器异常");
}
}
// 获取小程序二维码码1
@RequestMapping(value = "/getminicode1", method = RequestMethod.POST)
public @ResponseBody Vo_msg getMiniCode1(String path, int width) {
String result = WxMiniUtil.getMiniCode1(path, width, true, 0, 0, 0);
return new Vo_msg(0, result);
}
// 获取小程序二维码码2
@RequestMapping(value = "/getminicode2", method = RequestMethod.POST)
public @ResponseBody Vo_msg getMiniCode2(String scene, String page, int width) {
try {
scene = URLEncoder.encode("lcc", "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = WxMiniUtil.getMiniCode2(scene, page, width, true, 0, 0, 0);
return new Vo_msg(0, result);
}
// 获取小程序二维码码1
@RequestMapping(value = "/getminicode3", method = RequestMethod.POST)
public @ResponseBody Vo_msg getMiniCode3(String path, int width) {
String result = WxMiniUtil.getMiniCode3(path, width);
return new Vo_msg(0, result);
}
// 强制刷新Token
@RequestMapping(value = "/refresh_token", method = RequestMethod.POST)
public @ResponseBody String refreshToken(String pwd) {
if (!pwd.equals("nmdzpsmhs"))
return "error pwd";
return WxMiniUtil.refreshToken();
}
// 文章列表
@RequestMapping(value = "/getArticlesList", method = RequestMethod.GET)
@ResponseBody
public Object getArticlesList(Integer page, Integer pagesize) {
return new Vo_msg(0, articleService.getArticlesList(page, pagesize, "", null));
}
// 增加文章浏览数
@RequestMapping(value = "/addArticleViewCount", method = RequestMethod.GET)
@ResponseBody
public Object addArticleViewCount(Integer id) {
articleService.addArticleViewCount(id);
return new Vo_msg(0, "ok");
}
// 获取
@RequestMapping(value = "/getArticle", method = RequestMethod.GET)
@ResponseBody
public Object getArticle(Integer id) {
Articles article = articleService.getArticle(id);
if (article != null) {
return new Vo_msg(0, article);
} else {
return new Vo_msg(-1, "获取失败,数据为空");
}
}
// //用户隐藏了小程序时调用此接口,将会向用户发送一个小程序卡片,一天只发送一次。
// @RequestMapping(value = "/leave")
// @ResponseBody
// public String leaveMini(@RequestParam(value = "fansId") Integer fansId) {
// if (fansId<1) return "Failed: fansId < 1";
//
// Activity activitySetting = activityService.getActivitySetting();
// if (activitySetting != null && activitySetting.getStatus() ==
// ActivityStatus.进行中.getIndex() ) //抽奖活动进行中
// {
// Fans fans = fansDao.selectByPrimaryKey(fansId);
// if (null==fans) return "Failed:fans is null";
//
// //一天最多发送一次
// SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
// Date last = fans.getLastSendProgram();
// if(last!=null &&
// fmt.format(last).equals(fmt.format(DateTime.now().toDate()))) return "faild:
// allowed send one time everyday.";
//
// //发送小程序卡片
// String media_id = RedisUtil.get("zzhnc_leave_program_media_id");
// String thumb_url = RedisUtil.get("zzhnc_leave_program_thumb_url");
//
// if (null==media_id || media_id.length()<1 || null==thumb_url ||
// thumb_url.length()<1)
// {
// //获取默认首页的海报
// Example example = new Example(Postertemplet.class);
// example.createCriteria().andEqualTo("postertype",
// 2).andEqualTo("deleted",false);
// java.util.List<Postertemplet> posterList =
// postertempletDao.selectByExample(example);
// if (posterList.size() < 1) return "failed: Postertemplet Img not exists!";
// thumb_url = posterList.get(0).getImgurl();
//
// Vo_msg imgMsg = WxMiniUtil.uploadImage(thumb_url, false);
// if (imgMsg.code==0)
// {
// media_id = imgMsg.data.toString();
// RedisUtil.set("zzhnc_leave_program_media_id",media_id,60*60);
// RedisUtil.set("zzhnc_leave_program_thumb_url",thumb_url,60*60);
// }
// else
// {
// return "failed: upload media img failed,errmsg=" + imgMsg.msg;
// }
// }
//
//
// String result = WxMiniUtil.sendCustoMiniprogrampage(fans.getMiniopenid(),
// "买房就找万小二","pages/openAnimation/openAnimation", thumb_url, media_id);
// System.out.println("粉丝离开小程序时发送小程序卡片:" + result);
// fans.setLastSendProgram(DateTime.now().toDate());
// fansDao.updateByPrimaryKeySelective(fans);
// return result;
// }
// return "ok";
// }
//
// 清除小程序卡片的media_id的方法
@RequestMapping(value = "/cleanProgramMedia", method = RequestMethod.GET)
@ResponseBody
public String cleanProgramMedia(@RequestParam(value = "code") String code) {
if (!code.equals("nmdzpsmhs"))
return "error pwd";
RedisUtil.remove("zzhnc_leave_program_media_id");
RedisUtil.remove("zzhnc_leave_program_thumb_url");
return "ok";
}
@Autowired
BannerService bannerService;
@Autowired
GoodsService goodsService;
@RequestMapping(value = "/index", method = RequestMethod.GET)
@ResponseBody
public Object index(@RequestParam("openId") String openId) {
try {
Map<String, Object> result = Maps.newHashMap();
List<Banner> bannerList = bannerService.getAll();
List<Articles> homeData = articleService.getHomeData();
List<Goods> homeData2 = goodsService.getHomeData();
result.put("bannerList", bannerList);
result.put("articlesList", homeData);
result.put("goodsList", homeData2);
return new Vo_msg(0, result);
} catch (Exception e) {
return new Vo_msg(-1, "系统繁忙");
}
}
@RequestMapping(value = "/second", method = RequestMethod.GET)
@ResponseBody
public Object second(@RequestParam(value = "openId") String openId,
@RequestParam(value = "key", defaultValue = "", required = false) String key,
@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
@RequestParam(value = "size", defaultValue = "20", required = false) Integer size) {
try {
Map<String, Object> result = Maps.newHashMap();
List<Banner> bannerList = bannerService.getAll();
PageResults<Goods> seacrh = goodsService.seacrh(null, key, null, page, size);
List<Goods> rows = seacrh.getRows();
result.put("banner", bannerList);
result.put("data", rows);
return new Vo_msg(0, result);
} catch (Exception e) {
return new Vo_msg(-1, "系统繁忙");
}
}
/** 查看我的奖品 */
@RequestMapping(value = "/prizes/my", method = RequestMethod.GET)
@ResponseBody
public Object prizes(@RequestParam(value = "openId") String openId) {
Fans fans = fansService.getFansByMiniOpenid(openId);
if (fans != null) {
try {
List<Lotterylog> list = prizeService.getMyLotteryLog(fans.getId());
return new Vo_msg(0, list);
} catch (Exception e) {
e.printStackTrace();
}
}
return new Vo_msg(0, null);
}
/** 中奖 */
@RequestMapping(value = "/prizes/kill", method = RequestMethod.GET)
@ResponseBody
public Object prizesKill(@RequestParam(value = "openId") String openId) {
Fans fans = fansService.getFansByMiniOpenid(openId);
Integer logId = prizeService.randomKill(fans.getId(), 0);
if (logId == 0) {
return new Vo_msg(-1, null, "不好意思你没中奖");
}
if (logId == -1) {
return new Vo_msg(-1, null, "你已经中过奖了");
}
Lotterylog log = prizeService.getLog(logId);
return new Vo_msg(0, log);
}
/** 获取奖品列表 */
@RequestMapping(value = "/prizes/all", method = RequestMethod.GET)
@ResponseBody
public Object prizesList(@RequestParam(value = "openId") String openId) {
List<Prize> prizeList = activityService.getPrizeList();
return prizeList;
}
@Autowired
RedisTemplate<String, Long> redisTemplate;
/** 核销奖品 */
@ApiOperation( value = "二维码地址")
@RequestMapping(value = "/prizes/sqr", method = RequestMethod.GET)
@ResponseBody
public Object scanQr() {
long timeMillis = System.currentTimeMillis();
redisTemplate.opsForValue().set(PRIZE_KEY_TIME + timeMillis, timeMillis);
redisTemplate.expire(PRIZE_KEY_TIME + timeMillis, 300, TimeUnit.SECONDS);
return timeMillis;
}
/** 核销奖品 */
@RequestMapping(value = "/prizes/{id}", method = RequestMethod.GET)
@ResponseBody
public Object prizes(@RequestParam(value = "openId") String openId,
@RequestParam(value = "timeMillis") Long timeMillis, @RequestParam(value = "localMillis") Long localMillis,
@PathVariable(value = "id") Integer id) {
Fans fans = fansService.getFansByMiniOpenid(openId);
return prizeService.updateLotteryLog(id, fans.getId());
}
@RequestMapping(value = "/goods/{id}", method = RequestMethod.GET)
@ResponseBody
public Object goods(@RequestParam(value = "openId") String openId, @PathVariable(value = "id") Integer id) {
PageResults<Goods> seacrh = goodsService.seacrh(id, null, null, null, null);
return new Vo_msg(0, seacrh);
}
@RequestMapping(value = "/goods/share/{id}", method = RequestMethod.GET)
@ResponseBody
public Object goodsShare(@RequestParam(value = "openId") String openId, @PathVariable(value = "id") Integer id) {
if (id > 0) {
PageResults<Goods> seacrh = goodsService.seacrh(id, null, null, null, null);
Goods goods = seacrh.getRows().get(0);
goods.setShareCount(goods.getShareCount() + 1);
goodsService.update(goods);
}
Fans fans = fansService.getFansByMiniOpenid(openId);
fans.setShareCount(fans.getShareCount() + 1);
fansService.updateFans(fans);
return new Vo_msg(0, null);
}
}