微信点餐系统

请求流程

项目的从求流程:

(1)、首先,当在手机端点sell.com时,项目前台页面会检查页面cookie中是否有openid,若没有进入第(2)步;
(2)、前台页面会根据index.js中配置的openidUrl请求地址进去请求认认证获取openid:
build: {
            env: require('./prod.env'),
            index: path.resolve(__dirname, '../dist/index.html'),
            assetsRoot: path.resolve(__dirname, '../dist'),
            assetsSubDirectory: 'static',
            assetsPublicPath: '/',
            productionSourceMap: false,
            // Gzip off by default as many popular static hosts such as
            // Surge or Netlify already gzip all static assets for you.
            // Before setting to `true`, make sure to:
            // npm install --save-dev compression-webpack-plugin
            productionGzip: false,
            productionGzipExtensions: ['js', 'css'],
            port: 9000,
            sellUrl: 'http://sell.com',
            openidUrl: 'http://wxorder.natapp1.cc/sell/wechat/authorize',
            wechatPayUrl: 'http://wxorder.natapp1.cc/sell/pay/create'
    },


请求认证的过程如下:
先通过openUrl,即http://wxorder.natapp1.cc/sell/wechat/authorize?returnUrl=http://sell.com/#/,然后请求根据地址/sell/wechat/authorize,到达controller中:
/**
 * 在通过调用http://wxorder.natapp1.cc/sell/wechat/authorize?returnUrl=http://www.imooc.com 时,会传一个returnUrl,用来重定向的网页地址
 * 通过将returnUrl、url(重定向到方法userInfo,获取opendi)传入方法oauth2buildAuthorizationUrl,该方法返回值为:
 * https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0807f8636ccc9325&redirect_uri=
 * http%3A%2F%2Fwxorder.natapp1.cc%2Fsell%2Fwechat%2FuserInfo&
 * response_type=code&scope=snsapi_userinfo&state=http%3A%2F%2Fwww.imooc.com#wechat_redirect
 * 然后重定向到userInfo,并传入参数code与state(其实就是传入的returnUrl),从而获取到了code参数
 * @param returnUrl
 * @return
 */
@RequestMapping("/authorize")
public String authorize(@RequestParam("returnUrl")String returnUrl) {

    //1、配置:基于类的配置

    //2、调用方法:该方法返回重定向的url,即url,并且带上了参数code与state
    String url = "http://wxorder.natapp1.cc/sell/wechat/userInfo";
    String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
    log.info("【微信网页授权】获取code,result={}",redirectUrl);
    return "redirect:"+redirectUrl;
}

/**
 * code参数传入方法oauth2getAccessToken 获取access_token,返回对象WxMpOAuth2AccessToken,该对象中封装了各种信息
 * @param code
 * @param returnUrl
 * @return
 */
@RequestMapping("/userInfo")
public String userInfo(@RequestParam("code") String code,
                     @RequestParam("state")String returnUrl){
    WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
    try {
        wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
    } catch (WxErrorException e) {
        e.printStackTrace();
        log.error("【微信网页授权】{}",e);
        throw new SellException(ResultEums.WECHAT_ERROR.getCode(),e.getError().getErrorMsg());
    }
    String openid = wxMpOAuth2AccessToken.getOpenId();
    return "redirect:"+returnUrl+"?openid="+openid;


}

请求到达后,根据方法 wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));传入回调地址returnUrl和重定向地址url

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx0807f8636ccc9325&redirect_uri=
  http%3A%2F%2Fwxorder.natapp1.cc%2Fsell%2Fwechat%2FuserInfo&
 response_type=code&scope=snsapi_userinfo&state=http%3A%2F%2Fwww.imooc.com#wechat_redirect
  然后重定向到userInfo,并传入参数code与state(其实就是传入的returnUrl),从而获取到了code参数。在userinfo方法中根据code获取到openid,
  再根据return "redirect:"+returnUrl+"?openid="+openid;此时的redirect=http://sell/#/?openid=***********,返回前端,从此将openid写入cookie。
(3)、接下来就是用于请求支付的过程了----->sell/buyer/order/create创建订单----->sell/pay/create  发起支付
文章目录