第七章 企业级微信点餐项目(微信域名回调)

微信域名回调

标签(空格分隔): springboot java wechat


普通方法

  • 设置域名(内网穿透工具natapp)

需要在微信中设置回调域名

  • 获取code

经过发送 微信服务器,微信重定向到本地。

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

APPID = 对接公众号账户id
REDIRECT_URI = 回调域名地址
SCOPE = 获取用户信息方式两种(snsapi_base、snsapi_userinfo)
  • 换取access_token

通过重定向获取code,重新发送请求获取用户信息。

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code 
APPID = 对接公众号账户id
SECRET = 对接公众号账户secret
CODE = 重定向获取的id

/**
* 获取code测试
* @param code
*/
@GetMapping("/auth")
public void auth(@RequestParam("code") String code){
    log.info("code={}",code);

    String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code="+code+"&grant_type=authorization_code";
    RestTemplate restTemplate = new RestTemplate();
    String response = restTemplate.getForObject(url,String.class);
    log.info("response={}",response);
}

使用SDK方法

  • 引用

    com.github.binarywang
    weixin-java-mp
    2.8.0

  • 获取code
//引用配置参数
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
    /** 账户 .*/
    private String wechat_appid;
    /** 密码 .*/
    private String wechat_secret;
}

//参数设置
@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(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getWechat_appid());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getWechat_secret());
        return wxMpConfigStorage;
    }
}

//获取code
/**
* 获取code
* @param returnUrl
* @return
*/
@GetMapping("/authrize")
public String authrize(@RequestParam("returnUrl") String returnUrl){
    String url = "http://www.xiaojinzi.top/sell/wechat/userInfo";
    String rediectUrl = wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
    return "rediectUrl:" + rediectUrl;
}

  • 获取openid
/**
* 微信openid
* @param code
* @param returnUrl
* @return
*/
@GetMapping("/userInfo")
public String userinfo(@RequestParam("code") String code,
    @RequestParam("state") String returnUrl){
    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
    try {
        wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
    }catch (WxErrorException e){
        log.error("【微信网页授权】 e={}",e);
        throw new SellException(ResultEnum.WECHAT_WEB_ERROR.getCode(),e.getError().getErrorMsg());
    }
    String openid = wxMpOAuth2AccessToken.getOpenId();

    return "rediectUrl:"+ returnUrl + "?openid="+openid;
}

前端调试

  • 设置前端参数
cd /opt/code/sell_fe_buyer/config/
vim index.js
npm run build
  • 抓包使用fiddle 或者 anyproxy 操作请查看githubwiki

  • 原视频UP主慕课网(SpringBoot企业级微信点餐项目)
  • 本篇博客撰写人: XiaoJinZi 转载请注明出处
  • 学生能力有限 附上邮箱: [email protected] 不足以及误处请大佬指责

你可能感兴趣的:(第七章 企业级微信点餐项目(微信域名回调))