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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
| import com.alibaba.fastjson.JSON; import com.itwork.business.api.AuthInfoApi; import com.itwork.business.api.NoticeEventApi; import com.itwork.common.config.consts.Consts; import com.itwork.common.dto.AuthInfoInDto; import com.itwork.common.dto.NoticeEventOutDto; import com.itwork.common.utils.DtoUtils; import com.itwork.weixin.cp.bean.WxAccessToken; import com.itwork.weixin.cp.bean.WxCorpAccessToken; import com.itwork.weixin.cp.bean.WxCorpAuthInfo; import com.itwork.weixin.cp.bean.WxProAuthCode; import com.itwork.weixin.cp.bean.WxProviderAccessToken; import com.itwork.weixin.cp.bean.WxSuiteAccessToken; import com.itwork.weixin.cp.properties.WxCpProperties; import com.itwork.weixin.cp.service.SuiteServcie; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;
import java.util.Arrays; import java.util.Map;
@Slf4j @Service public class SuiteServiceImpl implements SuiteServcie {
@Autowired private RestTemplate restTemplate;
@Autowired private RedisTemplate<Object, Object> redisTemplate;
@Autowired private WxCpProperties properties;
protected final Object getGlobalProviderAccessTokenRefreshLock = new Object(); protected final Object getGlobalSuiteAccessTokenRefreshLock = new Object(); protected final Object getGlobalProAuthCodeRefreshLock = new Object(); protected final Object getGlobalCorpAccessTokenRefreshLock = new Object(); protected final Object getGlobalContactAccessTokenRefreshLock = new Object();
@Override public String getProviderAccessToken() throws WxErrorException { if (this.isProviderAddessTokenExpires()) { synchronized (this.getGlobalProviderAccessTokenRefreshLock) { log.info("获取服务商凭证...................start....................."); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token"; Map<String, String> params = Maps.newHashMap(); params.put("corpid", this.properties.getCorpId()); params.put("provider_secret", this.properties.getProviderSecret());
log.info("获取服务商凭证请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("获取服务商凭证响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); }
WxProviderAccessToken token = WxProviderAccessToken.formJson(response); long expiresIn = System.currentTimeMillis() + (token.getExpiresIn() - 200) * 1000L; redisTemplate.opsForValue().set(Consts.PROVIDER_ACCESS_TOKEN + this.properties.getCorpId(), token.getProviderAccessToken()); redisTemplate.opsForValue().set(Consts.PROVIDER_ACCESS_TOKEN_EXPIRES + this.properties.getCorpId(), expiresIn); log.info("获取服务商凭证...................end.....................");
} } return redisTemplate.opsForValue().get(Consts.PROVIDER_ACCESS_TOKEN + this.properties.getCorpId()).toString(); }
private boolean isProviderAddessTokenExpires() { Long expiresIn = (Long) redisTemplate.opsForValue().get(Consts.PROVIDER_ACCESS_TOKEN_EXPIRES + this.properties.getSuiteId()); if (expiresIn != null) { return System.currentTimeMillis() > expiresIn; } return true; }
@Override public String getSuiteAccessToken() throws WxErrorException { if (this.isSuiteAccessTokeExpires()) { synchronized (this.getGlobalSuiteAccessTokenRefreshLock) { log.info("获取suite_access_token.................start...................."); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token";
Map<Object, Object> params = Maps.newHashMap(); params.put("suite_id", this.properties.getSuiteId()); params.put("suite_secret", this.properties.getSuiteSecret()); params.put("suite_ticket", redisTemplate.opsForValue().get(WxConsts.XmlInfoType.SUITE_TICKET + ":" + this.properties.getSuiteId()));
log.info("获取suite_access_token请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("获取suite_access_token响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } WxSuiteAccessToken accessToken = WxSuiteAccessToken.fromJson(response);
long expiresIn = System.currentTimeMillis() + (accessToken.getExpiresIn() - 200) * 1000L;
redisTemplate.opsForValue().set(Consts.SUITE_ACCESS_TOKEN + this.properties.getSuiteId(), accessToken.getSuiteAccessToken()); redisTemplate.opsForValue().set(Consts.SUITE_ACCESS_TOKEN_EXPIRES + this.properties.getSuiteId(), expiresIn); log.info("获取suite_access_token.................end...................."); } } return redisTemplate.opsForValue().get(Consts.SUITE_ACCESS_TOKEN + this.properties.getSuiteId()).toString(); }
private boolean isSuiteAccessTokeExpires() { Long expiresIn = (Long) redisTemplate.opsForValue().get(Consts.SUITE_ACCESS_TOKEN_EXPIRES + this.properties.getSuiteId()); if (expiresIn != null) { return System.currentTimeMillis() > expiresIn; } return true; }
@Override public String getPreAuthCode() throws WxErrorException { if (this.isPreAuthCodeExpires()) { synchronized (this.getGlobalProAuthCodeRefreshLock) { log.info("获取pro_auth_code.................start...................."); String suiteAccessToken = getSuiteAccessToken(); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=" + suiteAccessToken;
String response = restTemplate.getForObject(url, String.class); log.info("获取pro_auth_code响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); }
WxProAuthCode code = WxProAuthCode.fromJson(response); long expiresIn = System.currentTimeMillis() + (code.getExpiresIn() - 200) * 1000L;
redisTemplate.opsForValue().set(Consts.PRO_AUTH_CODE + this.properties.getSuiteId(), code.getPreAuthCode()); redisTemplate.opsForValue().set(Consts.PRO_AUTH_CODE_EXPIRES + this.properties.getSuiteId(), expiresIn); log.info("获取pro_auth_code.................end...................."); } }
return redisTemplate.opsForValue().get(Consts.PRO_AUTH_CODE + this.properties.getSuiteId()).toString(); }
private boolean isPreAuthCodeExpires() { Long expiresIn = (Long) redisTemplate.opsForValue().get(Consts.PRO_AUTH_CODE_EXPIRES + this.properties.getSuiteId()); if (expiresIn != null) { return System.currentTimeMillis() > expiresIn; } return true; }
@Override public void setSessionInfo() throws WxErrorException { log.info("设置授权配置......................start..................."); String proAuthCode = this.getPreAuthCode();
String suiteAccessToken = this.getSuiteAccessToken();
String url = "https://qyapi.weixin.qq.com/cgi-bin/service/set_session_info?suite_access_token=" + suiteAccessToken;
Map<String, Object> params = Maps.newHashMap(); params.put("pre_auth_code", proAuthCode); Map<String, Object> sessInfo = Maps.newHashMap(); sessInfo.put("auth_type", 1); params.put("session_info", sessInfo);
log.info("设置授权配置请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("设置授权配置响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } log.info("设置授权配置......................end..................."); }
@Override public String getPermanentCode(String authCode) throws WxErrorException { log.info("获取企业永久授权码...................start..................."); String suiteAccessToken = this.getSuiteAccessToken(); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=" + suiteAccessToken;
Map<String, String> params = Maps.newHashMap(); params.put("auth_code", authCode); log.info("获取企业永久授权码请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("获取企业永久授权码响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } WxCorpAuthInfo authInfo = WxCorpAuthInfo.formJson(response); String corpId = authInfo.getAuthCorpInfo().getCorpid(); long expiresIn = System.currentTimeMillis() + (authInfo.getExpiresIn() - 200) * 1000L; redisTemplate.opsForValue().set(Consts.PERMANENT_CODE + corpId, authInfo.getPermanentCode()); redisTemplate.opsForValue().set(Consts.CORP_ACCESS_TOKEN + corpId, authInfo.getAccessToken()); redisTemplate.opsForValue().set(Consts.CORP_ACCESS_TOKEN_EXPIRES + corpId, expiresIn); log.info("获取企业永久授权码...................end..................."); this.getAuthInfo(corpId); return authInfo.getPermanentCode(); }
@Override public String getCorpToken(String authCorpId) throws WxErrorException { if (this.isCorpAccessTokenExpires(authCorpId)) { synchronized (this.getGlobalCorpAccessTokenRefreshLock) { log.info("获取企业凭证...................start.........................."); String suiteAccessToken = this.getSuiteAccessToken(); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token?suite_access_token=" + suiteAccessToken; String permanentCode = "企业永久授权码,这里持久到数据库了"; Map<String, String> params = Maps.newHashMap(); params.put("auth_corpid", authCorpId); params.put("permanent_code", permanentCode);
log.info("获取企业凭证请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("获取企业凭证响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); }
WxCorpAccessToken token = WxCorpAccessToken.fromJson(response); long expiresIn = System.currentTimeMillis() + (token.getExpiresIn() - 200) * 1000L; redisTemplate.opsForValue().set(Consts.CORP_ACCESS_TOKEN + authCorpId, token.getAccessToken()); redisTemplate.opsForValue().set(Consts.CORP_ACCESS_TOKEN_EXPIRES + authCorpId, expiresIn); log.info("获取企业凭证...................end.........................."); } }
return redisTemplate.opsForValue().get(Consts.CORP_ACCESS_TOKEN + authCorpId).toString(); }
private boolean isCorpAccessTokenExpires(String authCorpId) { Long expiresIn = (Long) redisTemplate.opsForValue().get(Consts.CORP_ACCESS_TOKEN_EXPIRES + authCorpId); if (expiresIn != null) { return System.currentTimeMillis() > expiresIn; } return true; }
@Override public void getAuthInfo(String authCorpId) throws WxErrorException { log.info("获取企业授权信息....................start....................."); String suiteAccessToken = this.getSuiteAccessToken(); String url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info?suite_access_token=" + suiteAccessToken; String permanentCode = redisTemplate.opsForValue().get(Consts.PERMANENT_CODE + authCorpId).toString();
Map<String, String> params = Maps.newHashMap(); params.put("auth_corpid", authCorpId); params.put("permanent_code", permanentCode);
log.info("获取企业授权信息请求参数:{}", params); String response = restTemplate.postForObject(url, JSON.toJSONString(params), String.class); log.info("获取企业授权信息响应参数:{}", response);
WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); }
WxCorpAuthInfo authInfo = WxCorpAuthInfo.formJson(response); log.info("获取企业授权信息....................end....................."); }
@Override public String getContactAccessToken(String authCorpId) throws WxErrorException { if (this.isContactAccessTokenExpires(authCorpId)) { synchronized (this.getGlobalContactAccessTokenRefreshLock) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s"; String secert = "这里的secert是用到企业微信中通讯录同步助手中的secert"; url = String.format(url, authCorpId, secert); String response = restTemplate.getForObject(url, String.class); WxError error = WxError.fromJson(response); if (error.getErrorCode() != 0) { throw new WxErrorException(error); }
WxAccessToken accessToken = WxAccessToken.fromJson(response); long expiresIn = System.currentTimeMillis() + (accessToken.getExpiresIn() - 200) * 1000L; redisTemplate.opsForValue().set(Consts.CONTACT_ACCESS_TOKEN + authCorpId, accessToken.getAccessToken()); redisTemplate.opsForValue().set(Consts.CONTACT_ACCESS_TOKEN_EXPIRES + authCorpId, expiresIn); } } return redisTemplate.opsForValue().get(Consts.CONTACT_ACCESS_TOKEN + authCorpId).toString(); }
private boolean isContactAccessTokenExpires(String authCorpId) { Long expiresIn = (Long) redisTemplate.opsForValue().get(Consts.CONTACT_ACCESS_TOKEN_EXPIRES + authCorpId); if (expiresIn != null) { return System.currentTimeMillis() > expiresIn; } return true; } }
|