package com.muzhi.dingtalk.test.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
import com.muzhi.dingtalk.test.service.DingAccessTokenService;
import com.muzhi.dingtalk.test.service.FangDeDingAccessTokenService;
import com.taobao.api.ApiException;
import org.springframework.stereotype.Service;


/**
 * @Author ZhangKan
 * @Data 15:39
 */

@Service
public class FangDeDingAccessTokenServiceImpl implements FangDeDingAccessTokenService {
    //DingAccessTokenService

    //房德
    //AUTHCORPID(被授权方（房德）corpid)
    private static String AUTHCORPID = "ding81b4dfad3a436bd735c2f4657eb6378f";
    //CUSTOMKEY
    private static String CUSTOMKEY = "suitekzs42ayrw8ovg8li";
    //CUSTOMSECTET
    private static String CUSTOMSECTET = "AiJrQg4eZfJHEkoWj5bC_NZUlMWdKdOzSNG8NewaDIfL25q2BucxJDbanEPe99I5";
    //SUITETICKET
    private static String SUITETICKET = "";

    public static String dingzhiaccessToken = null;


    //获取定制服务access_token授权的方法（房德）
//    @Override
//    public String getFangDeAccessToken(){
//        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
//        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
//        req.setAuthCorpid(AUTHCORPID);
//        try {
//            OapiServiceGetCorpTokenResponse execute = client.execute(req,CUSTOMKEY,CUSTOMSECTET, SUITETICKET);
//            dingzhiaccessToken = execute.getAccessToken();
//            System.out.println("房德DingzhiAccessToken：" + dingzhiaccessToken);
////            String string = JSON.toJSONString(execute);
////            System.out.println("execute：" + string);
//        }catch (ApiException e){
//            e.printStackTrace();
//        }
//        return dingzhiaccessToken;
//    }

    //房德
    //APPKEY
    private static String APPKEY = "dingqtvicxlaua1zktiy";
    //APPSECRET
    private static String APPSECRET = "f43HS3ylAbpelFPwdxUhcuMCWQhIsC5vo7KSTm84KANOotLyC34XL6foDYwXY8WD";

    public static String accessToken = null;


    //获取access_token的方法（房德）
    @Override
    public String getFangDeAccessToken() {
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(APPKEY);
        request.setAppsecret(APPSECRET);
        request.setHttpMethod("GET");
        try {
            OapiGettokenResponse response = client.execute(request);
            accessToken = response.getAccessToken();
            System.out.println("房德AccessToken：" + accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return accessToken;
    }
}
