提交代码

dev-discount
胡锐 3 years ago
parent defa041e83
commit 767826297b
  1. 4
      hai-cweb/src/main/java/com/cweb/controller/HighUserCardController.java
  2. 8
      hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java
  3. 2
      hai-cweb/src/main/java/com/cweb/controller/SendSmsController.java
  4. 2
      hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java
  5. 297
      hai-cweb/src/main/java/com/cweb/controller/WechatController.java

@ -142,7 +142,7 @@ public class HighUserCardController {
} }
// 获取手机号验证码 // 获取手机号验证码
String phoneSmsCode = (String) redisUtil.get(body.getString("phone")); String phoneSmsCode = (String) redisUtil.get("SMS_"+body.getString("phone"));
// 验证码校验 // 验证码校验
if (StringUtils.isNotBlank(phoneSmsCode) && Objects.equals(phoneSmsCode,body.getString("smsCode"))) { if (StringUtils.isNotBlank(phoneSmsCode) && Objects.equals(phoneSmsCode,body.getString("smsCode"))) {
@ -153,7 +153,7 @@ public class HighUserCardController {
userCard.setType(UserCardType.type1.getType()); userCard.setType(UserCardType.type1.getType());
highUserCardService.editCard(userCard); highUserCardService.editCard(userCard);
redisUtil.del(body.getString("phone")); redisUtil.del("SMS_"+body.getString("phone"));
// 定义个人所有数据 // 定义个人所有数据
HighUserModel highUserModel = new HighUserModel(); HighUserModel highUserModel = new HighUserModel();

@ -59,13 +59,13 @@ public class HighUserCommonController {
try { try {
// 获取手机号验证码 // 获取手机号验证码
String phoneSmsCode = (String) redisUtil.get(phone); String phoneSmsCode = (String) redisUtil.get("SMS_"+phone);
// 校验验证码 // 校验验证码
if (StringUtils.isBlank(phoneSmsCode) || !Objects.equals(phoneSmsCode,code)) { if (StringUtils.isBlank(phoneSmsCode) || !Objects.equals(phoneSmsCode,code)) {
log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); log.error("HighUserController --> bindUserPhone() error!", "验证码错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误");
} }
redisUtil.del(phone); redisUtil.del("SMS_"+phone);
HighUser user = highUserService.findByPhone(phone); HighUser user = highUserService.findByPhone(phone);
if(user == null) { if(user == null) {
@ -87,7 +87,7 @@ public class HighUserCommonController {
HighUser detailData = highUserService.getDetailDataByUser(user.getId()); HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null); detailData.setPassword(null);
highUserModel.setHighUser(detailData); highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(StringUtils.isNotBlank(user.getUnionId())?user.getUnionId():detailData.getId().toString(), 1 , highUserModel); SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel);
userCenter.save(request, response, so); userCenter.save(request, response, so);
return ResponseMsgUtil.success(so); return ResponseMsgUtil.success(so);
} catch (Exception e) { } catch (Exception e) {
@ -145,7 +145,7 @@ public class HighUserCommonController {
HighUser detailData = highUserService.getDetailDataByUser(user.getId()); HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null); detailData.setPassword(null);
highUserModel.setHighUser(detailData); highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(StringUtils.isNotBlank(user.getUnionId())?user.getUnionId():detailData.getId().toString(), 1 , highUserModel); SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel);
userCenter.save(request, response, so); userCenter.save(request, response, so);
return ResponseMsgUtil.success(so); return ResponseMsgUtil.success(so);
} catch (Exception e) { } catch (Exception e) {

@ -93,7 +93,7 @@ public class SendSmsController {
JSONObject object = HttpsUtils.doPostSendSms(url, map); JSONObject object = HttpsUtils.doPostSendSms(url, map);
String code = object.getString("code"); String code = object.getString("code");
if (code.equals("000000")) { if (code.equals("000000")) {
redisUtil.set(phone,smsCode, 60*10); redisUtil.set("SMS_"+phone,smsCode, 60*10);
return ResponseMsgUtil.success("发送成功"); return ResponseMsgUtil.success("发送成功");
} }
return ResponseMsgUtil.success(object); return ResponseMsgUtil.success(object);

@ -170,7 +170,7 @@ public class SmsContentController {
e.printStackTrace(); e.printStackTrace();
} }
} }
redisUtil.set(phone,smsCode, 60*10); redisUtil.set("SMS_"+phone,smsCode, 60*10);
return ResponseMsgUtil.success("发送成功"); return ResponseMsgUtil.success("发送成功");
} catch (Exception e) { } catch (Exception e) {

@ -87,36 +87,6 @@ public class WechatController {
} }
} }
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "小程序授权用户登录接口")
public ResponseData login(@RequestParam(value = "openId", required = true) String openId,
HttpServletRequest request, HttpServletResponse response) {
try {
//校验code,openId不能同时为空
if (StringUtils.isEmpty(openId)) {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, "");
}
HighUser highUser = highUserService.findByOpenId(openId);
if (highUser == null) {
log.error("login error!", "未找到用户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, "");
}
// 定义个人所有数据
HighUserModel highUserModel = new HighUserModel();
HighUser detailData = highUserService.getDetailDataByUser(highUser.getId());
detailData.setPassword(null);
highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(highUser.getUnionId(), 1 , highUserModel);
userCenter.save(request, response, so);
return ResponseMsgUtil.success(so);
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/loginByPhone", method = RequestMethod.GET) @RequestMapping(value = "/loginByPhone", method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ApiOperation(value = "小程序根据手机号登录或注册") @ApiOperation(value = "小程序根据手机号登录或注册")
@ -148,18 +118,8 @@ public class WechatController {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败");
} }
HighUser user = highUserService.findByOpenId(session.getOpenid());
if (user != null) {
if (StringUtils.isBlank(user.getPhone())) {
if (highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()) != null) {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.COMMON_ERROR, "手机号已被绑定,请更换后重新授权");
}
user.setPhone(phoneNoInfo.getPurePhoneNumber());
highUserService.updateUser(user);
}
} else {
// 根据手机号查询用户 // 根据手机号查询用户
user = highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()); HighUser user = highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber());
if (user == null) { if (user == null) {
user = new HighUser(); user = new HighUser();
user.setName("用户" + IDGenerator.nextId(5)); user.setName("用户" + IDGenerator.nextId(5));
@ -176,9 +136,7 @@ public class WechatController {
user.setInfoCompleteStatus(1); user.setInfoCompleteStatus(1);
highUserService.insertUser(user); highUserService.insertUser(user);
} else { } else {
if (StringUtils.isBlank(user.getName())) { if (session.getOpenid() == null) {
user.setName("用户" + IDGenerator.nextId(5));
}
user.setOpenId(session.getOpenid()); user.setOpenId(session.getOpenid());
user.setUnionId(session.getUnionid()); user.setUnionId(session.getUnionid());
highUserService.updateUser(user); highUserService.updateUser(user);
@ -190,62 +148,13 @@ public class WechatController {
HighUser detailData = highUserService.getDetailDataByUser(user.getId()); HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null); detailData.setPassword(null);
highUserModel.setHighUser(detailData); highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel);
userCenter.save(request, response, so);
return ResponseMsgUtil.success(so);
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/getPhoneNumber", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "小程序获取用户手机号码")
public ResponseData getPhoneNumber(@RequestParam(value = "encryptedData", required = true) String encryptedData,
@RequestParam(value = "iv", required = true) String iv,
@RequestParam(value = "openId", required = true) String openId,
HttpServletRequest request, HttpServletResponse response) {
try {
log.error("origin encryptedData:" + encryptedData + ";iv:" + iv);
//校验openId不能为空
//encryptedData,iv与unionId不能同时为空
encryptedData = encryptedData.replace(" ", "+");
iv = iv.replace(" ", "+");
log.error("dest encryptedData:" + encryptedData + ";iv:" + iv);
if (StringUtils.isEmpty(openId)
|| StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, "");
}
//请求微信api,获取用户session_key以及openId
Object skObject = redisUtil.hget(WX_OPENID_SESSION_REDIS,openId);
if (skObject == null){
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_LOGIN_ERROR);
}
WxMaJscode2SessionResult session = (WxMaJscode2SessionResult)skObject;
final WxMaService wxService = WxMaConfiguration.getMaService();
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解析失败");
}
HighUser user = highUserService.bindUserPhone(phoneNoInfo.getPurePhoneNumber(), request);
// 定义个人所有数据
HighUserModel highUserModel = new HighUserModel();
HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null);
highUserModel.setHighUser(detailData);
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);
} catch (Exception e) { } catch (Exception e) {
return ResponseMsgUtil.exception(e); return ResponseMsgUtil.exception(e);
} }
} }
@ -257,12 +166,16 @@ public class WechatController {
@RequestParam(value = "openId", required = true) String openId, @RequestParam(value = "openId", required = true) String openId,
HttpServletRequest request, HttpServletResponse response) { HttpServletRequest request, HttpServletResponse response) {
try { try {
// 用户
SessionObject sessionObject = userCenter.getSessionObject(request);
HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();
//校验openId不能为空 //校验openId不能为空
//encryptedData,iv与unionId不能同时为空 //encryptedData,iv与unionId不能同时为空
encryptedData = encryptedData.replace(" ", "+"); encryptedData = encryptedData.replace(" ", "+");
iv = iv.replace(" ", "+"); iv = iv.replace(" ", "+");
if (StringUtils.isEmpty(openId) if (StringUtils.isEmpty(openId) || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) {
|| StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) {
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
@ -277,22 +190,11 @@ public class WechatController {
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解析失败");
} }
if (StringUtils.isBlank(session.getUnionid())) {
throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "获取UnionId失败"); HighUser user = highUserService.findByUserId(userInfoModel.getHighUser().getId());
}
HighUser user = highUserService.findByOpenId(session.getOpenid());
if (user == null) { if (user == null) {
user = new HighUser(); throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.COMMON_ERROR, "未找到用户信息");
user.setUnionId(session.getUnionid()); }
user.setOpenId(openId);
user.setRegTime(new Date());
user.setStatus(1);
user.setGold(0);
user.setHeaderImg(userInfo.getAvatarUrl());
user.setName(userInfo.getNickName());
user.setInfoCompleteStatus(2);
highUserService.insertUser(user);
} else {
// 用户信息完整状态 // 用户信息完整状态
// 0:完整 // 0:完整
// 1:需要填写用户信息 // 1:需要填写用户信息
@ -304,19 +206,20 @@ public class WechatController {
user.setInfoCompleteStatus(2); user.setInfoCompleteStatus(2);
} }
} }
user.setUnionId(session.getUnionid());
user.setHeaderImg(userInfo.getAvatarUrl());
user.setName(userInfo.getNickName()); user.setName(userInfo.getNickName());
user.setOpenId(userInfo.getOpenId()); user.setHeaderImg(userInfo.getAvatarUrl());
highUserService.updateUser(user); highUserService.updateUser(user);
}
// 定义个人所有数据 // 定义个人所有数据
HighUserModel highUserModel = new HighUserModel(); HighUserModel highUserModel = new HighUserModel();
HighUser detailData = highUserService.getDetailDataByUser(user.getId()); HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null); detailData.setPassword(null);
highUserModel.setHighUser(detailData); highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); if (user.getPhone() == null) {
log.error("login error!", "未绑定手机号");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未绑定手机号");
}
SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel);
userCenter.save(request, response, so); userCenter.save(request, response, so);
return ResponseMsgUtil.success(so); return ResponseMsgUtil.success(so);
@ -326,39 +229,6 @@ public class WechatController {
} }
} }
@RequestMapping(value = "/getH5Code", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "【H5】获取Code")
public ResponseData getH5Code() {
try {
Map<String, Object> params = new LinkedHashMap<>();
params.put("appid", SysConst.getSysConfig().getWxH5AppId());
params.put("redirect_uri", URLEncoder.encode("https://hsgcs.dctpay.com/H5/index.html#/", "utf-8"));
//params.put("redirect_uri", "https://hsgcs.dctpay.com/H5/index.html#/");
params.put("response_type", "code");
params.put("scope", "snsapi_userinfo");
params.put("state", "STATE");
String apiUrl = "https://open.weixin.qq.com/connect/oauth2/authorize";
StringBuffer param = new StringBuffer();
int i = 0;
for (String key : params.keySet()) {
if (i == 0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(params.get(key));
i++;
}
apiUrl += param + "#wechat_redirect";
return ResponseMsgUtil.success(apiUrl);
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET) @RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET)
@ResponseBody @ResponseBody
@ApiOperation(value = "【H5】获取AccessToken") @ApiOperation(value = "【H5】获取AccessToken")
@ -375,133 +245,4 @@ public class WechatController {
return ResponseMsgUtil.exception(e); return ResponseMsgUtil.exception(e);
} }
} }
@RequestMapping(value = "/getH5UserInfo", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "【H5】获取用户信息")
public ResponseData getH5UserInfo(@RequestParam(value = "code", required = true) String code,
HttpServletRequest request, HttpServletResponse response) {
try {
// 获取access_token
Map<String, Object> params = new HashMap<>();
params.put("appid", SysConst.getSysConfig().getWxH5AppId());
params.put("secret", SysConst.getSysConfig().getWxH5AppSecret());
params.put("code", code);
params.put("grant_type", "authorization_code");
JSONObject jsonObject = HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params);
String accessToken = jsonObject.getString("access_token");
String openid = jsonObject.getString("openid");
if (StringUtils.isBlank(accessToken) || StringUtils.isBlank(openid)) {
log.error("getH5UserInfo error!", "获取微信access_token失败" );
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败");
}
params.clear();
params.put("access_token", accessToken);
params.put("openid", openid);
params.put("lang", "zh_CN");
JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params);
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) {
user = new HighUser();
user.setUnionId(userInfo.getString("unionid"));
user.setOpenIdH5(openid);
user.setRegTime(new Date());
user.setStatus(1);
user.setGold(0);
user.setHeaderImg(userInfo.getString("headimgurl"));
user.setName(userInfo.getString("nickname"));
user.setInfoCompleteStatus(2);
highUserService.insertUser(user);
} else {
// 用户信息完整状态
// 0:完整
// 1:需要填写用户信息
// 2:需要填写手机号
if (user.getInfoCompleteStatus() == 1) {
if (StringUtils.isNotBlank(user.getPhone())) {
user.setInfoCompleteStatus(0);
} else {
user.setInfoCompleteStatus(2);
}
}
user.setOpenIdH5(openid);
user.setHeaderImg(userInfo.getString("headimgurl"));
user.setName(userInfo.getString("nickname"));
highUserService.updateUser(user);
}
// 定义个人所有数据
HighUserModel highUserModel = new HighUserModel();
HighUser detailData = highUserService.getDetailDataByUser(user.getId());
detailData.setPassword(null);
highUserModel.setHighUser(detailData);
SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel);
userCenter.save(request, response, so);
return ResponseMsgUtil.success(so);
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@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);
}
}
} }

Loading…
Cancel
Save