diff --git a/nxhs-service/src/main/java/cc/yunxi/domain/vo/device/CMDEnum.java b/nxhs-service/src/main/java/cc/yunxi/domain/vo/device/CMDEnum.java index 0800a36..5bbd818 100644 --- a/nxhs-service/src/main/java/cc/yunxi/domain/vo/device/CMDEnum.java +++ b/nxhs-service/src/main/java/cc/yunxi/domain/vo/device/CMDEnum.java @@ -10,6 +10,7 @@ import lombok.Getter; @SwaggerDisplayEnum public enum CMDEnum { + login("login", "登录成功"), reboot("reboot", "重启设备"), restart("restart", "重启应用"), update("update", "更新应用"), diff --git a/nxhs-service/src/main/java/cc/yunxi/service/impl/CommonService.java b/nxhs-service/src/main/java/cc/yunxi/service/impl/CommonService.java index 7f75599..613d685 100644 --- a/nxhs-service/src/main/java/cc/yunxi/service/impl/CommonService.java +++ b/nxhs-service/src/main/java/cc/yunxi/service/impl/CommonService.java @@ -1,5 +1,6 @@ package cc.yunxi.service.impl; +import cc.yunxi.common.domain.CommonResult; import cc.yunxi.common.exception.BizIllegalException; import cc.yunxi.common.exception.BusinessLogicException; import cc.yunxi.common.exception.ForbiddenException; @@ -12,6 +13,9 @@ import cc.yunxi.domain.po.Client; import cc.yunxi.domain.po.Manager; import cc.yunxi.domain.po.Recycler; import cc.yunxi.domain.query.RecyclerQuery; +import cc.yunxi.domain.vo.device.CMDEnum; +import cc.yunxi.domain.vo.device.CommandVO; +import cc.yunxi.domain.vo.device.LoginRespVO; import cc.yunxi.enums.UserTypeEnum; import cc.yunxi.mapper.ManagerMapper; import cc.yunxi.mapper.ProductMapper; @@ -19,10 +23,8 @@ import cc.yunxi.service.IClientService; import cc.yunxi.service.ICommonService; import cc.yunxi.service.IManagerService; import cc.yunxi.service.IRecyclerService; -import cc.yunxi.utils.JwtTool; -import cc.yunxi.utils.RedisTool; -import cc.yunxi.utils.UserContext; -import cc.yunxi.utils.WeChatUtil; +import cc.yunxi.utils.*; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; @@ -31,6 +33,7 @@ import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import io.swagger.annotations.ApiModelProperty; import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.validation.annotation.Validated; @@ -67,6 +70,9 @@ public class CommonService implements ICommonService { @Resource private WxShProperties wxShProperties; + @Resource + private CommandUtil commandUtil; + @Resource private JwtTool jwtTool; @@ -117,15 +123,15 @@ public class CommonService implements ICommonService { Recycler recycler = recyclerService.getLatestRecycleByPhone(loginName); Client client = clientService.getLastestClientByPhone(loginName); if (recycler == null) { - if(client != null){ + if (client != null) { // 设置投递员信息 zsgLoginDTO.setId(Long.parseLong(client.getId())); zsgLoginDTO.setLoginName(loginName); - zsgLoginDTO.setStat(client.getEnabledMark().equals(1)?0:2); + zsgLoginDTO.setStat(client.getEnabledMark().equals(1) ? 0 : 2); zsgLoginDTO.setRole(9); zsgLoginDTO.setScore(client.getMembershipPoint()); } - }else { + } else { // 设置清运员信息 zsgLoginDTO.setId(Long.parseLong(recycler.getId())); zsgLoginDTO.setLoginName(loginName); @@ -156,7 +162,26 @@ public class CommonService implements ICommonService { userDTO.setUsername(recycler.getStaffsName()); userDTO.setToken(this.createToken(userDTO)); userDTO.setCompanyId(recycler.getCompanyId()); - + if (StrUtil.isNotEmpty(wxLoginDTO.getDevCode())) { + // 通知设备登录成功 + LoginRespVO respVO = new LoginRespVO(); + respVO.setRole(2); + respVO.setStatus(recycler.getStatus()); + respVO.setToken(userDTO.getToken()); + respVO.setUserId(userDTO.getId()); + respVO.setUserName(userDTO.getUsername()); + respVO.setOpenId(userDTO.getOpenid()); + respVO.setTimeExpire(userDTO.getTimeExpire()); + String data = JSONUtil.toJsonStr(respVO); + CommandVO command = new CommandVO(); + command.setCmd(CMDEnum.login); + command.setDeviceCode(wxLoginDTO.getDevCode()); + command.setData(data); + command.setOptTime(DateUtil.now()); + command.setRemark("登录成功"); + CommonResult commonResult = commandUtil.sendCommand(command); + log.info("设备登录结果 回收员 ===> code:{},msg:{}", commonResult.getCode(), commonResult.getMsg()); + } return userDTO; } @@ -173,6 +198,26 @@ public class CommonService implements ICommonService { userDTO.setUserType(UserTypeEnum.CLIENT); userDTO.setUsername(client.getNickName()); userDTO.setToken(this.createToken(userDTO)); + if (StrUtil.isNotEmpty(wxLoginDTO.getDevCode())) { + // 通知设备登录成功 + LoginRespVO respVO = new LoginRespVO(); + respVO.setUserId(userDTO.getId()); + respVO.setOpenId(userDTO.getOpenid()); + respVO.setUserName(userDTO.getUsername()); + respVO.setStatus(client.getEnabledMark()); + respVO.setToken(userDTO.getToken()); + respVO.setTimeExpire(userDTO.getTimeExpire()); + respVO.setRole(1); + String data = JSONUtil.toJsonStr(respVO); + CommandVO command = new CommandVO(); + command.setCmd(CMDEnum.login); + command.setDeviceCode(wxLoginDTO.getDevCode()); + command.setData(data); + command.setOptTime(DateUtil.now()); + command.setRemark("登录成功"); + CommonResult commonResult = commandUtil.sendCommand(command); + log.info("设备登录结果 散户 ===> code:{},msg:{}", commonResult.getCode(), commonResult.getMsg()); + } return userDTO; } @@ -222,6 +267,7 @@ public class CommonService implements ICommonService { /** * 创建token + * * @param userDTO * @return token */ @@ -237,7 +283,7 @@ public class CommonService implements ICommonService { * 删除token */ public void removeToken(String token) { - redisTool.expireKeyAt(TOKEN_KEY + token, new Date()); + redisTool.expireKeyAt(TOKEN_KEY + token, new Date()); } } diff --git a/nxhs-service/src/main/java/cc/yunxi/utils/CommandUtil.java b/nxhs-service/src/main/java/cc/yunxi/utils/CommandUtil.java index 36ff170..5dc50fb 100644 --- a/nxhs-service/src/main/java/cc/yunxi/utils/CommandUtil.java +++ b/nxhs-service/src/main/java/cc/yunxi/utils/CommandUtil.java @@ -2,16 +2,42 @@ package cc.yunxi.utils; import cc.yunxi.common.domain.CommonResult; import cc.yunxi.domain.vo.device.CommandVO; +import cc.yunxi.domain.vo.device.DeviceStatus; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONUtil; import org.springframework.stereotype.Component; +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + @Component public class CommandUtil { private static final int PORT = 5000; - public CommonResult sendCommand(CommandVO command,String ip){ + @Resource + private RedisTool redisTool; + + public CommonResult sendCommand(CommandVO command) { + DeviceStatus status = getDeviceStatus(command.getDeviceCode()); + if (null == status) return CommonResult.error(400, "设备不在线!"); + String ip = status.getIp(); + String res = HttpUtil.post("http://" + ip + ":" + PORT + "/command", JSONUtil.toJsonStr(command), 1000); + return JSONUtil.toBean(res, CommonResult.class); + } + + public CommonResult sendCommand(CommandVO command, String ip) { String res = HttpUtil.post("http://" + ip + ":" + PORT + "/command", JSONUtil.toJsonStr(command), 1000); return JSONUtil.toBean(res, CommonResult.class); } + + private DeviceStatus getDeviceStatus(String deviceCode) { + boolean exist = redisTool.existsKey(deviceCode); + long expire = redisTool.getKeyExpire(deviceCode, TimeUnit.SECONDS); + if (!exist || expire <= 0) { + return null; + } + Object value = redisTool.getValue(deviceCode); + if (value == null) return null; + return JSONUtil.toBean(value.toString(), DeviceStatus.class); + } }