WechatUtil.java 2.07 KB
package com.meishu.util;

import com.alibaba.fastjson.JSONObject;
import com.meishu.common.constant.WxConstant;
import com.meishu.vo.wechat.AccessTokenVO;
import com.meishu.vo.wechat.MemberVO;
import com.meishu.vo.wechat.QrConnectVO;
import com.meishu.vo.wechat.WXUserInfoVO;

import java.util.HashMap;
import java.util.Map;

public class WechatUtil {

    /**
     * Access Token
     * @return
     */
    public static AccessTokenVO getAccessToken() {
        Map<String, Object> param = new HashMap<>();
        param.put("appid", WxConstant.APPID);
        param.put("secret", WxConstant.SECRENT);
        JSONObject object = HttpUtil.get(param, WxConstant.GET_SUITE_TOKEN);
        return JSONObject.toJavaObject(object, AccessTokenVO.class);
    }

    /**
     * 获取访问用户身份
     * @param accessToken
     * @param code
     * @return
     */
    public static WXUserInfoVO getUserInfo(String accessToken, String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("access_token", accessToken);
        param.put("code", code);
        JSONObject object = HttpUtil.get(param, WxConstant.GETUSERINFO);
        return JSONObject.toJavaObject(object, WXUserInfoVO.class);
    }

    /**
     * 读取成员
     * @param accessToken
     * @param userId
     * @return
     */
    public static MemberVO getMember(String accessToken, String userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("access_token", accessToken);
        param.put("userid", userId);
        JSONObject object = HttpUtil.get(param, WxConstant.GET);
        return JSONObject.toJavaObject(object, MemberVO.class);
    }

    /**
     * 扫码登陆参数
     * @return
     */
    public static QrConnectVO getQRConnectParams() {
        QrConnectVO qrConnectVO = new QrConnectVO();
        qrConnectVO.setId("双创系统");
        qrConnectVO.setAppId(WxConstant.APPID);
//        qrConnectVO.setAgentId(WxConstant.AGENTID);
        qrConnectVO.setRedirectUri(WxConstant.QR_REDIRECT_URL);
        qrConnectVO.setState("MASS");
        return qrConnectVO;
    }
}