UserController.java 3.23 KB
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;
	}

}