WxKeeperPassportService.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.care.keeper.service;
  2. import cn.hutool.json.JSONObject;
  3. import com.care.common.cache.RedisKeyConstant;
  4. import com.care.common.cache.RedisUtil;
  5. import com.care.common.util.HttpUtil;
  6. import com.care.common.util.Result;
  7. import com.care.keeper.config.KeeperWxConfig;
  8. import com.care.keeper.config.PlatformPinanshouhuConfig;
  9. import com.care.keeper.vo.KeeperInitParams;
  10. import com.google.gson.Gson;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import java.text.MessageFormat;
  15. @Slf4j
  16. @Service
  17. public class WxKeeperPassportService extends AbstractKeeperPassportService {
  18. @Resource
  19. private RedisUtil redisUtil;
  20. @Resource
  21. private PlatformPinanshouhuConfig platformPinanshouhuConfig;
  22. Gson gson = new Gson();
  23. public Result<Object> login(KeeperInitParams params) {
  24. KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
  25. // 解析codeUrl
  26. String codeUrl = MessageFormat.format(wxConfig.getCodeUrl(), wxConfig.getAppId(), wxConfig.getSecret(), params.getCode());
  27. // 通过codeUrl获取openid
  28. JSONObject jsonObject = HttpUtil.httpGet(codeUrl);
  29. log.warn("-----jsonObject:[{}]", jsonObject);
  30. String openid = jsonObject.getStr("openid");
  31. String sessionKey = jsonObject.getStr("session_key");
  32. log.warn("-----openid:[{}], session_key:[{}]", openid, sessionKey);
  33. KeeperInitParams keeperInitParams = new KeeperInitParams();
  34. params.setLoginType("wx");
  35. keeperInitParams.setOpenid(openid);
  36. keeperInitParams.setSessionKey(sessionKey);
  37. redisUtil.hset(RedisKeyConstant.PINANSHOUHU_WX_LOGIN_INFO, params.getCode(), keeperInitParams, RedisKeyConstant.PINANSHOUHU_WX_LOGIN_INFO_TIME);
  38. return Result.success();
  39. }
  40. public String accessToken() {
  41. KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
  42. String access_token = (String) redisUtil.hget(RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN, wxConfig.getAppId());
  43. if (access_token != null) {
  44. return access_token;
  45. }
  46. return this.flushWxToken();
  47. }
  48. private String flushWxToken() {
  49. KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
  50. // 解析codeUrl
  51. String tokenUrl = MessageFormat.format(wxConfig.getTokenUrl(), wxConfig.getAppId(), wxConfig.getSecret());
  52. // 通过codeUrl获取openid
  53. JSONObject jsonObject = HttpUtil.httpGet(tokenUrl);
  54. String access_token = jsonObject.getStr("access_token");
  55. redisUtil.hset(RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN, wxConfig.getAppId(), access_token, RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN_TIME);
  56. return access_token;
  57. }
  58. }