Commit 48116e61 by wnb0128

车家智能数据服务开发 用户登录--think.wang

1 parent 7b3415cb
package com.glsx.smart.constant;
/**
* @Title: Constants.java
* @Description: 常量
* @date 2021年3月12日 上午9:27:06
* @version V1.0
* @Company: glsx.com.cn
* @Copyright Copyright (c) 2021
*/
public class Constants {
public final static String DEFAULT_PASSWORD = "123456";
}
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:用户关系控制类
......@@ -20,6 +31,12 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/user")
public class UserRelationController {
@Value("${device_remote_url}")
private String remoteUrl;
@Value("${tuya_user_sync}")
private String syncUser;
@Autowired
private SmartUserRelationService userRelationService;
......@@ -43,4 +60,61 @@ public class UserRelationController {
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((byte)1); //涂鸦
SmartUserRelation user = userRelationService.getSingle(request);
if(user != null){
if(StringUtils.isEmpty(user.getThirdId())){
if(request.getChannel() == 1){
String tuYaId = this.syncUser(user.getPhone(), user.getPassword());
request.setThirdId(tuYaId);
request.setId(user.getId());
userRelationService.updateByPrimaryKeySelective(request);
}
}
return R.ok().data(user.getId());
}
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(id);
}
}
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;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!