com.ge.apm.web.WeChatCoreController.java Source code

Java tutorial

Introduction

Here is the source code for com.ge.apm.web.WeChatCoreController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ge.apm.web;

import com.ge.apm.service.wechat.CoreService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import webapp.framework.context.ExternalLoginHandler;

/**
 *
 * @author 212595360
 */
@Controller
public class WeChatCoreController {

    @Autowired
    protected WxMpConfigStorage configStorage;
    @Autowired
    protected WxMpService wxMpService;
    @Autowired
    protected CoreService coreService;

    @Autowired
    protected ExternalLoginHandler loginHandler;

    /**
     * ?webservice??????
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "core")
    public void wechatCore(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);

        String signature = request.getParameter("signature");
        String nonce = request.getParameter("nonce");
        String timestamp = request.getParameter("timestamp");

        if (!this.wxMpService.checkSignature(timestamp, nonce, signature)) {
            // ?????????
            response.getWriter().println("?");
            return;
        }

        String echoStr = request.getParameter("echostr");
        if (StringUtils.isNotBlank(echoStr)) {
            // ??echostr
            String echoStrOut = String.copyValueOf(echoStr.toCharArray());
            response.getWriter().println(echoStrOut);
            return;
        }
        System.out.println("current token is \n" + wxMpService.getAccessToken());
        String apiTicket = wxMpService.getJsapiTicket();
        System.out.println("current apiTicket is \n" + apiTicket);
        String encryptType = StringUtils.isBlank(request.getParameter("encrypt_type")) ? "raw"
                : request.getParameter("encrypt_type");

        if ("raw".equals(encryptType)) {
            // ?
            WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
            WxMpXmlOutMessage outMessage = this.coreService.route(inMessage);
            response.getWriter().write(outMessage == null ? "" : outMessage.toXml());
            return;
        }

        if ("aes".equals(encryptType)) {
            // aes?
            String msgSignature = request.getParameter("msg_signature");
            WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(request.getInputStream(), this.configStorage,
                    timestamp, nonce, msgSignature);
            WxMpXmlOutMessage outMessage = this.coreService.route(inMessage);
            response.getWriter().write(outMessage == null ? "" : outMessage.toEncryptedXml(this.configStorage));
            return;
        }

        response.getWriter().println("??");
        return;
    }

    /**
     * ??
     * @param request
     * @param response 
     */
    @RequestMapping(value = "authurl")
    public String authUrl() {
        return "userInfo";
    }

    @RequestMapping(value = "binduser")
    public @ResponseBody String bindUser(HttpServletRequest request, HttpServletResponse response, String openId,
            String username, String password) throws WxErrorException {
        return coreService.bindingUserInfo(request, response, openId, username, password) == 0 ? "success"
                : "failed";
    }

}