|
@@ -1,30 +1,44 @@
|
|
|
package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
|
|
|
+import cn.dev33.satoken.stp.SaTokenInfo;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.hfln.framework.common.redis.service.RedisService;
|
|
|
import cn.hfln.framework.extension.BizException;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.hfln.portal.common.constant.redis.RedisCacheConstant;
|
|
|
import com.hfln.portal.common.dto.DevInfoImportDto;
|
|
|
import com.hfln.portal.common.dto.data.device.DeviceDTO;
|
|
|
+import com.hfln.portal.common.request.admin.AdminLoginParam;
|
|
|
+import com.hfln.portal.common.request.admin.AdminResetParam;
|
|
|
+import com.hfln.portal.common.request.admin.DeviceAddParam;
|
|
|
import com.hfln.portal.common.request.device.DeviceListQueryReq;
|
|
|
+import com.hfln.portal.common.response.admin.AdminLoginRes;
|
|
|
import com.hfln.portal.common.vo.PageRecord;
|
|
|
import com.hfln.portal.common.vo.UploadRes;
|
|
|
+import com.hfln.portal.domain.customer.DeviceType;
|
|
|
import com.hfln.portal.domain.customer.util.CopyUtils;
|
|
|
+import com.hfln.portal.domain.customer.util.PasswordUtil;
|
|
|
import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
import com.hfln.portal.domain.gateway.AdminGateway;
|
|
|
-import com.hfln.portal.infrastructure.mqtt.MqttClient;
|
|
|
-import com.hfln.portal.infrastructure.oss.OssClient;
|
|
|
+import com.hfln.portal.domain.gateway.sms.SmsGateway;
|
|
|
+import com.hfln.portal.infrastructure.po.AdminUserInfo;
|
|
|
import com.hfln.portal.infrastructure.po.BasePO;
|
|
|
import com.hfln.portal.infrastructure.po.DevInfo;
|
|
|
-import com.hfln.portal.infrastructure.service.*;
|
|
|
+import com.hfln.portal.infrastructure.service.AdminUserService;
|
|
|
+import com.hfln.portal.infrastructure.service.DevInfoService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -32,38 +46,16 @@ import java.util.stream.Collectors;
|
|
|
public class AdminGatewayImpl implements AdminGateway {
|
|
|
|
|
|
@Autowired
|
|
|
- private DevShareService devShareService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
private DevInfoService devInfoService;
|
|
|
|
|
|
@Autowired
|
|
|
- private GroupShareService groupShareService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DevGroupService devGroupService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private EventService eventService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private OssFileService ossFileService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DevRoomService devRoomService;
|
|
|
+ private RedisService redisService;
|
|
|
|
|
|
@Autowired
|
|
|
- private UserService userService;
|
|
|
+ private AdminUserService adminUserService;
|
|
|
|
|
|
@Autowired
|
|
|
- private MqttClient mqttClient;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private OssClient ossClient;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private AlarmEventService alarmEventService;
|
|
|
-
|
|
|
+ private SmsGateway smsGateway;
|
|
|
|
|
|
@Override
|
|
|
public UploadRes uploadDev(MultipartFile file) throws IOException {
|
|
@@ -120,17 +112,17 @@ public class AdminGatewayImpl implements AdminGateway {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void addOneDevice(DeviceDTO deviceDTO) {
|
|
|
+ public void addOneDevice(DeviceAddParam deviceAddParam) {
|
|
|
|
|
|
- DevInfo devInfo = devInfoService.queryOneByClientId(deviceDTO.getClientId());
|
|
|
+ DevInfo devInfo = devInfoService.queryOneByClientId(deviceAddParam.getClientId());
|
|
|
if (devInfo != null) {
|
|
|
throw new BizException(ErrorEnum.DEVICE_IS_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_EXIST.getErrorMessage());
|
|
|
}
|
|
|
|
|
|
DevInfo saveInfo = new DevInfo();
|
|
|
- saveInfo.setClientId(deviceDTO.getClientId());
|
|
|
- saveInfo.setDevType(deviceDTO.getDevType());
|
|
|
- saveInfo.setSoftware(deviceDTO.getSoftware());
|
|
|
+ saveInfo.setClientId(deviceAddParam.getClientId());
|
|
|
+ saveInfo.setDevType(deviceAddParam.getDevType());
|
|
|
+ saveInfo.setSoftware(deviceAddParam.getSoftware());
|
|
|
saveInfo.setStatusLight(1);
|
|
|
saveInfo.setOnline(0);
|
|
|
saveInfo.setIsDeleted(BasePO.DeleteFlag.NOT_DELETED);
|
|
@@ -140,4 +132,71 @@ public class AdminGatewayImpl implements AdminGateway {
|
|
|
devInfoService.save(saveInfo);
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public AdminLoginRes login(AdminLoginParam param) {
|
|
|
+
|
|
|
+ AdminUserInfo adminUser = adminUserService.checkUserByPassword(param.getAccount(), param.getPassword());
|
|
|
+ if (Objects.isNull(adminUser)) {
|
|
|
+ throw new BizException(ErrorEnum.USERNAME_OR_PASSWORD_ERROR.getErrorCode(), ErrorEnum.USERNAME_OR_PASSWORD_ERROR.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 这里如果使用 多账号体系,需要新建一个类似 SptUtil 的 SptAdminUtil , 且在 gateway 校验的逻辑中,需要加上 对 SptAdminUtil 的判断
|
|
|
+ // 1 需要保证 userId 和 adminUserId 不能一样
|
|
|
+ // 2 可以在 stputil.login 的 loginId 参数前加前缀 user_ or admin_
|
|
|
+ // 3 添加 SptAdminUtil,不过在不同地方,需要注意对 stputil sptadminutil 的不同引用
|
|
|
+ StpUtil.login(adminUser.getUserId(), DeviceType.WEB.getCode());
|
|
|
+ SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
|
|
|
+ AdminLoginRes res = CopyUtils.copy(tokenInfo, AdminLoginRes.class);
|
|
|
+ BeanUtils.copyProperties(adminUser, res);
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void sendCode() {
|
|
|
+
|
|
|
+ Object loginIdObj = StpUtil.getLoginId();
|
|
|
+ AdminUserInfo adminUserInfo = adminUserService.getById(Long.parseLong(String.valueOf(loginIdObj)));
|
|
|
+ if (adminUserInfo == null || !StringUtils.hasText(adminUserInfo.getPhone())) {
|
|
|
+ throw new BizException(ErrorEnum.PHONE_IS_NULL.getErrorCode(), ErrorEnum.PHONE_IS_NULL.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ smsGateway.sendResetSmsCode(adminUserInfo.getPhone());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void reset(AdminResetParam request) {
|
|
|
+
|
|
|
+ //校验验证码
|
|
|
+ String redisKey = RedisCacheConstant.SMS_RESET_CODE + request.getPhone();
|
|
|
+ log.info("验证注册验证码:手机号={}, 输入验证码={}, Redis key={}", request.getPhone(), request.getCode(), redisKey);
|
|
|
+
|
|
|
+ Object cacheCodeObj = redisService.getCacheObject(redisKey);
|
|
|
+ log.info("从Redis获取验证码:key={}, value={}", redisKey, cacheCodeObj);
|
|
|
+
|
|
|
+ if (Objects.isNull(cacheCodeObj)) {
|
|
|
+ log.warn("验证码已过期或不存在:手机号={}, Redis key={}", request.getPhone(), redisKey);
|
|
|
+ throw new BizException(ErrorEnum.SMS_CODE_EXPIRED.getErrorCode(), ErrorEnum.SMS_CODE_EXPIRED.getErrorMessage());
|
|
|
+ }
|
|
|
+ String cacheCode = String.valueOf(cacheCodeObj);
|
|
|
+ log.info("验证码比较:输入={}, 缓存={}", request.getCode(), cacheCode);
|
|
|
+
|
|
|
+ if (!Objects.equals(cacheCode, request.getCode())) {
|
|
|
+ log.warn("验证码错误:手机号={}, 输入={}, 缓存={}", request.getPhone(), request.getCode(), cacheCode);
|
|
|
+ throw new BizException(ErrorEnum.SMS_CODE_ERROR.getErrorCode(), ErrorEnum.SMS_CODE_ERROR.getErrorMessage());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证通过 清除验证码入库
|
|
|
+ redisService.deleteObject(redisKey);
|
|
|
+
|
|
|
+ AdminUserInfo adminUser = adminUserService.queryByPhone(request.getPhone());
|
|
|
+ if (adminUser == null) {
|
|
|
+ log.warn("手机号不存在错误:手机号={}", request.getPhone());
|
|
|
+ throw new BizException(ErrorEnum.USER_NOT_FOUND.getErrorCode(), ErrorEnum.USER_NOT_FOUND.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ adminUser.setPassword(PasswordUtil.encrypt(request.getPassword()));
|
|
|
+ adminUserService.updateById(adminUser);
|
|
|
+ }
|
|
|
}
|