博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微信接口开发 控制层代码
阅读量:7297 次
发布时间:2019-06-30

本文共 1562 字,大约阅读时间需要 5 分钟。

hot3.png

@Controllerpublic class UserController {	String scope1 = "snsapi_base"; //不弹授权页面	String scope2 = "snsapi_userinfo"; //弹出授权页面	String scope = scope1;	@RequestMapping("/login.do")	public String login(HttpServletRequest request){		String path = request.getContextPath();		String basePath = request.getScheme()+"://"+request.getServerName()+path+"/";		String url1 = "https"+"://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=";		url1= url1.replace("APPID", Param.APPID);		String url2 = basePath+"index.do&response_type=code&scope="+scope+"&state=STATE#wechat_redirect";		return "redirect:"+url1+url2;	}	@RequestMapping("/index.do")	public String index(String code,ModelMap model,HttpSession session) {		System.out.println("code:"+code);		try {			if(code==null){				return "/login.do";			}			model.addAttribute("ticket",Param.TICKET);			User user = (User) session.getAttribute("user");			if(user==null){				OAuthInfo auth = WeiXinUtil.getAccess_token(code);				if(scope.equals(scope1)){					user = WeiXinUtil.getUser(Param.TOKEN.getAccess_token(), auth.getOpenid(),false);					if(user.getSubscribe().equals("0")){						scope = scope2;						return "/login.do";					}else{						scope = scope1;					}				}else{					user = WeiXinUtil.getUser(auth.getAccess_token(), auth.getOpenid(),true);				}			}			model.addAttribute("user", user);			scope = scope1;			session.setAttribute("user", user);		} catch (Exception e) {			session.removeAttribute("user");			e.printStackTrace();		}		return "index.jsp";	}	}

转载于:https://my.oschina.net/u/1428688/blog/685924

你可能感兴趣的文章
ubuntu解压命令(转)
查看>>
C#获取获取北京时间多种方法
查看>>
动态语言的灵活性是把双刃剑 -- 以 Python 语言为例
查看>>
1. 字节序的转换
查看>>
ubuntu14.04LTS下ECLIPSE+JAVA搭建
查看>>
学习RxJS: 导入
查看>>
PowerDesigner大小写转换
查看>>
纯 CSS 利用 label + input 实现选项卡
查看>>
Ubuntu 上安装R
查看>>
How .Net code can call unmanaged code?
查看>>
Archlinux安装配置以及遇到的问题
查看>>
怎么写出好代码——坏味道
查看>>
Java将CST的时间字符串转换成需要的日期格式字符串
查看>>
cisco 查看接口进出流量
查看>>
Dhcp+nfs+tftp+ks 完全无人值守安装linux操作系统
查看>>
TemplateDirectiveModel接口execute方法参数说明
查看>>
Js删除数组重复元素的多种方法
查看>>
att与intel风格对照实例
查看>>
backpack
查看>>
【转+分析】JAVA: 为什么要使用"抽象类"? 使用"抽象类"有什么好处?
查看>>