微信官方文档

小程序登录 | 微信开放文档 (qq.com)

直接复制即可使用

根据前端wx.login获取到的code传到后端 

/**
     *新版小程序登录获取openID
     * @param code
     * @return
     */
    @SaIgnore
    @PostMapping("/gotWxAuth")
    public JSONObject gotWxAuth(String code) {

        if (StrUtil.isBlank(code)) {
            return null;
        }
        Map<String, Object> requestUrlParam = new HashMap<String, Object>();
        requestUrlParam.put("appid", WXConfig.appid);    //开发者设置中的appId
        requestUrlParam.put("secret", WXConfig.secret);    //开发者设置中的appSecret
        requestUrlParam.put("js_code", code);    //小程序调用wx.login返回的code
        requestUrlParam.put("grant_type", "authorization_code");//默认参数
        String resultWx = HttpRequest.post(WX_URL)
            .form(requestUrlParam)//表单内容
            .timeout(20000)//超时,毫秒
            .execute().body();
        return JSONUtil.parseObj(resultWx);
    }
/**
 * 微信小程序相关配置
 * @Description: TODO
 * @Author Leo
 * @Date 2022/12/12
 * @Version V1.0
 **/
public class WXConfig {
    public static final String appid = "wxf806*********0bd";
    public static final String secret = "722b**********************7bd6b";
}
//微信官方获取微信用户唯一标识的openID的接口
private final String WX_URL = "https://api.weixin.qq.com/sns/jscode2session";

前端调完接口返回的结果

apifox接口调用测试

 

 

 

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐