-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBindUserAction.java
More file actions
215 lines (171 loc) · 6.18 KB
/
Copy pathBindUserAction.java
File metadata and controls
215 lines (171 loc) · 6.18 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
package com.yaoba.app.web.m.weixin.mp;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import com.google.common.collect.Maps;
import com.opensymphony.xwork2.ActionSupport;
import com.yaoba.app.entity.account.User;
import com.yaoba.app.entity.weixin.WeixinUser;
import com.yaoba.app.service.account.UserManager;
import com.yaoba.app.service.system.ParameterManager;
import com.yaoba.app.service.weixin.WeixinUserManager;
import com.yaoba.app.service.weixin.WxMpManager;
/**
*
* @author yaoba
* @version 创建时间:2016-2-28 下午03:14:05
*/
@Results({ @Result(name = "reload", location = "bind-user.action", type = "redirect") })
public class BindUserAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1412880463907879750L;
private WeixinUserManager weixinUserManager;
private UserManager userManager;
private ParameterManager parameterManager;
private List<User> userList;
private String loginName;
private String password;
private String userId;
/**
* OAuth2.0验证
*
* @throws Exception
*/
public void oauth2() throws Exception {
System.out.println("oauth2");
WxMpService wxMpService = WxMpManager.getWxMpService();
HttpServletRequest request = Struts2Utils.getRequest();
HttpServletResponse response = Struts2Utils.getResponse();
String code = request.getParameter("code");
WxMpOAuth2AccessToken token = wxMpService.oauth2getAccessToken(code);
String userId = token.getOpenId();
Struts2Utils.getSession().setAttribute(WeixinUserManager.WEIXIN_USER_ID, userId);
System.out.println(userId);
String redirectUrl = request.getParameter("redirectUrl");
System.out.println("oauth2:redirectUrl=" + redirectUrl);
response.sendRedirect(redirectUrl);
}
@Override
public String execute() throws Exception {
String weixinUserId = (String) Struts2Utils.getSession().getAttribute(WeixinUserManager.WEIXIN_USER_ID);
if (weixinUserId == null) {
// 取微信的用户ID,如果ID为空去OAuth2验证取用户的ID
WxMpService wxMpService = WxMpManager.getWxMpService();
String systemUrl = parameterManager.getParameterValue(ParameterManager.SYSTEM_URL);
String successUrl = "./bind-user.action";
String redirectUrl = systemUrl + "m/weixin/mp/bind-user!oauth2.action?redirectUrl=" + URLEncoder.encode(successUrl, "utf-8");
System.out.println("execute1");
System.out.println(redirectUrl);
String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, "snsapi_base", "state");
System.out.println("execute2");
System.out.println(url);
HttpServletResponse response = Struts2Utils.getResponse();
response.sendRedirect(url);
return null;
}
WeixinUser weixinUser = weixinUserManager.findUnique(Restrictions.eq("id", weixinUserId));
if (weixinUser == null) {
// 系统里也要创建一微信用户
WxMpService wxMpService = WxMpManager.getWxMpService();
WxMpUser wxMpUser = wxMpService.userInfo(weixinUserId, "zh_CN");
weixinUser = new WeixinUser();
weixinUser.setId(wxMpUser.getOpenId());
weixinUser.setName(wxMpUser.getNickname());
weixinUser.setSex(wxMpUser.getSex());
weixinUser.setCountry(wxMpUser.getCountry());
weixinUser.setProvince(wxMpUser.getProvince());
weixinUser.setCity(wxMpUser.getCity());
weixinUserManager.insert(weixinUser);
}
userList = weixinUser.getUserList();
return SUCCESS;
}
/**
* 绑定保存
*
* @throws Exception
*/
public void save() throws Exception {
Map<String, Object> json = Maps.newHashMap();
json.put("success", true);
String weixinUserId = (String) Struts2Utils.getSession().getAttribute(WeixinUserManager.WEIXIN_USER_ID);
if (weixinUserId == null) {
json.put("success", false);
json.put("message", "请重新登录。");
Struts2Utils.renderJson(json);
return;
}
User user = userManager.findByLoginName(StringUtils.upperCase(loginName));
if (user == null || !StringUtils.equals(user.getPassword(), password)) {
json.put("success", false);
json.put("message", "登录名或密码错误。");
Struts2Utils.renderJson(json);
return;
}
WeixinUser weixinUser = weixinUserManager.findUnique(Restrictions.eq("id", weixinUserId));
if (!user.getWeixinUserList().contains(weixinUser)) {
user.getWeixinUserList().add(weixinUser);
weixinUser.getUserList().add(user);
userManager.save(user);
}
System.out.println(user.getWeixinUserNames());
json.put("message", "绑定成功");
Struts2Utils.renderJson(json);
}
/**
* 解绑
*
* @throws Exception
*/
public String unbundling() throws Exception {
String weixinUserId = (String) Struts2Utils.getSession().getAttribute(WeixinUserManager.WEIXIN_USER_ID);
if (weixinUserId == null) {
return execute();
}
User user = userManager.get(userId);
WeixinUser weixinUser = weixinUserManager.get(weixinUserId);
if (user.getWeixinUserList().contains(weixinUser)) {
user.getWeixinUserList().remove(weixinUser);
weixinUser.getUserList().remove(user);
userManager.save(user);
}
return "reload";
}
@Autowired
public void setWeixinUserManager(WeixinUserManager weixinUserManager) {
this.weixinUserManager = weixinUserManager;
}
@Autowired
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
@Autowired
public void setParameterManager(ParameterManager parameterManager) {
this.parameterManager = parameterManager;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserId(String userId) {
this.userId = userId;
}
public List<User> getUserList() {
return userList;
}
}