Commit aae6cde3 by tyfanchz

订阅用户设备功能

1 parent 828e465a
...@@ -2,8 +2,11 @@ package com.glsx.smart.service.impl; ...@@ -2,8 +2,11 @@ package com.glsx.smart.service.impl;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils; import com.glsx.plat.common.utils.HttpUtils;
...@@ -90,16 +93,19 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser ...@@ -90,16 +93,19 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser
if (StringUtils.isBlank(req.getUid())) { if (StringUtils.isBlank(req.getUid())) {
throw BusinessException.create(ResultEnum.PARAM_EMPTY, "uid"); throw BusinessException.create(ResultEnum.PARAM_EMPTY, "uid");
} }
// 根据第三方用户 id 查询用户绑定信息
SmartUserChannel ucRecord = new SmartUserChannel(); SmartUserChannel ucRecord = new SmartUserChannel();
ucRecord.setThirdId(req.getUid()); ucRecord.setThirdId(req.getUid());
SmartUserChannel savedUserChannel = userChannelMapper.selectOne(ucRecord); SmartUserChannel savedUserChannel = userChannelMapper.selectOne(ucRecord);
if (savedUserChannel == null) { if (savedUserChannel == null) {
throw BusinessException.create(ResultEnum.USER_NOT_FOUND); throw BusinessException.create(ResultEnum.USER_NOT_FOUND);
} }
// 插入用户信息
req.setUserId(savedUserChannel.getUserId()); req.setUserId(savedUserChannel.getUserId());
req.setCreateTime(now); req.setCreateTime(now);
req.setUpdateTime(now); req.setUpdateTime(now);
rows = userDeviceMapper.insertSelective(req); rows = userDeviceMapper.insertSelective(req);
// 订阅用户设备
this.subscribeDevice(req); this.subscribeDevice(req);
} else { } else {
if (StringUtils.isBlank(req.getDeviceName())) { if (StringUtils.isBlank(req.getDeviceName())) {
...@@ -156,17 +162,24 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser ...@@ -156,17 +162,24 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser
if (StringUtils.isBlank(req.getPlatformProductId())) { if (StringUtils.isBlank(req.getPlatformProductId())) {
throw BusinessException.create(ResultEnum.PARAM_EMPTY, "platformProductId"); throw BusinessException.create(ResultEnum.PARAM_EMPTY, "platformProductId");
} }
// 通过用户手机号获取用户 token
SmartUserRelation userRelation = userRelationMapper.selectByPrimaryKey(req.getUserId()); SmartUserRelation userRelation = userRelationMapper.selectByPrimaryKey(req.getUserId());
String phone = userRelation.getPhone(); String phone = userRelation.getPhone();
String token = (String) redisService.get(phone + "_aqara"); String token = (String) redisService.get(phone + "_aqara");
// 查询支持的指令集
List<String> commands = productMapper.getSupportedCommands(req.getPlatformProductId()); List<String> commands = productMapper.getSupportedCommands(req.getPlatformProductId());
// 请求 url 参数
Map<String, Object> reqParam = new HashMap<>();
reqParam.put("token", token);
// 请求 body
JSONObject reqData = new JSONObject(); JSONObject reqData = new JSONObject();
reqData.put("did", req.getDeviceId()); reqData.put("did", req.getDeviceId());
reqData.put("attrs", commands); reqData.put("attrs", commands);
JSONObject reqObj = new JSONObject(); JSONObject reqObj = new JSONObject();
reqObj.put("data", reqData); reqObj.put("data", reqData);
reqObj.put("token", token); String rawUrl = remoteUrl + userDeviceSubscribeApi;
String url = remoteUrl + userDeviceSubscribeApi; String url = HttpUtils.urlWithForm(rawUrl, reqParam, StandardCharsets.UTF_8,
true);
log.info("$$$$ subscribeDevice url = {}, req = {}", url, reqObj.toJSONString()); log.info("$$$$ subscribeDevice url = {}, req = {}", url, reqObj.toJSONString());
String respJson = HttpUtils.post(url, reqObj.toJSONString()); String respJson = HttpUtils.post(url, reqObj.toJSONString());
log.info("$$$$ subscribeDevice resp = {}", respJson); log.info("$$$$ subscribeDevice resp = {}", respJson);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!