WechatMod.java
5.09 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
package vion.third;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import vion.dto.WxDTO;
import vion.event.WxDTOEvent;
import java.util.List;
/**
* @author HlQ
* @date 2023/11/21
*/
@Component
@RequiredArgsConstructor
@Slf4j
public class WechatMod {
private final WxMpService wxMpService;
private final ApplicationEventPublisher publisher;
/**
* 用于微信公众号内提交单子。
* 用户已关注公众号,直接获取 openid 即可。
*
* @param code 授权码
* @return java.lang.Object
*/
public Object getOpenid(String code) {
try {
WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
String openid = userInfo.getOpenid();
String nickname = userInfo.getNickname();
return MapUtil.<String, String>builder()
.put("nickname", nickname)
.put("openid", openid).build();
} catch (WxErrorException e) {
log.error("调用微信接口异常!", e);
}
return new ModelAndView("weChatError");
}
/**
* 生成微信网页授权链接用于 pc 端故障保修、点位设计等页面,绑定openid
*
* @param id 生成单子的 id
* @param flag 1:故障保修 2:点位设计
* @return java.lang.String
*/
public String genCallbackQRCode(Long id, Integer flag) {
String redirectUri = StrUtil.format("https://yunwei.vionyun.com/yunwei/api/getFollowingOpenid?id={}&flag={}", id, flag);
return wxMpService.getOAuth2Service().buildAuthorizationUrl(redirectUri, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
}
/**
* 用户提交单子,需要判断用户是否关注公众号。
* 关注公众号的用户,直接获取 openid,返回前端,前端提交工单(或其他单子)时带上 openid。
* 未关注公众号的用户,直接返回一个页面,提示用户关注公众号。
*
* @param code 授权码
* @return java.lang.Object
*/
public Object getFollowingOpenid(Long id, Integer flag, String code) {
try {
WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
String openid = userInfo.getOpenid();
WxMpUser wxMpUser = wxMpService.getUserService().userInfo(openid);
if (!wxMpUser.getSubscribe()) {
return new ModelAndView("weChatNeedAttention");
}
String nickname = userInfo.getNickname();
WxDTO wxDTO = WxDTO.builder().nickname(nickname).openid(openid).id(id).flag(flag).build();
WxDTOEvent wxDTOEvent = new WxDTOEvent(this, wxDTO);
publisher.publishEvent(wxDTOEvent);
return new ModelAndView("weChatSuccess");
} catch (WxErrorException e) {
log.error("调用微信接口异常!", e);
}
return new ModelAndView("weChatError");
}
/**
* 点位设计详情页获取绑定微信二维码
*
* @param uuid 点位记录 uuid
* @return java.lang.String
*/
public String genBindOpenidQRCode(String uuid) {
String redirectUri = StrUtil.format("https://yunwei.vionyun.com/yunwei/api/point/getBindOpenid?uuid={}&rid={}", uuid, IdUtil.fastSimpleUUID());
return wxMpService.getOAuth2Service().buildAuthorizationUrl(redirectUri, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null);
}
public String sendMsg(String templateId, String openId, List<WxMpTemplateData> wxMpTemplateDataList, String url) {
try {
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openId)
.templateId(templateId)
.data(wxMpTemplateDataList)
.url(url)
.build();
String s = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
log.info("微信消息推送成功,msgid:{}", s);
return "微信公众号消息推送成功";
} catch (WxErrorException e) {
log.error("调用微信接口发送模板消息异常!", e);
}
return "微信公众号消息推送失败";
}
}