|
|
@@ -10,22 +10,12 @@ import com.aijiuyi.admin.common.util.RedisUtil;
|
|
|
import com.aijiuyi.admin.common.util.SmsUtil;
|
|
|
import com.aijiuyi.admin.common.util.TokenUtil;
|
|
|
import com.aijiuyi.admin.controller.dto.AppLoginDTO;
|
|
|
-import com.aijiuyi.admin.controller.dto.AppUserQueryDTO;
|
|
|
-import com.aijiuyi.admin.controller.dto.LoginDTO;
|
|
|
-import com.aijiuyi.admin.entity.AdminPermission;
|
|
|
import com.aijiuyi.admin.entity.AppUser;
|
|
|
-import com.aijiuyi.admin.entity.UserCategory;
|
|
|
-import com.aijiuyi.admin.mapper.AdminPermissionMapper;
|
|
|
import com.aijiuyi.admin.mapper.AppUserMapper;
|
|
|
-import com.aijiuyi.admin.mapper.UserCategoryMapper;
|
|
|
import com.aijiuyi.admin.service.AppUserService;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.DigestUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -33,14 +23,12 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- * 应用用户 Service 实现类
|
|
|
- * 统一用户中心:用户、专家、管理员均使用此表
|
|
|
+ * APP用户账号 Service 实现类
|
|
|
+ * 仅处理APP端手机号登录、短信验证码等逻辑
|
|
|
*/
|
|
|
@Service
|
|
|
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {
|
|
|
|
|
|
- private static final int NORMAL_USER_TYPE = 1;
|
|
|
-
|
|
|
/** Token 在 Redis 中的 key 前缀 */
|
|
|
private static final String TOKEN_REDIS_PREFIX = TokenInterceptor.REDIS_TOKEN_KEY_PREFIX;
|
|
|
|
|
|
@@ -65,10 +53,7 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
/** 短信发送限流:60秒内不重复发送 */
|
|
|
private static final long SMS_LIMIT_SECONDS = 60L;
|
|
|
|
|
|
- /**
|
|
|
- * 短信模板 Code(阿里云短信控制台配置的模板)
|
|
|
- * TODO: 替换为实际申请的验证码模板 Code
|
|
|
- */
|
|
|
+ /** 短信模板 Code */
|
|
|
private static final String SMS_TEMPLATE_CODE = "SMS_316430242";
|
|
|
|
|
|
/** 开发调试用万能验证码(仅 dev/test 环境生效) */
|
|
|
@@ -80,297 +65,8 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
@Autowired
|
|
|
private RedisUtil redisUtil;
|
|
|
|
|
|
- @Autowired
|
|
|
- private AdminPermissionMapper adminPermissionMapper;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private UserCategoryMapper userCategoryMapper;
|
|
|
-
|
|
|
- /**
|
|
|
- * 管理员登录
|
|
|
- * 通过用户名+密码校验身份,生成 JWT Token 并存入 Redis
|
|
|
- *
|
|
|
- * @param loginDTO 登录参数
|
|
|
- * @return 包含 token 和用户基本信息的 Result
|
|
|
- */
|
|
|
- @Override
|
|
|
- public Result<Map<String, Object>> login(LoginDTO loginDTO) {
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "用户[{}]尝试登录", loginDTO.getUsername());
|
|
|
-
|
|
|
- // 根据用户名查询用户(仅管理员可通过用户名登录)
|
|
|
- AppUser user = lambdaQuery()
|
|
|
- .eq(AppUser::getUsername, loginDTO.getUsername())
|
|
|
- .one();
|
|
|
-
|
|
|
- // 用户不存在
|
|
|
- if (user == null) {
|
|
|
- LogUtil.warn(AppUserServiceImpl.class, "用户[{}]不存在", loginDTO.getUsername());
|
|
|
- throw new BusinessException(ResultCode.USER_NOT_FOUND);
|
|
|
- }
|
|
|
-
|
|
|
- // 验证密码(前端传明文,后端 MD5 加密后比对)
|
|
|
- String md5Password = DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes());
|
|
|
- if (!md5Password.equals(user.getPassword())) {
|
|
|
- LogUtil.warn(AppUserServiceImpl.class, "用户[{}]密码错误", loginDTO.getUsername());
|
|
|
- throw new BusinessException(ResultCode.USER_PASSWORD_ERROR);
|
|
|
- }
|
|
|
-
|
|
|
- // 验证用户状态
|
|
|
- if (user.getStatus() != null && user.getStatus() == 0) {
|
|
|
- LogUtil.warn(AppUserServiceImpl.class, "用户[{}]已被禁用", loginDTO.getUsername());
|
|
|
- throw new BusinessException(ResultCode.USER_DISABLED);
|
|
|
- }
|
|
|
-
|
|
|
- // 验证角色(仅 userType=3=管理员 可登录后台)
|
|
|
- if (user.getUserType() == null || user.getUserType() != 3) {
|
|
|
- LogUtil.warn(AppUserServiceImpl.class, "用户[{}]无后台登录权限", loginDTO.getUsername());
|
|
|
- throw new BusinessException(ResultCode.FORBIDDEN);
|
|
|
- }
|
|
|
-
|
|
|
- // 生成 JWT Token(携带 userType)
|
|
|
- String token = TokenUtil.generateToken(user.getId(), user.getUsername(), user.getUserType());
|
|
|
-
|
|
|
- // 将 Token 存入 Redis,3年过期
|
|
|
- redisUtil.set(TOKEN_REDIS_PREFIX + token, user.getId(), TOKEN_EXPIRE_SECONDS);
|
|
|
-
|
|
|
- // 更新最后登录时间
|
|
|
- AppUser updateLogin = new AppUser();
|
|
|
- updateLogin.setId(user.getId());
|
|
|
- updateLogin.setLastLoginTime(LocalDateTime.now());
|
|
|
- updateById(updateLogin);
|
|
|
-
|
|
|
- // 登录成功后将用户信息写入 RequestContext,供 LogAspect 正确记录操作人
|
|
|
- RequestContext.setUserId(user.getId());
|
|
|
- RequestContext.setUserName(user.getUsername());
|
|
|
- RequestContext.setUserType(user.getUserType());
|
|
|
-
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "用户[{}]登录成功", user.getUsername());
|
|
|
-
|
|
|
- // 查询权限路由(超级管理员返回 null,代表全部权限)
|
|
|
- String routePaths = null;
|
|
|
- if (!Integer.valueOf(1).equals(user.getAdminRole())) {
|
|
|
- AdminPermission permission = adminPermissionMapper.selectOne(
|
|
|
- new LambdaQueryWrapper<AdminPermission>()
|
|
|
- .eq(AdminPermission::getAdminId, user.getId())
|
|
|
- );
|
|
|
- routePaths = permission != null ? permission.getRoutePaths() : "[]";
|
|
|
- }
|
|
|
-
|
|
|
- // 组装返回数据
|
|
|
- Map<String, Object> resultData = new HashMap<>(8);
|
|
|
- resultData.put("token", token);
|
|
|
- resultData.put("userId", user.getId());
|
|
|
- resultData.put("username", user.getUsername());
|
|
|
- resultData.put("nickname", user.getNickname());
|
|
|
- resultData.put("avatar", user.getAvatar());
|
|
|
- resultData.put("userType", user.getUserType());
|
|
|
- resultData.put("adminRole", user.getAdminRole());
|
|
|
- resultData.put("permissions", routePaths);
|
|
|
-
|
|
|
- return Result.success(resultData);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 退出登录
|
|
|
- * 从 Redis 中删除 Token,使 Token 立即失效
|
|
|
- *
|
|
|
- * @param token 当前用户的 Token
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void logout(String token) {
|
|
|
- if (StringUtils.hasText(token)) {
|
|
|
- // 去掉 Bearer 前缀
|
|
|
- if (token.startsWith("Bearer ")) {
|
|
|
- token = token.substring(7);
|
|
|
- }
|
|
|
- // 从 Redis 删除 Token
|
|
|
- redisUtil.del(TOKEN_REDIS_PREFIX + token);
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "用户退出登录,Token 已失效");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 分页查询应用用户列表
|
|
|
- *
|
|
|
- * @param queryDTO 查询条件
|
|
|
- * @return 分页结果
|
|
|
- */
|
|
|
- @Override
|
|
|
- public IPage<AppUser> pageList(AppUserQueryDTO queryDTO) {
|
|
|
- Page<AppUser> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
|
|
|
- LambdaQueryWrapper<AppUser> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.like(StringUtils.hasText(queryDTO.getPhone()), AppUser::getPhone, queryDTO.getPhone())
|
|
|
- .like(StringUtils.hasText(queryDTO.getNickname()), AppUser::getNickname, queryDTO.getNickname())
|
|
|
- .eq(queryDTO.getStatus() != null, AppUser::getStatus, queryDTO.getStatus());
|
|
|
- if (queryDTO.getUserCategoryId() != null) {
|
|
|
- wrapper.eq(AppUser::getUserType, NORMAL_USER_TYPE)
|
|
|
- .eq(AppUser::getUserCategoryId, queryDTO.getUserCategoryId());
|
|
|
- } else {
|
|
|
- wrapper.eq(queryDTO.getUserType() != null, AppUser::getUserType, queryDTO.getUserType());
|
|
|
- }
|
|
|
- wrapper.orderByDesc(AppUser::getCreateTime);
|
|
|
- return page(page, wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修改用户状态(禁用/启用)
|
|
|
- *
|
|
|
- * @param id 用户ID
|
|
|
- * @param status 目标状态:1=启用,0=禁用
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void updateStatus(Long id, Integer status) {
|
|
|
- AppUser user = getById(id);
|
|
|
- if (user == null) {
|
|
|
- throw new BusinessException(ResultCode.APP_USER_NOT_FOUND);
|
|
|
- }
|
|
|
- AppUser update = new AppUser();
|
|
|
- update.setId(id);
|
|
|
- update.setStatus(status);
|
|
|
- updateById(update);
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "应用用户[{}]状态修改为[{}]", id, status);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 新增应用用户
|
|
|
- * 若为管理员(userType=3)则校验用户名唯一性并对密码做 MD5 加密
|
|
|
- *
|
|
|
- * @param appUser 用户信息
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void addUser(AppUser appUser) {
|
|
|
- normalizeUserCategory(appUser.getUserType(), appUser);
|
|
|
- // 手机号唯一性校验(手机号不为空时校验)
|
|
|
- if (StringUtils.hasText(appUser.getPhone())) {
|
|
|
- long phoneCount = lambdaQuery().eq(AppUser::getPhone, appUser.getPhone()).count();
|
|
|
- if (phoneCount > 0) {
|
|
|
- throw new BusinessException(ResultCode.PHONE_EXISTS);
|
|
|
- }
|
|
|
- }
|
|
|
- // 管理员需校验用户名唯一性,并对密码加密
|
|
|
- if (Integer.valueOf(3).equals(appUser.getUserType())) {
|
|
|
- if (StringUtils.hasText(appUser.getUsername())) {
|
|
|
- long usernameCount = lambdaQuery().eq(AppUser::getUsername, appUser.getUsername()).count();
|
|
|
- if (usernameCount > 0) {
|
|
|
- throw new BusinessException(ResultCode.USERNAME_EXISTS);
|
|
|
- }
|
|
|
- }
|
|
|
- // 密码 MD5 加密
|
|
|
- if (StringUtils.hasText(appUser.getPassword())) {
|
|
|
- appUser.setPassword(DigestUtils.md5DigestAsHex(appUser.getPassword().getBytes()));
|
|
|
- }
|
|
|
- }
|
|
|
- save(appUser);
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "新增应用用户,手机号[{}],类型[{}]", appUser.getPhone(), appUser.getUserType());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修改应用用户
|
|
|
- *
|
|
|
- * @param appUser 用户信息
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void updateUser(AppUser appUser) {
|
|
|
- AppUser exist = getById(appUser.getId());
|
|
|
- if (exist == null) {
|
|
|
- throw new BusinessException(ResultCode.APP_USER_NOT_FOUND);
|
|
|
- }
|
|
|
- Integer targetUserType = appUser.getUserType() != null ? appUser.getUserType() : exist.getUserType();
|
|
|
- normalizeUserCategory(targetUserType, appUser);
|
|
|
- boolean shouldClearUserCategory = !Integer.valueOf(NORMAL_USER_TYPE).equals(targetUserType);
|
|
|
- // 如果修改了手机号,校验唯一性
|
|
|
- if (StringUtils.hasText(appUser.getPhone()) && !appUser.getPhone().equals(exist.getPhone())) {
|
|
|
- long count = lambdaQuery().eq(AppUser::getPhone, appUser.getPhone()).count();
|
|
|
- if (count > 0) {
|
|
|
- throw new BusinessException(ResultCode.PHONE_EXISTS);
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果修改了用户名,校验唯一性
|
|
|
- if (StringUtils.hasText(appUser.getUsername()) && !appUser.getUsername().equals(exist.getUsername())) {
|
|
|
- long count = lambdaQuery().eq(AppUser::getUsername, appUser.getUsername()).count();
|
|
|
- if (count > 0) {
|
|
|
- throw new BusinessException(ResultCode.USERNAME_EXISTS);
|
|
|
- }
|
|
|
- }
|
|
|
- // 如果修改了密码,重新加密
|
|
|
- if (StringUtils.hasText(appUser.getPassword())) {
|
|
|
- appUser.setPassword(DigestUtils.md5DigestAsHex(appUser.getPassword().getBytes()));
|
|
|
- } else {
|
|
|
- // 不修改密码字段
|
|
|
- appUser.setPassword(null);
|
|
|
- }
|
|
|
- updateById(appUser);
|
|
|
- if (shouldClearUserCategory) {
|
|
|
- lambdaUpdate()
|
|
|
- .set(AppUser::getUserCategoryId, null)
|
|
|
- .eq(AppUser::getId, appUser.getId())
|
|
|
- .update();
|
|
|
- }
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "修改应用用户[{}]", appUser.getId());
|
|
|
- }
|
|
|
-
|
|
|
- private void normalizeUserCategory(Integer userType, AppUser appUser) {
|
|
|
- if (Integer.valueOf(NORMAL_USER_TYPE).equals(userType)) {
|
|
|
- if (appUser.getUserCategoryId() == null) {
|
|
|
- appUser.setUserCategoryId(getDefaultUserCategoryId());
|
|
|
- } else {
|
|
|
- validateUserCategory(appUser.getUserCategoryId());
|
|
|
- }
|
|
|
- return;
|
|
|
- }
|
|
|
- appUser.setUserCategoryId(null);
|
|
|
- }
|
|
|
-
|
|
|
- private Long getDefaultUserCategoryId() {
|
|
|
- UserCategory category = userCategoryMapper.selectOne(new LambdaQueryWrapper<UserCategory>()
|
|
|
- .eq(UserCategory::getUserType, NORMAL_USER_TYPE)
|
|
|
- .eq(UserCategory::getName, "普通用户")
|
|
|
- .last("LIMIT 1"));
|
|
|
- if (category == null) {
|
|
|
- category = userCategoryMapper.selectOne(new LambdaQueryWrapper<UserCategory>()
|
|
|
- .eq(UserCategory::getUserType, NORMAL_USER_TYPE)
|
|
|
- .orderByAsc(UserCategory::getId)
|
|
|
- .last("LIMIT 1"));
|
|
|
- }
|
|
|
- return category != null ? category.getId() : null;
|
|
|
- }
|
|
|
-
|
|
|
- private void validateUserCategory(Long userCategoryId) {
|
|
|
- if (userCategoryId == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- long count = userCategoryMapper.selectCount(new LambdaQueryWrapper<UserCategory>()
|
|
|
- .eq(UserCategory::getId, userCategoryId)
|
|
|
- .eq(UserCategory::getUserType, NORMAL_USER_TYPE));
|
|
|
- if (count <= 0) {
|
|
|
- throw new BusinessException(ResultCode.USER_CATEGORY_NOT_FOUND);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除应用用户(逻辑删除)
|
|
|
- *
|
|
|
- * @param id 用户ID
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void deleteUser(Long id) {
|
|
|
- AppUser user = getById(id);
|
|
|
- if (user == null) {
|
|
|
- throw new BusinessException(ResultCode.APP_USER_NOT_FOUND);
|
|
|
- }
|
|
|
- removeById(id);
|
|
|
- LogUtil.info(AppUserServiceImpl.class, "删除应用用户[{}]", id);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* App 端发送短信验证码
|
|
|
- * 处理逻辑:
|
|
|
- * 1. 检60秒限流(同一手机号内不重复发送)
|
|
|
- * 2. 生成6位随机验证码
|
|
|
- * 3. 调用阿里云短信发送
|
|
|
- * 4. 将验证码存入 Redis,有效期 5 分钟
|
|
|
- *
|
|
|
- * @param phone 手机号
|
|
|
*/
|
|
|
@Override
|
|
|
public void sendSmsCode(String phone) {
|
|
|
@@ -380,13 +76,13 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
LogUtil.info(AppUserServiceImpl.class, "[开发模式] 跳过真实短信发送, phone={}, code={}", phone, DEV_BYPASS_CODE);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 检60秒内是否已发送(限流)
|
|
|
String limitKey = SMS_LIMIT_PREFIX + phone;
|
|
|
if (redisUtil.hasKey(limitKey)) {
|
|
|
throw new BusinessException(ResultCode.SMS_SEND_TOO_FREQUENT);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 检查每日发送次数是否已达上限
|
|
|
String dailyKey = SMS_DAILY_COUNT_PREFIX + phone;
|
|
|
Object dailyCount = redisUtil.get(dailyKey);
|
|
|
@@ -394,8 +90,8 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
if (count >= SMS_DAILY_MAX_COUNT) {
|
|
|
throw new BusinessException(ResultCode.SMS_DAILY_LIMIT);
|
|
|
}
|
|
|
-
|
|
|
- // 生或6位随机验证码
|
|
|
+
|
|
|
+ // 生成6位随机验证码
|
|
|
String code = String.format("%06d", (int) (Math.random() * 1000000));
|
|
|
|
|
|
// 发送短信
|
|
|
@@ -408,13 +104,12 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
|
|
|
// 存入 Redis(5 分钟有效)
|
|
|
redisUtil.set(SMS_CODE_PREFIX + phone, code, SMS_CODE_EXPIRE_SECONDS);
|
|
|
- // 设置60秒限流(设置在发送成功后)
|
|
|
+ // 设置60秒限流
|
|
|
redisUtil.set(limitKey, "1", SMS_LIMIT_SECONDS);
|
|
|
- // 累加每日发送次数(过期时间到当天结束)
|
|
|
+ // 累加每日发送次数
|
|
|
if (redisUtil.hasKey(dailyKey)) {
|
|
|
redisUtil.set(dailyKey, String.valueOf(count + 1), redisUtil.getExpire(dailyKey));
|
|
|
} else {
|
|
|
- // 计算到当天23:59:59的剩余秒数
|
|
|
long secondsUntilMidnight = java.time.Duration.between(
|
|
|
java.time.LocalDateTime.now(),
|
|
|
java.time.LocalDate.now().plusDays(1).atStartOfDay()
|
|
|
@@ -434,15 +129,6 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
|
|
|
/**
|
|
|
* App 端手机号验证码登录
|
|
|
- * 处理逻辑:
|
|
|
- * 1. 校验验证码(是否存在、是否正确)
|
|
|
- * 2. 验证成功后删除 Redis 中的验证码(一次性)
|
|
|
- * 3. 查询用户,不存在则自动注册
|
|
|
- * 4. 生成 JWT Token 并存入 Redis(3 年有效)
|
|
|
- * 5. 更新最后登录时间
|
|
|
- *
|
|
|
- * @param loginDTO 手机号+验证码
|
|
|
- * @return 包含 token 和用户信息的 Result
|
|
|
*/
|
|
|
@Override
|
|
|
public Result<Map<String, Object>> appLogin(AppLoginDTO loginDTO) {
|
|
|
@@ -472,12 +158,10 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
user = new AppUser();
|
|
|
user.setPhone(phone);
|
|
|
user.setNickname("用户" + phone.substring(7));
|
|
|
- user.setUserType(1);
|
|
|
- user.setUserCategoryId(getDefaultUserCategoryId());
|
|
|
user.setStatus(1);
|
|
|
user.setLoginType(1);
|
|
|
save(user);
|
|
|
- // 注册成功后生成 AppId(前缀AJY + 8位用户ID)
|
|
|
+ // 注册成功后生成 AppId
|
|
|
user.setAppId("AJY" + String.format("%08d", user.getId()));
|
|
|
updateById(user);
|
|
|
LogUtil.info(AppUserServiceImpl.class, "App 新用户自动注册: phone={}, appId={}", phone, user.getAppId());
|
|
|
@@ -486,10 +170,16 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
if (user.getStatus() != null && user.getStatus() == 0) {
|
|
|
throw new BusinessException(ResultCode.APP_USER_DISABLED);
|
|
|
}
|
|
|
+ // 兼容历史数据:已有用户若未生成 app_id,登录时自动补生成
|
|
|
+ if (!StringUtils.hasText(user.getAppId())) {
|
|
|
+ user.setAppId("AJY" + String.format("%08d", user.getId()));
|
|
|
+ updateById(user);
|
|
|
+ LogUtil.info(AppUserServiceImpl.class, "App 登录时补生成 AppId: phone={}, appId={}", phone, user.getAppId());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 生成 JWT Token(username 字段存手机号)
|
|
|
- String token = TokenUtil.generateToken(user.getId(), phone, user.getUserType());
|
|
|
+ // 生成 JWT Token(userType=1 表示APP用户)
|
|
|
+ String token = TokenUtil.generateToken(user.getId(), phone, 1);
|
|
|
|
|
|
// Token 存入 Redis(3 年有效)
|
|
|
redisUtil.set(TOKEN_REDIS_PREFIX + token, user.getId(), TOKEN_EXPIRE_SECONDS);
|
|
|
@@ -501,10 +191,10 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
updateLogin.setLoginType(1);
|
|
|
updateById(updateLogin);
|
|
|
|
|
|
- // 写入请求上下文(供 LogAspect 正确记录操作人)
|
|
|
+ // 写入请求上下文
|
|
|
RequestContext.setUserId(user.getId());
|
|
|
RequestContext.setUserName(phone);
|
|
|
- RequestContext.setUserType(user.getUserType());
|
|
|
+ RequestContext.setUserType(1);
|
|
|
|
|
|
LogUtil.info(AppUserServiceImpl.class, "App 用户[{}]登录成功", phone);
|
|
|
|
|
|
@@ -515,9 +205,21 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
|
|
|
resultData.put("nickname", user.getNickname());
|
|
|
resultData.put("avatar", user.getAvatar());
|
|
|
resultData.put("appId", user.getAppId());
|
|
|
- resultData.put("userType", user.getUserType());
|
|
|
- resultData.put("userCategoryId", user.getUserCategoryId());
|
|
|
|
|
|
return Result.success(resultData);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * APP用户退出登录
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void logout(String token) {
|
|
|
+ if (StringUtils.hasText(token)) {
|
|
|
+ if (token.startsWith("Bearer ")) {
|
|
|
+ token = token.substring(7);
|
|
|
+ }
|
|
|
+ redisUtil.del(TOKEN_REDIS_PREFIX + token);
|
|
|
+ LogUtil.info(AppUserServiceImpl.class, "APP用户退出登录,Token 已失效");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|