jiangping
2023-08-18 5dfbc4af25ab4f5e8d75f99c6e889b5004a49ef0
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package doumeemes.core.utils.edpg;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iflytek.antelope.other.client.dto.resp.UserDTO;
import doumeemes.core.utils.HttpsUtil;
import doumeemes.core.utils.edpg.bean.AccessTokenModel;
import doumeemes.core.utils.edpg.bean.AppDepartListModel;
import doumeemes.core.utils.edpg.bean.AppUserInfoModel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.List;
 
 
@Slf4j
@Data
public class EdgpServerUtil {
 
    public static void main(String[] args) {
    }
    private String appKey;
    private String appSecret;
    private String aesToken;
    private String url;
    public static final String GET_ACCESSTOKEN_URL = "/apps/web/accessToken";
    public static final String GET_USERINFO_URL = "/apps/web/user/infoById";
    public static final String GET_USERINFO_TOKEN_URL = "/apps/web//user/infoByToken";
    public static final String GET_ALL_DEPARTMENT_TREE_URL = "/apps/web/department/tree";
    public EdgpServerUtil(String url,String appKey,String appSecret){
        this.appKey = appKey;
        this.url=url;
        this.appSecret=appSecret;
    }
 
    public   String getAccessToken()   {
        try {
            String acturl = url+GET_ACCESSTOKEN_URL+"?accessKey="+appKey+"&secret="+appSecret;
            String result = HttpsUtil.sendGetByHttps(acturl,null);
            log.error("EDGP:===============获取accessToken:"+acturl+"\n返回:"+result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject !=null && jsonObject.getBoolean("success")){
                AccessTokenModel model = JSONObject.toJavaObject(jsonObject.getJSONObject("data"), AccessTokenModel.class);
                if(model != null){
                    return  model.getAccessToken();
                }
            }
        }catch (Exception e){
            log.error("EDGP:===============获取accessToken失败!"+e.getMessage());
        }
        return  null;
    }
    public AppUserInfoModel getUserInfoById(String acccessToken, String userid )   {
        try {
            String acturl  =url+GET_ALL_DEPARTMENT_TREE_URL+"?accessToken="+acccessToken+"&userId="+userid;
            String result = HttpsUtil.sendGetByHttps(acturl,null);
            log.error("EDGP:===============获取用户编码获取用户信息:"+acturl+"\n返回:"+result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject !=null && jsonObject.getBoolean("success")){
                AppUserInfoModel model = JSONObject.toJavaObject(jsonObject.getJSONObject("data"), AppUserInfoModel.class);
                if(model != null){
                    return model;
                }
            }
        }catch (Exception e){
            log.error("EDGP:===============获取用户编码获取用户信息失败!"+e.getMessage());
        }
        return  null;
    }
    public List<AppDepartListModel> getAllDepartList(String acccessToken ,String companyId)   {
        List<AppDepartListModel> list = null;
        try {
            String acturl  =url+GET_USERINFO_URL+"?accessToken="+acccessToken+"&companyId="+companyId;
            String result = HttpsUtil.sendGetByHttps(acturl,null);
            log.error("EDGP:===============获取企业部门树形信息失败:"+acturl+"\n返回:"+result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject !=null && jsonObject.getBoolean("success")){
              JSONArray model =  jsonObject.getJSONArray("data") ;
                if(model != null && model.size()>0){
                    list = new ArrayList<>();
                    for (int i = 0; i < model.size(); i++) {
                        list.add(JSONObject.toJavaObject(model.getJSONObject(i),AppDepartListModel.class));
                    }
                }
            }
        }catch (Exception e){
            log.error("EDGP:===============获取企业部门树形信息失败!"+e.getMessage());
        }
        return  list;
    }
    public AppUserInfoModel getUserInfoByToken(String token  )   {
        try {
            String acturl = url+GET_USERINFO_TOKEN_URL+"?token="+token;
            String result = HttpsUtil.sendGetByHttps(acturl,null);
            log.error("EDGP:===============根据授权登录码获取用户信息:"+acturl+"\n返回:"+result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject !=null && jsonObject.getBoolean("success")){
                AppUserInfoModel model = JSONObject.toJavaObject(jsonObject.getJSONObject("data"), AppUserInfoModel.class);
                if(model != null){
                    return model;
                }
            }
        }catch (Exception e){
            log.error("EDGP:===============根据授权登录码获取用户信息失败!"+e.getMessage());
        }
        return  null;
    }
}