Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重新实现获取三方授权码实现 #1450

Merged
merged 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.chanjar.weixin.common.util.http.RequestHttp;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;

/**
Expand Down Expand Up @@ -88,8 +89,36 @@ public interface WxCpTpService {
* @param authCode .
* @return .
*/
@Deprecated
WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException;

/**
* 获取企业永久授权码信息
* <pre>
* 原来的方法实现不全
* </pre>
*
* @param authCode
* @return
*
* @author yuan
* @since 2020-03-18
*
* @throws WxErrorException
*/
WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException;

/**
* <pre>
* 获取预授权链接
* </pre>
* @param redirectUri 授权完成后的回调网址
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
* @return
* @throws WxErrorException
*/
String getPreAuthUrl(String redirectUri,String state) throws WxErrorException;

/**
* 当本Service没有实现某个API的时候,可以用这个,针对所有微信API中的GET请求.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Joiner;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.bean.WxAccessToken;
Expand All @@ -17,10 +18,14 @@
import me.chanjar.weixin.cp.api.WxCpTpService;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
import me.chanjar.weixin.cp.bean.WxCpTpPreauthCode;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -123,6 +128,26 @@ public WxCpTpCorp getPermanentCode(String authCode) throws WxErrorException {
return wxCpTpCorp;
}

@Override
public WxCpTpPermanentCodeInfo getPermanentCodeInfo(String authCode) throws WxErrorException{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("auth_code", authCode);
String result = post(configStorage.getApiUrl(GET_PERMANENT_CODE), jsonObject.toString());
return WxCpTpPermanentCodeInfo.fromJson(result);
}

@Override
@SneakyThrows
public String getPreAuthUrl(String redirectUri,String state) throws WxErrorException{
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE),null);
WxCpTpPreauthCode preauthCode = WxCpTpPreauthCode.fromJson(result);
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id="+configStorage.getSuiteId()+
"&pre_auth_code="+preauthCode.getPreAuthCode()+"&redirect_uri="+ URLEncoder.encode(redirectUri,"utf-8");
if(StringUtils.isNotBlank(state))
preAuthUrl += "&state="+state;
return preAuthUrl;
}

@Override
public String get(String url, String queryParam) throws WxErrorException {
return execute(SimpleGetRequestExecutor.create(this), url, queryParam);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package me.chanjar.weixin.cp.bean;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.util.List;

/**
* 微信部门.
*
* @author Daniel Qian
*/
@Getter
@Setter
public class WxCpTpPermanentCodeInfo extends WxCpBaseResp {

private static final long serialVersionUID = -5028321625140879571L;

@SerializedName("access_token")
private String accessToken;

@SerializedName("expires_in")
private Long ExpiresIn;

@SerializedName("permanent_code")
private String permanentCode;

/**
* 授权企业信息
*/
@SerializedName("auth_corp_info")
private AuthCorpInfo authCorpInfo;

/**
* 授权信息。如果是通讯录应用,且没开启实体应用,是没有该项的。通讯录应用拥有企业通讯录的全部信息读写权限
*/
@SerializedName("auth_info")
private AuthInfo authInfo;

/**
* 授权用户信息
*/
@SerializedName("auth_user_info")
private AuthUserInfo authUserInfo;


@Getter
@Setter
public static class AuthCorpInfo {
@SerializedName("corpid")
private String corpId;

@SerializedName("corp_name")
private String corpName;

@SerializedName("corp_type")
private String corpType;

@SerializedName("corp_square_logo_url")
private String corpSquareLogoUrl;

@SerializedName("corp_round_logo_url")
private String corpRoundLogoUrl;

@SerializedName("corp_user_max")
private String corpUserMax;

@SerializedName("corp_agent_max")
private String corpAgentMax;

/**
* 所绑定的企业微信主体名称(仅认证过的企业有)
*/
@SerializedName("corp_full_name")
private String corpFullName;

/**
* 认证到期时间
*/
@SerializedName("verified_end_time")
private Long verifiedEndTime;

/**
* 企业类型,1. 企业; 2. 政府以及事业单位; 3. 其他组织, 4.团队号
*/
@SerializedName("subject_type")
private Integer subjectType;

/**
* 授权企业在微工作台(原企业号)的二维码,可用于关注微工作台
*/
@SerializedName("corp_wxqrcode")
private String corpWxqrcode;

@SerializedName("corp_scale")
private String corpScale;

@SerializedName("corp_industry")
private String corpIndustry;

@SerializedName("corp_sub_industry")
private String corpSubIndustry;

@SerializedName("location")
private String location;

}

/**
* 授权信息
*/
@Getter
@Setter
public static class AuthInfo {

/**
* 授权的应用信息,注意是一个数组,但仅旧的多应用套件授权时会返回多个agent,对新的单应用授权,永远只返回一个agent
*/
@SerializedName("agent")
private List<Agent> agent;

}

@Getter
@Setter
public static class Agent {
@SerializedName("agentid")
private Integer agentid;

@SerializedName("name")
private String name;

@SerializedName("round_logo_url")
private String roundLogoUrl;

@SerializedName("square_logo_url")
private String squareLogoUrl;

/**
* 旧的多应用套件中的对应应用id,新开发者请忽略
*/
@SerializedName("appid")
@Deprecated
private String appid;

/**
* 应用权限
*/
@SerializedName("privilege")
private Privilege privilege;

}

/**
* 授权人员信息
*/
@Getter
@Setter
public static class AuthUserInfo {
@SerializedName("userid")
private String userid;

@SerializedName("name")
private String name;

@SerializedName("avatar")
private String avatar;
}

/**
* 应用对应的权限
*/
@Getter
@Setter
public static class Privilege {

/**
* 权限等级。
* 1:通讯录基本信息只读
* 2:通讯录全部信息只读
* 3:通讯录全部信息读写
* 4:单个基本信息只读
* 5:通讯录全部信息只写
*/
@SerializedName("level")
private Integer level;

@SerializedName("allow_party")
private List<Integer> allowParty;

@SerializedName("allow_user")
private List<String> allowUser;

@SerializedName("allow_tag")
private List<Integer> allowTag;

@SerializedName("extra_party")
private List<Integer> extraParty;

@SerializedName("extra_user")
private List<String> extraUser;

@SerializedName("extra_tag")
private List<Integer> extraTag;


}


public static WxCpTpPermanentCodeInfo fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTpPermanentCodeInfo.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.chanjar.weixin.cp.bean;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
* 预授权码返回
* @author yqx
* @date 2020/3/19
*/
@Getter
@Setter
public class WxCpTpPreauthCode extends WxCpBaseResp {

@SerializedName("pre_auth_code")
String preAuthCode;

@SerializedName("expires_in")
Long expiresIn;

public static WxCpTpPreauthCode fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpTpPreauthCode.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class Tp {
public static final String GET_PERMANENT_CODE = "/cgi-bin/service/get_permanent_code";
public static final String GET_SUITE_TOKEN = "/cgi-bin/service/get_suite_token";
public static final String GET_PROVIDER_TOKEN = "/cgi-bin/service/get_provider_token";
public static final String GET_PREAUTH_CODE = "/cgi-bin/service/get_pre_auth_code";
}

public static class User {
Expand Down
Loading