package org.springblade.openinterface.dianxin.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springblade.openinterface.dianxin.constants.JiYueConf;
import org.springblade.openinterface.dianxin.xiaoyigj.entity.OathLogin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author lidexi
 * @date 2021/6/22 19:06
 */
@RestController
@RequestMapping("/openinterface/jiyue")
public class AuthUtil {

	@PostMapping("/getToken")
	public  String getToken(){
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		String sign = Base64Util.encode(MD5Util.encode(JiYueConf.APPKEY + timestamp));
		String app_secret = Base64Util.encode(MD5Util.encode(JiYueConf.APPKEY + sign));

		OathLogin login = new OathLogin();
		login.setApp_id(JiYueConf.APPID);
		login.setApp_secret(app_secret);
		login.setTimestamp(timestamp);
		login.setSign(sign);
		System.out.println(sign);
		System.out.println(timestamp);
		System.out.println(app_secret);

		HttpUtil http = new HttpUtil();
		String str = http.url(JiYueConf.UrlConstant.TOKEN.getUrl()).post(login).asString();
		System.out.println("str:"+str);

		ObjectMapper objectMapper = new ObjectMapper();
		Map map = null;
		try {
			map = objectMapper.readValue(str, Map.class);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println("map.tostring"+map.toString());
		Map data = (Map) map.get("data");
		return data.get("accesstoken").toString();
	}
}
