UserController.java
3.41 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
package com.glsx.api.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.api.constant.Constant;
import com.glsx.api.exception.ResultEnum;
import com.glsx.api.pojo.request.UserRelationReq;
import com.glsx.api.pojo.response.UserRelationResp;
import com.glsx.api.remote.UserFeignClient;
import com.glsx.cloudframework.core.util.Md5Encrypt;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.groups.Default;
import java.util.Date;
/**
* @Title: UserController.java
* @Description: 用户逻辑控制
* @author think.wang
* @version V1.0
* @Company: Didihu.com.cn
* @Copyright Copyright (c) 2017
*/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
@Value("${device_remote_url}")
private String remoteUrl;
@Value("${tuya_user_sync}")
private String syncUser;
@Autowired
private UserFeignClient userFeignClient;
@ResponseBody
@RequestMapping("/login")
public R home(@RequestBody @Validated(value = Default.class) UserRelationReq req, BindingResult result){
this.checkRequestParam(result);
log.info("/user/login参数:{}", JSON.toJSONString(req));
UserRelationResp user = userFeignClient.getSingle(req);
if(user != null){
if(StringUtils.isEmpty(user.getThirdId())){
if(req.getChannel() == 1){
String tuYaId = this.syncUser(user.getPhone(), user.getPassword());
req.setThirdId(tuYaId);
req.setId(user.getId());
userFeignClient.update(req);
}
}
return R.ok().data(user.getId());
}
String password = Md5Encrypt.md5(Constant.DEFAULT_PASSWORD);
req.setCreateTime(new Date());
req.setPassword(password);
if(req.getChannel() == 1){ //渠道为涂鸦
String tuYaId = this.syncUser(req.getPhone(), password);
req.setThirdId(tuYaId);
Long id = userFeignClient.add(req);
if(id != null && id > 0){
return R.ok().data(id);
}
}
return R.error(ResultEnum.PARAM_ERROR.getMessage());
}
private String syncUser(String mobile, String password){
JSONObject object = new JSONObject();
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(), object.toJSONString());
String result = HttpUtils.post(syncUserUrl.toString(), object.toJSONString());
log.info("注册涂鸦平台返回结果:{}", result);
if (StringUtils.isNotEmpty(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;
}
}