|
|
@@ -251,6 +251,64 @@ public class AuthController {
|
|
|
return Result.success(buildLoginResult(user, token, cfcUser, isNew(user), null));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 跨小程序手动绑定:按手机号关联 cfc 已有会员。
|
|
|
+ * 适用于:用户在爱伴已注册(openid_B),但在浠艾福已注册(openid_A,手机号相同)。
|
|
|
+ * 逻辑:
|
|
|
+ * 1. 取当前登录 userId → 查 train_user
|
|
|
+ * 2. 按 phone 在 cfc users 中查找已有会员
|
|
|
+ * 3. 该会员即当前已关联会员 → 提示已绑定;否则更新 train_user.cfcUserId + phone
|
|
|
+ * 4. 返回最新 buildLoginResult(含 totalPoints/memberLevel/referralCode)
|
|
|
+ */
|
|
|
+ @Operation(summary = "跨小程序绑定(按手机号关联 cfc 已有会员)")
|
|
|
+ @PostMapping("/bind-cross-program")
|
|
|
+ public Result<LoginResultDTO> bindCrossProgram(@RequestBody Map<String, String> body,
|
|
|
+ @org.springframework.web.bind.annotation.RequestAttribute("userId") Long userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return Result.error(401, "请先登录");
|
|
|
+ }
|
|
|
+ String phone = body != null ? body.get("phone") : null;
|
|
|
+ if (!StringUtils.hasText(phone) || !phone.matches("^1[3-9]\\d{9}$")) {
|
|
|
+ return Result.error("手机号格式不正确");
|
|
|
+ }
|
|
|
+ TrainUser user = trainUserMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ return Result.error("用户不存在");
|
|
|
+ }
|
|
|
+ // 按 phone 查 cfc 已有会员
|
|
|
+ CfcUser targetCfc = cfcUserMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<CfcUser>()
|
|
|
+ .eq(CfcUser::getPhone, phone)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (targetCfc == null) {
|
|
|
+ return Result.error("未找到该手机号对应的浠艾福账号");
|
|
|
+ }
|
|
|
+ // 该手机号对应的就是当前已关联的会员 → 已绑定,无需重复操作
|
|
|
+ if (user.getCfcUserId() != null && user.getCfcUserId().equals(targetCfc.getId())) {
|
|
|
+ return Result.error("该手机号已与当前账号绑定");
|
|
|
+ }
|
|
|
+ // 更新 train_user.cfcUserId + phone
|
|
|
+ user.setCfcUserId(targetCfc.getId());
|
|
|
+ if (!phone.equals(user.getPhone())) {
|
|
|
+ user.setPhone(phone);
|
|
|
+ }
|
|
|
+ user.setUpdatedAt(new Date());
|
|
|
+ trainUserMapper.updateById(user);
|
|
|
+ // 同步 phone 到 cfc 会员(若 cfc 会员 phone 尚未设置)
|
|
|
+ if (!phone.equals(targetCfc.getPhone())) {
|
|
|
+ targetCfc.setPhone(phone);
|
|
|
+ targetCfc.setUpdatedAt(new Date());
|
|
|
+ try {
|
|
|
+ cfcUserMapper.updateById(targetCfc);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("绑定跨小程序时同步 cfc phone 失败: cfcUserId={}, err={}", targetCfc.getId(), e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("跨小程序绑定: trainUid={} 已关联 cfcUserId={}(phone={})", user.getId(), targetCfc.getId(), phone);
|
|
|
+ String token = jwtConfig.generateToken(user.getId(), user.getRole());
|
|
|
+ return Result.success(buildLoginResult(user, token, targetCfc, isNew(user), user.getOpenid()));
|
|
|
+ }
|
|
|
+
|
|
|
// ========== 内部方法 ==========
|
|
|
|
|
|
private CfcUser upsertCfcUser(String openid, String unionid) {
|
|
|
@@ -300,9 +358,19 @@ public class AuthController {
|
|
|
trainUserMapper.insert(user);
|
|
|
} else {
|
|
|
boolean dirty = false;
|
|
|
+ // cfcUserId 为空则回填
|
|
|
if (user.getCfcUserId() == null) {
|
|
|
user.setCfcUserId(cfcUser.getId());
|
|
|
dirty = true;
|
|
|
+ } else {
|
|
|
+ // phone 冲突复用场景:cfc 侧已指向另一个会员(如跨小程序),
|
|
|
+ // 强制更新 cfcUserId,避免历史数据遗留导致 train_user 仍指向旧 cfc 用户。
|
|
|
+ if (!user.getCfcUserId().equals(cfcUser.getId())) {
|
|
|
+ log.warn("train_user.id={} cfcUserId {} 与本次登录 cfcUser {} 不一致(phone 冲突复用),强制更新",
|
|
|
+ user.getId(), user.getCfcUserId(), cfcUser.getId());
|
|
|
+ user.setCfcUserId(cfcUser.getId());
|
|
|
+ dirty = true;
|
|
|
+ }
|
|
|
}
|
|
|
if (StringUtils.hasText(unionid) && !StringUtils.hasText(user.getUnionid())) {
|
|
|
user.setUnionid(unionid);
|