package com.example.demo.module.wxapi;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.HttpClientUtils;
import org.springframework.stereotype.Service;

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

@Service
public class WxApiService {

    public JSONObject requestAccessToken(String loginCode) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("appid", WxConst.appID);
        paramMap.put("secret", WxConst.appSecret);
        paramMap.put("code", loginCode);
        paramMap.put("grant_type", "authorization_code");
        String res = HttpClientUtils.get("https://api.weixin.qq.com/sns/oauth2/access_token", paramMap);
        return JSONObject.parseObject(res);
    }

    public JSONObject requestUserInfo(String accessToken, String openid) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("access_token", accessToken);
        paramMap.put("openid", openid);
        String res = HttpClientUtils.get("https://api.weixin.qq.com/sns/userinfo", paramMap);
        return JSONObject.parseObject(res);
    }
}
