Commit a8bf69e2 by wnb0128

车家智能移动端 用户注册--think.wang

1 parent 94ad1af3
package com.glsx.api.constant;
/**
* 常量池
*/
public interface Constant {
String DEFAULT_CHARSET = "UTF-8"; //默认字符集
String DEFAULT_PASSWORD = "123456";
}
package com.glsx.api.controller;
import com.glsx.api.exception.BusinessException;
import com.glsx.api.exception.ResultEnum;
import com.glsx.plat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author think.wang
* @version 1.0.0
* @Title BaseController
* @Package com.glsx.api.controller
* @Description controller基类
* @date 2021/3/17
*/
@Slf4j
public class BaseController {
/**
* hibernate-validator参数校验返回错误方法
*
* @param result
* @return
*/
public Map<String, String> getErrors(BindingResult result) {
Map<String, String> map = new HashMap<>();
List<FieldError> list = result.getFieldErrors();
for (FieldError error : list) {
map.put(error.getField(), error.getDefaultMessage());
}
return map;
}
/**
* 将请求参数进行校验,校验不成功则抛出 {@link BusinessException} 异常
*
* @param bindingResult Spring校验结果
*/
protected void checkRequestParam(BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
Map<String, String> errorMap = this.getErrors(bindingResult);
Collection<String> values = errorMap.values();
String errorMsg = StringUtils.join(values, ";");
throw BusinessException.create(ResultEnum.PARAM_ERROR.getCode(), errorMsg);
}
}
}
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("${tu_ya_sync_interface}")
private String syncUserUrl;
@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 {
log.info("请求注册涂鸦平台接口地址:{},参数:{}", syncUserUrl, object.toJSONString());
String result = HttpUtils.post(syncUserUrl, object.toJSONString());
log.info("注册涂鸦平台返回结果:{}", result);
if (StringUtils.isNotEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.getInteger("code").equals(200)) {
return json.getString("message");
}
}
} catch (Exception e) {
log.error("同步涂鸦平台失败:{}", e);
}
return null;
}
}
......@@ -34,4 +34,15 @@ public class BusinessException extends RuntimeException {
public void setMessage(String message) {
this.message = message;
}
public static BusinessException create(int code, String message){
BusinessException businessException = new BusinessException();
businessException.code = code;
businessException.message = message;
return businessException;
}
public static BusinessException create(ResultEnum resultEnum) {
return create(resultEnum.getCode(), resultEnum.getMessage());
}
}
package com.glsx.api.pojo;
/**
* @author think.wang
* @version 1.0.0
* @Title GroupA
* @Package com.glsx.api.pojo
* @Description Hibernate-validator的分组
* @date 2021/3/8
*/
public interface GroupA {
}
package com.glsx.api.pojo.request;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.Date;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-15
*/
/**
* 车家智能用户平台注册信息表
*/
@Data
public class UserRelationReq {
private Long id;
/**
* 用户昵称
*/
private String name;
/**
* 手机号 来自嘀加平台用户
*/
@NotEmpty(message = "phone不能为空", groups = {Default.class})
private String phone;
/**
* 用户密码
*/
private String password;
/**
* 第三方平台标识 1涂鸦
*/
@NotNull(message = "channel不能为空", groups = {Default.class})
private Byte channel;
/**
* 第三方平台用户id
*/
private String thirdId;
/**
* 状态 0注销 1正常
*/
private Byte status;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
private Date createTime;
}
package com.glsx.api.pojo.response;
import lombok.Data;
import java.util.Date;
/**
* <p>Description:
* <p>Project: chejia-smart
*
* @author wbh
* @date 2021-03-15
*/
/**
* 车家智能用户平台注册信息表
*/
@Data
public class UserRelationResp {
private Long id;
/**
* 用户昵称
*/
private String name;
/**
* 手机号 来自嘀加平台用户
*/
private String phone;
/**
* 用户密码
*/
private String password;
/**
* 第三方平台标识 1涂鸦
*/
private Byte channel;
/**
* 第三方平台用户id
*/
private String thirdId;
/**
* 状态 0注销 1正常
*/
private Byte status;
/**
* 备注
*/
private String remark;
/**
* 创建时间
*/
private Date createTime;
}
package com.glsx.api.remote;
import com.glsx.api.pojo.request.UserRelationReq;
import com.glsx.api.pojo.response.UserRelationResp;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@FeignClient(name = "${feign.server.name}",url = "${feign.server.url}")
public interface UserFeignClient {
@ResponseBody
@RequestMapping(value = "/user/getSingle", method = {RequestMethod.POST})
UserRelationResp getSingle(@RequestBody UserRelationReq userRelation);
@ResponseBody
@RequestMapping(value = "/user/add", method = {RequestMethod.POST})
Long add(@RequestBody UserRelationReq userRelation);
@ResponseBody
@RequestMapping(value = "/user/update", method = {RequestMethod.POST})
Integer update(@RequestBody UserRelationReq userRelation);
}
......@@ -21,3 +21,6 @@ feign.server.url=http://localhost:8209/smartService
dubbo.registry.address=zookeeper://192.168.1.36:2181?init.timeout=60000
dubbo.registry.timeout=10000
#\u6D82\u9E26\u540C\u6B65\u7528\u6237\u63A5\u53E3
tu_ya_sync_interface=http://192.168.3.137:8091/smartApi/sync_user
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!