要做微信支付的第一步就是在微信获取用户信息(微信登录)
先讲讲怎么做微信登录
1.配置好微信公众号的微信的appid和appsecert
2.配置微信的ip白名单
3.配置微信公众号的网页授权的域名
配置好这些可以获取用户信息了。我贴出代码
public class WxLoginController {
//AppId
public static final String WX_APP_ID = "xxxxxxxxxx";
//AppSecret
public static final String WX_APP_KEY = "xxxxxxxxx";
@Autowired
private SystemService systemService;
@Autowired
private BywxshareService bywxshareService;
/**
* 微信登录
* @param req
* @param resp
* @return
* @throws IOException
*/
@RequestMapping(value ="wxlogin")
@ResponseBody
public Map<String, Object> getOpenid(HttpServletRequest req,HttpServletResponse resp) throws IOException{
SetRespHander.setHander(resp);
HashMap<String, Object> m=new HashMap<String, Object>();
if(req.getParameter("code")==null){
m.put("status", 401);
m.put("message","参数为空");
return m;
}
String code=req.getParameter("code");
//发送请求获取openid
BufferedReader in = null;
URL tirc = new URL("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WX_APP_ID+"&secret="+WX_APP_KEY+"&code="+code+"&grant_type=authorization_code");
in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());
}
String str = sb.toString();
JSONObject parseObject = JSON.parseObject(str);
System.out.println(str);
String openid=parseObject.getString("openid");
String access_token=parseObject.getString("access_token");
if(openid==null){
m.put("status", 401);
m.put("message", "未关注公众号,请关注");
return m;
}
//判断openid数据库是否存在
User usersBywxopenid = systemService.getUsersBywxopenid(openid);
if(usersBywxopenid==null){
//获取微信用户的详细信息
String[] S = GetNicknameAndHeadimgurl(openid);
//创建一个新用户
List<Role> roleList = Lists.newArrayList();
Role role = new Role();
role=systemService.findRoleIdByEnName("vip");
roleList.add(role);
User user=new User();
user.setRoleList(roleList);
user.setCreateDate(new Date());
user.setId(CreateIdUtil.Createid());
user.setName(S[0]);
user.setPhoto(S[1]);
user.setWecatopenid(openid);
systemService.insertUserRole(user);
systemService.insertUser(user);
req.getSession().setAttribute(Constants.FRONT_USER_SESSION, user);
m.put("status", 200);
return m;
}else{
//更新用户的昵称和头像
String[] S = GetNicknameAndHeadimgurl(openid);
System.out.println(S[0]+S[1]);
usersBywxopenid.setName(S[0]);
usersBywxopenid.setPhoto(S[1]);
systemService.updateUserInfo(usersBywxopenid);
req.getSession().setAttribute(Constants.FRONT_USER_SESSION, usersBywxopenid);
m.put("status", 200);
return m;
}
}
/**
* 判断用户登录没有
*/
@RequestMapping(value="isUserlogin")
@ResponseBody
public Map<String, Object> isUserlogin(HttpServletRequest req,HttpServletResponse resp){
SetRespHander.setHander(resp);
HashMap<String, Object> m=new HashMap<String, Object>();
if(req.getSession().getAttribute(Constants.FRONT_USER_SESSION)==null){
m.put("status", 403);//获取失败
m.put("message","未登录");
return m;
}
m.put("status", 200);
return m;
}
/**
* 获取用户信息
* @param req
* @param resp
* @return
*/
@RequestMapping(value="getuserinfo")
@ResponseBody
public Map<String, Object> getuserinfo(HttpServletRequest req,HttpServletResponse resp){
SetRespHander.setHander(resp);
HashMap<String, Object> m=new HashMap<String, Object>();
if(req.getSession().getAttribute(Constants.FRONT_USER_SESSION)==null){
m.put("status", 403);//获取失败
m.put("message","未登录");
return m;
}
User currentUser = (User) req.getSession().getAttribute(Constants.FRONT_USER_SESSION);
m.put("status", 200);
m.put("data", currentUser);
return m;
}
/**
* 获取access_token和jsapi_ticket
* @param in
* @param ticket
* @return
* @throws IOException
*/
public static String[] getTticket( BufferedReader in,String ticket) throws IOException{
//获取token
URL tirc = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxxxxx&secret=xxxxxxx");
in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while((res = in.readLine())!=null){
sb.append(res.trim());
}
String str = sb.toString();
JSONObject parseObject = JSON.parseObject(str);
String token=parseObject.getString("access_token");
//获取ticket
URL tir = new URL("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi");
in = new BufferedReader(new InputStreamReader(tir.openStream(),"UTF-8"));
String re;
StringBuilder s = new StringBuilder("");
while((re = in.readLine())!=null){
s.append(re.trim());
}
String st = s.toString();
parseObject= JSON.parseObject(st);
ticket=parseObject.getString("ticket");
String cache[]={token,ticket};
return cache;
}
}
微信获取access_token是有时效的,有效期是两个小时。可以通过缓存实现刷新它,这里我是将获取的数据通过数据库存放。
并在它过期的时候刷新它的。
贴出数据库的表
做好微信登录了。
下面介绍微信支付