|
|
@ -8,6 +8,9 @@ import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.cweb.config.SessionKeyCache; |
|
|
|
import com.cweb.config.SessionKeyCache; |
|
|
|
import com.cweb.config.SysConst; |
|
|
|
import com.cweb.config.SysConst; |
|
|
|
import com.cweb.config.WxMaConfiguration; |
|
|
|
import com.cweb.config.WxMaConfiguration; |
|
|
|
|
|
|
|
import com.github.pagehelper.Page; |
|
|
|
|
|
|
|
import com.github.pagehelper.PageHelper; |
|
|
|
|
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import com.hai.common.exception.ErrorCode; |
|
|
|
import com.hai.common.exception.ErrorCode; |
|
|
|
import com.hai.common.exception.ErrorHelp; |
|
|
|
import com.hai.common.exception.ErrorHelp; |
|
|
|
import com.hai.common.exception.SysCode; |
|
|
|
import com.hai.common.exception.SysCode; |
|
|
@ -35,10 +38,7 @@ import javax.annotation.Resource; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.*; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Controller |
|
|
|
@Controller |
|
|
|
@RequestMapping(value = "/wechat") |
|
|
|
@RequestMapping(value = "/wechat") |
|
|
@ -68,7 +68,7 @@ public class WechatController { |
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
WxMaJscode2SessionResult session = wxService.jsCode2SessionInfo(code); |
|
|
|
WxMaJscode2SessionResult session = wxService.jsCode2SessionInfo(code); |
|
|
|
//保存小程序用户登录的openId及sessionKey信息
|
|
|
|
//保存小程序用户登录的openId及sessionKey信息
|
|
|
|
SessionKeyCache.setData(session.getOpenid(), session.getSessionKey(), -1); |
|
|
|
SessionKeyCache.setData(session.getOpenid(), session, -1); |
|
|
|
JSONObject jo = new JSONObject(); |
|
|
|
JSONObject jo = new JSONObject(); |
|
|
|
jo.put("openId", session.getOpenid()); |
|
|
|
jo.put("openId", session.getOpenid()); |
|
|
|
return ResponseMsgUtil.success(jo); |
|
|
|
return ResponseMsgUtil.success(jo); |
|
|
@ -126,11 +126,11 @@ public class WechatController { |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
|
|
|
} |
|
|
|
} |
|
|
|
//请求微信api,获取用户session_key以及openId
|
|
|
|
//请求微信api,获取用户session_key以及openId
|
|
|
|
String sessionKey = SessionKeyCache.getData(openId); |
|
|
|
WxMaJscode2SessionResult session = SessionKeyCache.getData(openId); |
|
|
|
|
|
|
|
|
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
|
|
|
|
|
|
|
|
WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); |
|
|
|
WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(session.getSessionKey(), encryptedData, iv); |
|
|
|
if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { |
|
|
|
if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
|
|
|
} |
|
|
|
} |
|
|
@ -170,18 +170,22 @@ public class WechatController { |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); |
|
|
|
} |
|
|
|
} |
|
|
|
//请求微信api,获取用户session_key以及openId
|
|
|
|
//请求微信api,获取用户session_key以及openId
|
|
|
|
String sessionKey = SessionKeyCache.getData(openId); |
|
|
|
WxMaJscode2SessionResult session = SessionKeyCache.getData(openId); |
|
|
|
|
|
|
|
System.out.println(JSONObject.toJSONString(session)); |
|
|
|
|
|
|
|
|
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
final WxMaService wxService = WxMaConfiguration.getMaService(); |
|
|
|
|
|
|
|
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(session.getSessionKey(), encryptedData, iv); |
|
|
|
|
|
|
|
|
|
|
|
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv); |
|
|
|
|
|
|
|
if (userInfo == null) { |
|
|
|
if (userInfo == null) { |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
HighUser user = highUserService.findByUnionId(userInfo.getUnionId()); |
|
|
|
if (StringUtils.isBlank(session.getUnionid())) { |
|
|
|
|
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "获取UnionId失败"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
HighUser user = highUserService.findByOpenId(session.getOpenid()); |
|
|
|
if (user == null) { |
|
|
|
if (user == null) { |
|
|
|
user = new HighUser(); |
|
|
|
user = new HighUser(); |
|
|
|
user.setUnionId(userInfo.getUnionId()); |
|
|
|
user.setUnionId(session.getUnionid()); |
|
|
|
user.setOpenId(openId); |
|
|
|
user.setOpenId(openId); |
|
|
|
user.setRegTime(new Date()); |
|
|
|
user.setRegTime(new Date()); |
|
|
|
user.setStatus(1); |
|
|
|
user.setStatus(1); |
|
|
@ -190,8 +194,10 @@ public class WechatController { |
|
|
|
user.setName(userInfo.getNickName()); |
|
|
|
user.setName(userInfo.getNickName()); |
|
|
|
highUserService.insertUser(user); |
|
|
|
highUserService.insertUser(user); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
user.setUnionId(session.getUnionid()); |
|
|
|
user.setHeaderImg(userInfo.getAvatarUrl()); |
|
|
|
user.setHeaderImg(userInfo.getAvatarUrl()); |
|
|
|
user.setName(userInfo.getNickName()); |
|
|
|
user.setName(userInfo.getNickName()); |
|
|
|
|
|
|
|
user.setOpenId(userInfo.getOpenId()); |
|
|
|
highUserService.updateUser(user); |
|
|
|
highUserService.updateUser(user); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -200,7 +206,7 @@ public class WechatController { |
|
|
|
user.setPassword(null); |
|
|
|
user.setPassword(null); |
|
|
|
highUserModel.setHighUser(user); |
|
|
|
highUserModel.setHighUser(user); |
|
|
|
|
|
|
|
|
|
|
|
SessionObject so = new SessionObject(userInfo.getUnionId(), 1 , highUserModel); |
|
|
|
SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); |
|
|
|
userCenter.save(request, response, so); |
|
|
|
userCenter.save(request, response, so); |
|
|
|
return ResponseMsgUtil.success(so); |
|
|
|
return ResponseMsgUtil.success(so); |
|
|
|
|
|
|
|
|
|
|
@ -283,11 +289,13 @@ public class WechatController { |
|
|
|
params.put("openid", openid); |
|
|
|
params.put("openid", openid); |
|
|
|
params.put("lang", "zh_CN"); |
|
|
|
params.put("lang", "zh_CN"); |
|
|
|
JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params); |
|
|
|
JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params); |
|
|
|
System.out.println(userInfo.toJSONString()); |
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(userInfo.getString("errcode"))) { |
|
|
|
if (StringUtils.isNotBlank(userInfo.getString("errcode"))) { |
|
|
|
log.error("getH5UserInfo error!", "获取微信用户信息失败"); |
|
|
|
log.error("getH5UserInfo error!", "获取微信用户信息失败"); |
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信用户信息失败"); |
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信用户信息失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (StringUtils.isBlank(userInfo.getString("unionid"))) { |
|
|
|
|
|
|
|
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "获取UnionId失败"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
HighUser user = highUserService.findByUnionId(userInfo.getString("unionid")); |
|
|
|
HighUser user = highUserService.findByUnionId(userInfo.getString("unionid")); |
|
|
|
if (user == null) { |
|
|
|
if (user == null) { |
|
|
@ -301,6 +309,7 @@ public class WechatController { |
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
highUserService.insertUser(user); |
|
|
|
highUserService.insertUser(user); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
user.setOpenIdH5(openid); |
|
|
|
user.setHeaderImg(userInfo.getString("headimgurl")); |
|
|
|
user.setHeaderImg(userInfo.getString("headimgurl")); |
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
user.setName(userInfo.getString("nickname")); |
|
|
|
highUserService.updateUser(user); |
|
|
|
highUserService.updateUser(user); |
|
|
@ -320,4 +329,54 @@ public class WechatController { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/getUnionId", method = RequestMethod.GET) |
|
|
|
|
|
|
|
@ResponseBody |
|
|
|
|
|
|
|
@ApiOperation(value = "刷取unionid") |
|
|
|
|
|
|
|
public ResponseData getUnionId(HttpServletRequest request, HttpServletResponse response) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// 获取access_token
|
|
|
|
|
|
|
|
Map<String, Object> params = new HashMap<>(); |
|
|
|
|
|
|
|
params.put("appid", SysConst.getSysConfig().getWxAppId()); |
|
|
|
|
|
|
|
params.put("secret", SysConst.getSysConfig().getWxAppSecret()); |
|
|
|
|
|
|
|
params.put("grant_type", "client_credential"); |
|
|
|
|
|
|
|
JSONObject jsonObject = HttpsUtils.doGet("https://api.weixin.qq.com/cgi-bin/token", params); |
|
|
|
|
|
|
|
System.out.println(jsonObject.toJSONString()); |
|
|
|
|
|
|
|
String accessToken = jsonObject.getString("access_token"); |
|
|
|
|
|
|
|
if (StringUtils.isBlank(accessToken)) { |
|
|
|
|
|
|
|
log.error("getH5UserInfo error!", "获取微信access_token失败" ); |
|
|
|
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
params.clear(); |
|
|
|
|
|
|
|
List<Map<String,Object>> userMapList = new ArrayList<>(); |
|
|
|
|
|
|
|
Map<String,Object> userMap; |
|
|
|
|
|
|
|
int pageNum = 1; |
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
|
|
userMapList.clear();; |
|
|
|
|
|
|
|
PageHelper.startPage(pageNum, 100); |
|
|
|
|
|
|
|
PageInfo<HighUser> pageInfo = new PageInfo<>(highUserService.getListUser(new HashMap<>())); |
|
|
|
|
|
|
|
if (pageInfo.getList().size() > 0) { |
|
|
|
|
|
|
|
for (HighUser user : pageInfo.getList()) { |
|
|
|
|
|
|
|
userMap = new HashMap<>(); |
|
|
|
|
|
|
|
userMap.put("openid", user.getOpenId()); |
|
|
|
|
|
|
|
userMap.put("lang", "zh_CN"); |
|
|
|
|
|
|
|
userMapList.add(userMap); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.put("user_list", userMapList); |
|
|
|
|
|
|
|
System.out.println(JSONObject.toJSONString(params)); |
|
|
|
|
|
|
|
JSONObject userInfo = HttpsUtils.doPost("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" + accessToken, JSONObject.toJSONString(params)); |
|
|
|
|
|
|
|
System.out.println(userInfo.toJSONString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
pageNum++; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResponseMsgUtil.success("处理成功"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
return ResponseMsgUtil.exception(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|