1. 程式人生 > 實用技巧 >使用sdk獲取微信的使用者資訊

使用sdk獲取微信的使用者資訊

一、pom.xml 依賴

        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>weixin-java-mp</artifactId>
            <version>2.7.0</version>
        </dependency>

二、application.yml配置

wechat:
  mpAppId: wx6beb2249b6c*****(appID)
  mpAppSecret: 9138282d930eb7ee7f7a59*******(appsecret)

三、從application.yml中獲取配置資訊

@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

    /**
     * 公眾平臺id
     */
    private String mpAppId;

    /**
     * 公眾平臺金鑰
     */
    private String mpAppSecret;
}

四、配置微信引數資訊

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component;
@Component public class WechatMpConfig { @Autowired private WechatAccountConfig accountConfig; @Bean public WxMpService wxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); return wxMpService; } @Bean public WxMpConfigStorage wxMpConfigStorage() { WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); wxMpConfigStorage.setAppId(accountConfig.getMpAppId()); wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret()); return wxMpConfigStorage; } }

五、微信授權介面


import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import com.liuhq.config.ProjectUrlConfig;
import com.liuhq.enums.ResultEnum;
import com.liuhq.exception.SellException;


import java.net.URLEncoder;


@Controller @RequestMapping(
"/wechat") @Slf4j public class WechatController { @Autowired private WxMpService wxMpService; @Autowired private ProjectUrlConfig projectUrlConfig; @GetMapping("/authorize") public String authorize(@RequestParam("returnUrl") String returnUrl) { //1. 配置 //2. 呼叫方法 String url = http://域名 + "/wechat/userInfo"; String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl)); return "redirect:" + redirectUrl; } @GetMapping("/userInfo") public String userInfo(@RequestParam("code") String code, @RequestParam("state") String returnUrl) { WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); try { wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null); //微信使用者資訊 }
catch (WxErrorException e) { log.error("【微信網頁授權】{}", e); throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); } String openId = wxMpOAuth2AccessToken.getOpenId(); //獲取openId return "redirect:" + returnUrl + "?openid=" + openId; } }