AuthWechatAppletsRequest.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package jnpf.socials.request;
  2. import com.alibaba.fastjson.JSONObject;
  3. import jnpf.socials.enums.AuthDefaultSourceNew;
  4. import me.zhyd.oauth.cache.AuthStateCache;
  5. import me.zhyd.oauth.config.AuthConfig;
  6. import me.zhyd.oauth.enums.AuthResponseStatus;
  7. import me.zhyd.oauth.exception.AuthException;
  8. import me.zhyd.oauth.log.Log;
  9. import me.zhyd.oauth.model.AuthCallback;
  10. import me.zhyd.oauth.model.AuthResponse;
  11. import me.zhyd.oauth.model.AuthToken;
  12. import me.zhyd.oauth.model.AuthUser;
  13. import me.zhyd.oauth.request.AuthDefaultRequest;
  14. import me.zhyd.oauth.utils.HttpUtils;
  15. import me.zhyd.oauth.utils.StringUtils;
  16. import me.zhyd.oauth.utils.UrlBuilder;
  17. /**
  18. * 流程设计
  19. *
  20. * @author JNPF开发平台组
  21. * @version V3.4.2
  22. * @copyright 引迈信息技术有限公司
  23. * @date 2022/9/16 12:11:36
  24. */
  25. public class AuthWechatAppletsRequest extends AuthDefaultRequest {
  26. public AuthWechatAppletsRequest(AuthConfig config) {
  27. super(config, AuthDefaultSourceNew.WECHAT_APPLETS);
  28. }
  29. public AuthWechatAppletsRequest(AuthConfig config, AuthStateCache authStateCache) {
  30. super(config, AuthDefaultSourceNew.WECHAT_APPLETS, authStateCache);
  31. }
  32. @Override
  33. public AuthToken getAccessToken(AuthCallback authCallback) {
  34. return null;
  35. }
  36. @Override
  37. public AuthUser getUserInfo(AuthToken authToken) {
  38. return null;
  39. }
  40. protected String getuserInfoUrl(AuthCallback authCallback) {
  41. return UrlBuilder.fromBaseUrl(this.source.userInfo())
  42. .queryParam("appid", this.config.getClientId())
  43. .queryParam("secret", this.config.getClientSecret())
  44. .queryParam("js_code", authCallback.getCode())
  45. .queryParam("grant_type", "authorization_code").build();
  46. }
  47. AuthResponse responseError(Exception e) {
  48. int errorCode = AuthResponseStatus.FAILURE.getCode();
  49. String errorMsg = e.getMessage();
  50. if (e instanceof AuthException) {
  51. AuthException authException = (AuthException) e;
  52. errorCode = authException.getErrorCode();
  53. if (StringUtils.isNotEmpty(authException.getErrorMsg())) {
  54. errorMsg = authException.getErrorMsg();
  55. }
  56. }
  57. return AuthResponse.builder().code(errorCode).msg(errorMsg).build();
  58. }
  59. @Override
  60. public AuthResponse login(AuthCallback authCallback) {
  61. try {
  62. AuthUser user = this.getUserUnionid(authCallback);
  63. return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
  64. } catch (Exception var4) {
  65. Log.error("Failed to login with oauth authorization.", var4);
  66. return this.responseError(var4);
  67. }
  68. }
  69. protected AuthUser getUserUnionid(AuthCallback authCallback) {
  70. String response = (new HttpUtils(this.config.getHttpConfig())).get(this.getuserInfoUrl(authCallback)).getBody();
  71. JSONObject object = JSONObject.parseObject(response);
  72. AuthToken authToken = new AuthToken();
  73. if (object.containsKey("unionid")) {
  74. authToken.setUnionId(object.getString("unionid"));
  75. }
  76. return AuthUser.builder().rawUserInfo(object).token(authToken).source(this.source.toString()).build();
  77. }
  78. }