UserService.java
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package cn.com.glsx.modules.service;
import cn.com.glsx.common.DeviceErrorEnum;
import cn.com.glsx.common.redis.RedisService;
import cn.com.glsx.modules.model.param.ResponseBody;
import cn.com.glsx.modules.model.param.UserBo;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.core.web.R;
import com.lumi.opencloud.model.v1.request.AccessTokenRequest;
import com.lumi.opencloud.model.v1.request.AuthorizeRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author zhouhaibao
* @date 2021/3/3 17:23
*/
@Service
@Slf4j
public class UserService {
@Value("${tuya.registerUserUrl}")
private String registerUserUrl;
@Value("${tuya.getUserInfoUrl}")
private String getUserInfoUrl;
@Value("${tuya.getAllUserUrl}")
private String getAllUserUrl;
@Autowired
private SendRequestService sendRequestService;
@Autowired
private OauthService oauthService;
@Autowired
private RedisService redisService;
/**
* 注册用户
*
* @param userBo 用户
*/
public R registerUser(UserBo userBo) {
//获取令牌
ResponseBody responseBody = sendRequestService.sendRequestPost(registerUserUrl, JSONObject.toJSONString(userBo));
if (responseBody != null && responseBody.isSuccess()) {
String result = responseBody.getResult();
return R.ok(result);
}
Integer code = responseBody == null ? 500 : responseBody.getCode();
String errorMessage=responseBody == null ? "调用涂鸦失败" : DeviceErrorEnum.searchValue(code+"");
return R.error(errorMessage);
}
/**
* 获取用户信息
*
* @param id 用户 ID
*/
public R getUserInfo(String id) {
ResponseBody responseBody = sendRequestService.sendRequestGet(getUserInfoUrl.replace("{uid}", id));
if (responseBody != null && responseBody.isSuccess()) {
String result = responseBody.getResult() + "";
return R.ok(result);
}
Integer code = responseBody == null ? 500 : responseBody.getCode();
String errorMessage=responseBody == null ? "调用涂鸦失败" : DeviceErrorEnum.searchValue(code+"");
return R.error(errorMessage);
}
/**
* 获取所有用户信息
*
*/
public R getAllUser() {
ResponseBody responseBody = sendRequestService.sendRequestGet(getAllUserUrl);
if (responseBody != null && responseBody.isSuccess()) {
String result = responseBody.getResult() + "";
return R.ok(result);
}
Integer code = responseBody == null ? 500 : responseBody.getCode();
String errorMessage=responseBody == null ? "调用涂鸦失败" : DeviceErrorEnum.searchValue(code+"");
return R.error(errorMessage);
}
/**
* aqara注册接口
*
* @param authorizeRequest 入参
* @return
*/
public R registerAqaraUser(AuthorizeRequest authorizeRequest) {
R authorize = oauthService.authorize(authorizeRequest);
if(authorize.isSuccess()){
String message = authorize.getMessage();
Map objectToMap = JSONObject.parseObject(message, Map.class);
String authorizationCode = objectToMap.get("authorizationCode")+"";
AccessTokenRequest accessTokenRequest=new AccessTokenRequest();
accessTokenRequest.setCode(authorizationCode);
R accessToken = oauthService.accessToken(accessTokenRequest);
if(accessToken.isSuccess()){
String key= authorizeRequest.getAccount() + "_aqara";
String accessTokenMessage = accessToken.getMessage();
Map toMap = JSONObject.parseObject(accessTokenMessage, Map.class);
String result = toMap.get("result")+"";
Map resultMap = JSONObject.parseObject(result, Map.class);
Object o = redisService.get(key);
if(o==null){
redisService.setOutTime(key, resultMap.get("accessToken"), 1, TimeUnit.DAYS);
}
return R.ok(result);
}else{
return R.error("获取accessToken失败");
}
}
return R.error("获取authorizationCode失败");
}
}