Forráskód Böngészése

根据code获取openid缓存到redis 10秒(因同一个code只能获取一次openid,短时间内用户可能会刷新页面,多次获取)

chenyongxing 5 éve
szülő
commit
d8d09c1457

+ 14 - 1
svr/svr-patient/src/main/java/com/yihu/jw/patient/endpoint/wechat/WechatInfoEndPoint.java

@ -13,11 +13,13 @@ import com.yihu.jw.wechat.service.WxAccessTokenService;
import com.yihu.jw.wechat.service.WxUrlConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -28,6 +30,7 @@ import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
 * Created by Trick on 2018/12/3.
@ -45,6 +48,8 @@ public class WechatInfoEndPoint extends EnvelopRestEndpoint {
    private WechatInfoService wechatInfoService;
    @Value("${wechat.id}")
    private String wxId;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private WxUrlConfigService wxUrlConfigService;
@ -96,7 +101,15 @@ public class WechatInfoEndPoint extends EnvelopRestEndpoint {
    @RequestMapping(value = "getOpenidByCode", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Envelop getOpenidByCode(String code) throws Exception {
        String openid = wechatInfoService.getOpenidByCode(code, wxId);
        //通过redis获取openid, 获取不到,则调用微信接口去取
        String key = wxId+":code";
        String openid = redisTemplate.opsForValue().get(key);
        if(StringUtils.isNotBlank(openid)){
            return success(PatientRequestMapping.Wechat.api_success,openid);
        }
        openid = wechatInfoService.getOpenidByCode(code, wxId);
        redisTemplate.opsForValue().set(key,openid);
        redisTemplate.expire(key,10, TimeUnit.SECONDS);
        return success(PatientRequestMapping.Wechat.api_success,openid);
    }