123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package com.care.keeper.service;
- import cn.hutool.json.JSONObject;
- import com.care.common.cache.RedisKeyConstant;
- import com.care.common.cache.RedisUtil;
- import com.care.common.util.HttpUtil;
- import com.care.common.util.Result;
- import com.care.keeper.config.KeeperWxConfig;
- import com.care.keeper.config.PlatformPinanshouhuConfig;
- import com.care.keeper.vo.KeeperInitParams;
- import com.google.gson.Gson;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.text.MessageFormat;
- @Slf4j
- @Service
- public class WxKeeperPassportService extends AbstractKeeperPassportService {
- @Resource
- private RedisUtil redisUtil;
- @Resource
- private PlatformPinanshouhuConfig platformPinanshouhuConfig;
- Gson gson = new Gson();
- public Result<Object> login(KeeperInitParams params) {
- KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
- // 解析codeUrl
- String codeUrl = MessageFormat.format(wxConfig.getCodeUrl(), wxConfig.getAppId(), wxConfig.getSecret(), params.getCode());
- // 通过codeUrl获取openid
- JSONObject jsonObject = HttpUtil.httpGet(codeUrl);
- log.warn("-----jsonObject:[{}]", jsonObject);
- String openid = jsonObject.getStr("openid");
- String sessionKey = jsonObject.getStr("session_key");
- log.warn("-----openid:[{}], session_key:[{}]", openid, sessionKey);
- KeeperInitParams keeperInitParams = new KeeperInitParams();
- params.setLoginType("wx");
- keeperInitParams.setOpenid(openid);
- keeperInitParams.setSessionKey(sessionKey);
- redisUtil.hset(RedisKeyConstant.PINANSHOUHU_WX_LOGIN_INFO, params.getCode(), keeperInitParams, RedisKeyConstant.PINANSHOUHU_WX_LOGIN_INFO_TIME);
- return Result.success();
- }
- public String accessToken() {
- KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
- String access_token = (String) redisUtil.hget(RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN, wxConfig.getAppId());
- if (access_token != null) {
- return access_token;
- }
- return this.flushWxToken();
- }
- private String flushWxToken() {
- KeeperWxConfig wxConfig = platformPinanshouhuConfig.keeperWxConfig();
- // 解析codeUrl
- String tokenUrl = MessageFormat.format(wxConfig.getTokenUrl(), wxConfig.getAppId(), wxConfig.getSecret());
- // 通过codeUrl获取openid
- JSONObject jsonObject = HttpUtil.httpGet(tokenUrl);
- String access_token = jsonObject.getStr("access_token");
- redisUtil.hset(RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN, wxConfig.getAppId(), access_token, RedisKeyConstant.WX_PINANSHOUHU_ACCESS_TOKEN_TIME);
- return access_token;
- }
- }
|