CustomController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. package com.crm.custom.controller;
  2. import com.crm.custom.service.CustomInfoService;
  3. import com.crm.login.rely.backend.controller.BaseLoginController;
  4. import com.crm.rely.backend.core.constant.*;
  5. import com.crm.rely.backend.core.dto.base.BaseResultDto;
  6. import com.crm.rely.backend.model.dto.custom.InfoDto;
  7. import com.crm.rely.backend.model.dto.custom.login.CustomInfoAmountDto;
  8. import com.crm.rely.backend.model.dto.user.info.UserInfoDto;
  9. import com.crm.rely.backend.model.entity.custom.info.*;
  10. import com.crm.rely.backend.model.pojo.table.CustomInfoTable;
  11. import com.crm.rely.backend.service.EmailService;
  12. import com.crm.rely.backend.service.SysCountryService;
  13. import com.crm.rely.backend.util.GetIpAndMac;
  14. import com.crm.rely.backend.util.MD5Util;
  15. import org.springframework.beans.BeanUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.validation.annotation.Validated;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import org.springframework.web.servlet.LocaleResolver;
  23. import org.springframework.web.servlet.support.RequestContextUtils;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.math.BigDecimal;
  27. import java.util.Date;
  28. import java.util.Locale;
  29. /**
  30. * @author: houn
  31. */
  32. @RestController
  33. @RequestMapping("/custom")
  34. public class CustomController extends BaseLoginController {
  35. @Autowired
  36. private CustomInfoService customInfoService;
  37. @Autowired
  38. private EmailService emailService;
  39. @Autowired
  40. private SysCountryService sysCountryService;
  41. /**
  42. * 注册用户 发送短信验证码
  43. *
  44. * @param entity
  45. * @return
  46. * @throws Exception
  47. */
  48. @PostMapping("/register/send/code")
  49. public BaseResultDto registerSendCode(@RequestBody @Validated CustomInfoRegisterSendEmailCodeEntity entity, HttpServletRequest req) throws Exception {
  50. entity.setIp(GetIpAndMac.getIp(req));
  51. entity.setTime(new Date());
  52. customInfoService.registerSendEmailCode(entity);
  53. return BaseResultDto.success(Constants.SEND_SUCCESS);
  54. }
  55. /**
  56. * 注册用户
  57. *
  58. * @param entity
  59. * @return
  60. * @throws Exception
  61. */
  62. @PostMapping("/register")
  63. public BaseResultDto add(@RequestBody @Validated CustomInfoRegisterEntity entity) throws Exception {
  64. entity.setEmail(entity.getEmail().toLowerCase());
  65. //密码
  66. entity.setPassword(MD5Util.getMD5(entity.getPassword()));
  67. entity.setValid(1);
  68. entity.setStatus(StatusConstants.PASSED_STATUS);
  69. //entity.setPosition(CustomConstants.POSITION1);
  70. customInfoService.add(entity);
  71. return BaseResultDto.success(Constants.REGISTER_SUCCESS);
  72. }
  73. @PostMapping("/apply/real")
  74. public BaseResultDto applyReal(CustomInfoEntity infoEntity, HttpServletRequest httpServletRequest) throws Exception {
  75. Runnable runnable = () -> {
  76. CustomApplyRealEntity customApplyRealEntity = new CustomApplyRealEntity();
  77. customApplyRealEntity.setCustomId(infoEntity.getId());
  78. customApplyRealEntity.setApplyRealTime(new Date());
  79. CustomInfoTable customInfoTable = customInfoService.applyReal(customApplyRealEntity);
  80. infoEntity.setApplyRealStatus(customInfoTable.getApplyRealStatus());
  81. infoEntity.setStatus(customInfoTable.getStatus());
  82. infoEntity.setApplyRealTime(customInfoTable.getApplyRealTime());
  83. try {
  84. super.saveLoginInfo(infoEntity);
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. };
  89. redisService.tryLock(infoEntity, httpServletRequest, runnable, "no_repeat_submit_error");
  90. return BaseResultDto.success();
  91. }
  92. /**
  93. * 修改邮件发送验证码
  94. *
  95. * @param entity
  96. * @param req
  97. * @return
  98. * @throws Exception
  99. */
  100. @PostMapping("/update/email/send/code")
  101. public BaseResultDto registerSendCode(@RequestBody @Validated CustomInfoUpdateEmailSendEmailCodeEntity entity, HttpServletRequest req, CustomInfoEntity infoEntity) throws Exception {
  102. if (!entity.getOldEmail().equals(infoEntity.getEmail())) {
  103. return BaseResultDto.error(Constants.LOGIN_EMAIL_ERROR);
  104. }
  105. entity.setCountry(infoEntity.getCountry());
  106. entity.setIp(GetIpAndMac.getIp(req));
  107. entity.setTime(new Date());
  108. customInfoService.updateEmailSendEmailCode(entity);
  109. return BaseResultDto.success(Constants.SEND_SUCCESS);
  110. }
  111. @PostMapping("/update/email")
  112. public BaseResultDto updateEmail(@RequestBody @Validated CustomInfoUpdateEmailEntity entity, HttpServletRequest req, CustomInfoEntity infoEntity) throws Exception {
  113. entity.setEmail(entity.getEmail().toLowerCase());
  114. entity.setOldEmail(entity.getOldEmail().toLowerCase());
  115. if (!entity.getOldEmail().equals(infoEntity.getEmail())) {
  116. return BaseResultDto.error(Constants.LOGIN_EMAIL_ERROR);
  117. }
  118. entity.setCustomId(infoEntity.getId());
  119. entity.setModifyIp(GetIpAndMac.getIp(req));
  120. entity.setModifyTime(new Date());
  121. entity.setModifyUser(infoEntity.getId());
  122. customInfoService.updateEmail(entity);
  123. //更新缓存上的信息
  124. infoEntity.setEmail(entity.getEmail());
  125. if (infoEntity.getIbInfo() != null) {
  126. infoEntity.getIbInfo().setEmail(entity.getEmail());
  127. }
  128. super.saveLoginInfo(infoEntity);
  129. return BaseResultDto.success(Constants.REGISTER_SUCCESS);
  130. }
  131. /**
  132. * 登录信息
  133. *
  134. * @param entity
  135. * @return
  136. * @throws Exception
  137. */
  138. @PostMapping("/login")
  139. public BaseResultDto login(@RequestBody @Validated CustomInfoLoginEntity entity, HttpServletRequest request, HttpServletResponse response) throws Exception {
  140. entity.setLoginName(entity.getLoginName().toLowerCase());
  141. //密码处理为
  142. entity.setPassword(MD5Util.getMD5(entity.getPassword()));
  143. /**
  144. * 处理登录时间 登录ip 登录地区
  145. */
  146. entity.setIp(sysCountryService.getAdderByIp(request));
  147. entity.setAddress(entity.getIp());
  148. entity.setTime(new Date());
  149. CustomInfoEntity infoEntity = customInfoService.login(entity);
  150. infoEntity.setPrefix(PrefixEnum.PREFIX_CUSTOM);
  151. String accessToken = super.login(infoEntity, entity.getSource());
  152. setLocaleResolver(infoEntity.getLang(), request, response);
  153. return BaseResultDto.success(Constants.LOGIN_SUCCESS, accessToken);
  154. }
  155. @PostMapping("/get/language")
  156. public BaseResultDto getLanguage(HttpServletRequest request) throws Exception {
  157. String language = request.getHeader("accept-language");
  158. return BaseResultDto.success(language);
  159. }
  160. /**
  161. * 退出登录
  162. *
  163. * @param httpServletRequest
  164. * @throws Exception
  165. */
  166. @Override
  167. @PostMapping("/logout")
  168. public BaseResultDto logout(HttpServletRequest httpServletRequest) throws Exception {
  169. return super.logout(httpServletRequest);
  170. }
  171. /**
  172. * 获取登录信息
  173. *
  174. * @param entity
  175. * @return
  176. * @throws Exception
  177. */
  178. @PostMapping("/info")
  179. public BaseResultDto info(CustomInfoEntity entity) throws Exception {
  180. InfoDto infoDto = new InfoDto();
  181. BeanUtils.copyProperties(entity, infoDto);
  182. if (entity.getIbInfo() != null) {
  183. UserInfoDto ibInfoDto = new UserInfoDto();
  184. BeanUtils.copyProperties(entity.getIbInfo(), ibInfoDto);
  185. infoDto.setIbInfo(ibInfoDto);
  186. } else {
  187. infoDto.setIbInfo(null);
  188. }
  189. //infoDto.setPosition(customInfoService.getPosition(entity.getId()));
  190. return BaseResultDto.success(infoDto);
  191. }
  192. @PostMapping("/get/amount")
  193. public BaseResultDto getAmount(CustomInfoEntity entity) throws Exception {
  194. CustomInfoAmountDto customInfoAmountDto = new CustomInfoAmountDto();
  195. CustomInfoTable customInfoTable = customInfoService.getById(entity.getId());
  196. BeanUtils.copyProperties(customInfoTable, customInfoAmountDto);
  197. return BaseResultDto.success(customInfoAmountDto);
  198. }
  199. /**
  200. * 修改用户信息,
  201. *
  202. * @param entity
  203. * @return
  204. * @throws Exception
  205. */
  206. @PostMapping("/update/info")
  207. public BaseResultDto update(@RequestBody @Validated CustomInfoUpdateEntity entity, HttpServletRequest req, CustomInfoEntity infoEntity) throws Exception {
  208. entity.setId(infoEntity.getId());
  209. entity.setModifyIp(sysCountryService.getAdderByIp(req));
  210. entity.setModifyTime(new Date());
  211. entity.setValid(1);
  212. customInfoService.update(entity);
  213. infoEntity.setValid(entity.getValid());
  214. //更新redis数据
  215. BeanUtils.copyProperties(entity, infoEntity);
  216. saveLoginInfo(infoEntity);
  217. return BaseResultDto.success();
  218. }
  219. @PostMapping("/manage/update/info")
  220. @FeignClientAnnotation
  221. public BaseResultDto manageUpdate(@RequestBody @Validated CustomInfoUpdateEntity entity, HttpServletRequest req) throws Exception {
  222. entity.setModifyIp(sysCountryService.getAdderByIp(req));
  223. entity.setModifyTime(new Date());
  224. entity.setValid(1);
  225. customInfoService.update(entity);
  226. CustomInfoEntity customInfoEntity =
  227. redisService.getEntity(PrefixEnum.PREFIX_CUSTOM.getCode() + entity.getId(), CustomInfoEntity.class);
  228. if (customInfoEntity != null) {
  229. BeanUtils.copyProperties(entity, customInfoEntity);
  230. saveLoginInfo(customInfoEntity);
  231. }
  232. return BaseResultDto.success();
  233. }
  234. /**
  235. * 登录修改密码
  236. *
  237. * @param entity
  238. * @param req
  239. * @param infoEntity
  240. * @return
  241. */
  242. @PostMapping("/update/login/password")
  243. public BaseResultDto updatePassword(@RequestBody @Validated CustomUpdateLoginPasswordEntity entity, HttpServletRequest req, CustomInfoEntity infoEntity) throws Exception {
  244. entity.setId(infoEntity.getId());
  245. if (entity.getNewPassword().equals(entity.getOldPassword())) {
  246. return BaseResultDto.error(Constants.PASSWORD_NEW_AND_OLD_IDENTICAL_ERROR);
  247. }
  248. entity.setModifyIp(sysCountryService.getAdderByIp(GetIpAndMac.getIp(req)));
  249. entity.setModifyTime(new Date());
  250. entity.setModifyUser(infoEntity.getId());
  251. entity.setNewPassword(MD5Util.getMD5(entity.getNewPassword()));
  252. entity.setOldPassword(MD5Util.getMD5(entity.getOldPassword()));
  253. customInfoService.updatePassword(entity);
  254. return BaseResultDto.success(Constants.UPDATE_SUCCESS);
  255. }
  256. /**
  257. * 未登录修改密码发送邮件
  258. *
  259. * @param entity
  260. * @param req
  261. * @return
  262. */
  263. @PostMapping("/update/password/send/email")
  264. public BaseResultDto updatePasswordSendEmail(@RequestBody @Validated CustomUpdatePasswordSendEmailEntity entity, HttpServletRequest req) throws Exception {
  265. String ip = GetIpAndMac.getIp(req);
  266. emailService.validate(ip, EmailSendEnum.FORGET_PASSWORD.getCode(), ip);
  267. entity.setIp(ip);
  268. entity.setTime(new Date());
  269. customInfoService.updatePasswordSendEmail(entity);
  270. return BaseResultDto.success(Constants.SEND_SUCCESS);
  271. }
  272. /**
  273. * 未登录根据邮件修改密码
  274. *
  275. * @param entity
  276. * @param req
  277. * @return
  278. */
  279. @PostMapping("/update/email/password")
  280. public BaseResultDto updatePassword(@RequestBody @Validated CustomUpdateEmailPasswordEntity entity, HttpServletRequest req) throws Exception {
  281. entity.setModifyIp(GetIpAndMac.getIp(req));
  282. entity.setModifyTime(new Date());
  283. entity.setPassword(MD5Util.getMD5(entity.getPassword()));
  284. customInfoService.updatePassword(entity);
  285. return BaseResultDto.success(Constants.SUCCESS);
  286. }
  287. @PostMapping("/manage/update/agent")
  288. @FeignClientAnnotation
  289. public BaseResultDto manageUpdateAgentId(@RequestBody @Validated CustomInfoUpdateAgentEntity entity) throws Exception {
  290. customInfoService.updateAgentId(entity);
  291. return BaseResultDto.success();
  292. }
  293. private void setLocaleResolver(String lang, HttpServletRequest request, HttpServletResponse response) {
  294. LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
  295. try {
  296. if ("cn".equals(lang)) {
  297. localeResolver.setLocale(request, response, new Locale("zh", "CN"));
  298. } else {
  299. localeResolver.setLocale(request, response, new Locale("en", "US"));
  300. }
  301. } catch (Exception e) {
  302. e.printStackTrace();
  303. }
  304. }
  305. @RequestMapping("/get/balance")
  306. public BaseResultDto balance(CustomInfoEntity infoEntity) throws Exception {
  307. BigDecimal balance = customInfoService.getBalance(infoEntity.getId());
  308. return BaseResultDto.success(balance);
  309. }
  310. }