diff --git a/hiver-core/src/main/java/cc/hiver/core/common/constant/WorkerConstant.java b/hiver-core/src/main/java/cc/hiver/core/common/constant/WorkerConstant.java index 2caf5638..1e3c1e31 100644 --- a/hiver-core/src/main/java/cc/hiver/core/common/constant/WorkerConstant.java +++ b/hiver-core/src/main/java/cc/hiver/core/common/constant/WorkerConstant.java @@ -14,11 +14,11 @@ public interface WorkerConstant { /** * 抢单工不可接单状态(押金不足) */ - Integer USER_STATUS_LOCK = 2; + Integer WORKER_STATUS_LOCK = 2; /** * 抢单工禁用状态(管理员直接禁用) */ - Integer USER_STATUS_DISABLE = 3; + Integer WORKER_STATUS_DISABLE = 3; } diff --git a/hiver-core/src/main/java/cc/hiver/core/common/utils/SecurityUtil.java b/hiver-core/src/main/java/cc/hiver/core/common/utils/SecurityUtil.java index 8aabc279..3b7f66cd 100644 --- a/hiver-core/src/main/java/cc/hiver/core/common/utils/SecurityUtil.java +++ b/hiver-core/src/main/java/cc/hiver/core/common/utils/SecurityUtil.java @@ -98,9 +98,25 @@ public class SecurityUtil { return user; } + public Worker findWorkerByUsername(String username) { + String key = "workername::" + username; + // 读取缓存 + String res = redisTemplate.get(key); + if (StrUtil.isNotBlank(res)) { + return new Gson().fromJson(res, Worker.class); + } + Worker worker = workerDao.findByUsername(username); + // 缓存 + redisTemplate.set(key, new Gson().toJson(worker), 15L, TimeUnit.DAYS); + return worker; + } + public User findUserByMobile(String mobile) { return userToDTO(userDao.findByMobile(mobile)); } + public Worker findWorkerByMobile(String mobile) { + return workerDao.findByMobile(mobile); + } public User findUserByEmail(String email) { return userToDTO(userDao.findByEmail(email)); @@ -145,6 +161,25 @@ public class SecurityUtil { return user; } + public Worker checkWorkerPassword(String username, String password) { + Worker worker; + // 校验用户名 + if (NameUtil.mobile(username)) { + worker = findWorkerByMobile(username); + } else { + worker = findWorkerByUsername(username); + } + if (worker == null) { + return null; + } + // 校验密码 + Boolean isValid = new BCryptPasswordEncoder().matches(password, worker.getPassword()); + if (!isValid) { + return null; + } + return worker; + } + public String getToken(String username, Boolean saveLogin) { if (StrUtil.isBlank(username)) { throw new HiverException("username不能为空"); @@ -221,6 +256,20 @@ public class SecurityUtil { return findUserByUsername(authentication.getName()); } + /** + * 获取当前登录用户 包含所有信息 + * + * @return + */ + public Worker getCurrWorker() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication == null || !authentication.isAuthenticated() || authentication.getName() == null + || authentication instanceof AnonymousAuthenticationToken) { + throw new HiverException("未检测到登录用户"); + } + return findWorkerByUsername(authentication.getName()); + } + /** * 获取当前登录用户部分基本信息 id、username、nickname、mobile、email、departmentId、type、permissions(角色和菜单名) * @@ -464,7 +513,7 @@ public class SecurityUtil { if (worker == null) { throw new HiverException("worker不能为空"); } - if (WorkerConstant.USER_STATUS_DISABLE.equals(worker.getWorkerStatus())) { + if (WorkerConstant.WORKER_STATUS_DISABLE.equals(worker.getWorkerStatus())) { throw new HiverException("账户被禁用,请联系管理员"); } diff --git a/hiver-core/src/main/java/cc/hiver/core/dao/WorkerDao.java b/hiver-core/src/main/java/cc/hiver/core/dao/WorkerDao.java index 44a2de26..170dba42 100644 --- a/hiver-core/src/main/java/cc/hiver/core/dao/WorkerDao.java +++ b/hiver-core/src/main/java/cc/hiver/core/dao/WorkerDao.java @@ -8,4 +8,6 @@ public interface WorkerDao extends HiverBaseDao { Worker findByMobile(String mobile); Worker findByWorkerId(String workerId); + + Worker findByUsername(String username); } \ No newline at end of file diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/common/constant/WorkerConstant.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/common/constant/WorkerConstant.java deleted file mode 100644 index bb4a497c..00000000 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/common/constant/WorkerConstant.java +++ /dev/null @@ -1,24 +0,0 @@ -package cc.hiver.mall.common.constant; - -/** - * 用户常量 - * - * @author Yazhi Li - */ -public interface WorkerConstant { - - /** - * 抢单工正常抢单状态 - */ - Integer WORKER_STATUS_NORMAL = 1; - /** - * 抢单工不可接单状态(押金不足) - */ - Integer USER_STATUS_LOCK = 2; - - /** - * 抢单工禁用状态(管理员直接禁用) - */ - Integer USER_STATUS_DISABLE = 3; - -} diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/WorkerController.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/WorkerController.java index b6265d17..9f0982ff 100644 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/WorkerController.java +++ b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/WorkerController.java @@ -1,10 +1,10 @@ package cc.hiver.mall.controller; +import cc.hiver.core.common.constant.WorkerConstant; import cc.hiver.core.common.utils.*; import cc.hiver.core.entity.Worker; import cc.hiver.core.service.WorkerService; -import cc.hiver.mall.common.constant.WorkerConstant; import cc.hiver.core.common.vo.PageVo; import cc.hiver.core.common.vo.Result; import cc.hiver.core.common.vo.SearchVo; @@ -122,7 +122,7 @@ public class WorkerController { @ApiOperation(value = "后台人工禁用抢单工") public Result disable(@ApiParam("用户唯一id标识") @PathVariable String workerId) { Worker worker = workerService.get(workerId); - worker.setWorkerStatus(WorkerConstant.USER_STATUS_DISABLE); + worker.setWorkerStatus(WorkerConstant.WORKER_STATUS_DISABLE); workerService.update(worker); // 手动更新缓存 //redisTemplate.delete(WORKER + worker.getWorkerId());