SpringCloud : 接入 微信公眾號平台(四)、獲取微信用戶信息接口


代碼參考:

import com.phpdragon.wechat.proxy.config.WeChatConfig;
import com.phpdragon.wechat.proxy.dto.mp.user.GetOauthUserInfoDto;
import com.phpdragon.wechat.proxy.dto.mp.user.GetOpenidDto;
import com.phpdragon.wechat.proxy.dto.mp.user.GetUserInfoDto;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RequestMapping("/user/")
@RestController
public class UserController {

    @Autowired
    private WeChatConfig weChatConfig;

    /**
     * 通過openid獲得基本用戶信息
     * 詳情請見: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#3
     *
     * @param req
     * @return
     */
    @ResponseBody
    @PostMapping("/getUserInfo")
    public WxMpUser getUserInfo(@RequestBody @Valid GetUserInfoDto req) throws WxErrorException {
        WxMpService wxMpService = weChatConfig.getWxMpService(req.getAppId());
        return wxMpService.getUserService().userInfo(req.getOpenid(), req.getLang());
    }

    /**
     * 通過code獲得基本用戶信息
     * 詳情請見:
     * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#1
     * https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#3
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("/getOAuth2UserInfo")
    public WxMpUser getOAuth2UserInfo(@RequestBody @Valid GetOauthUserInfoDto req) throws WxErrorException {
        WxMpService wxMpService = weChatConfig.getWxMpService(req.getAppId());
        WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(req.getCode());
        return wxMpService.getUserService().userInfo(accessToken.getOpenId(), req.getLang());
    }

    /**
     * 用code換取oauth2的openid
     * 詳情請見: https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#1
     *
     * @param req
     */
    @ResponseBody
    @PostMapping("/getOpenid")
    public String getOpenid(@RequestBody @Valid GetOpenidDto req) throws WxErrorException {
        WxMpService wxMpService = weChatConfig.getWxMpService(req.getAppId());
        WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(req.getCode());
        return accessToken.getOpenId();
    }

}

 

 

PS:

公眾號開發文檔wiki

Java開發微信公眾號之整合weixin-java-tools框架開發微信公眾號

從零實現 Spring Boot 2.0 整合 weixin-java-mp(weixin-java-tools) 獲取 openId,用於微信授權

 

Demo 列表

  1. 微信支付 Demo:GitHub碼雲
  2. 企業號/企業微信 Demo:GitHub碼雲
  3. 微信小程序 Demo:GitHub碼雲
  4. 開放平台 Demo:GitHub碼雲
  5. 公眾號 Demo:
    • 使用 Spring MVC 實現的公眾號 Demo:GitHub碼雲
    • 使用 Spring Boot 實現的公眾號 Demo(支持多公眾號):GitHub碼雲
    • 含公眾號和部分微信支付代碼的 Demo:GitHub碼雲

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM