package com.fzzy.appwx.util;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.fzzy.appwx.constant.WeChatConst;
|
import com.fzzy.igds.data.ConfigData;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
|
/**
|
* 微信工具类
|
*
|
* @author chen
|
*/
|
@Slf4j
|
@Component
|
public class WeChatUtil {
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
@Autowired
|
private ConfigData configData;
|
|
/**
|
* get请求
|
*
|
* @param url
|
* @return
|
*/
|
public String doGet(String url) {
|
return restTemplate.getForObject(url, String.class);
|
}
|
|
|
/**
|
* 根据网页授权code获取openid
|
*
|
* @param code
|
* @return
|
*/
|
public String getOpenid(String code) {
|
|
String url = WeChatConst.OAUTH2_CODE_ACCESS_TOKEN_URL
|
.replace("APPID", configData.getWxAppId())
|
.replace("SECRET", configData.getWxSecret()).replace("CODE", code);
|
|
String result = this.doGet(url);
|
if (null == result) {
|
return null;
|
}
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
return jsonObject.getString("openid");
|
}
|
|
}
|