UserServiceImpl.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. package com.uas.sso.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.uas.sso.common.encrypt.MD5;
  5. import com.uas.sso.common.util.HttpUtil;
  6. import com.uas.sso.core.Const;
  7. import com.uas.sso.core.ICallable;
  8. import com.uas.sso.core.Status;
  9. import com.uas.sso.core.Type;
  10. import com.uas.sso.dao.UserDao;
  11. import com.uas.sso.dao.UserRecordDao;
  12. import com.uas.sso.entity.*;
  13. import com.uas.sso.exception.VisibleError;
  14. import com.uas.sso.logging.LoggerManager;
  15. import com.uas.sso.logging.SyncBufferedLogger;
  16. import com.uas.sso.logging.UserBufferedLogger;
  17. import com.uas.sso.service.*;
  18. import com.uas.sso.util.AccountTypeUtils;
  19. import com.uas.sso.util.ExecuteUtils;
  20. import com.uas.sso.util.PasswordLevelUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.data.domain.Page;
  23. import org.springframework.data.domain.PageRequest;
  24. import org.springframework.data.domain.Pageable;
  25. import org.springframework.data.jpa.domain.Specification;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.ui.ModelMap;
  28. import org.springframework.util.CollectionUtils;
  29. import org.springframework.util.StringUtils;
  30. import javax.persistence.criteria.*;
  31. import javax.validation.constraints.NotNull;
  32. import java.sql.Timestamp;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import java.util.Set;
  37. /**
  38. * 用户service实现类
  39. *
  40. * @author wangmh
  41. * @date 2018/1/2
  42. */
  43. @Service
  44. public class UserServiceImpl implements UserService {
  45. @Autowired
  46. private UserDao userDao;
  47. @Autowired
  48. private UserRecordDao userRecordDao;
  49. @Autowired
  50. private UserValidService userValidService;
  51. @Autowired
  52. private UserspaceService userspaceService;
  53. @Autowired
  54. private AppService appService;
  55. @Autowired
  56. private UserQuestionService userQuestionService;
  57. private UserBufferedLogger userLog = LoggerManager.getLogger(UserBufferedLogger.class);
  58. private SyncBufferedLogger syncLog = LoggerManager.getLogger(SyncBufferedLogger.class);
  59. @Override
  60. public User findByMobile(String mobile, String mobileArea) {
  61. return userDao.findByMobileAndMobileArea(mobile, mobileArea);
  62. }
  63. @Override
  64. public User findByMobile(String mobile) {
  65. return userDao.findByMobile(mobile);
  66. }
  67. @Override
  68. public boolean mobileHasRegistered(String mobile) {
  69. User user = userDao.findByMobile(mobile);
  70. if (user == null) {
  71. return false;
  72. }
  73. return true;
  74. }
  75. @Override
  76. public boolean emailHasRegistered(String email) {
  77. List<User> users = userDao.findByEmail(email);
  78. if (CollectionUtils.isEmpty(users)) {
  79. return false;
  80. }
  81. return true;
  82. }
  83. @Override
  84. public synchronized User register(User user) {
  85. String noEncryPwd = user.getPassword();
  86. // 校验手机号是否被注册
  87. if (mobileHasRegistered(user.getMobile())) {
  88. throw new VisibleError("该手机号已被注册");
  89. }
  90. // 由于现在不考虑手机号所属区域,默认为中国大陆
  91. if (StringUtils.isEmpty(user.getMobileArea())) {
  92. user.setMobileArea(Const.CONTINENT);
  93. }
  94. if (user.getMobile() == null || !user.getMobile().matches(Const.REGEXP_MOBILE_CONTINENT)) {
  95. throw new VisibleError("请填写正确的手机号");
  96. }
  97. // 设置基本属性,手机号默认已认证
  98. user.setRegisterDate(new Timestamp(System.currentTimeMillis()));
  99. Long uu = userDao.findMaxUU();
  100. user.setUserUU(uu == null ? 1000060000L : (uu + 1));
  101. user.setSalt(String.valueOf(user.getUserUU()));
  102. user.setMobileValidCode((short) Status.AUTHENTICATED.getCode());
  103. user.setEmailValidCode((short) Status.NOT_APPLYING.getCode());
  104. user.setIdentityValidCode((short) Status.NOT_APPLYING.getCode());
  105. user.setPassword(getEncryPassword(Const.ENCRY_FORMAT, user.getPassword(), user.getSalt()));
  106. user.setUserRecord(new UserRecord());
  107. user.getUserRecord().setUser(user);
  108. user.getUserRecord().setUserUU(user.getUserUU());
  109. userDao.save(user);
  110. userLog.info(user, Type.UPDATE_REGISTER.getValue());
  111. // 同步到各个应用
  112. return syncUserInfo(user.getUserUU(), noEncryPwd, "个人注册");
  113. }
  114. @Override
  115. public String getEncryPassword(String format, String noEncryPwd, String salt) {
  116. if (StringUtils.isEmpty(format)) {
  117. return noEncryPwd;
  118. }
  119. // 超过32认为是已加密过的密文
  120. if (noEncryPwd.length() >= 32) {
  121. /// 之后添加日志时恢复
  122. //logger.error("用户密码加密", String.format("传递过来的密码(%s)必须是未加密的明文", noEncryPwd));
  123. throw new VisibleError("密码过长,请重新输入");
  124. }
  125. // $password{$salt}
  126. String password = format.replace(Const.ENCRY_PARAM_PASSWORD, noEncryPwd);
  127. password = password.replace(Const.ENCRY_PARAM_SALT, salt == null ? "" : salt);
  128. return MD5.toMD5(password);
  129. }
  130. @Override
  131. public User save(User user) {
  132. user = userDao.save(user);
  133. return syncUserInfo(user, null, "修改用户信息");
  134. }
  135. @Override
  136. public void checkPassword(Long userUU, String password, boolean isEncry) {
  137. // 根据用户uu号找到旧数据
  138. User oldUser = userDao.findByUserUU(userUU);
  139. if (oldUser == null) {
  140. throw new VisibleError("用户名或密码错误");
  141. }
  142. // 校验密码
  143. checkPassword(oldUser, password, isEncry);
  144. }
  145. @Override
  146. public void checkPasswordByMobile(String mobile, String password, boolean isEncry) {
  147. // 找到用户
  148. User oldUser = userDao.findByMobile(mobile);
  149. if (oldUser == null) {
  150. throw new VisibleError("用户名或密码错误");
  151. }
  152. // 校验密码
  153. checkPassword(oldUser, password, isEncry);
  154. }
  155. @Override
  156. public void checkPasswordByEmail(String email, String password, boolean isEncry) {
  157. // 找到用户
  158. List<User> oldUsers = userDao.findByEmailAndEmailValidCode(email, (short) Status.AUTHENTICATED.getCode());
  159. if (CollectionUtils.isEmpty(oldUsers)) {
  160. throw new VisibleError("该邮箱未认证,请使用手机号登录");
  161. }
  162. // 校验密码
  163. for (User oldUser : oldUsers) {
  164. checkPassword(oldUser, password, isEncry);
  165. }
  166. }
  167. @Override
  168. public int getPwdErrorCount(String username) {
  169. User user = findByUsername(username);
  170. if (user == null) {
  171. throw new VisibleError("用户名不存在");
  172. }
  173. if (user.getUserRecord() == null) {
  174. return 0;
  175. }
  176. return user.getUserRecord().getPwdErrorCount();
  177. }
  178. @Override
  179. public User findByUsername(String username) {
  180. String type = AccountTypeUtils.getAccountType(username);
  181. User user = null;
  182. if (AccountTypeUtils.MOBILE.equals(type)) {
  183. // 手机号
  184. user = userDao.findByMobile(username);
  185. } else if (AccountTypeUtils.EMAIL.equals(type)) {
  186. // 邮箱
  187. List<User> users = userDao.findByEmailAndEmailValidCode(username, (short) Status.AUTHENTICATED.getCode());
  188. // 认证邮箱只有一条记录,直接选择第一个
  189. if (!CollectionUtils.isEmpty(users)) {
  190. user = users.get(0);
  191. }
  192. } else if (AccountTypeUtils.UU_NUMBER.equals(type)) {
  193. // uu号
  194. user = userDao.findByUserUU(Long.valueOf(username));
  195. }
  196. return user;
  197. }
  198. /**
  199. * 校验用户密码
  200. *
  201. * @param oldUser 用户信息
  202. * @param password 需要校验的密码
  203. * @param isEncry 需校验的密码是否被加密
  204. */
  205. private void checkPassword(User oldUser, String password, boolean isEncry) {
  206. // 密码未加密,转换成加密后的密码
  207. String encryPassword = password;
  208. if (!isEncry) {
  209. encryPassword = getEncryPassword(Const.ENCRY_FORMAT, password, oldUser.getSalt());
  210. }
  211. // 校验密码
  212. if (!encryPassword.equals(oldUser.getPassword())) {
  213. throw new VisibleError("您输入的密码与已有账号的登录密码不一致,请重新输入。");
  214. }
  215. }
  216. @Override
  217. public UserRecord save(UserRecord userRecord) {
  218. return userRecordDao.save(userRecord);
  219. }
  220. @Override
  221. public User findOne(Long userUU) {
  222. return userDao.findOne(userUU);
  223. }
  224. @Override
  225. public boolean realNameIsValid(String realName) {
  226. User user = userDao.findByRealName(realName);
  227. if (user != null && user.getIdentityValidCode() == Status.AUTHENTICATED.getCode()) {
  228. return true;
  229. }
  230. return false;
  231. }
  232. @Override
  233. public boolean idCardIsValid(String idCard) {
  234. User user = userDao.findByIdCard(idCard);
  235. if (user != null && user.getIdentityValidCode() == Status.AUTHENTICATED.getCode()) {
  236. return true;
  237. }
  238. return false;
  239. }
  240. @Override
  241. public void submitIdValidInfo(User user) {
  242. // 校验企业名和营业执照是否被认证
  243. boolean isValid = idCardIsValid(user.getIdCard());
  244. if (isValid) {
  245. throw new VisibleError("该身份证号已被认证,请确认");
  246. }
  247. User oldUser = userDao.findByUserUU(user.getUserUU());
  248. oldUser.setIdentityValidCode((short) Status.TO_BE_CERTIFIED.getCode());
  249. oldUser.setRealName(user.getRealName());
  250. oldUser.setIdCard(user.getIdCard());
  251. this.save(oldUser);
  252. // 保存日志
  253. userValidService.submitValid(user);
  254. }
  255. @Override
  256. public void updateMobile(Long userUU, String newMobile) {
  257. // 获取用户信息
  258. User user = userDao.findOne(userUU);
  259. if (user == null) {
  260. throw new VisibleError("用户不存在");
  261. }
  262. // 修改手机号
  263. User oldUser = userDao.findByMobile(newMobile);
  264. if (oldUser != null) {
  265. throw new VisibleError("该手机号已被注册");
  266. }
  267. user.setMobile(newMobile);
  268. user.setMobileValidCode((short) Status.AUTHENTICATED.getCode());
  269. // 保存用户信息
  270. userDao.save(user);
  271. // 保存日志
  272. userLog.info(user, Type.UPDATE_MOBILE.getValue());
  273. // 同步到各个应用
  274. syncUserInfo(user.getUserUU(), null, "修改手机号");
  275. }
  276. @Override
  277. public void updateEmail(Long userUU, String newEmail) {
  278. // 获取用户信息
  279. User user = userDao.findOne(userUU);
  280. if (user == null) {
  281. throw new VisibleError("用户不存在");
  282. }
  283. // 修改手机号
  284. user.setEmail(newEmail);
  285. user.setEmailValidCode((short) Status.AUTHENTICATED.getCode());
  286. // 保存用户信息
  287. userDao.save(user);
  288. // 保存日志
  289. userLog.info(user, Type.UPDATE_EMAIL.getValue());
  290. // 同步信息到各应用
  291. syncUserInfo(user.getUserUU(), null, "修改邮箱");
  292. }
  293. @Override
  294. public Page<User> findMemberBySpaceUU(int page, int size, final Long spaceUU) {
  295. Pageable pageable = PageInfo.pageRequest(new PageRequest(page, size));
  296. Page<User> pUsers = userDao.findAll(new Specification<User>() {
  297. @Override
  298. public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
  299. List<Predicate> list = new ArrayList<>();
  300. list.add(cb.equal(root.join("userSpaces", JoinType.INNER).get("spaceUU").as(Long.class), spaceUU));
  301. Predicate[] predicates = new Predicate[list.size()];
  302. predicates = list.toArray(predicates);
  303. return cb.and(predicates);
  304. }
  305. }, pageable);
  306. return new PageInfo<User>(pUsers.getContent(), pageable, pUsers.getTotalElements());
  307. }
  308. @Override
  309. public void bindUserspace(String appId, Long userUU, Long spaceUU) {
  310. if (StringUtils.isEmpty(userUU) || StringUtils.isEmpty(spaceUU)) {
  311. throw new VisibleError("参数错误");
  312. }
  313. // 找到用户和企业
  314. User user = findOne(userUU);
  315. Userspace userspace = userspaceService.findOne(spaceUU);
  316. // 将企业添加到用户列表上
  317. Set<Userspace> userspaces = user.getUserSpaces();
  318. userspaces.add(userspace);
  319. // 保存
  320. userDao.save(user);
  321. syncUserBindSpace(userUU, spaceUU);
  322. // 保存日志
  323. userLog.info(user, Type.BIND_USERSPACE.getValue()+spaceUU);
  324. }
  325. /**
  326. * 同步用户绑定企业关系
  327. * @param userUU 用户uu号
  328. * @param spaceUU 企业uu号
  329. */
  330. private void syncUserBindSpace(Long userUU, Long spaceUU) {
  331. syncRelation(userUU, spaceUU, "bind");
  332. }
  333. /**
  334. * 同步用户解除绑定企业关系
  335. * @param userUU 用户uu号
  336. * @param spaceUU 企业uu号
  337. */
  338. private void syncUserUnbindSpace(Long userUU, Long spaceUU) {
  339. syncRelation(userUU, spaceUU, "unbind");
  340. }
  341. /**
  342. * 同步用户与企业的关系
  343. * @param userUU 用户uu号
  344. * @param spaceUU 企业uu号
  345. * @param type 类型 (bind or unbind)
  346. */
  347. private void syncRelation(final Long userUU, final Long spaceUU, final String type) {
  348. List<String> apps = appService.findUid();
  349. ExecuteUtils.execute(new ICallable<Void, String>() {
  350. @Override
  351. public Void call(String appId) {
  352. App tempApp = appService.findOne(appId);
  353. if (tempApp != null && StringUtils.isEmpty(tempApp.getUserControl())
  354. && !StringUtils.isEmpty(tempApp.getBackRelationUrl())) {
  355. String url = tempApp.getBackRelationUrl();
  356. ModelMap formData = new ModelMap();
  357. formData.put("userUU", userUU);
  358. formData.put("spaceUU", spaceUU);
  359. formData.put("type", type);
  360. HttpUtil.ResponseWrap res = null;
  361. try {
  362. res = HttpUtil.doPost(url, formData, 10000);
  363. if (!res.isSuccess()) {
  364. syncLog.error(appId, "同步绑定信息失败", JSON.toJSONString(formData), res.getContent());
  365. } else {
  366. syncLog.info(appId, "同步绑定信息成功", JSON.toJSONString(formData));
  367. }
  368. } catch (Exception e) {
  369. syncLog.error(appId, "同步绑定信息失败", JSON.toJSONString(formData), e.getMessage());
  370. }
  371. }
  372. return null;
  373. }
  374. }, apps);
  375. }
  376. @Override
  377. public void unbindUserspace(Long userUU, Long spaceUU) {
  378. if (StringUtils.isEmpty(userUU) || StringUtils.isEmpty(spaceUU)) {
  379. throw new VisibleError("参数错误");
  380. }
  381. // 找到用户和企业
  382. User user = findOne(userUU);
  383. if (user == null) {
  384. throw new VisibleError("未找到用户信息");
  385. }
  386. Userspace userspace = userspaceService.findOne(spaceUU);
  387. if (userspace == null) {
  388. throw new VisibleError("未找到企业信息");
  389. }
  390. // 将企业添加到用户列表上
  391. Set<Userspace> userspaces = user.getUserSpaces();
  392. userspaces.remove(userspace);
  393. // 保存
  394. userDao.save(user);
  395. syncUserUnbindSpace(userUU, spaceUU);
  396. // 保存日志
  397. userLog.info(user, Type.UNBIND_USERSPACE.getValue()+spaceUU);
  398. }
  399. @Override
  400. public void setQuestion(Long userUU, List<UserQuestion> questions) {
  401. // 找到用户密保
  402. User user = userDao.findOne(userUU);
  403. List<UserQuestion> userQuestions = user.getQuestions();
  404. // 清空旧的并添加新的
  405. if (CollectionUtils.isEmpty(userQuestions)) {
  406. user.setQuestions(questions);
  407. } else {
  408. for (int i=0; i<questions.size(); i++) {
  409. if (userQuestions.get(i) == null) {
  410. user.getQuestions().add(questions.get(i));
  411. } else {
  412. userQuestions.get(i).setQuestion(questions.get(i).getQuestion());
  413. userQuestions.get(i).setAnswer(questions.get(i).getAnswer());
  414. userQuestions.get(i).setSort(questions.get(i).getSort());
  415. }
  416. }
  417. }
  418. // 保存并添加日志
  419. user = userDao.save(user);
  420. syncUserInfo(user, null, "修改密保");
  421. userLog.info(user, Type.UPDATE_QUESTION.getValue(), JSON.toJSONString(user.getQuestions()));
  422. }
  423. @Override
  424. public List<String> findRepeatEmail() {
  425. return userDao.findRepeatEmail();
  426. }
  427. @Override
  428. public List<User> findByEmail(String email) {
  429. return userDao.findByEmail(email);
  430. }
  431. @Override
  432. public User updatePassword(Long userUU, String noEncryPwd) {
  433. User user = userDao.findOne(userUU);
  434. if (user == null) {
  435. throw new VisibleError("该用户不存在");
  436. }
  437. user.setPassword(getEncryPassword(Const.ENCRY_FORMAT, noEncryPwd, user.getSalt()));
  438. user.setPasswordLevel(PasswordLevelUtils.checkPasswordLevel(noEncryPwd).getValue());
  439. user = syncUserInfo(user , noEncryPwd, "用户修改密码");
  440. return userDao.save(user);
  441. }
  442. @Override
  443. public List<UserSpaceDetailInfo> findUserByTels(List<String> tels) {
  444. // 获取用户列表
  445. List<User> users = userDao.findUsersByTels(tels);
  446. if (CollectionUtils.isEmpty(users)) {
  447. return null;
  448. }
  449. List<UserSpaceDetailInfo> data = new ArrayList<>(users.size());
  450. UserSpaceDetailInfo info;
  451. // 遍历用户列表取数据
  452. for (User user : users) {
  453. info = new UserSpaceDetailInfo();
  454. Set<Userspace> spaces = user.getUserSpaces();
  455. if (!CollectionUtils.isEmpty(spaces)) {
  456. // 有企业的话随便取一个,uu互联需求
  457. Iterator<Userspace> iterator = spaces.iterator();
  458. Userspace userspace = iterator.next();
  459. info.setAddress(userspace.getRegAddress());
  460. info.setCompany(userspace.getSpaceName());
  461. }
  462. info.setEmail(user.getEmail());
  463. info.setImid(user.getImId());
  464. info.setUsertel(user.getMobile());
  465. info.setUsername(user.getVipName());
  466. data.add(info);
  467. }
  468. return data;
  469. }
  470. @Override
  471. public void resetErrorCount(Long userUU) {
  472. UserRecord userRecord = userRecordDao.findOne(userUU);
  473. if (userRecord == null) {
  474. userRecord = new UserRecord(userUU);
  475. }
  476. userRecord.setPwdErrorCount(0);
  477. userRecordDao.save(userRecord);
  478. }
  479. @Override
  480. public User updateUser(@NotNull Long userUU, User newUser) {
  481. User oldUser = userDao.findOne(userUU);
  482. if (oldUser == null) {
  483. throw new VisibleError("未找到用户信息");
  484. }
  485. // 修改手机号
  486. if (!StringUtils.isEmpty(newUser.getMobile()) && !newUser.getMobile().equals(oldUser.getMobile())) {
  487. if (mobileHasRegistered(newUser.getMobile())) {
  488. throw new VisibleError("手机号已被注册");
  489. }
  490. if (!newUser.getMobile().matches(Const.REGEXP_MOBILE_CONTINENT)) {
  491. throw new VisibleError("请输入正确的手机号");
  492. }
  493. oldUser.setMobile(newUser.getMobile());
  494. oldUser.setMobileValidCode((short) Status.NOT_APPLYING.getCode());
  495. }
  496. // 修改邮箱
  497. if (!StringUtils.isEmpty(newUser.getEmail()) && !newUser.getEmail().equals(oldUser.getEmail())) {
  498. if (emailHasRegistered(newUser.getEmail())) {
  499. throw new VisibleError("邮箱已被注册");
  500. }
  501. if (!newUser.getEmail().matches(Const.REGEXP_EMAIL)) {
  502. throw new VisibleError("请输入正确的邮箱");
  503. }
  504. oldUser.setEmail(newUser.getEmail());
  505. oldUser.setEmailValidCode((short) Status.NOT_APPLYING.getCode());
  506. }
  507. // 修改用户名
  508. if (!StringUtils.isEmpty(newUser.getVipName())) {
  509. oldUser.setVipName(newUser.getVipName());
  510. }
  511. // 保存用户信息
  512. oldUser = userDao.save(oldUser);
  513. oldUser = syncUserInfo(oldUser, null, "(接口调用)修改用户信息");
  514. return oldUser;
  515. }
  516. /**
  517. * 同步用户信息到各个应用
  518. * @param userUU 用户uu号
  519. * @param noEncryPwd 未加密密码,用于同步im
  520. * @param msg 同步信息描述,用户区分同步类型
  521. */
  522. private User syncUserInfo(Long userUU, String noEncryPwd, String msg) {
  523. return syncUserInfo(findOne(userUU), noEncryPwd, msg);
  524. }
  525. /**
  526. * 同步用户信息到各个应用
  527. * @param user 用户信息
  528. * @param noEncryPwd 未加密密码,用于同步im
  529. * @param msg 同步信息描述,用户区分同步类型
  530. */
  531. private User syncUserInfo(User user, String noEncryPwd, final String msg) {
  532. List<String> apps = appService.findUid();
  533. final boolean hasQuestion = !CollectionUtils.isEmpty(user.getQuestions());
  534. try {
  535. // 同步信息到im
  536. String imId = syncUserToIm(user, noEncryPwd, msg);
  537. user.setImId(imId);
  538. user = userDao.save(user);
  539. } catch (Exception e) {
  540. e.printStackTrace();
  541. }
  542. final User finalUser = user;
  543. ExecuteUtils.execute(new ICallable<Void, String>() {
  544. @Override
  545. public Void call(String appId) {
  546. App tempApp = appService.findOne(appId);
  547. if (tempApp != null && StringUtils.isEmpty(tempApp.getUserControl())
  548. && !StringUtils.isEmpty(tempApp.getBackUserUrl()) && !"im".equals(appId)) {
  549. String url = tempApp.getBackUserUrl();
  550. JSONObject formData = JSON.parseObject(JSON.toJSONString(finalUser));
  551. formData.put("password", finalUser.getPassword());
  552. formData.put("hasQuestion", hasQuestion);
  553. HttpUtil.ResponseWrap res = null;
  554. try {
  555. res = HttpUtil.doPost(url, formData, 10000);
  556. if (!res.isSuccess()) {
  557. syncLog.error(appId, msg + ",同步用户信息失败", JSON.toJSONString(formData), res.getContent());
  558. } else {
  559. syncLog.info(appId, msg + ",同步用户信息成功", JSON.toJSONString(formData));
  560. }
  561. } catch (Exception e) {
  562. syncLog.error(appId, msg + ",同步用户信息失败", JSON.toJSONString(formData), e.getMessage());
  563. }
  564. }
  565. return null;
  566. }
  567. }, apps);
  568. return user;
  569. }
  570. private String syncUserToIm(User user, String noEncryPwd, String msg) throws Exception {
  571. String appId = "im";
  572. App app = appService.findOne(appId);
  573. if (!StringUtils.isEmpty(app.getBackUserUrl())) {
  574. String url = app.getBackUserUrl();
  575. HttpUtil.ResponseWrap res = null;
  576. ModelMap formData = new ModelMap();
  577. formData.put("dialectUID", user.getImId());
  578. formData.put("realName", StringUtils.isEmpty(user.getRealName()) ? user.getVipName() : user.getRealName());
  579. formData.put("password", noEncryPwd);
  580. formData.put("email", StringUtils.isEmpty(user.getEmail()) ? "0" : user.getEmail());
  581. formData.put("mobile", user.getMobile());
  582. res = HttpUtil.doPost(url, formData, 10000);
  583. if (!res.isSuccess()) {
  584. syncLog.error(appId, msg + ",同步用户信息失败", formData.toString(), res.getContent());
  585. throw new Exception(res.getContent());
  586. } else {
  587. JSONObject obj = JSON.parseObject(res.getContent());
  588. if (obj.getString("resultMsg") != null) {
  589. syncLog.error(appId, msg + ",同步用户信息失败", formData.toString(), res.getContent());
  590. }
  591. syncLog.info(appId, msg + ",同步用户信息成功", formData.toString(), res.getContent());
  592. return obj.getString("dialectUID");
  593. }
  594. }
  595. return null;
  596. }
  597. }