'提交代码'

dev-discount
= 4 years ago
parent 94f2a489e0
commit eb3bed79fc
  1. 1
      hai-cweb/src/main/java/com/cweb/config/AuthConfig.java
  2. 85
      hai-cweb/src/main/java/com/cweb/controller/WechatController.java
  3. 3
      hai-service/src/main/java/com/hai/service/impl/HighUserServiceImpl.java

@ -105,6 +105,7 @@ public class AuthConfig implements WebMvcConfigurer {
.excludePathPatterns("/sms/sendSmsCode")
.excludePathPatterns("/sms/getSmsCode")
.excludePathPatterns("/user/login")
.excludePathPatterns("/user/getUnionId")
.excludePathPatterns("/wechat/*")
;
}

@ -8,6 +8,9 @@ import com.alibaba.fastjson.JSONObject;
import com.cweb.config.SessionKeyCache;
import com.cweb.config.SysConst;
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.ErrorHelp;
import com.hai.common.exception.SysCode;
@ -35,10 +38,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.*;
@Controller
@RequestMapping(value = "/wechat")
@ -68,7 +68,7 @@ public class WechatController {
final WxMaService wxService = WxMaConfiguration.getMaService();
WxMaJscode2SessionResult session = wxService.jsCode2SessionInfo(code);
//保存小程序用户登录的openId及sessionKey信息
SessionKeyCache.setData(session.getOpenid(), session.getSessionKey(), -1);
SessionKeyCache.setData(session.getOpenid(), session, -1);
JSONObject jo = new JSONObject();
jo.put("openId", session.getOpenid());
return ResponseMsgUtil.success(jo);
@ -126,11 +126,11 @@ public class WechatController {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, "");
}
//请求微信api,获取用户session_key以及openId
String sessionKey = SessionKeyCache.getData(openId);
WxMaJscode2SessionResult session = SessionKeyCache.getData(openId);
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())) {
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, "");
}
//请求微信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();
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(session.getSessionKey(), encryptedData, iv);
WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
if (userInfo == null) {
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) {
user = new HighUser();
user.setUnionId(userInfo.getUnionId());
user.setUnionId(session.getUnionid());
user.setOpenId(openId);
user.setRegTime(new Date());
user.setStatus(1);
@ -190,8 +194,10 @@ public class WechatController {
user.setName(userInfo.getNickName());
highUserService.insertUser(user);
} else {
user.setUnionId(session.getUnionid());
user.setHeaderImg(userInfo.getAvatarUrl());
user.setName(userInfo.getNickName());
user.setOpenId(userInfo.getOpenId());
highUserService.updateUser(user);
}
@ -200,7 +206,7 @@ public class WechatController {
user.setPassword(null);
highUserModel.setHighUser(user);
SessionObject so = new SessionObject(userInfo.getUnionId(), 1 , highUserModel);
SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel);
userCenter.save(request, response, so);
return ResponseMsgUtil.success(so);
@ -283,11 +289,13 @@ public class WechatController {
params.put("openid", openid);
params.put("lang", "zh_CN");
JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params);
System.out.println(userInfo.toJSONString());
if (StringUtils.isNotBlank(userInfo.getString("errcode"))) {
log.error("getH5UserInfo 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"));
if (user == null) {
@ -301,6 +309,7 @@ public class WechatController {
user.setName(userInfo.getString("nickname"));
highUserService.insertUser(user);
} else {
user.setOpenIdH5(openid);
user.setHeaderImg(userInfo.getString("headimgurl"));
user.setName(userInfo.getString("nickname"));
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);
}
}
}

@ -112,7 +112,7 @@ public class HighUserServiceImpl implements HighUserService {
if (highUser == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户");
}
if (StringUtils.isBlank(highUser.getPhone())) {
if (StringUtils.isNotBlank(highUser.getPhone())) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "已绑定手机号,请勿重复绑定");
}
@ -125,6 +125,7 @@ public class HighUserServiceImpl implements HighUserService {
highUser.setStatus(0);
highUserMapper.updateByPrimaryKey(highUser);
user.setGold(user.getGold() + highUser.getGold());
user.setHeaderImg(highUser.getHeaderImg());
user.setName(highUser.getName());
user.setOpenId(highUser.getOpenId());

Loading…
Cancel
Save