springboot第二章---打造企业级微信点餐系统(4)--微信授权获取openID

本篇文章会涉及到微信的特性 :微信授权,微信支付 ,微信退款

这个网站已经将微信的各种功能写的很清楚了:

https://github.com/Wechat-Group/WxJava

接口文档: 

springboot第二章---打造企业级微信点餐系统(4)--微信授权获取openID_第1张图片

一、微信授权

 获取openID ,有两种方式:最好完整的多看几遍微信文档

1. 手工方式

2. 利用第三方SDK(推荐使用),介绍SDK使用

springboot第二章---打造企业级微信点餐系统(4)--微信授权获取openID_第2张图片

编写代码

1. 添加依赖:pom.xml


    
      com.github.binarywang
      weixin-java-mp
      3.7.0
    

2. WeChatController.java

package com.fjz.vxsell.controller;

import com.fjz.vxsell.enums.ExceptionEnums;
import com.fjz.vxsell.exception.VxsellException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
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 java.net.URLEncoder;

/**
 * 微信操作
 * @author 冯师兄
 * @date 2020-05-15 16:23
 */
//因为要重定向,所以不能使用RestController
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WeChatController {
    @Autowired
    private WxMpService wxMpService;

    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        WxMpService mpService = new WxMpServiceImpl();
        //配置(已完成WeChatMpConfig,WeChatAccountConfig及application.yml)
        //调用方法
        String url = "http://e724bad2.ngrok.io/vxsell/wechat/userInfo";//实际项目中,网站根目录应该做一个全局配置,此处为了方便,直接写
        String redirectUri = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_BASE,
                URLEncoder.encode(returnUrl));
        log.info("redirectUri={}", redirectUri);//
        return "redirect:" + redirectUri;

    }

    /**
     * 获得用户信息
     * @param code
     * @param returnUrl
     * @return
     */
    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,
                           @RequestParam("state") String returnUrl) {
        //获得access token
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        }catch (WxErrorException e){
            log.error("【微信网页授权】{}",e);
            throw new VxsellException(ExceptionEnums.WECHAT_MP_ERROR);//授权失败抛出异常
        }
        //获取openId
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("openId={}",openId);
        return "redirect:" + returnUrl+"?openid="+openId;
    }

}

3. 添加全局配置

springboot第二章---打造企业级微信点餐系统(4)--微信授权获取openID_第3张图片

WeChatMpConfig.java

package com.fjz.vxsell.config;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author 冯师兄
 * @date 2020-05-15 17:01
 */
@Component
public class WeChatMpConfig {

    @Autowired
    private WeChatAccountConfig weChatAccountConfig;

    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpDefaultConfigImpl wxMpConfigStorage = new WxMpDefaultConfigImpl();
        wxMpConfigStorage.setAppId(weChatAccountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(weChatAccountConfig.getMpAppSecret());
        return wxMpConfigStorage;
    }
}

 WeChatAccountConfig.java

package com.fjz.vxsell.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author 冯师兄
 * @date 2020-05-15 17:42
 */
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WeChatAccountConfig {
    private String mpAppId;

    private String mpAppSecret;
}

application.yml

ExceptionEnums.java

springboot第二章---打造企业级微信点餐系统(4)--微信授权获取openID_第4张图片

 

 

你可能感兴趣的:(springboot,openID,微信授权)