sgj
18 小时以前 67b2e6a3b7e4254be92e3ecbcd7b6e9f61b3aae3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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");
    }
 
}