All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eworkcloud.wechat.WechatTemplate Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
package com.eworkcloud.wechat;

import com.eworkcloud.jackson.JacksonTemplate;
import com.eworkcloud.okhttp.OkHttpTemplate;
import com.eworkcloud.okhttp.model.OkHttpMessage;
import com.eworkcloud.wechat.model.WechatAccessToken;
import com.eworkcloud.wechat.model.WechatResponse;
import com.eworkcloud.wechat.model.WechatUserInfo;
import okhttp3.Response;

import java.io.IOException;

public class WechatTemplate {
    /**
     * 通过code获取access_token
     * ?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
     */
    private static final String access_token = "https://api.weixin.qq.com/sns/oauth2/access_token";
    /**
     * 刷新或续期access_token
     * ?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
     */
    private static final String refresh_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token";
    /**
     * 检验access_token是否有效
     * ?access_token=ACCESS_TOKEN&openid=OPENID
     */
    private static final String auth = "https://api.weixin.qq.com/sns/auth";
    /**
     * 获取用户信息(UnionID机制)
     * ?access_token=ACCESS_TOKEN&openid=OPENID
     */
    private static final String userinfo = "https://api.weixin.qq.com/sns/userinfo";

    private WechatProperties wechatProperties;
    private OkHttpTemplate okHttpTemplate;
    private JacksonTemplate jacksonTemplate;

    public WechatTemplate(WechatProperties wechatProperties, OkHttpTemplate okHttpTemplate, JacksonTemplate jacksonTemplate) {
        this.wechatProperties = wechatProperties;
        this.okHttpTemplate = okHttpTemplate;
        this.jacksonTemplate = jacksonTemplate;
    }

    /**
     * 通过code获取access_token
     *
     * @param code CODE
     * @return 访问令牌
     */
    public WechatAccessToken accessToken(String code) {
        OkHttpMessage message = new OkHttpMessage();
        message.setUrl(access_token);
        message.addQuery("appid", wechatProperties.getAppid());
        message.addQuery("secret", wechatProperties.getSecret());
        message.addQuery("code", code);
        message.addQuery("grant_type", "authorization_code");

        Response response = okHttpTemplate.execute(message);
        if (!response.isSuccessful()) {
            throw new RuntimeException(response.message());
        }

        try {
            String bodyString = response.body().string();
            return jacksonTemplate.deserialize(bodyString, WechatAccessToken.class);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 刷新或续期access_token
     *
     * @param refreshToken 刷新令牌
     * @return 访问令牌
     */
    public WechatAccessToken refreshToken(String refreshToken) {
        OkHttpMessage message = new OkHttpMessage();
        message.setUrl(refresh_token);
        message.addQuery("appid", wechatProperties.getAppid());
        message.addQuery("grant_type", "refresh_token");
        message.addQuery("refresh_token", refreshToken);

        Response response = okHttpTemplate.execute(message);
        if (!response.isSuccessful()) {
            throw new RuntimeException(response.message());
        }

        try {
            String bodyString = response.body().string();
            return jacksonTemplate.deserialize(bodyString, WechatAccessToken.class);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 检验access_token是否有效
     *
     * @param accessToken 访问令牌
     * @param openid      OPENID
     * @return 是否有效
     */
    public WechatResponse validate(String accessToken, String openid) {
        OkHttpMessage message = new OkHttpMessage();
        message.setUrl(auth);
        message.addQuery("access_token", accessToken);
        message.addQuery("openid", openid);

        Response response = okHttpTemplate.execute(message);
        if (!response.isSuccessful()) {
            throw new RuntimeException(response.message());
        }

        try {
            String bodyString = response.body().string();
            return jacksonTemplate.deserialize(bodyString, WechatResponse.class);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取用户信息(UnionID机制)
     *
     * @param accessToken 访问令牌
     * @param openid      OPENID
     * @return 用户信息
     */
    public WechatUserInfo userinfo(String accessToken, String openid) {
        OkHttpMessage message = new OkHttpMessage();
        message.setUrl(userinfo);
        message.addQuery("access_token", accessToken);
        message.addQuery("openid", openid);

        Response response = okHttpTemplate.execute(message);
        if (!response.isSuccessful()) {
            throw new RuntimeException(response.message());
        }

        try {
            String bodyString = response.body().string();
            return jacksonTemplate.deserialize(bodyString, WechatUserInfo.class);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy