package com.farriver.bwf.web.controller.wechat; import com.alibaba.fastjson2.JSON; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.common.utilities.HttpUtil; import com.farriver.bwf.data.transferobject.wechat.GongZhongHaoAccessToken; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Component @Service @CacheConfig(cacheNames = "bwf_WeChatOauthViaGZHConfig_cache") public class WeChatOauthViaGZHConfig { private static final Logger logger = LoggerFactory.getLogger(WeChatOauthViaGZHConfig.class); @Value("${wx.gzh_appid}") private String gzh_appid = ""; @Value("${wx.gzh_secret}") private String gzh_secret = ""; @Value("${wx.gzh_notify_url}") private String gzh_notify_url = ""; @Value("${wx.mch_id}") private String mch_id = ""; @Value("${wx.paykey}") private String paykey = ""; @Value("${wx.serialNo}") private String serialNo = ""; @Value("${wx.privateKey}") private String privateKey = ""; @Cacheable public ApiData GetAccessToken(String code) throws Exception { String getAccessTokenUri = String.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code", gzh_appid, gzh_secret, code); String tokenData = HttpUtil.Get(getAccessTokenUri); GongZhongHaoAccessToken data = JSON.parseObject(tokenData, GongZhongHaoAccessToken.class); if (data != null && data.getOpenid() != null && !data.getOpenid().isEmpty()) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, data); } else { return ApiData.error(LangConstants.MSG_INFO_FAILED); } } }