From ca93e2cf3c265fecaef839577b27fd650aa74fb9 Mon Sep 17 00:00:00 2001 From: XinYi Song <2037158277@qq.com> Date: Wed, 1 Dec 2021 14:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E9=99=86=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/controller/WeChatController.java | 7 +++++-- .../java/com/xkrs/model/entity/WeChatCode.java | 15 ++++++++++++++- .../xkrs/service/impl/AppletsUserServiceImpl.java | 2 ++ src/main/resources/application.properties | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/xkrs/controller/WeChatController.java b/src/main/java/com/xkrs/controller/WeChatController.java index 0ddf3a1..e8793c0 100644 --- a/src/main/java/com/xkrs/controller/WeChatController.java +++ b/src/main/java/com/xkrs/controller/WeChatController.java @@ -9,6 +9,7 @@ import com.xkrs.model.entity.WeChatCode; import com.xkrs.utils.HttpClientUtil; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @@ -29,12 +30,11 @@ public class WeChatController { private WeChatCodeDao weChatCodeDao; @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(); try{ //1.获取code值,临时票据,类似于验证码 System.out.println(code); - //2.拿着code请求微信固定的地址,得到两个值access_token和openid String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" + "?appid=%s" + @@ -71,11 +71,13 @@ public class WeChatController { String userInfo = HttpClientUtil.doGet(userInfoUrl); //获取返回的userInfo字符串的扫描人信息 JSONObject jsonObject1 = JSON.parseObject(userInfo); + System.out.println("------>" + jsonObject1); //昵称 String nickname =(String) jsonObject1.get("nickname"); //头像 String headimgurl =(String) jsonObject1.get("headimgurl"); Integer sex = (Integer) jsonObject1.get("sex"); + String unionid = (String) jsonObject1.get("unionid"); WeChatCode weChatCode = new WeChatCode(); @@ -83,6 +85,7 @@ public class WeChatController { weChatCode.setNickName(nickname); weChatCode.setUserPhoto(headimgurl); weChatCode.setUserSex(sex.toString()); + weChatCode.setUnid(unionid); weChatCodeDao.save(weChatCode); } diff --git a/src/main/java/com/xkrs/model/entity/WeChatCode.java b/src/main/java/com/xkrs/model/entity/WeChatCode.java index 66f00ea..da3575e 100644 --- a/src/main/java/com/xkrs/model/entity/WeChatCode.java +++ b/src/main/java/com/xkrs/model/entity/WeChatCode.java @@ -38,10 +38,13 @@ public class WeChatCode { /** sessionKey */ private String sessionKey; + + private String unid; + 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.nickName = nickName; this.userPhone = userPhone; @@ -49,6 +52,7 @@ public class WeChatCode { this.userSex = userSex; this.openId = openId; this.sessionKey = sessionKey; + this.unid = unid; } public Integer getId() { @@ -107,6 +111,14 @@ public class WeChatCode { this.sessionKey = sessionKey; } + public String getUnid() { + return unid; + } + + public void setUnid(String unid) { + this.unid = unid; + } + @Override public String toString() { return "WeChatCode{" + @@ -117,6 +129,7 @@ public class WeChatCode { ", userSex='" + userSex + '\'' + ", openId='" + openId + '\'' + ", sessionKey='" + sessionKey + '\'' + + ", unid='" + unid + '\'' + '}'; } } diff --git a/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java b/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java index dcc0c8d..e1db068 100644 --- a/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/AppletsUserServiceImpl.java @@ -2,6 +2,7 @@ package com.xkrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.xkrs.common.config.ConstantConfig; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.dao.AppletsUserDao; import com.xkrs.model.entity.AppletsUser; @@ -67,6 +68,7 @@ public class AppletsUserServiceImpl implements AppletsUserService { String sessionKeys = jsonObject.get("session_key").toString(); log.info("sessionKey-------"+sessionKeys); String openId = jsonObject.get("openid").toString(); + //校验openId是否有效 if (StringUtils.isBlank(openId) || StringUtils.isBlank(sessionKeys)) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"用户登陆失败",locale); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8c35754..087fb0b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -83,4 +83,4 @@ wx.open.app_id=wx545abff5921505f2 # 微信开放平台 appsecret wx.open.app_secret=9e485c69daa2483981fe35f74c22b5ea # 微信开放平台 重定向url -wx.open.redirect_url=https://api.earth-rs.com/wh/callback +wx.open.redirect_url=https://api.star-rising.cn/wh/callback