修改微信登陆相关功能模块

This commit is contained in:
XinYi Song 2021-12-01 14:31:51 +08:00
parent 8886bb2eb8
commit ca93e2cf3c
4 changed files with 22 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import com.xkrs.model.entity.WeChatCode;
import com.xkrs.utils.HttpClientUtil; import com.xkrs.utils.HttpClientUtil;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -29,12 +30,11 @@ public class WeChatController {
private WeChatCodeDao weChatCodeDao; private WeChatCodeDao weChatCodeDao;
@GetMapping("/callback") @GetMapping("/callback")
public String callback(String code,String state) throws Exception { public String callback(@RequestParam("code") String code, String state) throws Exception {
Locale locale = LocaleContextHolder.getLocale(); Locale locale = LocaleContextHolder.getLocale();
try{ try{
//1.获取code值,临时票据,类似于验证码 //1.获取code值,临时票据,类似于验证码
System.out.println(code); System.out.println(code);
//2.拿着code请求微信固定的地址,得到两个值access_token和openid //2.拿着code请求微信固定的地址,得到两个值access_token和openid
String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" + String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
"?appid=%s" + "?appid=%s" +
@ -71,11 +71,13 @@ public class WeChatController {
String userInfo = HttpClientUtil.doGet(userInfoUrl); String userInfo = HttpClientUtil.doGet(userInfoUrl);
//获取返回的userInfo字符串的扫描人信息 //获取返回的userInfo字符串的扫描人信息
JSONObject jsonObject1 = JSON.parseObject(userInfo); JSONObject jsonObject1 = JSON.parseObject(userInfo);
System.out.println("------>" + jsonObject1);
//昵称 //昵称
String nickname =(String) jsonObject1.get("nickname"); String nickname =(String) jsonObject1.get("nickname");
//头像 //头像
String headimgurl =(String) jsonObject1.get("headimgurl"); String headimgurl =(String) jsonObject1.get("headimgurl");
Integer sex = (Integer) jsonObject1.get("sex"); Integer sex = (Integer) jsonObject1.get("sex");
String unionid = (String) jsonObject1.get("unionid");
WeChatCode weChatCode = new WeChatCode(); WeChatCode weChatCode = new WeChatCode();
@ -83,6 +85,7 @@ public class WeChatController {
weChatCode.setNickName(nickname); weChatCode.setNickName(nickname);
weChatCode.setUserPhoto(headimgurl); weChatCode.setUserPhoto(headimgurl);
weChatCode.setUserSex(sex.toString()); weChatCode.setUserSex(sex.toString());
weChatCode.setUnid(unionid);
weChatCodeDao.save(weChatCode); weChatCodeDao.save(weChatCode);
} }

View File

@ -38,10 +38,13 @@ public class WeChatCode {
/** sessionKey */ /** sessionKey */
private String sessionKey; private String sessionKey;
private String unid;
public WeChatCode() { public WeChatCode() {
} }
public WeChatCode(Integer id, String nickName, String userPhone, String userPhoto, String userSex, String openId, String sessionKey) { public WeChatCode(Integer id, String nickName, String userPhone, String userPhoto, String userSex, String openId, String sessionKey, String unid) {
this.id = id; this.id = id;
this.nickName = nickName; this.nickName = nickName;
this.userPhone = userPhone; this.userPhone = userPhone;
@ -49,6 +52,7 @@ public class WeChatCode {
this.userSex = userSex; this.userSex = userSex;
this.openId = openId; this.openId = openId;
this.sessionKey = sessionKey; this.sessionKey = sessionKey;
this.unid = unid;
} }
public Integer getId() { public Integer getId() {
@ -107,6 +111,14 @@ public class WeChatCode {
this.sessionKey = sessionKey; this.sessionKey = sessionKey;
} }
public String getUnid() {
return unid;
}
public void setUnid(String unid) {
this.unid = unid;
}
@Override @Override
public String toString() { public String toString() {
return "WeChatCode{" + return "WeChatCode{" +
@ -117,6 +129,7 @@ public class WeChatCode {
", userSex='" + userSex + '\'' + ", userSex='" + userSex + '\'' +
", openId='" + openId + '\'' + ", openId='" + openId + '\'' +
", sessionKey='" + sessionKey + '\'' + ", sessionKey='" + sessionKey + '\'' +
", unid='" + unid + '\'' +
'}'; '}';
} }
} }

View File

@ -2,6 +2,7 @@ package com.xkrs.service.impl;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.xkrs.common.config.ConstantConfig;
import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.common.encapsulation.PromptMessageEnum;
import com.xkrs.dao.AppletsUserDao; import com.xkrs.dao.AppletsUserDao;
import com.xkrs.model.entity.AppletsUser; import com.xkrs.model.entity.AppletsUser;
@ -67,6 +68,7 @@ public class AppletsUserServiceImpl implements AppletsUserService {
String sessionKeys = jsonObject.get("session_key").toString(); String sessionKeys = jsonObject.get("session_key").toString();
log.info("sessionKey-------"+sessionKeys); log.info("sessionKey-------"+sessionKeys);
String openId = jsonObject.get("openid").toString(); String openId = jsonObject.get("openid").toString();
//校验openId是否有效 //校验openId是否有效
if (StringUtils.isBlank(openId) || StringUtils.isBlank(sessionKeys)) { if (StringUtils.isBlank(openId) || StringUtils.isBlank(sessionKeys)) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"用户登陆失败",locale); return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"用户登陆失败",locale);

View File

@ -83,4 +83,4 @@ wx.open.app_id=wx545abff5921505f2
# 微信开放平台 appsecret # 微信开放平台 appsecret
wx.open.app_secret=9e485c69daa2483981fe35f74c22b5ea wx.open.app_secret=9e485c69daa2483981fe35f74c22b5ea
# 微信开放平台 重定向url # 微信开放平台 重定向url
wx.open.redirect_url=https://api.earth-rs.com/wh/callback wx.open.redirect_url=https://api.star-rising.cn/wh/callback