fix:支付宝小程序登录以及支付宝支付
This commit is contained in:
+42
@@ -145,11 +145,53 @@ public class SysUserController {
|
||||
return appUserService.findByWxOpenid(openId,mobile,unionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据支付宝userId查询用户信息(Feign内部调用)
|
||||
* <p>
|
||||
* 已有用户按 openId 匹配后更新信息返回;新用户自动注册。
|
||||
*
|
||||
* @param alipayUserId 支付宝 userId(alipay.system.oauth.token 返回,必填)
|
||||
* @param unionId 支付宝 union_id(同 PID 下唯一,可选)
|
||||
* @param mobile 手机号(自动注册时填入,可选)
|
||||
* @param appId 支付宝应用ID(预留,可选)
|
||||
* @return SysUser 系统用户对象(含 id/username/mobile/openId/enabled)
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/alipayOpenId")
|
||||
@Operation(summary = "根据支付宝userId查询或注册用户(Feign内部)",
|
||||
description = "入参:alipayUserId(必填)+unionId+mobile+appId(可选)。"
|
||||
+ "出参:SysUser 对象。逻辑:已有用户更新信息,新用户自动创建。")
|
||||
@Parameter(name = "alipayUserId", description = "支付宝 userId(alipay.system.oauth.token 返回)", required = true, example = "2088xxxxxxxxxxxx")
|
||||
@Parameter(name = "unionId", description = "支付宝 union_id(同 PID 下唯一)")
|
||||
@Parameter(name = "mobile", description = "手机号(注册时填入)", example = "138xxxx1234")
|
||||
@Parameter(name = "appId", description = "支付宝应用ID(预留)", example = "2021000000000001")
|
||||
public SysUser findByAlipayOpenId(@RequestParam("alipayUserId") String alipayUserId,
|
||||
@RequestParam(required = false) String unionId,
|
||||
@RequestParam(required = false) String mobile,
|
||||
@RequestParam(required = false) String appId) throws Exception {
|
||||
return appUserService.findByAlipayOpenid(alipayUserId, unionId, mobile, appId);
|
||||
}
|
||||
|
||||
@GetMapping("/users/{id}")
|
||||
public SysUser findUserById(@PathVariable Long id) {
|
||||
return appUserService.getById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据系统用户ID查询支付宝 openId(供 order-center 下单时获取 buyerId)
|
||||
*
|
||||
* @param userId 系统用户ID
|
||||
* @param appId 支付宝应用ID
|
||||
* @return 支付宝 userId,无映射时返回 null
|
||||
*/
|
||||
@GetMapping("/users/{userId}/alipay-openid")
|
||||
@Operation(summary = "根据系统用户ID查询支付宝openId",
|
||||
description = "查询 sys_user_third_openid 映射表,获取该用户在指定支付宝应用下的 openId")
|
||||
@Parameter(name = "userId", description = "系统用户ID", required = true, example = "1001")
|
||||
@Parameter(name = "appId", description = "支付宝应用ID", required = true, example = "2021000000000001")
|
||||
public String getAlipayOpenIdByUserId(@PathVariable Long userId, @RequestParam String appId) {
|
||||
return appUserService.getAlipayOpenIdByUserId(userId, appId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理后台修改用户
|
||||
*
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.central.user.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.central.user.model.SysUserThirdOpenid;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 第三方用户 OpenId 映射 Mapper
|
||||
*
|
||||
* @author duzhichao
|
||||
* @date 2026-05-12
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysUserThirdOpenidMapper extends BaseMapper<SysUserThirdOpenid> {
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.central.user.model;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 第三方用户 OpenId 映射表
|
||||
* <p>
|
||||
* 统一管理微信/支付宝等各家 openId/userId,与 SysUser.openId 解耦。
|
||||
*
|
||||
* @author duzhichao
|
||||
* @date 2026-05-12
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_user_third_openid")
|
||||
@Schema(description = "第三方用户OpenId映射")
|
||||
public class SysUserThirdOpenid {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
@Schema(description = "主键")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "系统用户ID")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "第三方平台:WECHAT / ALIPAY", example = "ALIPAY")
|
||||
private String provider;
|
||||
|
||||
@Schema(description = "第三方应用ID")
|
||||
private String appId;
|
||||
|
||||
@Schema(description = "第三方用户唯一标识", example = "2088123456789012")
|
||||
private String openId;
|
||||
|
||||
@Schema(description = "第三方 unionId(微信 unionId / 支付宝 union_id)")
|
||||
private String unionId;
|
||||
|
||||
@Schema(description = "手机号")
|
||||
private String mobile;
|
||||
}
|
||||
@@ -31,6 +31,16 @@ public interface ISysUserService extends ISuperService<SysUser> {
|
||||
|
||||
SysUser findByWxOpenid(String wxOpenid,String mobile,String unionId) throws Exception;
|
||||
|
||||
/**
|
||||
* 根据支付宝userId查询用户(关联或自动注册)
|
||||
*/
|
||||
SysUser findByAlipayOpenid(String alipayUserId, String unionId, String mobile, String appId) throws Exception;
|
||||
|
||||
/**
|
||||
* 根据系统用户ID查询支付宝 openId(从 sys_user_third_openid 映射表)
|
||||
*/
|
||||
String getAlipayOpenIdByUserId(Long userId, String appId);
|
||||
|
||||
/**
|
||||
* 通过SysUser 把roles和permissions也查询出来
|
||||
* @param sysUser
|
||||
|
||||
+146
-10
@@ -5,9 +5,9 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.datascope.annotation.IgnoreTenantScope;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.lock.ZLock;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.SysRole;
|
||||
@@ -22,8 +22,10 @@ import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.user.feign.OrderCenterService;
|
||||
import com.central.user.mapper.SysRoleMenuMapper;
|
||||
import com.central.user.mapper.SysUserMapper;
|
||||
import com.central.user.mapper.SysUserThirdOpenidMapper;
|
||||
import com.central.user.model.SysRoleUser;
|
||||
import com.central.user.model.SysUserExcel;
|
||||
import com.central.user.model.SysUserThirdOpenid;
|
||||
import com.central.user.service.ISysRoleUserService;
|
||||
import com.central.user.service.ISysUserService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -58,6 +60,9 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
@Resource
|
||||
private SysRoleMenuMapper roleMenuMapper;
|
||||
|
||||
@Resource
|
||||
private SysUserThirdOpenidMapper sysUserThirdOpenidMapper;
|
||||
|
||||
@Autowired
|
||||
private DistributedLock lock;
|
||||
|
||||
@@ -106,15 +111,34 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
sysUser.setMobile(mobile);
|
||||
}
|
||||
} else {
|
||||
// 创建新用户
|
||||
sysUser = new SysUser();
|
||||
sysUser.setOpenId(wxOpenid);
|
||||
sysUser.setUsername(wxOpenid);
|
||||
sysUser.setType(UserType.APP.name());
|
||||
sysUser.setPassword(wxOpenid);
|
||||
sysUser.setEnabled(Boolean.TRUE);
|
||||
sysUser.setMobile(mobile);
|
||||
sysUser.setUnionId(unionId);
|
||||
// 未找到 → 先按手机号匹配已有账号(支付宝用户改用微信登录时自动关联)
|
||||
sysUser = null;
|
||||
if (StringUtils.isNotEmpty(mobile)) {
|
||||
sysUser = this.selectByMobile(mobile);
|
||||
if (ObjectUtil.isNotEmpty(sysUser)) {
|
||||
log.info("微信登录按手机号关联已有用户: userId={}, mobile={}", sysUser.getId(), mobile);
|
||||
// 将微信 openId 写入已有用户
|
||||
sysUser.setOpenId(wxOpenid);
|
||||
if (StringUtils.isNotEmpty(unionId)) {
|
||||
sysUser.setUnionId(unionId);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(mobile)) {
|
||||
sysUser.setMobile(mobile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sysUser == null) {
|
||||
// 全新用户
|
||||
sysUser = new SysUser();
|
||||
sysUser.setOpenId(wxOpenid);
|
||||
sysUser.setUsername(wxOpenid);
|
||||
sysUser.setType(UserType.APP.name());
|
||||
sysUser.setPassword(wxOpenid);
|
||||
sysUser.setEnabled(Boolean.TRUE);
|
||||
sysUser.setMobile(mobile);
|
||||
sysUser.setUnionId(unionId);
|
||||
}
|
||||
}
|
||||
|
||||
String username = sysUser.getUsername();
|
||||
@@ -126,6 +150,118 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
return result ? sysUser : new SysUser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysUser findByAlipayOpenid(String alipayUserId, String unionId, String mobile, String appId) throws Exception {
|
||||
log.info("支付宝登录: alipayUserId={}, unionId={}, mobile={}, appId={}", alipayUserId, unionId, mobile, appId);
|
||||
|
||||
// 1. 查 sys_user_third_openid 映射表(不走 SysUser.openId,避免与 grantType=openId 冲突)
|
||||
QueryWrapper<SysUserThirdOpenid> mappingQuery = new QueryWrapper<SysUserThirdOpenid>()
|
||||
.eq("provider", "ALIPAY")
|
||||
.eq("app_id", appId)
|
||||
.eq("open_id", alipayUserId);
|
||||
SysUserThirdOpenid mapping = sysUserThirdOpenidMapper.selectOne(mappingQuery);
|
||||
|
||||
SysUser sysUser = null;
|
||||
if (mapping != null) {
|
||||
// 已有映射 → 查到对应的 SysUser
|
||||
sysUser = baseMapper.selectById(mapping.getUserId());
|
||||
if (sysUser == null) {
|
||||
log.error("sys_user_third_openid 映射存在但 SysUser 不存在: userId={}", mapping.getUserId());
|
||||
throw new RuntimeException("用户数据异常");
|
||||
}
|
||||
// 更新映射信息
|
||||
if (StringUtils.isNotEmpty(mobile)) {
|
||||
sysUser.setMobile(mobile);
|
||||
mapping.setMobile(mobile);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(unionId)) {
|
||||
mapping.setUnionId(unionId);
|
||||
}
|
||||
sysUserThirdOpenidMapper.updateById(mapping);
|
||||
log.info("支付宝用户已存在: userId={}, alipayUserId={}", sysUser.getId(), alipayUserId);
|
||||
} else {
|
||||
// 无映射 → 先按手机号匹配已有账号(分布式锁防并发重复创建)
|
||||
String lockKey = "alipay:user:" + alipayUserId;
|
||||
ZLock zLock = null;
|
||||
try {
|
||||
zLock = lock.tryLock(lockKey, 5, java.util.concurrent.TimeUnit.SECONDS);
|
||||
if (zLock == null) {
|
||||
log.warn("获取分布式锁失败,可能并发登录: {}", alipayUserId);
|
||||
throw new RuntimeException("系统繁忙,请稍后重试");
|
||||
}
|
||||
// 双重检查:获取锁后再查一次映射表
|
||||
SysUserThirdOpenid recheck = sysUserThirdOpenidMapper.selectOne(mappingQuery);
|
||||
if (recheck != null) {
|
||||
sysUser = baseMapper.selectById(recheck.getUserId());
|
||||
} else if (StringUtils.isNotEmpty(mobile)) {
|
||||
sysUser = selectByMobile(mobile);
|
||||
if (ObjectUtil.isNotEmpty(sysUser)) {
|
||||
log.info("按手机号关联已有用户: userId={}, mobile={}", sysUser.getId(), mobile);
|
||||
sysUser.setMobile(mobile);
|
||||
super.saveOrUpdateIdempotency(sysUser, lock,
|
||||
LOCK_KEY_USERNAME + sysUser.getUsername(),
|
||||
new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()),
|
||||
sysUser.getUsername() + "已存在");
|
||||
insertThirdOpenidMapping(sysUser.getId(), "ALIPAY", appId, alipayUserId, unionId, mobile);
|
||||
}
|
||||
}
|
||||
if (sysUser == null) {
|
||||
sysUser = new SysUser();
|
||||
sysUser.setUsername("alipay_" + alipayUserId);
|
||||
sysUser.setType(UserType.APP.name());
|
||||
sysUser.setPassword(alipayUserId);
|
||||
sysUser.setEnabled(Boolean.TRUE);
|
||||
sysUser.setMobile(mobile);
|
||||
boolean saved = super.saveOrUpdateIdempotency(sysUser, lock,
|
||||
LOCK_KEY_USERNAME + sysUser.getUsername(),
|
||||
new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()),
|
||||
sysUser.getUsername() + "已存在");
|
||||
if (!saved) { return new SysUser(); }
|
||||
log.info("创建支付宝新用户: userId={}, alipayUserId={}", sysUser.getId(), alipayUserId);
|
||||
insertThirdOpenidMapping(sysUser.getId(), "ALIPAY", appId, alipayUserId, unionId, mobile);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("支付宝用户关联加锁异常", e);
|
||||
throw new RuntimeException("登录失败: " + e.getMessage(), e);
|
||||
} finally {
|
||||
if (zLock != null) {
|
||||
try { lock.unlock(zLock); } catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAlipayOpenIdByUserId(Long userId, String appId) {
|
||||
QueryWrapper<SysUserThirdOpenid> query = new QueryWrapper<SysUserThirdOpenid>()
|
||||
.eq("user_id", userId)
|
||||
.eq("provider", "ALIPAY")
|
||||
.eq("app_id", appId);
|
||||
SysUserThirdOpenid mapping = sysUserThirdOpenidMapper.selectOne(query);
|
||||
return mapping != null ? mapping.getOpenId() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入 sys_user_third_openid 映射记录(依赖 DB 唯一索引 uk_provider_appid_openid 保证幂等)
|
||||
*/
|
||||
private void insertThirdOpenidMapping(Long userId, String provider, String appId,
|
||||
String openId, String unionId, String mobile) {
|
||||
try {
|
||||
SysUserThirdOpenid mapping = new SysUserThirdOpenid();
|
||||
mapping.setUserId(userId);
|
||||
mapping.setProvider(provider);
|
||||
mapping.setAppId(appId);
|
||||
mapping.setOpenId(openId);
|
||||
mapping.setUnionId(unionId);
|
||||
mapping.setMobile(mobile);
|
||||
sysUserThirdOpenidMapper.insert(mapping);
|
||||
} catch (org.springframework.dao.DuplicateKeyException e) {
|
||||
log.info("sys_user_third_openid 映射已存在, userId={}, provider={}, openId={}", userId, provider, openId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserPermission(SysUser sysUser) {
|
||||
if (sysUser != null) {
|
||||
|
||||
Reference in New Issue
Block a user