From c5dee2529f17a711ad857345f0bb31d687f89197 Mon Sep 17 00:00:00 2001 From: "DESKTOP-G8BCEP0\\HP" <2037158277@qq.com> Date: Fri, 17 Sep 2021 09:22:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=9F=A5=E8=AF=A2=E7=BD=91=E7=AB=99=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=92=8C=E5=BE=AE=E4=BF=A1=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/controller/SysUserController.java | 29 +++++ .../com/xkrs/controller/WeChatController.java | 99 ++++++++++++++ src/main/java/com/xkrs/dao/SysUserDao.java | 9 ++ src/main/java/com/xkrs/dao/WeChatCodeDao.java | 19 +++ .../com/xkrs/model/entity/WeChatCode.java | 122 ++++++++++++++++++ 5 files changed, 278 insertions(+) create mode 100644 src/main/java/com/xkrs/controller/WeChatController.java create mode 100644 src/main/java/com/xkrs/dao/WeChatCodeDao.java create mode 100644 src/main/java/com/xkrs/model/entity/WeChatCode.java diff --git a/src/main/java/com/xkrs/controller/SysUserController.java b/src/main/java/com/xkrs/controller/SysUserController.java index bc306c5..9029ded 100644 --- a/src/main/java/com/xkrs/controller/SysUserController.java +++ b/src/main/java/com/xkrs/controller/SysUserController.java @@ -2,7 +2,9 @@ package com.xkrs.controller; import com.xkrs.common.encapsulation.PromptMessageEnum; import com.xkrs.common.tool.TokenUtil; +import com.xkrs.dao.AppletsUserDao; import com.xkrs.dao.SysUserDao; +import com.xkrs.model.entity.AppletsUser; import com.xkrs.model.entity.SysUserEntity; import com.xkrs.model.qo.SysUserQo; import com.xkrs.model.validation.SysUserQoInsert; @@ -40,6 +42,9 @@ public class SysUserController { @Resource private SysUserDao sysUserDao; + @Resource + private AppletsUserDao appletsUserDao; + /** * 登录用户Token验证 * @return @@ -276,4 +281,28 @@ public class SysUserController { return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"绑定成功!",locale); } + /** + * 查询微信用户信息和网站用户的信息 + * @param openId + * @return + */ + @GetMapping("/findUserAndWeChatUser") + public String findUserAndWeChatUser(@RequestParam("openId") String openId){ + Locale locale = LocaleContextHolder.getLocale(); + Map map = new HashMap(3); + AppletsUser appletsUser = appletsUserDao.findByOpenId(openId); + if(appletsUser == null){ + map.put("weChat",null); + }else { + map.put("weChat",appletsUser); + } + SysUserVo sysUserVo = sysUserDao.selectUserByOpenId(openId); + if(sysUserVo == null){ + map.put("user",null); + }else { + map.put("user",sysUserVo); + } + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,map,locale); + } + } diff --git a/src/main/java/com/xkrs/controller/WeChatController.java b/src/main/java/com/xkrs/controller/WeChatController.java new file mode 100644 index 0000000..c46bcae --- /dev/null +++ b/src/main/java/com/xkrs/controller/WeChatController.java @@ -0,0 +1,99 @@ +package com.xkrs.controller; + +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.WeChatCodeDao; +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.RestController; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Locale; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +/** + * @author XinYi Song + * 微信扫码登录 + */ +@RestController +public class WeChatController { + + @Resource + private WeChatCodeDao weChatCodeDao; + + @GetMapping("/callback") + public String callback(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" + + "&secret=%s" + + "&code=%s" + + "&grant_type=authorization_code"; + + String accessTokenUrl = String.format(baseAccessTokenUrl, + ConstantConfig.WX_OPEN_APP_ID, + ConstantConfig.WX_OPEN_APP_SECRET, + code); + //请求这个拼接好的地址得到返回的值,现在请求地址,不用浏览器了,用httpclient发送一个请求得到一个返回的地址 + String accessTokenInfo = HttpClientUtil.doGet(accessTokenUrl); + //所以我们需要对字符串进行分割,先将字符串转换成map集合,map是key-value结构,再根据map中的key得到结果,这里我们用gson + JSONObject jsonObject = JSON.parseObject(accessTokenInfo); + String access_token =(String) jsonObject.get("access_token"); + String openid = (String)jsonObject.get("openid"); + + //扫码人信息加到数据库中去 + //判断数据库中是否存在相同的数据库信息,我们可以根据openid来做判断 + WeChatCode byOpenId = weChatCodeDao.findByOpenId(openid); + if (byOpenId ==null){ + //member为空,表示数据库中没有相同的信息 + //3.拿到access_token和openid,再去请求微信提供固定的地址,获取到扫码人的信息 + String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" + + "?access_token=%s" + + "&openid=%s"; + String userInfoUrl = String.format( + baseUserInfoUrl, + access_token, + openid + ); + //发送请求 + String userInfo = HttpClientUtil.doGet(userInfoUrl); + //获取返回的userInfo字符串的扫描人信息 + JSONObject jsonObject1 = JSON.parseObject(userInfo); + //昵称 + String nickname =(String) jsonObject1.get("nickname"); + //头像 + String headimgurl =(String) jsonObject1.get("headimgurl"); + Integer sex = (Integer) jsonObject1.get("sex"); + + + WeChatCode weChatCode = new WeChatCode(); + weChatCode.setOpenId(openid); + weChatCode.setNickName(nickname); + weChatCode.setUserPhoto(headimgurl); + weChatCode.setUserSex(sex.toString()); + + weChatCodeDao.save(weChatCode); + } + //最后返回我们的首页面 + //由于我们在前端中需要显示用户名和头像,所以我们需要使用jwt在地址中传递token + //不能使用cookie,是因为cookie不能跨域访问,会导致值传递失败 + //使用jwt根据member对象生成token字符串 + //String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname()); + + return outputEncapsulationObject(PromptMessageEnum.SUCCESS,"操作成功!",locale); + }catch(Exception e){ + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL,"操作失败!",locale); + } + } +} diff --git a/src/main/java/com/xkrs/dao/SysUserDao.java b/src/main/java/com/xkrs/dao/SysUserDao.java index 7e46f37..89e6843 100644 --- a/src/main/java/com/xkrs/dao/SysUserDao.java +++ b/src/main/java/com/xkrs/dao/SysUserDao.java @@ -96,6 +96,15 @@ public interface SysUserDao extends JpaRepository { "from SysUserEntity where userName = :userName") SysUserVo selectUserByUserName(String userName); + /** + * 根据openid查询网站用户的信息 + * @param openId + * @return + */ + @Query(value = "select new com.xkrs.model.vo.SysUserVo (id,reallyName,telephone,userDepartment,activeFlag,addTime) " + + "from SysUserEntity where openId = :openId") + SysUserVo selectUserByOpenId(String openId); + /** * 启用 * @param userId diff --git a/src/main/java/com/xkrs/dao/WeChatCodeDao.java b/src/main/java/com/xkrs/dao/WeChatCodeDao.java new file mode 100644 index 0000000..b883c4b --- /dev/null +++ b/src/main/java/com/xkrs/dao/WeChatCodeDao.java @@ -0,0 +1,19 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.WeChatCode; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Component; + +/** + * @author HP + */ +@Component +public interface WeChatCodeDao extends JpaRepository { + + /** + * 根据openId查询信息 + * @param openId + * @return + */ + WeChatCode findByOpenId(String openId); +} diff --git a/src/main/java/com/xkrs/model/entity/WeChatCode.java b/src/main/java/com/xkrs/model/entity/WeChatCode.java new file mode 100644 index 0000000..66f00ea --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/WeChatCode.java @@ -0,0 +1,122 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; + +/** + * @author HP + * 微信扫码实体类 + */ +@Entity +@Table(name="wechat_code_user") +public class WeChatCode { + /** + * 指定主键,建立自增序列,主键值取自序列 + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "wechat_code_user_seq_gen") + @SequenceGenerator(name = "wechat_code_user_seq_gen", sequenceName = "wechat_code_user_id_seq",allocationSize = 1) + private Integer id; + + /** 昵称 */ + @Column(length = 65,columnDefinition = "varchar(65)") + private String nickName; + + /** 手机号 */ + @Column(length = 65,columnDefinition = "varchar(65)") + private String userPhone; + + /** 用户头像 */ + private String userPhoto; + + /** 性别 */ + @Column(length = 55,columnDefinition = "varchar(55)") + private String userSex; + + /** openid */ + private String openId; + + /** sessionKey */ + private String sessionKey; + + public WeChatCode() { + } + + public WeChatCode(Integer id, String nickName, String userPhone, String userPhoto, String userSex, String openId, String sessionKey) { + this.id = id; + this.nickName = nickName; + this.userPhone = userPhone; + this.userPhoto = userPhoto; + this.userSex = userSex; + this.openId = openId; + this.sessionKey = sessionKey; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getNickName() { + return nickName; + } + + public void setNickName(String nickName) { + this.nickName = nickName; + } + + public String getUserPhone() { + return userPhone; + } + + public void setUserPhone(String userPhone) { + this.userPhone = userPhone; + } + + public String getUserPhoto() { + return userPhoto; + } + + public void setUserPhoto(String userPhoto) { + this.userPhoto = userPhoto; + } + + public String getUserSex() { + return userSex; + } + + public void setUserSex(String userSex) { + this.userSex = userSex; + } + + public String getOpenId() { + return openId; + } + + public void setOpenId(String openId) { + this.openId = openId; + } + + public String getSessionKey() { + return sessionKey; + } + + public void setSessionKey(String sessionKey) { + this.sessionKey = sessionKey; + } + + @Override + public String toString() { + return "WeChatCode{" + + "id=" + id + + ", nickName='" + nickName + '\'' + + ", userPhone='" + userPhone + '\'' + + ", userPhoto='" + userPhoto + '\'' + + ", userSex='" + userSex + '\'' + + ", openId='" + openId + '\'' + + ", sessionKey='" + sessionKey + '\'' + + '}'; + } +}