@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 delete(String id) { User user = userDao.get(id); // user.setDeleted(true); userDao.update(user); return user; }
@Override public int addSharePoints(String userId) { int points = (int) (Math.random() * 59) + 1; User user = userDao.load(userId); user.setPoints(user.getPoints() + points); userDao.update(user); return points; }
@Override public void psychologicalTest(String userId, int points) { User user = userDao.load(userId); if (user.getPoints() < points) { throw new ServiceException("当前积分不足,无法进行评测!"); } user.setPoints(user.getPoints() - points); userDao.update(user); }
@Override public User login(String anyAccount, String password) throws ServiceException { QueryCondition qc = userDao.getQC(); qc.eq("mobile", anyAccount); User user = userDao.uniqueByQC(qc); if (null == user) { throw new ServiceException("用户不存在"); } if (!user.getPassword().equals(password)) { throw new ServiceException("密码错误"); } user = loginHandle(user); return user; }
@Override public void passwordReset(String mobile, String password, int captcha) { if (!checkCaptcha(mobile, captcha)) { throw new ServiceException("验证码错误!"); } QueryCondition qc = userDao.getQC(); qc.eq("mobile", mobile); User user = userDao.uniqueByQC(qc); if (null == user) { throw new ServiceException("用户不存在"); } user.setPassword(password); userDao.update(user); }
private User loginHandle(User user) { String token = UUID.randomUUID().toString().replaceAll("-", ""); user.setToken(token); user.setLastLoginTime(new Date()); userDao.update(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); }