fix(用户管理):新增用户unionId获取逻辑

This commit is contained in:
2025-04-24 11:42:56 +08:00
parent 8b0a2cc636
commit 29ed66e0e3
12 changed files with 71 additions and 18 deletions
@@ -0,0 +1,50 @@
package com.central.order.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.security.MessageDigest;
import java.util.Arrays;
@Slf4j
@RestController
@RequestMapping("/token/callback")
@RequiredArgsConstructor
public class WxCallbackController {
private static final String TOKEN = "codechangetheworld2025"; //MIUFvX040zDQtUHUGEFjpzDwgNLmPe6gWPVsnQMW3nL
@GetMapping()
public String verify(
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("echostr") String echostr) throws Exception {
log.info("开始验证微信token请求:{}", signature);
// 1. 字典序排序并拼接
String[] arr = {TOKEN, timestamp, nonce};
Arrays.sort(arr);
String rawStr = String.join("", arr);
// 2. SHA1 加密
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(rawStr.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
String hash = sb.toString();
// 3. 校验签名
if (hash.equals(signature)) {
return echostr;
} else {
return "Invalid signature";
}
}
}
@@ -85,10 +85,8 @@ aliyun:
wx: wx:
mp: mp:
app-id: ${WX_APP_ID:wx0ea85b13966d88e1} app-id: wx0ea85b13966d88e1
secret: ${WX_APP_SECRET:a90637566b2b73f41f8b1dabd64dc32b} secret: a90637566b2b73f41f8b1dabd64dc32b
cloud-env: ${WX_CLOUD_ENV:prod-1gyj8ywz55460ab1}
base-url: https://springboot-ztti-156622-7-1355625269.sh.run.tcloudbase.com
template-id: template-id:
payment-success: uscmjgLdw0cKBRq8vFyCOPbYj7p-fHuEOIhFwwIAe4A # 支付成功通知模板ID payment-success: uscmjgLdw0cKBRq8vFyCOPbYj7p-fHuEOIhFwwIAe4A # 支付成功通知模板ID
video-complete: TEMPLATE_ID_2 # 视频完成通知模板ID video-complete: TEMPLATE_ID_2 # 视频完成通知模板ID
@@ -124,8 +124,9 @@ public class SysUserController {
@GetMapping(value = "/users-anon/wxOpenId") @GetMapping(value = "/users-anon/wxOpenId")
@Operation(summary = "根据WxOpenId查询用户") @Operation(summary = "根据WxOpenId查询用户")
public SysUser findByWxOpenId(@RequestParam("openId") String openId, public SysUser findByWxOpenId(@RequestParam("openId") String openId,
@RequestParam(required = false) String mobile) throws Exception { @RequestParam(required = false) String mobile,
return appUserService.findByWxOpenid(openId,mobile); @RequestParam(required = false) String unionId) throws Exception {
return appUserService.findByWxOpenid(openId,mobile,unionId);
} }
@GetMapping("/users/{id}") @GetMapping("/users/{id}")
@@ -29,7 +29,7 @@ public interface ISysUserService extends ISuperService<SysUser> {
SysUser findByMobile(String username); SysUser findByMobile(String username);
SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception; SysUser findByWxOpenid(String wxOpenid,String mobile,String unionId) throws Exception;
/** /**
* 通过SysUser 把roles和permissions也查询出来 * 通过SysUser 把roles和permissions也查询出来
@@ -71,7 +71,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
} }
@Override @Override
public SysUser findByWxOpenid(String wxOpenid,String mobile) throws Exception { public SysUser findByWxOpenid(String wxOpenid,String mobile,String unionId) throws Exception {
SysUser sysUser = this.selectByOpenId(wxOpenid); SysUser sysUser = this.selectByOpenId(wxOpenid);
if (ObjectUtils.isNotEmpty(sysUser) && StringUtils.isNotEmpty(sysUser.getMobile())) { if (ObjectUtils.isNotEmpty(sysUser) && StringUtils.isNotEmpty(sysUser.getMobile())) {
return sysUser; return sysUser;
@@ -84,6 +84,7 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
sysUser.setPassword(wxOpenid); sysUser.setPassword(wxOpenid);
sysUser.setEnabled(Boolean.TRUE); sysUser.setEnabled(Boolean.TRUE);
sysUser.setMobile(mobile); sysUser.setMobile(mobile);
sysUser.setUnionId(unionId);
}else if (ObjectUtils.isEmpty(sysUser.getMobile()) && StringUtils.isNotEmpty(mobile)) { }else if (ObjectUtils.isEmpty(sysUser.getMobile()) && StringUtils.isNotEmpty(mobile)) {
sysUser.setMobile(mobile); sysUser.setMobile(mobile);
} }
@@ -57,7 +57,7 @@ public interface UserService {
* @param openId openId * @param openId openId
*/ */
@GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"}) @GetMapping(value = "/users-anon/wxOpenId", params = {"openId","mobile"})
SysUser findByWxOpenId(@RequestParam("openId") String openId, @RequestParam("mobile") String mobile); SysUser findByWxOpenId(@RequestParam("openId") String openId, @RequestParam("mobile") String mobile,@RequestParam("unionId") String unionId);
/** /**
@@ -45,7 +45,7 @@ public class UserServiceFallbackFactory implements FallbackFactory<UserService>
} }
@Override @Override
public SysUser findByWxOpenId(String openId,String mobile) { public SysUser findByWxOpenId(String openId,String mobile,String unionId) {
log.error("通过wxOpenId查询用户异常:{}", openId, throwable); log.error("通过wxOpenId查询用户异常:{}", openId, throwable);
return new SysUser(); return new SysUser();
} }
@@ -1,14 +1,14 @@
package com.central.common.model; package com.central.common.model;
import java.util.List;
import java.util.Set;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.util.List;
import java.util.Set;
/** /**
* @author zlt * @author zlt
* 系统用户实体 * 系统用户实体
@@ -28,6 +28,7 @@ public class SysUser extends SuperEntity {
private Boolean enabled; private Boolean enabled;
private String type; private String type;
private String openId; private String openId;
private String unionId;
private Long creatorId; private Long creatorId;
@TableLogic @TableLogic
private boolean isDel; private boolean isDel;
@@ -143,7 +143,8 @@ zlt:
/api-order/queue/first, /api-order/queue/first,
/api-order/wechat/**, /api-order/wechat/**,
/api-order/home/carousel/app, /api-order/home/carousel/app,
/api-order/device/status /api-order/device/status,
/api-order/token/callback
auth: auth:
urlPermission: urlPermission:
#是否开启url级别权限 #是否开启url级别权限
@@ -36,5 +36,5 @@ public interface ZltUserDetailsService extends UserDetailsService {
* 根据微信用户id/openId查询用户 * 根据微信用户id/openId查询用户
* @param wxOpenId 用户id/openId * @param wxOpenId 用户id/openId
*/ */
UserDetails loadUserByWxOpenId(String wxOpenId,String mobile) throws UsernameNotFoundException; UserDetails loadUserByWxOpenId(String wxOpenId,String mobile,String unionId) throws UsernameNotFoundException;
} }
@@ -51,8 +51,8 @@ public class UserDetailServiceImpl implements ZltUserDetailsService {
} }
@Override @Override
public UserDetails loadUserByWxOpenId(String wxOpenId,String mobile) throws UsernameNotFoundException { public UserDetails loadUserByWxOpenId(String wxOpenId,String mobile,String unionId) throws UsernameNotFoundException {
SysUser sysUser = userService.findByWxOpenId(wxOpenId,mobile); SysUser sysUser = userService.findByWxOpenId(wxOpenId,mobile,unionId);
checkUser(sysUser); checkUser(sysUser);
return LoginUserUtils.getLoginAppUser(sysUser); return LoginUserUtils.getLoginAppUser(sysUser);
} }
@@ -78,8 +78,9 @@ public class TencentAuthenticationProvider extends BaseAuthenticationProvider {
try { try {
String openId = wxLoginResult.get("openid").toString(); String openId = wxLoginResult.get("openid").toString();
String sessionKey = wxLoginResult.get("session_key").toString(); String sessionKey = wxLoginResult.get("session_key").toString();
String unionId = wxLoginResult.get("unionid").toString();
String mobile = decryptPhoneNumber(encryptedData,sessionKey,iv); String mobile = decryptPhoneNumber(encryptedData,sessionKey,iv);
userDetails = userDetailsServiceFactory.getService(authToken).loadUserByWxOpenId(openId,mobile); userDetails = userDetailsServiceFactory.getService(authToken).loadUserByWxOpenId(openId,mobile, unionId);
} catch (AuthenticationException e) { } catch (AuthenticationException e) {
throw new CustomOAuth2AuthenticationException(e.getMessage()); throw new CustomOAuth2AuthenticationException(e.getMessage());