| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068 |
- package com.uas.sso.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.uas.sso.common.encrypt.MD5;
- import com.uas.sso.common.util.HttpUtil;
- import com.uas.sso.core.Const;
- import com.uas.sso.core.Status;
- import com.uas.sso.core.Type;
- import com.uas.sso.dao.UserDao;
- import com.uas.sso.dao.UserRecordDao;
- import com.uas.sso.entity.*;
- import com.uas.sso.exception.VisibleError;
- import com.uas.sso.foreign.entity.ForeignInfo;
- import com.uas.sso.foreign.bihe.entity.BiHeInfo;
- import com.uas.sso.foreign.weixin.entity.OAuthInfo;
- import com.uas.sso.i.CountCallBack;
- import com.uas.sso.logging.LoggerManager;
- import com.uas.sso.logging.SyncBufferedLogger;
- import com.uas.sso.logging.UserBufferedLogger;
- import com.uas.sso.service.*;
- import com.uas.sso.support.SyncFail;
- import com.uas.sso.util.AccountTypeUtils;
- import com.uas.sso.util.CountUtils;
- import com.uas.sso.util.PasswordLevelUtils;
- import net.sf.ehcache.search.aggregator.Count;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.jpa.domain.Specification;
- import org.springframework.stereotype.Service;
- import org.springframework.ui.ModelMap;
- import org.springframework.util.Assert;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.StringUtils;
- import javax.persistence.Version;
- import javax.persistence.criteria.*;
- import javax.validation.constraints.NotNull;
- import java.sql.Timestamp;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.ExecutorService;
- /**
- * 用户service实现类
- *
- * @author wangmh
- * @date 2018/1/2
- */
- @Service
- public class UserServiceImpl implements UserService {
- @Autowired
- private UserDao userDao;
- @Autowired
- private UserRecordDao userRecordDao;
- @Autowired
- private UserValidService userValidService;
- @Autowired
- private UserspaceService userspaceService;
- @Autowired
- private AppService appService;
- @Autowired
- private ExecutorService executorService;
- @Autowired
- private UserQuestionService userQuestionService;
- @Autowired
- private TokenService tokenService;
- private UserBufferedLogger userLogger = LoggerManager.getLogger(UserBufferedLogger.class);
- private SyncBufferedLogger syncLogger = LoggerManager.getLogger(SyncBufferedLogger.class);
- @Override
- public User findByMobile(String mobile, String mobileArea) {
- return userDao.findByMobileAndMobileArea(mobile, mobileArea);
- }
- @Override
- public User findByMobile(String mobile) {
- return userDao.findByMobile(mobile);
- }
- @Override
- public boolean mobileHasRegistered(String mobile) {
- User user = userDao.findByMobile(mobile);
- if (user == null) {
- return false;
- }
- return true;
- }
- @Override
- public boolean emailHasRegistered(String email) {
- List<User> users = userDao.findByEmail(email);
- if (CollectionUtils.isEmpty(users)) {
- return false;
- }
- return true;
- }
- @Override
- public synchronized User register(User user, String appId) {
- String noEncryPwd = user.getPassword();
- // 校验手机号是否被注册
- if (mobileHasRegistered(user.getMobile())) {
- throw new VisibleError("该手机号已被注册");
- }
- // 由于现在不考虑手机号所属区域,默认为中国大陆
- if (StringUtils.isEmpty(user.getMobileArea())) {
- user.setMobileArea(Const.CONTINENT);
- }
- if (user.getMobile() == null || !user.getMobile().matches(Const.REGEXP_MOBILE_CONTINENT)) {
- throw new VisibleError("请填写正确的手机号");
- }
- // 设置基本属性,手机号默认已认证
- user.setRegisterDate(new Timestamp(System.currentTimeMillis()));
- Long uu = userDao.findMaxUU();
- user.setUserUU(uu == null ? 1000060000L : (uu + 1));
- user.setSalt(String.valueOf(user.getUserUU()));
- user.setMobileValidCode((short) Status.AUTHENTICATED.getCode());
- user.setEmailValidCode((short) Status.NOT_APPLYING.getCode());
- user.setIdentityValidCode((short) Status.NOT_APPLYING.getCode());
- user.setPassword(getEncryPassword(Const.ENCRY_FORMAT, user.getPassword(), user.getSalt()));
- user.setFromApp(appId);
- user = userDao.save(user);
- UserRecord userRecord = new UserRecord(user.getUserUU());
- userRecordDao.save(userRecord);
- userLogger.info(user, Type.UPDATE_REGISTER.getValue());
- // 同步到各个应用
- App app = appService.findOne(appId);
- return syncUserInfo(user, noEncryPwd, "个人注册", app);
- }
- @Override
- public String getEncryPassword(String format, String noEncryPwd, String salt) {
- if (StringUtils.isEmpty(format)) {
- return noEncryPwd;
- }
- // 超过32认为是已加密过的密文
- if (noEncryPwd.length() >= 32) {
- /// 之后添加日志时恢复
- //logger.error("用户密码加密", String.format("传递过来的密码(%s)必须是未加密的明文", noEncryPwd));
- throw new VisibleError("密码过长,请重新输入");
- }
- // $password{$salt}
- String password = format.replace(Const.ENCRY_PARAM_PASSWORD, noEncryPwd);
- password = password.replace(Const.ENCRY_PARAM_SALT, salt == null ? "" : salt);
- return MD5.toMD5(password);
- }
- @Override
- public User save(User user) {
- user = userDao.save(user);
- return syncUserInfo(user, null, "修改用户信息", null);
- }
- @Override
- public void checkPassword(Long userUU, String password, boolean isEncry) {
- // 根据用户uu号找到旧数据
- User oldUser = userDao.findByUserUU(userUU);
- if (oldUser == null) {
- throw new VisibleError("用户名或密码错误");
- }
- // 校验密码
- checkPassword(oldUser, password, isEncry);
- }
- @Override
- public void checkPasswordByMobile(String mobile, String password, boolean isEncry) {
- // 找到用户
- User oldUser = userDao.findByMobile(mobile);
- if (oldUser == null) {
- throw new VisibleError("用户名或密码错误");
- }
- // 校验密码
- checkPassword(oldUser, password, isEncry);
- }
- @Override
- public void checkPasswordByEmail(String email, String password, boolean isEncry) {
- // 找到用户
- List<User> oldUsers = userDao.findByEmailAndEmailValidCode(email, (short) Status.AUTHENTICATED.getCode());
- if (CollectionUtils.isEmpty(oldUsers)) {
- throw new VisibleError("该邮箱未认证,请使用手机号登录");
- }
- // 校验密码
- for (User oldUser : oldUsers) {
- checkPassword(oldUser, password, isEncry);
- }
- }
- @Override
- public boolean checkPasswordByUsername(String username, String password, boolean isEncry) {
- Assert.hasText(username, "用户名不能为空");
- Assert.hasText(password, "密码不能为空");
- User user = findByUsername(username);
- checkPassword(user, password, isEncry);
- return true;
- }
- @Override
- public int getPwdErrorCount(String username) {
- User user = findByUsername(username);
- if (user == null) {
- throw new VisibleError("用户名不存在");
- }
- UserRecord userRecord = userRecordDao.findOne(user.getUserUU());
- if (userRecord == null) {
- return 0;
- }
- return userRecord.getPwdErrorCount();
- }
- @Override
- public User findByUsername(String username) {
- String type = AccountTypeUtils.getAccountType(username);
- User user = null;
- if (AccountTypeUtils.MOBILE.equals(type)) {
- // 手机号
- user = userDao.findByMobile(username);
- } else if (AccountTypeUtils.EMAIL.equals(type)) {
- // 邮箱
- List<User> users = userDao.findByEmailAndEmailValidCode(username, (short) Status.AUTHENTICATED.getCode());
- // 认证邮箱只有一条记录,直接选择第一个
- if (!CollectionUtils.isEmpty(users)) {
- user = users.get(0);
- }
- } else if (AccountTypeUtils.UU_NUMBER.equals(type)) {
- // uu号
- user = userDao.findByUserUU(Long.valueOf(username));
- }
- return user;
- }
- /**
- * 校验用户密码
- *
- * @param oldUser 用户信息
- * @param password 需要校验的密码
- * @param isEncry 需校验的密码是否被加密
- */
- private void checkPassword(User oldUser, String password, boolean isEncry) {
- // 密码未加密,转换成加密后的密码
- String encryPassword = password;
- if (!isEncry) {
- encryPassword = getEncryPassword(Const.ENCRY_FORMAT, password, oldUser.getSalt());
- }
- // 校验密码
- if (!encryPassword.equals(oldUser.getPassword())) {
- throw new VisibleError("您输入的密码与已有账号的登录密码不一致,请重新输入。");
- }
- }
- @Override
- public UserRecord save(UserRecord userRecord) {
- return userRecordDao.save(userRecord);
- }
- @Override
- public User findOne(Long userUU) {
- return userDao.findOne(userUU);
- }
- @Override
- public boolean realNameIsValid(String realName) {
- User user = userDao.findByRealName(realName);
- if (user != null && user.getIdentityValidCode() == Status.AUTHENTICATED.getCode()) {
- return true;
- }
- return false;
- }
- @Override
- public boolean idCardIsValid(String idCard) {
- User user = userDao.findByIdCard(idCard);
- if (user != null && user.getIdentityValidCode() == Status.AUTHENTICATED.getCode()) {
- return true;
- }
- return false;
- }
- @Override
- public void submitIdValidInfo(User user) {
- // 校验企业名和营业执照是否被认证
- boolean isValid = idCardIsValid(user.getIdCard());
- if (isValid) {
- throw new VisibleError("该身份证号已被认证,请确认");
- }
- User oldUser = userDao.findByUserUU(user.getUserUU());
- oldUser.setIdentityValidCode((short) Status.TO_BE_CERTIFIED.getCode());
- oldUser.setRealName(user.getRealName());
- oldUser.setIdCard(user.getIdCard());
- this.save(oldUser);
- // 保存日志
- userValidService.submitValid(user);
- }
- @Override
- public void updateMobile(Long userUU, String newMobile) {
- // 参数空校验
- Assert.notNull(userUU, "userUU must not be null!");
- Assert.hasText(newMobile, "新手机号不能为空");
- // 获取用户信息
- User user = userDao.findOne(userUU);
- if (user == null) {
- throw new VisibleError("用户不存在");
- }
- // 判断手机号是否被注册
- if (!newMobile.equals(user.getMobile())) {
- User oldUser = userDao.findByMobile(newMobile);
- if (oldUser != null) {
- throw new VisibleError("该手机号已被注册");
- }
- }
- // 修改手机号
- user.setMobile(newMobile);
- user.setMobileValidCode((short) Status.AUTHENTICATED.getCode());
- // 保存用户信息
- user = userDao.save(user);
- // 保存日志
- userLogger.info(user, Type.UPDATE_MOBILE.getValue());
- // 同步到各个应用
- syncUserInfo(user, null, "修改手机号", null);
- }
- @Override
- public void updateEmail(Long userUU, String newEmail) {
- // 获取用户信息
- User user = userDao.findOne(userUU);
- if (user == null) {
- throw new VisibleError("用户不存在");
- }
- // 修改手机号
- user.setEmail(newEmail);
- user.setEmailValidCode((short) Status.AUTHENTICATED.getCode());
- // 保存用户信息
- user = userDao.save(user);
- // 保存日志
- userLogger.info(user, Type.UPDATE_EMAIL.getValue());
- // 同步信息到各应用
- syncUserInfo(user, null, "修改邮箱", null);
- }
- @Override
- public Page<User> findMemberBySpaceUU(int page, int size, final Long spaceUU) {
- Pageable pageable = PageInfo.pageRequest(new PageRequest(page, size));
- Page<User> pUsers = userDao.findAll(new Specification<User>() {
- @Override
- public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
- List<Predicate> list = new ArrayList<>();
- list.add(cb.equal(root.join("userSpaces", JoinType.INNER).get("spaceUU").as(Long.class), spaceUU));
- Predicate[] predicates = new Predicate[list.size()];
- predicates = list.toArray(predicates);
- return cb.and(predicates);
- }
- }, pageable);
- return new PageInfo<User>(pUsers.getContent(), pageable, pUsers.getTotalElements());
- }
- @Override
- public void bindUserspace(String appId, Long userUU, Long spaceUU) {
- if (StringUtils.isEmpty(userUU) || StringUtils.isEmpty(spaceUU)) {
- throw new VisibleError("参数错误");
- }
- // 找到用户和企业
- User user = findOne(userUU);
- Userspace userspace = userspaceService.findOne(spaceUU);
- // 将企业添加到用户列表上
- Set<Userspace> userspaces = user.getUserSpaces();
- userspaces.add(userspace);
- // 保存
- userDao.save(user);
- syncUserBindSpace(userUU, spaceUU);
- // 保存日志
- userLogger.info(user, Type.BIND_USERSPACE.getValue() + spaceUU);
- }
- /**
- * 同步用户绑定企业关系
- *
- * @param userUU 用户uu号
- * @param spaceUU 企业uu号
- */
- private void syncUserBindSpace(Long userUU, Long spaceUU) {
- syncRelation(userUU, spaceUU, "bind");
- }
- /**
- * 同步用户解除绑定企业关系
- *
- * @param userUU 用户uu号
- * @param spaceUU 企业uu号
- */
- private void syncUserUnbindSpace(Long userUU, Long spaceUU) {
- syncRelation(userUU, spaceUU, "unbind");
- }
- /**
- * 同步用户与企业的关系
- *
- * @param userUU 用户uu号
- * @param spaceUU 企业uu号
- * @param type 类型 (bind or unbind)
- */
- private void syncRelation(final Long userUU, final Long spaceUU, final String type) {
- Userspace userspace = userspaceService.findOne(spaceUU);
- List<App> apps = userspace.getApps();
- final ModelMap formData = new ModelMap();
- formData.put("userUU", userUU);
- formData.put("spaceUU", spaceUU);
- formData.put("type", type);
- for (final App app : apps) {
- executorService.execute(new Runnable() {
- @Override
- public void run() {
- String url = app.getBackRelationUrl();
- HttpUtil.ResponseWrap res = null;
- try {
- res = HttpUtil.doPost(url, formData, 10000);
- if (!res.isSuccess()) {
- SyncLog syncLog = syncLogger.error(app.getUid(), "同步绑定信息失败", JSON.toJSONString(formData), res.getContent());
- SyncFail.add(syncLog.getId(), formData, url, app.getUid());
- } else {
- syncLogger.info(app.getUid(), "同步绑定信息成功", JSON.toJSONString(formData));
- }
- } catch (Exception e) {
- syncLogger.error(app.getUid(), "同步绑定信息失败", JSON.toJSONString(formData), e.getMessage());
- }
- }
- });
- }
- }
- @Override
- public void unbindUserspace(Long userUU, Long spaceUU) {
- if (StringUtils.isEmpty(userUU) || StringUtils.isEmpty(spaceUU)) {
- throw new VisibleError("参数错误");
- }
- // 找到用户和企业
- User user = findOne(userUU);
- if (user == null) {
- throw new VisibleError("未找到用户信息");
- }
- Userspace userspace = userspaceService.findOne(spaceUU);
- if (userspace == null) {
- throw new VisibleError("未找到企业信息");
- }
- // 将企业添加到用户列表上
- Set<Userspace> userspaces = user.getUserSpaces();
- userspaces.remove(userspace);
- // 保存
- userDao.save(user);
- syncUserUnbindSpace(userUU, spaceUU);
- // 保存日志
- userLogger.info(user, Type.UNBIND_USERSPACE.getValue() + spaceUU);
- }
- @Override
- public void setQuestion(Long userUU, List<UserQuestion> questions) {
- // 找到用户密保
- User user = userDao.findOne(userUU);
- List<UserQuestion> userQuestions = user.getQuestions();
- // 清空旧的并添加新的
- if (CollectionUtils.isEmpty(userQuestions)) {
- user.setQuestions(questions);
- } else {
- for (int i = 0; i < questions.size(); i++) {
- if (userQuestions.get(i) == null) {
- user.getQuestions().add(questions.get(i));
- } else {
- userQuestions.get(i).setQuestion(questions.get(i).getQuestion());
- userQuestions.get(i).setAnswer(questions.get(i).getAnswer());
- userQuestions.get(i).setSort(questions.get(i).getSort());
- }
- }
- }
- // 保存并添加日志
- user = userDao.save(user);
- syncUserInfo(user, null, "修改密保", null);
- userLogger.info(user, Type.UPDATE_QUESTION.getValue(), JSON.toJSONString(user.getQuestions()));
- }
- @Override
- public List<String> findRepeatEmail() {
- return userDao.findRepeatEmail();
- }
- @Override
- public List<User> findByEmail(String email) {
- return userDao.findByEmail(email);
- }
- @Override
- public User updatePassword(Long userUU, String noEncryPwd) {
- User user = userDao.findOne(userUU);
- if (user == null) {
- throw new VisibleError("该用户不存在");
- }
- user.setPassword(getEncryPassword(Const.ENCRY_FORMAT, noEncryPwd, user.getSalt()));
- user.setPasswordLevel(PasswordLevelUtils.checkPasswordLevel(noEncryPwd).getValue());
- user = syncUserInfo(user, noEncryPwd, "用户修改密码", null);
- return userDao.save(user);
- }
- @Override
- public List<UserSpaceDetailInfo> findUserByTels(List<String> tels) {
- // 获取用户列表
- List<User> users = userDao.findUsersByTels(tels);
- if (CollectionUtils.isEmpty(users)) {
- return null;
- }
- List<UserSpaceDetailInfo> data = new ArrayList<>(users.size());
- UserSpaceDetailInfo info;
- // 遍历用户列表取数据
- for (User user : users) {
- info = new UserSpaceDetailInfo();
- Set<Userspace> spaces = user.getUserSpaces();
- if (!CollectionUtils.isEmpty(spaces)) {
- // 有企业的话随便取一个,uu互联需求
- Iterator<Userspace> iterator = spaces.iterator();
- Userspace userspace = iterator.next();
- info.setAddress(userspace.getRegAddress());
- info.setCompany(userspace.getSpaceName());
- }
- info.setEmail(user.getEmail());
- info.setImid(user.getImId());
- info.setUsertel(user.getMobile());
- info.setUsername(user.getVipName());
- data.add(info);
- }
- return data;
- }
- @Override
- public void resetErrorCount(Long userUU) {
- UserRecord userRecord = userRecordDao.findOne(userUU);
- if (userRecord == null) {
- userRecord = new UserRecord(userUU);
- }
- userRecord.setPwdErrorCount(0);
- userRecordDao.save(userRecord);
- }
- @Override
- public User updateUser(@NotNull Long userUU, User newUser) {
- User oldUser = userDao.findOne(userUU);
- if (oldUser == null) {
- throw new VisibleError("未找到用户信息");
- }
- // 修改手机号
- if (!StringUtils.isEmpty(newUser.getMobile()) && !newUser.getMobile().equals(oldUser.getMobile())) {
- if (mobileHasRegistered(newUser.getMobile())) {
- throw new VisibleError("手机号已被注册");
- }
- if (!newUser.getMobile().matches(Const.REGEXP_MOBILE_CONTINENT)) {
- throw new VisibleError("请输入正确的手机号");
- }
- oldUser.setMobile(newUser.getMobile());
- oldUser.setMobileValidCode((short) Status.NOT_APPLYING.getCode());
- }
- // 修改邮箱
- if (!StringUtils.isEmpty(newUser.getEmail()) && !newUser.getEmail().equals(oldUser.getEmail())) {
- if (emailHasRegistered(newUser.getEmail())) {
- throw new VisibleError("邮箱已被注册");
- }
- if (!newUser.getEmail().matches(Const.REGEXP_EMAIL)) {
- throw new VisibleError("请输入正确的邮箱");
- }
- oldUser.setEmail(newUser.getEmail());
- oldUser.setEmailValidCode((short) Status.NOT_APPLYING.getCode());
- }
- // 修改用户名
- if (!StringUtils.isEmpty(newUser.getVipName())) {
- oldUser.setVipName(newUser.getVipName());
- }
- // 保存用户信息
- oldUser = userDao.save(oldUser);
- oldUser = syncUserInfo(oldUser, null, "(接口调用)修改用户信息", null);
- return oldUser;
- }
- @Override
- public long count() {
- return userDao.count();
- }
- @Override
- public Long countByLogin(String start, String end) throws ParseException {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date startTime = simpleDateFormat.parse(start);
- Date endTime = simpleDateFormat.parse(end);
- Calendar starttime = Calendar.getInstance();
- starttime.setTime(startTime);
- Calendar endtime = Calendar.getInstance();
- endtime.setTime(endTime);
- return userRecordDao.getCountByLogin(new Timestamp(starttime.getTimeInMillis()),new Timestamp(endtime.getTimeInMillis()));
- }
- @Override
- public Long countByLoginInputMonth(String startTime,String endTime, int month) throws ParseException {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date startToDate = simpleDateFormat.parse(startTime);
- Date endToDate = simpleDateFormat.parse(endTime);
- Calendar start = Calendar.getInstance();
- start.setTime(startToDate);
- start.add(Calendar.MONTH, -month);
- Calendar end = Calendar.getInstance();
- end.setTime(endToDate);
- end.add(Calendar.MONTH, -month);
- System.out.println(new Timestamp(start.getTimeInMillis()));
- System.out.println(new Timestamp(end.getTimeInMillis()));
- return userRecordDao.getCountByLogin(new Timestamp(start.getTimeInMillis()),new Timestamp(end.getTimeInMillis()));
- }
- @Override
- public long countByRegisterDate(final Calendar start, final Calendar end) {
- return userDao.count(new Specification<User>() {
- @Override
- public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
- Predicate predicate = cb.between(root.get("registerDate").as(Calendar.class), start, end);
- query.where(predicate);
- return null;
- }
- });
- }
- @Override
- public Map<String, Long> countByRegisterDate(Timestamp start, Timestamp end, List<String> fromApps) {
- Map<String, Long> data = new HashMap<>();
- List<Map<String, Object>> counts = userDao.getCountByRegisterDate(start, end, fromApps);
- for (Map<String, Object> count : counts) {
- data.put((String) count.get("appId"), (Long) count.get("count"));
- fromApps.remove(count.get("appId"));
- }
- for(String fromApp: fromApps){
- data.put(fromApp, 0L);
- }
- return data;
- }
- @Override
- public long countInCurrentMonth() {
- return CountUtils.countInCurrentMonth(new CountCallBack<Long>() {
- @Override
- public Long countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(start, end);
- }
- });
- }
- @Override
- public long countInLastMonth() {
- return CountUtils.countInLastMonth(new CountCallBack<Long>() {
- @Override
- public Long countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(start, end);
- }
- });
- }
- @Override
- public long countInCurrentWeek() {
- return CountUtils.countInCurrentWeek(new CountCallBack<Long>() {
- @Override
- public Long countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(start, end);
- }
- });
- }
- @Override
- public Map<String, Long> count(final List<String> fromApps) {
- Map<String, Long> data = new HashMap<>();
- List<Map<String, Object>> counts = userDao.getCountByRegisterDate(fromApps);
- for (Map<String, Object> count : counts) {
- data.put((String) count.get("appId"), (Long) count.get("count"));
- }
- return data;
- }
- @Override
- public Map<String, Long> countInCurrentMonth(final List<String> fromApps) {
- return CountUtils.countInCurrentMonth(new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
- }
- });
- }
- @Override
- public Map<String, Long> countInCurrentWeek(final List<String> fromApps) {
- return CountUtils.countInCurrentWeek(new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
- }
- });
- }
- @Override
- public Map<String, Long> countInToday(List<String> fromApps) {
- return CountUtils.countInToday(new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
- }
- });
- }
- @Override
- public Map<String, Long> countInYesterday(List<String> fromApps) {
- return CountUtils.countInYesterday(new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
- }
- });
- }
- @Override
- public Map<String, Long> countgInLastMonth(List<String> fromApps) {
- return CountUtils.countInLastMonth(new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()), new Timestamp(end.getTimeInMillis()), fromApps);
- }
- });
- }
- @Override
- public Map<String, Long> countInInputTime(String startTime, String endTime, List<String> fromApps) throws ParseException {
- CountCallBack<Map<String, Long>> countCallBack = new CountCallBack<Map<String, Long>>() {
- @Override
- public Map<String, Long> countByTime(Calendar start, Calendar end) {
- return countByRegisterDate(new Timestamp(start.getTimeInMillis()),new Timestamp(end.getTimeInMillis()),fromApps);
- }
- };
- return CountUtils.countInInputTime(countCallBack,startTime,endTime);
- }
- @Override
- public User findByWxUnionid(String unionid) {
- return userDao.findByWxUnionid(unionid);
- }
- @Override
- public User findByBhOpenId(String openId) {
- return userDao.findByBhOpenId(openId);
- }
- @Override
- public User bindUnionId(String username, String password, String unionid) {
- Assert.hasText(username, "用户名不能为空");
- Assert.hasText(password, "密码不能为空");
- User user = findByUsername(username);
- if (user == null) {
- throw new IllegalArgumentException("该用户不存在");
- }
- checkPassword(user.getUserUU(), password, false);
- user.setWxUnionid(unionid);
- userDao.save(user);
- userLogger.info(user, Type.BIND_WEIXIN.getValue());
- return user;
- }
- @Override
- public User bindBhOpenId(String username, String password, String openId) {
- Assert.hasText(username, "用户名不能为空");
- Assert.hasText(password, "密码不能为空");
- User user = findByUsername(username);
- if (user == null) {
- throw new IllegalArgumentException("该用户不存在");
- }
- checkPassword(user.getUserUU(), password, false);
- user.setBhOpenId(openId);
- userDao.save(user);
- userLogger.info(user, Type.BIND_BIHE.getValue());
- return user;
- }
- @Override
- public User findByForeignId(ForeignInfo foreignInfo) {
- User user = null;
- if (foreignInfo instanceof OAuthInfo) {
- user = userDao.findByWxUnionid(foreignInfo.getForeignOpenId());
- } else if (foreignInfo instanceof BiHeInfo) {
- user = userDao.findByBhOpenId(foreignInfo.getForeignOpenId());
- }
- return user;
- }
- @Override
- public User bindForeignOpenId(String username, String password, ForeignInfo foreignInfo) {
- // 校验空参数
- try {
- Assert.hasText(username, "用户名不能为空");
- Assert.hasText(password, "密码不能为空");
- } catch (IllegalArgumentException e) {
- throw new VisibleError(e.getMessage());
- }
- // 校验用户名密码是否正确
- User user = findByUsername(username);
- if (user == null) {
- throw new VisibleError("该用户不存在");
- }
- checkPassword(user.getUserUU(), password, false);
- // 设置第三方openId
- setForeignOpenId(user, foreignInfo);
- userDao.save(user);
- userLogger.info(user, Type.BIND_FOREIGN.getValue());
- return user;
- }
- @Override
- public User bindForeignOpenId(String code, String mobile, String codeToken, ForeignInfo foreignInfo) {
- // 校验空参数
- try {
- Assert.hasText(code, "验证码不能为空");
- Assert.hasText(mobile, "手机号不能为空");
- Assert.hasText(codeToken, "token不能为空");
- } catch (IllegalArgumentException e) {
- throw new VisibleError(e.getMessage());
- }
- // 校验验证码是否正确
- Token existToken = tokenService.findOne(codeToken);
- if (existToken == null || existToken.isExpired()) {
- throw new VisibleError("验证码过期");
- }
- if (!mobile.equals(existToken.getMobile())) {
- throw new VisibleError("手机号已被修改,请重新获取验证码");
- }
- if (!code.equals(existToken.getBind())) {
- throw new VisibleError("验证码不正确,请重新输入");
- }
- // 设置第三方openId
- User user = userDao.findByMobile(mobile);
- if (user == null) {
- throw new VisibleError("该用户不存在");
- }
- setForeignOpenId(user, foreignInfo);
- userDao.save(user);
- userLogger.info(user, Type.BIND_FOREIGN.getValue());
- return user;
- }
- /**
- * 设置用户第三方openId,不同应用对应不同字段
- *
- * @param user 用户信息
- * @param foreignInfo
- * @return
- */
- @Override
- public User setForeignOpenId(User user, ForeignInfo foreignInfo) {
- if (user == null) {
- return null;
- }
- if (foreignInfo instanceof OAuthInfo) {
- user.setWxUnionid(foreignInfo.getForeignOpenId());
- } else if (foreignInfo instanceof BiHeInfo) {
- user.setBhOpenId(foreignInfo.getForeignOpenId());
- }
- return user;
- }
- @Override
- public User findByImId(String imId) {
- return userDao.findByImId(imId);
- }
- /**
- * 同步用户信息到各个应用
- *
- * @param user 用户信息
- * @param noEncryPwd 未加密密码,用于同步im
- * @param msg 同步信息描述,用户区分同步类型
- */
- private User syncUserInfo(User user, String noEncryPwd, final String msg, App otherApp) {
- List<App> apps = appService.findDefaultUseApp();
- if (otherApp != null && !apps.contains(otherApp)) {
- apps.add(otherApp);
- }
- try {
- // 其他应用可能会用到IMID,要先同步到im之后再同步到其他应用
- String imId = syncUserToIm(user, noEncryPwd, msg);
- user.setImId(imId);
- user = userDao.save(user);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 封装同步其他应用数据并同步
- boolean hasQuestion = !CollectionUtils.isEmpty(user.getQuestions());
- JSONObject formData = JSON.parseObject(JSON.toJSONString(user));
- formData.put("password", user.getPassword());
- formData.put("hasQuestion", hasQuestion);
- syncUserInfo(formData, msg, apps);
- return user;
- }
- private String syncUserToIm(User user, String noEncryPwd, String msg) throws Exception {
- String appId = "im";
- App app = appService.findOne(appId);
- if (StringUtils.isEmpty(app.getBackUserUrl())) {
- return null;
- }
- // 封装数据同步到im
- String url = app.getBackUserUrl();
- HttpUtil.ResponseWrap res = null;
- ModelMap formData = new ModelMap();
- formData.put("dialectUID", user.getImId());
- formData.put("realName", StringUtils.isEmpty(user.getRealName()) ? user.getVipName() : user.getRealName());
- formData.put("password", noEncryPwd);
- formData.put("email", StringUtils.isEmpty(user.getEmail()) ? "0" : user.getEmail());
- formData.put("mobile", user.getMobile());
- res = HttpUtil.doPost(url, formData, 10000);
- if (!res.isSuccess()) {
- syncLogger.error(appId, msg + ",同步用户信息失败", formData.toString(), res.getContent());
- throw new Exception(res.getContent());
- } else {
- JSONObject obj = JSON.parseObject(res.getContent());
- if (obj.getString("resultMsg") != null) {
- syncLogger.error(appId, msg + ",同步用户信息失败", formData.toString(), res.getContent());
- }
- syncLogger.info(appId, msg + ",同步用户信息成功", formData.toString(), res.getContent());
- return obj.getString("dialectUID");
- }
- }
- /**
- * 同步信息到指定应用
- *
- * @param formData 同步数据
- * @param msg 同步信息
- * @param apps 需要同步的应用
- */
- private void syncUserInfo(final Map<String, Object> formData, final String msg, List<App> apps) {
- for (final App app : apps) {
- executorService.execute(new Runnable() {
- @Override
- public void run() {
- String url = app.getBackUserUrl();
- if (StringUtils.isEmpty(url)) {
- return;
- }
- HttpUtil.ResponseWrap res;
- try {
- res = HttpUtil.doPost(url, formData, 10000);
- if (!res.isSuccess()) {
- SyncLog syncLog = syncLogger.error(app.getUid(), msg + ",同步用户信息失败", JSON.toJSONString(formData), res.getContent());
- SyncFail.add(syncLog.getId(), formData, url, app.getUid());
- } else {
- syncLogger.info(app.getUid(), msg + ",同步用户信息成功", JSON.toJSONString(formData));
- }
- } catch (Exception e) {
- syncLogger.error(app.getUid(), msg + ",同步用户信息失败", JSON.toJSONString(formData), e.getMessage());
- }
- }
- });
- }
- }
- @Override
- public long countByNotLogin(int month) {
- return CountUtils.countBeforeInputMonth(new CountCallBack<Long>() {
- @Override
- public Long countByTime(Calendar start, Calendar end) {
- return countBeforeTime(end);
- }
- }, month);
- }
- private long countBeforeTime(Calendar end) {
- return userRecordDao.count((root, query, cb) -> {
- Predicate lessPredicate = cb.lessThan(root.get("lastLoginTime").as(Calendar.class), end);
- Predicate nullPredicate = cb.isNull(root.get("lastLoginTime"));
- query.where(cb.or(lessPredicate, nullPredicate));
- return null;
- });
- }
- }
|