UserManagerController.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package com.uas.sso.controller;
  2. import com.uas.account.exception.AccountException;
  3. import com.uas.sso.core.Const;
  4. import com.uas.sso.entity.*;
  5. import com.uas.sso.service.ApplyUserSpaceService;
  6. import com.uas.sso.service.UserService;
  7. import com.uas.sso.service.UserspaceService;
  8. import org.apache.commons.codec.binary.Base64;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.ui.ModelMap;
  11. import org.springframework.util.StringUtils;
  12. import org.springframework.web.bind.annotation.*;
  13. import java.io.*;
  14. import java.text.SimpleDateFormat;
  15. import java.util.*;
  16. /**
  17. * 用户信息管理controller
  18. *
  19. * @author wangmh
  20. * @date 2018/1/2
  21. */
  22. @RestController
  23. @RequestMapping("/api/user")
  24. public class UserManagerController extends BaseController {
  25. @Autowired
  26. private UserService userService;
  27. @Autowired
  28. private UserspaceService userspaceService;
  29. @Autowired
  30. private ApplyUserSpaceService applyUserSpaceService;
  31. /**
  32. * 用户信息新增、修改
  33. *
  34. * @param userView
  35. * @return
  36. */
  37. @RequestMapping(method = RequestMethod.POST)
  38. @ResponseBody
  39. public ModelMap apiSave(@RequestBody User userView, String appId) {
  40. if (userView == null || appId == null) {
  41. throw new AccountException("参数错误");
  42. }
  43. User user = new User();
  44. if (userView.getUserUU() != null) {
  45. user = userService.findOne(userView.getUserUU());
  46. }
  47. if (user == null && userView.getMobile() != null) {
  48. user = userService.findByMobile(userView.getMobile());
  49. }
  50. if (user == null) {
  51. user = userService.register(userView, appId);
  52. } else {
  53. user = userService.updateUser(userView.getUserUU(), userView);
  54. }
  55. return success(user);
  56. }
  57. /**
  58. * 校验手机号是否被注册
  59. *
  60. * @param mobile 手机号
  61. * @return
  62. */
  63. @RequestMapping(value = "/checkMobile", method = RequestMethod.GET)
  64. public ModelMap checkMobile(String mobile) {
  65. return new ModelMap("hasRegister", userService.mobileHasRegistered(mobile));
  66. }
  67. /**
  68. * 校验手机号是否被注册
  69. *
  70. * @param email 手机号
  71. * @return
  72. */
  73. @RequestMapping(value = "/checkEmail", method = RequestMethod.GET)
  74. public ModelMap checkEmail(String email) {
  75. return new ModelMap("hasRegister", userService.emailHasRegistered(email));
  76. }
  77. /**
  78. * 校验真实姓名是否被认证
  79. *
  80. * @param realName 真实姓名
  81. * @return
  82. * @author wangmh
  83. * @date 2018/1/11 15:05
  84. */
  85. @RequestMapping(value = "/realName/valid", method = RequestMethod.GET)
  86. public ModelMap realNameIsValid(String realName) {
  87. return success(new ModelMap("isValid", userService.realNameIsValid(realName)));
  88. }
  89. /**
  90. * 校验身份证号是否被认证
  91. *
  92. * @param idCard 身份证号
  93. * @return {"isValid", b} b为true说明已认证
  94. * @author wangmh
  95. * @date 2018/1/11 15:06
  96. */
  97. @RequestMapping(value = "/idCard/valid", method = RequestMethod.GET)
  98. public ModelMap idCardIsValid(String idCard) {
  99. return success(new ModelMap("isValid", userService.idCardIsValid(idCard)));
  100. }
  101. /**
  102. * 根据企业uu号查询企业下所有用户信息
  103. *
  104. * @param page 当前页数
  105. * @param size 每页大小
  106. * @param spaceUU 企业uu号
  107. * @return org.springframework.data.domain.Page, content 为用户信息集合
  108. * @author wangmh
  109. * @date 2018/1/26 14:43
  110. */
  111. @RequestMapping(value = "/find/member", method = RequestMethod.GET)
  112. public ModelMap findMember(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int size, Long spaceUU) {
  113. return success(userService.findMemberBySpaceUU(page, size, spaceUU));
  114. }
  115. /**
  116. * 根据用户uu号查询用户信息
  117. *
  118. * @param userUU 用户uu号
  119. * @return 用户信息
  120. * @author wangmh
  121. * @date 2018/1/26 14:46
  122. */
  123. @RequestMapping(value = "/info", method = RequestMethod.GET)
  124. public ModelMap findOne(@RequestParam Long userUU) {
  125. return success(userService.findOne(userUU));
  126. }
  127. /**
  128. * 根据用户手机号查询用户信息
  129. *
  130. * @param mobile 用户手机号
  131. * @return 用户信息
  132. * @author wangmh
  133. * @date 2018/1/26 14:46
  134. */
  135. @RequestMapping(value = "/info/mobile", method = RequestMethod.GET)
  136. public ModelMap findByMobile(@RequestParam String mobile) {
  137. return success(userService.findByMobile(mobile));
  138. }
  139. /**
  140. * 用户申请绑定企业
  141. *
  142. * @param userUU 用户uu号
  143. * @param spaceUU 企业uu号
  144. * @return 用户信息
  145. * @author wangmh
  146. * @date 2018/1/31 18:22
  147. */
  148. @RequestMapping(value = "/apply/bind", method = RequestMethod.POST)
  149. public ModelMap bindUserSpace(@RequestParam Long userUU, Long spaceUU) {
  150. applyUserSpaceService.applyBindUserSpace(userUU, spaceUU);
  151. User user = userService.findOne(userUU);
  152. Userspace userspace = userspaceService.findOne(spaceUU);
  153. // 发送邮箱通知管理员
  154. SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
  155. ModelMap data = new ModelMap();
  156. data.put("username", user.getVipName());
  157. data.put("time", format.format(new Date()));
  158. data.put("enName", userspace.getSpaceName());
  159. sendEmail("templateForSendMailWhenApplyUserSpace", userspace.getAdmin().getEmail(), data);
  160. // 发送短信通知管理员
  161. sendSms("templateForSendSmsWhenApplyUserSpace", userspace.getAdmin().getMobile(), user.getVipName(), format.format(new Date()), userspace.getSpaceName());
  162. return success();
  163. }
  164. /**
  165. * 根据用户uu号查询分页查询申请信息
  166. *
  167. * @param userUU 用户uu号
  168. * @param page 当前页,默认值为1
  169. * @param size 每页大小,默认值为20
  170. * @return org.springframework.data.domain.Page,content为ApplyUserSpace集合
  171. */
  172. @RequestMapping(value = "/apply/info", method = RequestMethod.GET)
  173. public ModelMap findApplyInfo(Long userUU, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int size) {
  174. return success(applyUserSpaceService.findApplyByUserUU(userUU, page, size));
  175. }
  176. /**
  177. * 用户解除绑定企业
  178. *
  179. * @param userUU 用户uu号
  180. * @param spaceUU 企业uu号
  181. * @return
  182. */
  183. @RequestMapping(params = "_operate=unbind", method = RequestMethod.POST)
  184. public ModelMap unbindUserSpace(Long userUU, Long spaceUU) {
  185. userService.unbindUserspace(userUU, spaceUU);
  186. return success();
  187. }
  188. /**
  189. * 企业添加已注册用户
  190. *
  191. * @param appId 应用id
  192. * @param userUU 用户uu号
  193. * @param spaceUU 企业uu号
  194. * @return
  195. */
  196. @RequestMapping(params = "_operate=bind", method = RequestMethod.POST)
  197. public ModelMap addUser(String appId, Long userUU, Long spaceUU) {
  198. userService.bindUserspace(appId, userUU, spaceUU);
  199. return success();
  200. }
  201. /**
  202. * 企业添加未注册用户
  203. *
  204. * @param appId 应用id
  205. * @param user 用户信息
  206. * @param spaceUU 企业uu号
  207. * @return
  208. */
  209. @RequestMapping(params = "_operate=add", method = RequestMethod.POST)
  210. public ModelMap addUser(String appId, User user, Long spaceUU) {
  211. // 参数判断
  212. if (user == null || user.getUserUU() != null || spaceUU == null) {
  213. return error("参数错误");
  214. }
  215. // 根据手机号判断用户是否为新用户,新用户注册,旧用户绑定
  216. User oldUser = userService.findByMobile(user.getMobile());
  217. if (oldUser == null) {
  218. // 注册新用户,无密码则使用默认密码
  219. if (StringUtils.isEmpty(user.getPassword())) {
  220. user.setPassword("111111");
  221. }
  222. user = userService.register(user, appId);
  223. } else {
  224. user = oldUser;
  225. }
  226. // 绑定企业
  227. userService.bindUserspace(appId, user.getUserUU(), spaceUU);
  228. return success(user);
  229. }
  230. /**
  231. * 接口调用验证密码登录
  232. * <p>
  233. * <pre>
  234. * 这个比较特殊,指定应用可能密码为空,需要借用其他应用的密码来校验
  235. * </pre>
  236. *
  237. * @return
  238. */
  239. @RequestMapping(params = "_operate=fuzzyCheck")
  240. public ModelMap apiFuzzyLogin(User user) {
  241. if (null == user || null == user.getPassword()) {
  242. return error("参数错误");
  243. }
  244. if (!StringUtils.isEmpty(user.getMobile())) {
  245. User userInfo = userService.findByMobile(user.getMobile());
  246. if (userInfo == null) {
  247. return error("未找到用户");
  248. }
  249. // 允许应用在调用该接口前,已经将用户输入的明文加密为密文
  250. String encryPwd = user.getPassword().length() >= 32 ? user.getPassword() : userService.getEncryPassword(
  251. Const.ENCRY_FORMAT, user.getPassword(), userInfo.getSalt());
  252. if (encryPwd.equals(userInfo.getPassword())) {
  253. return success();
  254. }
  255. return error("密码错误");
  256. }
  257. return error("请填写手机号");
  258. }
  259. /**
  260. * erp修改用户信息 如果type为password则修改密码,为mobile则修改手机号,为email则修改邮箱
  261. *
  262. * @param type 修改类型
  263. * @param userUU 用户uu号
  264. * @param spaceUU 企业uu号
  265. * @param password 密码
  266. * @param data 其他数据
  267. * @return
  268. */
  269. @RequestMapping(value = "/update/{type}/{userUU}/{spaceUU}", method = RequestMethod.POST)
  270. public ModelMap updatePassword(@PathVariable String type, @PathVariable Long userUU, @PathVariable Long spaceUU, String password, String data) {
  271. // 获取企业密钥
  272. Userspace userspace = userspaceService.findOne(spaceUU);
  273. // 校验请求
  274. checkRequest(userspace.getAccessSecret());
  275. // 修改密码
  276. switch (type) {
  277. case "password":
  278. userService.updatePassword(userUU, password);
  279. break;
  280. case "mobile":
  281. userService.updateMobile(userUU, data);
  282. break;
  283. case "email":
  284. userService.updateEmail(userUU, data);
  285. break;
  286. default:
  287. return error("404", "请求错误");
  288. }
  289. return success();
  290. }
  291. /**
  292. * 众创商机导入注册
  293. *
  294. * @param user 用户信息(主要是用户名,手机号,邮箱和密码,密码base64加密)
  295. * @return
  296. */
  297. @RequestMapping(value = "/sysUserdata/uuzc", method = RequestMethod.POST)
  298. public ModelMap uuzcRegister(User user, @RequestParam String appId) {
  299. // 校验手机号
  300. checkMobile(user.getMobile(), null);
  301. // 用户名不能为空
  302. if (StringUtils.isEmpty(user.getVipName())) {
  303. return error("用户名不能为空");
  304. }
  305. // 获取密码
  306. try {
  307. String pwd = new String(Base64.decodeBase64(user.getPassword().getBytes("utf-8")), "utf-8");
  308. user.setPassword(pwd);
  309. } catch (UnsupportedEncodingException e) {
  310. e.printStackTrace();
  311. }
  312. // 传来的值可能带有uu号,去除uu号
  313. user.setUserUU(null);
  314. user = userService.register(user, appId);
  315. return success(user.getUserUU());
  316. }
  317. /**
  318. * erp根据token获取用户信息
  319. *
  320. * @param token tokenId
  321. * @return
  322. */
  323. @RequestMapping(value = "/getUserByToken", method = RequestMethod.GET)
  324. public ModelMap findUserByToken(String token) {
  325. Token existToken = tokenService.findOne(token);
  326. if (existToken == null) {
  327. return error("验证过期");
  328. }
  329. if (!(existToken.getBind() instanceof ModelMap)) {
  330. return error("验证信息错误");
  331. }
  332. ModelMap data = (ModelMap) existToken.getBind();
  333. User user = userService.findOne((Long) data.get("userUU"));
  334. return success(user);
  335. }
  336. /**
  337. * 根据手机号获取所在企业信息
  338. *
  339. * @param mobile 用户手机号
  340. * @return
  341. */
  342. @RequestMapping(value = "/getSpace", method = RequestMethod.GET)
  343. @Deprecated
  344. public ModelMap getSpace(String mobile) {
  345. User user = userService.findByMobile(mobile);
  346. Set<Userspace> userSpaces = user.getUserSpaces();
  347. ModelMap data = new ModelMap(user);
  348. data.put("spaces", userSpaces);
  349. return data;
  350. }
  351. @RequestMapping(value = "/getToken", method = RequestMethod.GET)
  352. @ResponseBody
  353. public ModelMap getToken(Long userUU, Long spaceUU) {
  354. ModelMap data = new ModelMap();
  355. data.put("userUU", userUU);
  356. data.put("spaceUU", spaceUU);
  357. Token token = new Token(data);
  358. tokenService.save(token);
  359. return success(token.getId());
  360. }
  361. @RequestMapping("/getTokenData")
  362. public ModelMap getTokenData(String token) {
  363. return success(tokenService.findOne(token).getBind());
  364. }
  365. /**
  366. * (消息)根据企业uu号和用户uu号获取用户和企业信息
  367. *
  368. * @param userUU 用户uu号
  369. * @param spaceUU 企业uu号
  370. * @return
  371. */
  372. @RequestMapping("/info/userUU")
  373. public ModelMap getUserInfo(Long userUU, Long spaceUU) {
  374. User user = userService.findOne(userUU);
  375. if (user == null) {
  376. return error("用户不存在");
  377. }
  378. Userspace userspace = userspaceService.findOne(spaceUU);
  379. if (userspace == null) {
  380. return error("企业不存在");
  381. }
  382. if (!user.getUserSpaces().contains(userspace)) {
  383. return error("用户(" + userUU + ")不属于企业(" + spaceUU + ")");
  384. }
  385. ModelMap data = new ModelMap();
  386. data.addAttribute("mobile", user.getMobile());
  387. data.addAttribute("imId", user.getImId());
  388. data.addAttribute("vipName", user.getVipName());
  389. data.addAttribute("email", user.getEmail());
  390. data.addAttribute("spaceName", userspace.getSpaceName());
  391. return success(data);
  392. }
  393. /**
  394. * 优软云个人用户注册总数
  395. */
  396. @RequestMapping(value = "/count", method = RequestMethod.GET)
  397. public ModelMap count() {
  398. return new ModelMap("count", userService.count());
  399. }
  400. /**
  401. * 获取本月用户注册数量
  402. *
  403. * @return
  404. */
  405. @RequestMapping(value = "/currentMonth/count", method = RequestMethod.GET)
  406. public ModelMap countInCurrentMonth() {
  407. return new ModelMap("count", userService.countInCurrentMonth());
  408. }
  409. /**
  410. * 获取上个月用户注册数量
  411. *
  412. * @return
  413. */
  414. @RequestMapping(value = "/lastMonth/count", method = RequestMethod.GET)
  415. public ModelMap countInLastMonth() {
  416. return new ModelMap("count", userService.countInLastMonth());
  417. }
  418. /**
  419. * 获取本周用户注册数量
  420. *
  421. * @return
  422. */
  423. @RequestMapping(value = "/currentWeek/count", method = RequestMethod.GET)
  424. public ModelMap countInCurrentWeek() {
  425. return new ModelMap("count", userService.countInCurrentWeek());
  426. }
  427. /**
  428. * 获取指定应用注册数量
  429. *
  430. * @param fromApps 应用id,逗号分隔
  431. * @return
  432. */
  433. @RequestMapping(value = "/count/app", method = RequestMethod.GET)
  434. public ModelMap count(String fromApps) {
  435. String[] apps = fromApps.split(",");
  436. return success(userService.count(Arrays.asList(apps)));
  437. }
  438. /**
  439. * 获取指定应用本月注册数量
  440. *
  441. * @param fromApps 应用id,逗号分隔
  442. * @return
  443. */
  444. @RequestMapping(value = "/currentMonth/count/app", method = RequestMethod.GET)
  445. public ModelMap countInCurrentMonth(String fromApps) {
  446. String[] apps = fromApps.split(",");
  447. return success(userService.countInCurrentMonth(new ArrayList<>(Arrays.asList(apps))));
  448. }
  449. /**
  450. * 获取指定应用本周注册数量
  451. *
  452. * @param fromApps 应用id,逗号分隔
  453. * @return
  454. */
  455. @RequestMapping(value = "/currentWeek/count/app", method = RequestMethod.GET)
  456. public ModelMap countInCurrentWeek(String fromApps) {
  457. String[] apps = fromApps.split(",");
  458. return success(userService.countInCurrentWeek(new ArrayList<>(Arrays.asList(apps))));
  459. }
  460. /**
  461. * 获取指定应用当天注册数量
  462. *
  463. * @param fromApps 应用id,逗号分隔
  464. * @return
  465. */
  466. @RequestMapping(value = "/today/count/app", method = RequestMethod.GET)
  467. public ModelMap countInToday(String fromApps) {
  468. String[] apps = fromApps.split(",");
  469. return success(userService.countInToday(new ArrayList<>(Arrays.asList(apps))));
  470. }
  471. /**
  472. * 获取指定应用前一天注册数量
  473. * @param fromApps 应用id,逗号分隔
  474. *
  475. * @return
  476. */
  477. @RequestMapping(value = "/yesterday/count/app", method = RequestMethod.GET)
  478. public ModelMap countInYesterday(String fromApps) {
  479. String[] apps = fromApps.split(",");
  480. return success(userService.countInYesterday(new ArrayList<>(Arrays.asList(apps))));
  481. }
  482. /**
  483. * 获取指定应用上一个月注册数量
  484. *
  485. * @param fromApps 应用id,逗号分隔
  486. * @return
  487. */
  488. @RequestMapping(value = "/lastMonth/count/app", method = RequestMethod.GET)
  489. public ModelMap countInLastMonth(String fromApps) {
  490. String[] apps = fromApps.split(",");
  491. return success(userService.countgInLastMonth(new ArrayList<>(Arrays.asList(apps))));
  492. }
  493. /**
  494. * 根据用户IMID获取女用户信息
  495. *
  496. * @param imId 用户的IMID
  497. * @return 用户信息
  498. */
  499. @GetMapping(params = "_operate=getUserByImId")
  500. public ModelMap getUserByImId(String imId) {
  501. return success(userService.findByImId(imId));
  502. }
  503. }