OutUtil.java 2.65 KB
Newer Older
zhangjianqian's avatar
zhangjianqian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
package com.zehong.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.zehong.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class OutUtil {
    @Autowired
    private RedisCache redisCache;

    @Value("${zhengfu.appId}")
    private String appId;

    @Value("${zhengfu.appSecret}")
    private String appSecret;

    @Value("${zhengfu.tokenUrl}")
    private String tokenUrl;

    @Value("${zhengfu.apiUrl}")
    private String apiUrl;

    /**
     * 获取token
     * @return
     * @throws Exception
     */
    public String getToken()throws Exception
    {
        String token = (String)redisCache.getCacheObject("apiToken");
        if(token!=null){
            token = getNewToken();
        }
        return token;
    }

    /**
     * 获取接口参数
     * @return
     * @throws Exception
     */
    public JSONObject getInfo(String domain, String methodType, Map<String,Object> map)throws Exception
    {
        String token = (String)redisCache.getCacheObject("apiToken");
        if(token==null){
            token = getNewToken();
        }
        JSONObject json = new JSONObject();
        json.put("domain", domain);
        json.put("method", methodType);
        json.put("condition", map);
        JSONObject jobct = (JSONObject) HttpClient.sendPostJson(apiUrl,json,token);
        return jobct;
    }

    /**
     * 上传数据
     * @param methodType
     * @param list
     * @return
     * @throws Exception
     */
耿迪迪's avatar
耿迪迪 committed
70
    public JSONObject setInfo(String domain, String methodType, List list)throws Exception
zhangjianqian's avatar
zhangjianqian committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    {
        String token = (String)redisCache.getCacheObject("apiToken");
        if(token==null){
            token = getNewToken();
        }
        JSONObject json = new JSONObject();
        json.put("domain", domain);
        json.put("method", methodType);
        json.put("data", list);
        JSONObject jobct = (JSONObject) HttpClient.sendPostJson(apiUrl,json,token);
        return jobct;
    }

    public String getNewToken()throws Exception{
        JSONObject json = new JSONObject();
        json.put("appId", appId);
        json.put("appSecret", appSecret);
        JSONObject jobct = (JSONObject) HttpClient.sendPostJson(tokenUrl,json,null);
        redisCache.setCacheObject("apiToken",(String)((Map<String,Object>)jobct.get("data")).get("accessToken"),7000, TimeUnit.SECONDS);
        return (String)((Map<String,Object>)jobct.get("data")).get("accessToken");
    }
}