package com.tzy.s.wx.common;

import com.alibaba.fastjson.JSONObject;
import com.tzy.s.wx.dto.Accesstoken;
import com.tzy.s.wx.dto.Result;
import com.tzy.s.wx.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * 一些公用方法
 */
@Slf4j
public class Common implements Serializable {
    /**
     * 調用接口公用方法-[get accessToken]
     * @param url
     * @return
     */
    public Result getAccesstoken(String url){
        log.info("get accessToken  url==>>"+url);
        String ret = HttpClient.httpsRequest(url, "GET", null);
        if(StringUtils.isEmpty(ret)) return Result.error(-1,"请求失败");
        try {
            JSONObject retJson = JSONObject.parseObject(ret);
            Accesstoken appletAccesstoken = JSONObject.toJavaObject(retJson,Accesstoken.class);
            return appletAccesstoken;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(-1,"请求异常，请联系开发人员");
        }
    }

}
