UserRelationController.java 4.56 KB
package com.glsx.smart.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.redis.utils.RedisUtils;
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 RedisUtils redisUtils;

	@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));
		String key = new StringBuffer(userRelation.getPhone())
				.append(userRelation.getChannel()).toString();
		if(redisUtils.hasKey(key)){
			log.info("{} 已发送请求", key);
			return R.ok();
		}
		redisUtils.setex(key, key, Constants.REDIS_CACHE_TIME);
		SmartUserRelation request = new SmartUserRelation();
		request.setPhone(userRelation.getPhone());
		request.setChannel(userRelation.getChannel());  //涂鸦
		SmartUserRelation user = userRelationService.getSingle(request);
		request.setUserId(userRelation.getUserId());
		String password = MD5Utils.md5Hex(request.getPhone(), "utf-8");
		request.setPassword(password);
		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());
		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;
	}

}