WechatAccessController.java 6.18 KB
package cn.com.glsx.dj.access.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.glsx.dj.access.service.WechatAccessService;
import cn.com.glsx.framework.core.beans.ResultEntity;

/**
 * 微信接入
 * <P>Description: </P>
 *
 * @author Alvin
 * @version 1.0
 */
@Controller
@RequestMapping("wechat")
public class WechatAccessController {

	@Autowired
	private WechatAccessService wechatAccessService;
	
	/**
	 * 授权事件处理
	 * @param request
	 * @return
	 * @author Alvin
	 * @date 2019年5月5日 下午9:02:04
	 */
	@PostMapping("access")
	@ResponseBody
	public String access(HttpServletRequest request){
		wechatAccessService.access(request);
		return "success";
	}
	
	/**
	 * 消息与事件处理
	 * @param request
	 * @param bizAppId
	 * @return
	 * @author Alvin
	 * @date 2019年5月5日 下午9:02:19
	 */
	@PostMapping("{appid}/event")
	@ResponseBody
	public String event(HttpServletRequest request, @PathVariable("appid") String bizAppId){
		return wechatAccessService.access(request, bizAppId);
	}
	
	/**
	 * 微信服务认证
	 * @param request
	 * @param bizAppId
	 * @return
	 * @author Alvin
	 * @date 2019年5月5日 下午9:02:19
	 */
	@GetMapping("{appid}/event")
	@ResponseBody
	public String auth(HttpServletRequest request, @PathVariable("appid") String bizAppId){
		return wechatAccessService.auth(request, bizAppId);
	}
	
	/**
	 * 跳转到授权页面
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:07:06
	 */
	@GetMapping("auth/pc")
	public String authPC(){
		return "auth";
	}
	
	/**
	 * PC端授权回调
	 * @param authCode
	 * @param authType 要授权的帐号类型, 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示
	 * @param merchantId
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:07:25
	 */
	@GetMapping("auth_{authType}_pc/callback/{merchantId}")
	public String pcAuthCallback(@RequestParam("auth_code")String authCode
			, @PathVariable(required = true, name = "authType")Integer authType
			, @PathVariable(required = true, name = "merchantId")Integer merchantId
			, Model model){
		ResultEntity<Boolean> result = wechatAccessService.authCallback(merchantId, authType, authCode);
		if(ResultEntity.ERROR.equals(result.getReturnCode())){
			model.addAttribute("msg", result.getMessage());
		}
		return "callback/pc";
	}
	
	/**
	 * 移动端授权回调
	 * @param authCode
	 * @param authType 要授权的帐号类型, 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示
	 * @param merchantId
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:08:12
	 */
	@GetMapping("auth_{authType}_h5/callback/{merchantId}")
	public String h5AuthCallback(@RequestParam("auth_code")String authCode
			, @PathVariable(required = true, name = "authType")Integer authType
			, @PathVariable(required = true, name = "merchantId") Integer merchantId){
		wechatAccessService.authCallback(merchantId, authType, authCode);
		return "callback/h5";
	}
	
	/**
	 * 快速注册小程序回调
	 * @param ticket
	 * @param merchantId
	 * @param authorizerAppId
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:08:24
	 */
	@GetMapping("fastregister/callback/{merchantId}/{authorizerAppId}")
	public String fastRegisterCallback(@RequestParam("ticket")String ticket
			, @PathVariable(required = true, name = "merchantId") Integer merchantId
			, @PathVariable(required = true, name = "authorizerAppId") String authorizerAppId
			, Model model){
		ResultEntity<Boolean> result = wechatAccessService.fastRegisterCallback(merchantId, authorizerAppId, ticket);
		if(ResultEntity.ERROR.equals(result.getReturnCode())){
			model.addAttribute("msg", result.getMessage());
		}
		return "callback/fastregister";
	}
	
	/**
	 * 获取移动端授权链接
	 * @param merchantId
	 * @param authType 要授权的帐号类型, 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:08:43
	 */
	@GetMapping("auth/h5/{merchantId}_{authType}")
	@ResponseBody
	public ResultEntity<String> authH5(@PathVariable(required = true, name = "merchantId") Integer merchantId
			, @PathVariable(required = true, name = "authType") Integer authType){
		return wechatAccessService.getH5AuthPath(merchantId, authType);
	}
	
	/**
	 * PC端授权跳转
	 * @param merchantId
	 * @param authType 要授权的帐号类型, 1则商户扫码后,手机端仅展示公众号、2表示仅展示小程序,3表示公众号和小程序都展示
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:09:01
	 */
	@GetMapping("wxauth/agent/{merchantId}_{authType}")
	public String authAgent(@PathVariable(required = true, name = "merchantId") Integer merchantId
			, @PathVariable(required = true, name = "authType") Integer authType){
		return "redirect:".concat(wechatAccessService.getPCAuthPath(merchantId, authType));
	}
	
	/**
	 * 快速注册小程序授权跳转
	 * @param merchantId
	 * @param authorizerAppId
	 * @return
	 * @author Alvin
	 * @date 2019年5月6日 下午2:09:42
	 */
	@GetMapping("fastregister/auth/agent/{merchantId}/{authorizerAppId}")
	public String fastRegisterAuthAgent(@PathVariable(required = true, name = "merchantId") Integer merchantId
			, @PathVariable(required = true, name = "authorizerAppId") String authorizerAppId){
		return "redirect:".concat(wechatAccessService.getFastRegisterAuthPath(merchantId, authorizerAppId).getData());
	}
	
	/**
	 * 跳转到升级提示页面
	 * @return
	 * @author Alvin
	 * @date 2019年6月6日 上午10:24:44
	 */
	@GetMapping("upgrade")
	public String upgrade(){
		return "upgrade";
	}
}