@Override public User loginFromWechat(String code, String siteId) { AccessToken accessToken = wechatService.getAccessToken(code); if (accessToken.isInvalid()) { throw new ServiceException(accessToken.getErrmsg()); } User user = userDao.getByProperty("wechatUserId", accessToken.getOpenid()); if (null == user) { WechartUserInfo userInfo = wechatService.getUserInfo(accessToken); if (userInfo.isInvalid()) { throw new ServiceException(userInfo.getErrmsg()); } user = new User(); user.setWechatUserId(userInfo.getOpenid()); user.setName(userInfo.getNickname()); if (null != userInfo.getSex()) { if ("1".equals(userInfo.getSex())) { user.setSex(User.Sex.MALE); } if ("2".equals(userInfo.getSex())) { user.setSex(User.Sex.FEMALE); } user = addUser(user, siteId); user.setPoints(5); userDao.update(user); } } user = loginHandle(user); return user; }
@Override public User getCurrentLoginUser(String token) { User user = userDao.getByProperty("token", token); if (null == user) { throw new ServiceException("令牌无效", "900"); } long diffTime = System.currentTimeMillis() - user.getLastLoginTime().getTime(); if (diffTime > 1000 * 60 * 60 * 3) { throw new ServiceException("令牌无效", "901"); } return user; }
@Override public User qqLogin(String accessToken) { User user = null; HttpClient httpClient = new HttpClient(); HttpMethod method = new GetMethod("https://graph.qq.com/oauth2.0/me?access_token=" + accessToken); try { httpClient.executeMethod(method); String json = method.getResponseBodyAsString(); json = json.replace("callback(", "").replace(");", ""); JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class); String openid = jsonObject.get("openid").getAsString(); user = userDao.getByProperty("qqOpenId", openid); if (null != user) { user = loginHandle(user); } } catch (Exception e) { e.printStackTrace(); throw new ServiceException(e.getMessage()); } return user; }
@Override public User getUserByMobile(String mobile) { return userDao.getByProperty("mobile", mobile); }