All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.mp.action.AtoWechatMpAction Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.wechat.mp.action;

import com.zcj.util.UtilString;
import com.zcj.util.UtilUri;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.exception.WechatException;
import com.zopen.wechat.mp.dto.oauth2.UserInfo;
import com.zopen.wechat.mp.dto.oauth2.WebAccessToken;
import com.zopen.wechat.mp.service.Oauth2Interface;
import com.zopen.wechat.mp.service.WechatMpOauth2Service;
import com.zopen.wechat.mp.task.WechatInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/ato/wechat/mp")
public class AtoWechatMpAction {

    private static final Logger logger = LoggerFactory.getLogger(AtoWechatMpAction.class);

    @Autowired
    private WechatMpProperties wechatProperties;
    @Autowired
    private WechatMpOauth2Service wechatOauth2Service;

    @Autowired(required = false)
    private Oauth2Interface oauth2Interface;

    // 微信公众号网页授权登录
    @RequestMapping("/login")
    public String login(HttpServletRequest request, String appId, String page, String param1, String param2) {
        if (UtilString.isBlank(appId)) {
            appId = wechatProperties.getAppId();
            logger.debug("授权登录请求中的 appId 参数为空,将使用 zopen.wechat.mp.app_id 配置的 appId [{}]", appId);
        }
        String redirectUri = wechatProperties.getDomainUrl() + "/ato/wechat/mp/callback";
        redirectUri = UtilUri.urlAddParam(redirectUri, "appId", appId);
        redirectUri = UtilUri.urlAddParam(redirectUri, "page", UtilUri.urlEncode(page));
        redirectUri = UtilUri.urlAddParam(redirectUri, "param1", UtilUri.urlEncode(param1));
        redirectUri = UtilUri.urlAddParam(redirectUri, "param2", UtilUri.urlEncode(param2));
        String callbackUrl = wechatOauth2Service.getAuthorizeUrl(UtilUri.urlEncode(redirectUri), oauth2Interface.setState(request, page, appId), appId);
        logger.debug("微信菜单进去后的跳转地址:[{}]", callbackUrl);
        return "redirect:" + callbackUrl;
    }

    @RequestMapping("/callback")
    public String callback(HttpServletRequest request, String appId, String page, String param1, String param2) {
        String appSecret = null;
        if (UtilString.isBlank(appId)) {
            appId = wechatProperties.getAppId();
            appSecret = wechatProperties.getAppSecret();
            logger.debug("请求中的 appId 参数为空,使用 zopen.wechat.mp.app_id 配置的 appId [{}]", appId);
        } else {
            appSecret = WechatInfo.getAppSecret(appId);
            logger.debug("请求中的 appId [{}],获取 secret [{}]", appId, appSecret);
        }

        String code = request.getParameter("code");
        String state = request.getParameter("state");
        oauth2Interface.validState(request, page, appId, state);

        // 获取 open_id 后就跳转
        WebAccessToken webAccessToken = wechatOauth2Service.getWebAccessToken(code, appId, appSecret);
        String redirectPath = oauth2Interface.redirect(webAccessToken.getOpenid(), appId, page, param1, param2);
        if (UtilString.isNotBlank(redirectPath)) {
            logger.debug("微信菜单回调后的请求地址:[{}]", redirectPath);
            return "redirect:" + redirectPath;
        }

        // 获取用户信息后再跳转
        UserInfo userInfo = wechatOauth2Service.getUserInfo(webAccessToken.getAccess_token(), webAccessToken.getOpenid());
        redirectPath = oauth2Interface.redirect(userInfo, appId, page, param1, param2);
        if (UtilString.isNotBlank(redirectPath)) {
            logger.debug("微信菜单回调后的请求地址:[{}]", redirectPath);
            return "redirect:" + redirectPath;
        }

        throw new WechatException("未配置 redirect 地址,跳转失败");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy