|
@@ -10,6 +10,7 @@ import com.train.cfc.entity.CfcUser;
|
|
|
import com.train.cfc.mapper.CfcUserMapper;
|
|
import com.train.cfc.mapper.CfcUserMapper;
|
|
|
import com.train.dto.LoginResultDTO;
|
|
import com.train.dto.LoginResultDTO;
|
|
|
import com.train.dto.AlumniVerifyDTO;
|
|
import com.train.dto.AlumniVerifyDTO;
|
|
|
|
|
+import com.train.service.SsoService;
|
|
|
import com.train.service.WechatService;
|
|
import com.train.service.WechatService;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
@@ -48,6 +49,9 @@ public class AuthController {
|
|
|
@Resource
|
|
@Resource
|
|
|
private WechatService wechatService;
|
|
private WechatService wechatService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private SsoService ssoService;
|
|
|
|
|
+
|
|
|
@Value("${wechat.test-mode}")
|
|
@Value("${wechat.test-mode}")
|
|
|
private boolean testMode;
|
|
private boolean testMode;
|
|
|
|
|
|
|
@@ -64,12 +68,14 @@ public class AuthController {
|
|
|
return Result.error("登录code不能为空");
|
|
return Result.error("登录code不能为空");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String openid = wechatService.code2Session(code);
|
|
|
|
|
- CfcUser cfcUser = upsertCfcUser(openid);
|
|
|
|
|
- TrainUser user = upsertTrainUser(openid, cfcUser);
|
|
|
|
|
|
|
+ Map<String, String> session = wechatService.code2SessionInfo(code);
|
|
|
|
|
+ String openid = session.get("openid");
|
|
|
|
|
+ String unionid = session.get("unionid");
|
|
|
|
|
+ CfcUser cfcUser = upsertCfcUser(openid, unionid);
|
|
|
|
|
+ TrainUser user = upsertTrainUser(openid, cfcUser, unionid);
|
|
|
|
|
|
|
|
String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
|
- return Result.success(buildLoginResult(user, token, cfcUser, isNew(user)));
|
|
|
|
|
|
|
+ return Result.success(buildLoginResult(user, token, cfcUser, isNew(user), openid));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -86,8 +92,10 @@ public class AuthController {
|
|
|
return Result.error("code 和 phoneCode 不能为空");
|
|
return Result.error("code 和 phoneCode 不能为空");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String openid = wechatService.code2Session(code);
|
|
|
|
|
- CfcUser cfcUser = upsertCfcUser(openid);
|
|
|
|
|
|
|
+ Map<String, String> session = wechatService.code2SessionInfo(code);
|
|
|
|
|
+ String openid = session.get("openid");
|
|
|
|
|
+ String unionid = session.get("unionid");
|
|
|
|
|
+ CfcUser cfcUser = upsertCfcUser(openid, unionid);
|
|
|
|
|
|
|
|
// 手机号回写到 cfc_user + train_user
|
|
// 手机号回写到 cfc_user + train_user
|
|
|
// 对齐 cfc 语义:phone 优先回填 openid(用户换了微信号,按手机号找到原账号复用),
|
|
// 对齐 cfc 语义:phone 优先回填 openid(用户换了微信号,按手机号找到原账号复用),
|
|
@@ -121,7 +129,7 @@ public class AuthController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- TrainUser user = upsertTrainUser(openid, cfcUser);
|
|
|
|
|
|
|
+ TrainUser user = upsertTrainUser(openid, cfcUser, unionid);
|
|
|
if (StringUtils.hasText(phone) && !phone.equals(user.getPhone())) {
|
|
if (StringUtils.hasText(phone) && !phone.equals(user.getPhone())) {
|
|
|
user.setPhone(phone);
|
|
user.setPhone(phone);
|
|
|
try {
|
|
try {
|
|
@@ -133,7 +141,7 @@ public class AuthController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
|
- return Result.success(buildLoginResult(user, token, cfcUser, isNew(user)));
|
|
|
|
|
|
|
+ return Result.success(buildLoginResult(user, token, cfcUser, isNew(user), openid));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -184,9 +192,68 @@ public class AuthController {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 跨小程序单点登录 - 签发一次性票据(需登录)。
|
|
|
|
|
+ * 请求体: { "target": "cfc" };返回 { ticket, target }。
|
|
|
|
|
+ * 前置:当前学员须已关联 cfc 统一会员(train_user.cfc_user_id 非空)。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Operation(summary = "SSO 签发一次性票据")
|
|
|
|
|
+ @PostMapping("/sso-ticket")
|
|
|
|
|
+ public Result<Map<String, Object>> ssoTicket(@RequestBody Map<String, String> body,
|
|
|
|
|
+ @org.springframework.web.bind.annotation.RequestAttribute("userId") Long userId) {
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ return Result.error(401, "请先登录");
|
|
|
|
|
+ }
|
|
|
|
|
+ String target = body != null ? body.get("target") : null;
|
|
|
|
|
+ if (!SsoService.SYS_CFC.equals(target)) {
|
|
|
|
|
+ return Result.error("不支持的目标系统");
|
|
|
|
|
+ }
|
|
|
|
|
+ TrainUser user = trainUserMapper.selectById(userId);
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ return Result.error("用户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.getCfcUserId() == null) {
|
|
|
|
|
+ return Result.error("尚未关联会员,无法跳转");
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ String ticket = ssoService.issueTicket(SsoService.SYS_TRAIN, target, user.getId(), user.getCfcUserId());
|
|
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
|
|
+ data.put("ticket", ticket);
|
|
|
|
|
+ data.put("target", target);
|
|
|
|
|
+ return Result.success(data);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("签发 SSO 票据失败: userId={}", userId, e);
|
|
|
|
|
+ return Result.error("签发票据失败,请稍后重试");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 跨小程序单点登录 - 消费一次性票据换取 train 登录态(公开接口)。
|
|
|
|
|
+ * 请求体: { "ticket": "..." }。按 cfc_user_id 找到/创建 train_user 后签发 train JWT。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Operation(summary = "SSO 票据换取登录态")
|
|
|
|
|
+ @PostMapping("/sso-login")
|
|
|
|
|
+ public Result<LoginResultDTO> ssoLogin(@RequestBody Map<String, String> body) {
|
|
|
|
|
+ String ticket = body != null ? body.get("ticket") : null;
|
|
|
|
|
+ if (!StringUtils.hasText(ticket)) {
|
|
|
|
|
+ return Result.error("票据不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ Long cfcUserId = ssoService.consumeTicket(ticket, SsoService.SYS_TRAIN);
|
|
|
|
|
+ if (cfcUserId == null) {
|
|
|
|
|
+ return Result.error("票据无效或已过期,请重新进入");
|
|
|
|
|
+ }
|
|
|
|
|
+ CfcUser cfcUser = cfcUserMapper.selectById(cfcUserId);
|
|
|
|
|
+ if (cfcUser == null) {
|
|
|
|
|
+ return Result.error("会员不存在,无法登录");
|
|
|
|
|
+ }
|
|
|
|
|
+ TrainUser user = upsertTrainUserByCfcId(cfcUserId, cfcUser.getUnionid());
|
|
|
|
|
+ String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
|
|
|
+ return Result.success(buildLoginResult(user, token, cfcUser, isNew(user), null));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// ========== 内部方法 ==========
|
|
// ========== 内部方法 ==========
|
|
|
|
|
|
|
|
- private CfcUser upsertCfcUser(String openid) {
|
|
|
|
|
|
|
+ private CfcUser upsertCfcUser(String openid, String unionid) {
|
|
|
CfcUser cfcUser = cfcUserMapper.selectOne(
|
|
CfcUser cfcUser = cfcUserMapper.selectOne(
|
|
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CfcUser>()
|
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CfcUser>()
|
|
|
.eq(CfcUser::getOpenid, openid)
|
|
.eq(CfcUser::getOpenid, openid)
|
|
@@ -194,6 +261,7 @@ public class AuthController {
|
|
|
if (cfcUser == null) {
|
|
if (cfcUser == null) {
|
|
|
cfcUser = new CfcUser();
|
|
cfcUser = new CfcUser();
|
|
|
cfcUser.setOpenid(openid);
|
|
cfcUser.setOpenid(openid);
|
|
|
|
|
+ cfcUser.setUnionid(StringUtils.hasText(unionid) ? unionid : "");
|
|
|
cfcUser.setRole("parent");
|
|
cfcUser.setRole("parent");
|
|
|
cfcUser.setNickname("营员" + openid.substring(Math.max(0, openid.length() - 6)));
|
|
cfcUser.setNickname("营员" + openid.substring(Math.max(0, openid.length() - 6)));
|
|
|
cfcUser.setMemberLevel("FREE");
|
|
cfcUser.setMemberLevel("FREE");
|
|
@@ -202,11 +270,21 @@ public class AuthController {
|
|
|
cfcUser.setUpdatedAt(new Date());
|
|
cfcUser.setUpdatedAt(new Date());
|
|
|
cfcUserMapper.insert(cfcUser);
|
|
cfcUserMapper.insert(cfcUser);
|
|
|
log.info("新建 cfc 会员: id={}, openid={}", cfcUser.getId(), openid);
|
|
log.info("新建 cfc 会员: id={}, openid={}", cfcUser.getId(), openid);
|
|
|
|
|
+ } else if (StringUtils.hasText(unionid) && !StringUtils.hasText(cfcUser.getUnionid())) {
|
|
|
|
|
+ // 补采:cfc 侧历史会员 unionid 为空时回填(跨小程序身份归一)
|
|
|
|
|
+ cfcUser.setUnionid(unionid);
|
|
|
|
|
+ cfcUser.setUpdatedAt(new Date());
|
|
|
|
|
+ try {
|
|
|
|
|
+ cfcUserMapper.updateById(cfcUser);
|
|
|
|
|
+ log.info("回填 cfc 会员 unionid: id={}", cfcUser.getId());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("回填 cfc 会员 unionid 失败: id={}, err={}", cfcUser.getId(), e.getMessage());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
return cfcUser;
|
|
return cfcUser;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private TrainUser upsertTrainUser(String openid, CfcUser cfcUser) {
|
|
|
|
|
|
|
+ private TrainUser upsertTrainUser(String openid, CfcUser cfcUser, String unionid) {
|
|
|
TrainUser user = trainUserMapper.selectOne(
|
|
TrainUser user = trainUserMapper.selectOne(
|
|
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<TrainUser>()
|
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<TrainUser>()
|
|
|
.eq(TrainUser::getOpenid, openid)
|
|
.eq(TrainUser::getOpenid, openid)
|
|
@@ -215,12 +293,49 @@ public class AuthController {
|
|
|
user = new TrainUser();
|
|
user = new TrainUser();
|
|
|
user.setCfcUserId(cfcUser.getId());
|
|
user.setCfcUserId(cfcUser.getId());
|
|
|
user.setOpenid(openid);
|
|
user.setOpenid(openid);
|
|
|
|
|
+ user.setUnionid(StringUtils.hasText(unionid) ? unionid : null);
|
|
|
user.setAlumniVerify("pending");
|
|
user.setAlumniVerify("pending");
|
|
|
user.setRole("student");
|
|
user.setRole("student");
|
|
|
user.setStatus("active");
|
|
user.setStatus("active");
|
|
|
trainUserMapper.insert(user);
|
|
trainUserMapper.insert(user);
|
|
|
- } else if (user.getCfcUserId() == null) {
|
|
|
|
|
- user.setCfcUserId(cfcUser.getId());
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ boolean dirty = false;
|
|
|
|
|
+ if (user.getCfcUserId() == null) {
|
|
|
|
|
+ user.setCfcUserId(cfcUser.getId());
|
|
|
|
|
+ dirty = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(unionid) && !StringUtils.hasText(user.getUnionid())) {
|
|
|
|
|
+ user.setUnionid(unionid);
|
|
|
|
|
+ dirty = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (dirty) {
|
|
|
|
|
+ trainUserMapper.updateById(user);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 按 cfc 统一会员ID 查找/创建本地学员:SSO 兑换用。
|
|
|
|
|
+ * 未找到则新建(openid 留空,待用户在本小程序首次微信登录时回填)。
|
|
|
|
|
+ */
|
|
|
|
|
+ private TrainUser upsertTrainUserByCfcId(Long cfcUserId, String unionid) {
|
|
|
|
|
+ TrainUser user = trainUserMapper.selectOne(
|
|
|
|
|
+ new LambdaQueryWrapper<TrainUser>()
|
|
|
|
|
+ .eq(TrainUser::getCfcUserId, cfcUserId)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ user = new TrainUser();
|
|
|
|
|
+ user.setCfcUserId(cfcUserId);
|
|
|
|
|
+ user.setUnionid(StringUtils.hasText(unionid) ? unionid : null);
|
|
|
|
|
+ user.setAlumniVerify("pending");
|
|
|
|
|
+ user.setRole("student");
|
|
|
|
|
+ user.setStatus("active");
|
|
|
|
|
+ trainUserMapper.insert(user);
|
|
|
|
|
+ log.info("SSO 兑换:按 cfcUserId={} 新建 train_user id={}", cfcUserId, user.getId());
|
|
|
|
|
+ } else if (StringUtils.hasText(unionid) && !StringUtils.hasText(user.getUnionid())) {
|
|
|
|
|
+ // 补采:SSO 兑换时从 cfc 会员回填 unionid
|
|
|
|
|
+ user.setUnionid(unionid);
|
|
|
trainUserMapper.updateById(user);
|
|
trainUserMapper.updateById(user);
|
|
|
}
|
|
}
|
|
|
return user;
|
|
return user;
|
|
@@ -231,7 +346,7 @@ public class AuthController {
|
|
|
return !StringUtils.hasText(user.getPhone()) && !StringUtils.hasText(user.getName());
|
|
return !StringUtils.hasText(user.getPhone()) && !StringUtils.hasText(user.getName());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private LoginResultDTO buildLoginResult(TrainUser user, String token, CfcUser cfcUser, boolean isNew) {
|
|
|
|
|
|
|
+ private LoginResultDTO buildLoginResult(TrainUser user, String token, CfcUser cfcUser, boolean isNew, String openid) {
|
|
|
LoginResultDTO dto = new LoginResultDTO();
|
|
LoginResultDTO dto = new LoginResultDTO();
|
|
|
dto.setToken(token);
|
|
dto.setToken(token);
|
|
|
dto.setUserId(user.getId());
|
|
dto.setUserId(user.getId());
|
|
@@ -278,9 +393,10 @@ public class AuthController {
|
|
|
dto.setProfile(profile);
|
|
dto.setProfile(profile);
|
|
|
|
|
|
|
|
// 🔧 跨小程序合并检测(仅在 train-backend 侧生效,cfc 只读不受影响)
|
|
// 🔧 跨小程序合并检测(仅在 train-backend 侧生效,cfc 只读不受影响)
|
|
|
|
|
+ // SSO 兑换无 openid(null),跳过合并检测,避免 NPE
|
|
|
Boolean crossProgramMerge = false;
|
|
Boolean crossProgramMerge = false;
|
|
|
Long mergedUserId = null;
|
|
Long mergedUserId = null;
|
|
|
- if (user.getCfcUserId() != null) {
|
|
|
|
|
|
|
+ if (user.getCfcUserId() != null && StringUtils.hasText(openid)) {
|
|
|
CfcUser existingCfc = cfcUserMapper.selectById(user.getCfcUserId());
|
|
CfcUser existingCfc = cfcUserMapper.selectById(user.getCfcUserId());
|
|
|
if (existingCfc != null && !openid.equals(existingCfc.getOpenid())) {
|
|
if (existingCfc != null && !openid.equals(existingCfc.getOpenid())) {
|
|
|
crossProgramMerge = true;
|
|
crossProgramMerge = true;
|