package com.kym.miniapp.utils;

import com.alibaba.fastjson2.JSONObject;
import com.kym.common.constant.ResponseEnum;
import com.kym.common.exception.BusinessException;
import com.kym.miniapp.enums.WxApi;
import lombok.SneakyThrows;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author skyline
 * @description
 * @date 2023-07-23 16:26
 */
public class WxUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(WxUtil.class);

/*
    @SneakyThrows
    String WxLogin(String code) {
        // 微信登录
        var appid = "";
        var secret = "";
        var js_code = "";
        var grant_type = "";

        OkHttpClient client = new OkHttpClient();
        HttpUrl url = HttpUrl.parse(WxApi.WX_MP_LOGIN.getApi()).newBuilder()
                .addQueryParameter("appid", appid)
                .addQueryParameter("secret", secret)
                .addQueryParameter("js_code", js_code)
                .addQueryParameter("grant_type", grant_type)
                .build();

        Request request = new Request.Builder()
                .url(url)
                .build();

        var response = client.newCall(request).execute().body().string();
        JSONObject json = JSONObject.parseObject(response);
        var errorCode = json.getIntValue("errcode");
        if (200 == errorCode) {
            // 业务逻辑处理
            var openid = json.getString("openid");
            var session_key = json.getString("session_key");
            var unionid = json.getString("unionid");
        } else {
            // 抛出异常
            LOGGER.error("微信登录异常,错误码{}，异常信息{}", errorCode, json.getString("errMsg"));
            throw new BusinessException(ResponseEnum.WX_MP_LOGIN_ERROR);
        }

        return null;
    }
*/


}



