|
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.xy.annotate.RestMappingController;
|
|
|
+import com.xy.dto.DeviceBluetoothAuthDto;
|
|
|
import com.xy.dto.DeviceInfoDto;
|
|
|
import com.xy.dto.DeviceStatusDto;
|
|
|
import com.xy.dto.MercMiniDeviceDto;
|
|
@@ -11,17 +12,17 @@ import com.xy.dto.be.MercDto;
|
|
|
import com.xy.dto.common.MercLineDto;
|
|
|
import com.xy.dto.common.MercPlaceDto;
|
|
|
import com.xy.dto.mini.MiniMercRegionDto;
|
|
|
+import com.xy.entity.DeviceBluetoothAuth;
|
|
|
import com.xy.entity.DeviceInfo;
|
|
|
import com.xy.enums.MercStatus;
|
|
|
+import com.xy.service.DeviceBluetoothAuthServiceImpl;
|
|
|
import com.xy.service.DeviceInfoServiceImpl;
|
|
|
import com.xy.service.be.MercFeignService;
|
|
|
import com.xy.service.common.MercLineService;
|
|
|
import com.xy.service.common.MercPlaceService;
|
|
|
-import com.xy.utils.Emptys;
|
|
|
-import com.xy.utils.MercAuthUtils;
|
|
|
-import com.xy.utils.PageBean;
|
|
|
-import com.xy.utils.R;
|
|
|
+import com.xy.utils.*;
|
|
|
import com.xy.utils.enums.DeviceActiveStateEnum;
|
|
|
+import com.xy.utils.enums.DeviceAuthCodeUseStatus;
|
|
|
import com.xy.utils.enums.DeviceNetSateType;
|
|
|
import com.xy.utils.enums.DictSonEnum;
|
|
|
import io.swagger.annotations.Api;
|
|
@@ -36,6 +37,7 @@ import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.xy.utils.Beans.copy;
|
|
@@ -59,6 +61,8 @@ public class MercMiniDeviceController {
|
|
|
private final MercPlaceService mercPlaceService;
|
|
|
private final MercLineService mercLineService;
|
|
|
|
|
|
+ private final DeviceBluetoothAuthServiceImpl deviceBluetoothAuthService;
|
|
|
+
|
|
|
|
|
|
@ApiOperation("商户设备首页统计")
|
|
|
@PostMapping("mercHomeStatistical")
|
|
@@ -174,6 +178,12 @@ public class MercMiniDeviceController {
|
|
|
return R.ok(getMyDevices());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取登录人的设备
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
List<Long> getMyDevices() {
|
|
|
boolean mercAdmin = MercAuthUtils.isMercAdmin();
|
|
|
Long mercId = MercAuthUtils.getMercId();
|
|
@@ -191,5 +201,104 @@ public class MercMiniDeviceController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @PostMapping("genCode")
|
|
|
+ @ApiOperation("蓝牙开门-生成授权码")
|
|
|
+ public R<Boolean> genCode(@RequestBody @Validated DeviceBluetoothAuthDto.GenCode dto) {
|
|
|
+ Long deviceId = dto.getDeviceId();
|
|
|
+ List<Long> myDevices = getMyDevices();
|
|
|
+ if (!myDevices.contains(deviceId)) {
|
|
|
+ R.fail("抱歉!您无权对此设备进行授权!");
|
|
|
+ }
|
|
|
+ Long mercId = MercAuthUtils.getMercId();
|
|
|
+ DeviceBluetoothAuth deviceBluetoothAuth = deviceBluetoothAuthService.getOne(Wrappers.<DeviceBluetoothAuth>lambdaQuery()
|
|
|
+ .eq(DeviceBluetoothAuth::getDeviceId, deviceId)
|
|
|
+ .eq(DeviceBluetoothAuth::getMercId, mercId)
|
|
|
+ .eq(DeviceBluetoothAuth::getUseStatus, DeviceAuthCodeUseStatus.UN_USED.getCode())
|
|
|
+ );
|
|
|
+ String cacheAuthCode = getCacheAuthCode(deviceId);
|
|
|
+ if (deviceBluetoothAuth != null) {
|
|
|
+ if (StrUtil.isNotEmpty(cacheAuthCode)) {
|
|
|
+ R.fail("此设备尚有未使用的授权码:【" + cacheAuthCode + "】,请使用后再来生成!");
|
|
|
+ } else {
|
|
|
+ //已失效了
|
|
|
+ deviceBluetoothAuthService.saveOrUpdate(deviceBluetoothAuth.setUseStatus(DeviceAuthCodeUseStatus.TIME_OUT.getCode()));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //授权码生成
|
|
|
+ String authCode = getAuthCode();
|
|
|
+ //缓存
|
|
|
+ setCacheAuthCode(deviceId, authCode);
|
|
|
+ DeviceBluetoothAuth saveInfo = new DeviceBluetoothAuth()
|
|
|
+ .setDeviceId(deviceId)
|
|
|
+ .setMercId(mercId)
|
|
|
+ .setDeviceId(deviceId).setAuthCode(authCode);
|
|
|
+ return R.ok(deviceBluetoothAuthService.save(saveInfo));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("verificationCode")
|
|
|
+ @ApiOperation("蓝牙开门-校验并使用授权码")
|
|
|
+ public R<Boolean> verificationCode(@RequestBody @Validated DeviceBluetoothAuthDto.VerificationCode dto) {
|
|
|
+ Long deviceId = dto.getDeviceId();
|
|
|
+ String authCode = dto.getAuthCode();
|
|
|
+ String cacheAuthCode = getCacheAuthCode(deviceId);
|
|
|
+ if (!authCode.equals(cacheAuthCode)) {
|
|
|
+ return R.fail("您输入的授权码有误或者无效!");
|
|
|
+ }
|
|
|
+ //验证通过,缓存去掉授权码,并更新状态
|
|
|
+ Long mercId = MercAuthUtils.getMercId();
|
|
|
+ DeviceBluetoothAuth deviceBluetoothAuth = deviceBluetoothAuthService.getOne(Wrappers.<DeviceBluetoothAuth>lambdaQuery()
|
|
|
+ .eq(DeviceBluetoothAuth::getDeviceId, deviceId)
|
|
|
+ .eq(DeviceBluetoothAuth::getAuthCode, authCode)
|
|
|
+ .eq(DeviceBluetoothAuth::getMercId, mercId)
|
|
|
+ .eq(DeviceBluetoothAuth::getUseStatus, DeviceAuthCodeUseStatus.UN_USED.getCode())
|
|
|
+ );
|
|
|
+ if (deviceBluetoothAuth == null) {
|
|
|
+ return R.fail("您输入的授权码无效!");
|
|
|
+ }
|
|
|
+ //缓存去掉授权码
|
|
|
+ delCacheAuthCode(deviceId);
|
|
|
+ deviceBluetoothAuth.setUseStatus(DeviceAuthCodeUseStatus.USED.getCode());
|
|
|
+ deviceBluetoothAuth.setUseUser(AuthorizeUtils.getLoginId(Long.class));
|
|
|
+ deviceBluetoothAuth.setUseTime(LocalDateTime.now());
|
|
|
+ return R.ok(deviceBluetoothAuthService.saveOrUpdate(deviceBluetoothAuth));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static final String BASE_NUMBER = "0123456789";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * auth-code有效期30分钟,单位:s ,
|
|
|
+ */
|
|
|
+ public static final Integer AUTH_CODE_TIMEOUT = 1800;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 随机6位数生成
|
|
|
+ */
|
|
|
+ public static String getAuthCode() {
|
|
|
+ StringBuilder sb = new StringBuilder(6);
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ int num = ThreadLocalRandom.current().nextInt(BASE_NUMBER.length());
|
|
|
+ sb.append(BASE_NUMBER.charAt(num));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setCacheAuthCode(Long deviceId, String code) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ redisService.set("device:auth:code:" + deviceId, code, AUTH_CODE_TIMEOUT);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCacheAuthCode(Long deviceId) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ return redisService.get("device:auth:code:" + deviceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void delCacheAuthCode(Long deviceId) {
|
|
|
+ RedisService<String> redisService = SpringBeanUtils.getBean(RedisService.class);
|
|
|
+ redisService.remove("device:auth:code:" + deviceId);
|
|
|
+ }
|
|
|
|
|
|
}
|