| @@ -1,8 +1,7 @@ | |||
| package com.xueyi.system.wechat.controller; | |||
| import com.baomidou.dynamic.datasource.toolkit.CryptoUtils; | |||
| import com.fasterxml.jackson.core.JsonProcessingException; | |||
| import com.fasterxml.jackson.databind.ObjectMapper; | |||
| import com.alibaba.fastjson2.JSON; | |||
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |||
| import com.xueyi.common.core.constant.basic.SecurityConstants; | |||
| import com.xueyi.common.core.utils.core.CryptoUtil; | |||
| import com.xueyi.common.core.utils.core.ObjectUtil; | |||
| @@ -11,16 +10,15 @@ import com.xueyi.common.core.web.result.R; | |||
| import com.xueyi.common.security.service.TokenService; | |||
| import com.xueyi.system.api.authority.feign.RemoteLoginService; | |||
| import com.xueyi.system.api.model.LoginUser; | |||
| import com.xueyi.system.wechat.domain.bean.WeChatSessionResponse; | |||
| import com.xueyi.system.wechat.domain.bean.WeChatSignUpReq; | |||
| import com.xueyi.system.wechat.domain.po.WeChatUserInfo; | |||
| import com.xueyi.system.wechat.service.WeChatService; | |||
| import com.xueyi.system.wechat.domain.bean.WeappSessionResponse; | |||
| import com.xueyi.system.wechat.domain.bean.WeappSignUpReq; | |||
| import com.xueyi.system.wechat.domain.po.WeappUserInfo; | |||
| import com.xueyi.system.wechat.service.WeappService; | |||
| import com.xueyi.tenant.api.tenant.domain.po.SysEnterpriseStaff; | |||
| import com.xueyi.tenant.api.tenant.feign.RemoteTenantService; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.GetMapping; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| @@ -38,11 +36,11 @@ import java.util.Map; | |||
| * @date 2024-01-02 12:42 | |||
| */ | |||
| @RestController | |||
| @RequestMapping("/api/wechat") | |||
| public class WeChatController { | |||
| Logger log = LoggerFactory.getLogger(WeChatController.class); | |||
| @RequestMapping("/api/weapp") | |||
| public class WeappController { | |||
| Logger log = LoggerFactory.getLogger(WeappController.class); | |||
| @Autowired | |||
| private WeChatService weChatService; | |||
| private WeappService weappService; | |||
| @Autowired | |||
| RemoteTenantService tenantService; | |||
| @@ -59,9 +57,9 @@ public class WeChatController { | |||
| public AjaxResult login(@RequestParam("code") String code, HttpServletResponse response) { | |||
| // 根据code获取微信用户信息 | |||
| WeChatSessionResponse weChatUserInfo = null; | |||
| WeappSessionResponse weChatUserInfo = null; | |||
| try { | |||
| weChatUserInfo = weChatService.getUserInfo(code); | |||
| weChatUserInfo = weappService.getUserInfo(code); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| response.setStatus(500); | |||
| @@ -72,7 +70,7 @@ public class WeChatController { | |||
| R<SysEnterpriseStaff> staff = tenantService.existStaff(weChatUserInfo.getOpenid(), SecurityConstants.INNER); | |||
| Map<String, Object> map = new HashMap<>(); | |||
| try { | |||
| map.put("wechat_auth", CryptoUtil.encrypt(weChatUserInfo.toString())); | |||
| map.put("weappAuth", CryptoUtil.encrypt(weChatUserInfo.toString())); | |||
| if (staff.getData() != null) { | |||
| R<LoginUser> loginInfoResult = remoteLoginService.getLoginInfoInnerByPhone(staff.getData().getPhone(), SecurityConstants.INNER); | |||
| map.putAll(tokenService.createToken(loginInfoResult.getData())); | |||
| @@ -88,23 +86,31 @@ public class WeChatController { | |||
| @PostMapping("/signup") | |||
| public AjaxResult signup(@RequestBody WeChatSignUpReq signUpReq, HttpServletResponse response) { | |||
| public AjaxResult signup(@RequestBody WeappSignUpReq signUpReq, HttpServletResponse response) { | |||
| log.info("微信注册请求信息:{}", signUpReq); | |||
| if (signUpReq == null || StringUtils.isEmpty(signUpReq.getWeappAuth())) { | |||
| response.setStatus(500); | |||
| return AjaxResult.error("请求参数错误"); | |||
| } | |||
| String decodeStr = null; | |||
| WeappSessionResponse obj = null; | |||
| try { | |||
| decodeStr = weChatService.decrypt(signUpReq.getEncryptedData(), signUpReq.getIv(), signUpReq.getWechatAuth()); | |||
| String str = CryptoUtil.decrypt(signUpReq.getWeappAuth()); | |||
| obj = JSON.parseObject(str, WeappSessionResponse.class); | |||
| decodeStr = weappService.decrypt(signUpReq.getEncryptedData(), signUpReq.getIv(), obj); | |||
| } catch (Exception e) { | |||
| e.printStackTrace(); | |||
| response.setStatus(500); | |||
| // throw new RuntimeException(e); | |||
| return AjaxResult.error("微信数据解析错误"); | |||
| } | |||
| ObjectMapper objectMapper = new ObjectMapper(); | |||
| WeChatUserInfo userInfo = null; | |||
| WeappUserInfo userInfo = null; | |||
| try { | |||
| userInfo = objectMapper.readValue(decodeStr, WeChatUserInfo.class); | |||
| } catch (JsonProcessingException e) { | |||
| userInfo = JSON.parseObject(decodeStr, WeappUserInfo.class); | |||
| // userInfo = objectMapper.readValue(decodeStr, WeChatUserInfo.class); | |||
| } catch (Exception e) { | |||
| response.setStatus(500); | |||
| // throw new RuntimeException(e); | |||
| return AjaxResult.error("WeChatUserInfo解析错误"); | |||
| @@ -115,7 +121,7 @@ public class WeChatController { | |||
| String phone = null; | |||
| if (staff.getData() == null) { | |||
| SysEnterpriseStaff staff1 = new SysEnterpriseStaff(); | |||
| // staff1.setOpenid(userInfo.get); | |||
| staff1.setOpenid(obj.getOpenid()); | |||
| staff1.setPhone(userInfo.getPhoneNumber()); | |||
| tenantService.saveEnterpriseStaff(staff1, SecurityConstants.INNER); | |||
| phone = userInfo.getPhoneNumber(); | |||
| @@ -0,0 +1,14 @@ | |||
| package com.xueyi.system.wechat.domain.bean; | |||
| import lombok.Data; | |||
| /** | |||
| * @author yk | |||
| * @description | |||
| * @date 2024-01-04 11:04 | |||
| */ | |||
| @Data | |||
| public class Watermark { | |||
| private Long timestamp; | |||
| private String appid; | |||
| } | |||
| @@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; | |||
| import lombok.Data; | |||
| @Data | |||
| public class WeChatSessionResponse { | |||
| public class WeappSessionResponse { | |||
| @JsonProperty("openid") | |||
| private String openid; | |||
| @@ -9,13 +9,16 @@ import lombok.Data; | |||
| * @date 2024-01-02 16:47 | |||
| */ | |||
| @Data | |||
| public class WeChatSignUpReq { | |||
| public class WeappSignUpReq { | |||
| private String iv; | |||
| private String encryptedData; | |||
| private JSONObject rawData; | |||
| private String signature; | |||
| private String wechatAuth; | |||
| private String weappAuth; | |||
| @Override | |||
| public String toString(){ | |||
| return JSONObject.toJSONString(this); | |||
| } | |||
| } | |||
| @@ -1,55 +0,0 @@ | |||
| package com.xueyi.system.wechat.domain.po; | |||
| import com.alibaba.fastjson2.JSON; | |||
| import com.baomidou.mybatisplus.annotation.TableName; | |||
| import com.xueyi.common.core.web.tenant.base.TBaseEntity; | |||
| import lombok.Data; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.CREATE_BY; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.CREATE_TIME; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.DEL_FLAG; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.NAME; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.REMARK; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.SORT; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.STATUS; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.UPDATE_BY; | |||
| import static com.xueyi.common.core.constant.basic.EntityConstants.UPDATE_TIME; | |||
| /** | |||
| * @author yk | |||
| * @description | |||
| * @date 2024-01-02 12:47 | |||
| */ | |||
| @Data | |||
| @TableName(value = "sys_wechat_user",excludeProperty = {STATUS, UPDATE_BY, SORT, CREATE_BY, DEL_FLAG, CREATE_TIME, UPDATE_TIME, REMARK, NAME }) | |||
| public class WeChatUserInfo extends TBaseEntity { | |||
| private String openId; | |||
| private String nickName; | |||
| private int gender; | |||
| private String city; | |||
| private String province; | |||
| private String country; | |||
| private String avatarUrl; | |||
| private String unionId; | |||
| private Watermark watermark; | |||
| private String phoneNumber; | |||
| // Getters and Setters | |||
| @Data | |||
| public static class Watermark { | |||
| private long timestamp; | |||
| private String appid; | |||
| // Getters and Setters | |||
| } | |||
| @Override | |||
| public String toString(){ | |||
| return JSON.toJSONString(this); | |||
| } | |||
| } | |||
| @@ -0,0 +1,31 @@ | |||
| package com.xueyi.system.wechat.domain.po; | |||
| import com.alibaba.fastjson2.JSON; | |||
| import com.xueyi.system.wechat.domain.bean.Watermark; | |||
| import lombok.Data; | |||
| /** | |||
| * @author yk | |||
| * @description | |||
| * @date 2024-01-02 12:47 | |||
| */ | |||
| @Data | |||
| public class WeappUserInfo { | |||
| private String openId; | |||
| private String countryCode; | |||
| private Watermark watermark; | |||
| private String phoneNumber; | |||
| private String purePhoneNumber; | |||
| // Getters and Setters | |||
| @Override | |||
| public String toString(){ | |||
| return JSON.toJSONString(this); | |||
| } | |||
| } | |||
| @@ -3,15 +3,13 @@ package com.xueyi.system.wechat.service; | |||
| import com.alibaba.fastjson2.JSON; | |||
| import com.xueyi.common.core.utils.core.CryptoUtil; | |||
| import com.xueyi.common.redis.utils.RedisUtil; | |||
| import com.xueyi.system.wechat.domain.bean.WeChatSessionResponse; | |||
| import com.xueyi.system.wechat.domain.bean.WeappSessionResponse; | |||
| import org.slf4j.Logger; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.beans.factory.annotation.Value; | |||
| import org.springframework.http.converter.StringHttpMessageConverter; | |||
| import org.springframework.stereotype.Service; | |||
| import org.springframework.web.client.RestClientException; | |||
| import org.springframework.web.client.RestTemplate; | |||
| import org.springframework.web.client.UnknownContentTypeException; | |||
| import javax.crypto.Cipher; | |||
| import javax.crypto.SecretKey; | |||
| @@ -25,8 +23,8 @@ import java.util.Base64; | |||
| * @date 2024-01-02 12:43 | |||
| */ | |||
| @Service | |||
| public class WeChatService { | |||
| Logger log = org.slf4j.LoggerFactory.getLogger(WeChatService.class); | |||
| public class WeappService { | |||
| Logger log = org.slf4j.LoggerFactory.getLogger(WeappService.class); | |||
| public static final String WECHAT_REDIS_SESSION_KEY = "wechat:session_key:"; | |||
| @@ -39,7 +37,7 @@ public class WeChatService { | |||
| @Autowired | |||
| private RestTemplate restTemplate; | |||
| public WeChatSessionResponse getUserInfo(String code) { | |||
| public WeappSessionResponse getUserInfo(String code) { | |||
| log.info("appId: {}, appSecret: {}, code: {}",appId, appSecret, code); | |||
| // 向微信服务器发送请求,获取用户的openid和session_key | |||
| String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + | |||
| @@ -52,7 +50,7 @@ public class WeChatService { | |||
| // WeChatSessionResponse sessionResponse = restTemplate.getForObject(url, WeChatSessionResponse.class); | |||
| String str = restTemplate.getForObject(url, String.class); | |||
| log.info("sessionResponse: str {}", str); | |||
| WeChatSessionResponse sessionResponse = JSON.parseObject(str, WeChatSessionResponse.class); | |||
| WeappSessionResponse sessionResponse = JSON.parseObject(str, WeappSessionResponse.class); | |||
| log.info("sessionResponse: {}", sessionResponse); | |||
| RedisUtil.setVal(WECHAT_REDIS_SESSION_KEY+sessionResponse.getOpenid(), sessionResponse.getSessionKey()); | |||
| log.info("sessionResponse: {}", sessionResponse); | |||
| @@ -62,10 +60,9 @@ public class WeChatService { | |||
| } | |||
| public String decrypt(String encryptedData, String iv, String wechatAuth) throws Exception{ | |||
| public String decrypt(String encryptedData, String iv, WeappSessionResponse obj) throws Exception{ | |||
| String str = CryptoUtil.decrypt(wechatAuth); | |||
| WeChatSessionResponse obj = JSON.parseObject(str, WeChatSessionResponse.class); | |||
| // Base64 解码 | |||
| byte[] sessionKeyBytes = Base64.getDecoder().decode(obj.getSessionKey()); | |||
| byte[] encryptedDataBytes = Base64.getDecoder().decode(encryptedData); | |||