UserRelationController.java
4.14 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
package com.glsx.smart.controller;
import cn.hutool.crypto.digest.MD5;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.service.SmartUserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @Title: UserRelationController.java
* @Description:用户关系控制类
* @author think.wang
* @version V1.0
* @Company: Didihu.com.cn
* @Copyright Copyright (c) 2017
*/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserRelationController {
@Value("${device_remote_url}")
private String remoteUrl;
@Value("${tuya_user_sync}")
private String syncUser;
@Autowired
private SmartUserRelationService userRelationService;
@ResponseBody
@RequestMapping(value = "/getSingle", method = {RequestMethod.POST})
public SmartUserRelation getSingle(@RequestBody SmartUserRelation userRelation){
return userRelationService.getSingle(userRelation);
}
@ResponseBody
@RequestMapping(value = "/add", method = {RequestMethod.POST})
public Long add(@RequestBody SmartUserRelation userRelation){
log.info("/user/add保存数据参数:{}", JSON.toJSONString(userRelation));
return userRelationService.insertSelective(userRelation);
}
@ResponseBody
@RequestMapping(value = "/update", method = {RequestMethod.POST})
public Integer update(@RequestBody SmartUserRelation userRelation){
log.info("/user/update保存数据参数:{}", JSON.toJSONString(userRelation));
return userRelationService.updateByPrimaryKeySelective(userRelation);
}
@ResponseBody
@RequestMapping(value = "/login", method = {RequestMethod.POST})
public R login(@RequestBody SmartUserRelation userRelation){
log.info("/user/login保存数据参数:{}", JSON.toJSONString(userRelation));
SmartUserRelation request = new SmartUserRelation();
request.setPhone(userRelation.getPhone());
request.setChannel(userRelation.getChannel()); //涂鸦
SmartUserRelation user = userRelationService.getSingle(request);
request.setUserId(userRelation.getUserId());
if(user != null){
String tuYaId = user.getThirdId();
if(StringUtils.isEmpty(tuYaId)){
if(request.getChannel() == 1){
tuYaId = this.syncUser(user.getPhone(), user.getPassword());
request.setThirdId(tuYaId);
request.setId(user.getId());
}
}
userRelationService.updateByPrimaryKeySelective(request);
return R.ok().data(tuYaId);
}
String password = MD5.create().digestHex(Constants.DEFAULT_PASSWORD);
request.setCreateTime(new Date());
request.setPassword(password);
if(request.getChannel() == 1){ //渠道为涂鸦
String tuYaId = this.syncUser(request.getPhone(), password);
request.setThirdId(tuYaId);
Long id = userRelationService.insertSelective(request);
if(id != null && id > 0){
return R.ok().data(tuYaId);
}
}
return R.error();
}
private String syncUser(String mobile, String password){
Map<String, String> object = new HashMap<>();
object.put("country_code", "86");
object.put("username", mobile);
object.put("username_type", "1");
object.put("password", password);
try {
StringBuffer syncUserUrl = new StringBuffer(remoteUrl);
syncUserUrl.append(syncUser);
log.info("请求注册涂鸦平台接口地址:{},参数:{}", syncUserUrl.toString(), JSON.toJSONString(object));
String result = HttpUtils.post(syncUserUrl.toString(), JSON.toJSONString(object));
log.info("注册涂鸦平台返回结果:{}", result);
if (!StringUtils.isEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
return json.getJSONObject("message").getString("uid");
}
}
} catch (Exception e) {
log.error("同步涂鸦平台失败:{}", e);
}
return null;
}
}