AppletLoginController.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.boman.wechat.controller;
  2. import com.boman.common.core.enums.UserStatus;
  3. import com.boman.common.core.exception.BaseException;
  4. import com.boman.common.security.service.TokenService;
  5. import com.boman.domain.AppletLoginForm;
  6. import com.boman.domain.SysUser;
  7. import com.boman.domain.constant.Constants;
  8. import com.boman.domain.dto.AjaxResult;
  9. import com.boman.domain.dto.AppletSessionDTO;
  10. import com.boman.system.api.RemoteLogService;
  11. import com.boman.system.api.RemoteUserService;
  12. import com.boman.system.api.model.LoginUser;
  13. import com.boman.wechat.utils.WxCodeSessionUtil;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.annotation.Resource;
  19. import java.util.Map;
  20. /**
  21. * @author shiqian
  22. * @date 2021年09月08日 17:29
  23. **/
  24. @RestController
  25. @RequestMapping("applet")
  26. public class AppletLoginController {
  27. @Resource
  28. private WxCodeSessionUtil codeUtil;
  29. @Resource
  30. private RemoteUserService remoteUserService;
  31. @Resource
  32. private RemoteLogService remoteLogService;
  33. @Resource
  34. private TokenService tokenService;
  35. @PostMapping("/login")
  36. public AjaxResult getPhone(@RequestBody AppletLoginForm form) {
  37. AppletSessionDTO dto = codeUtil.jscode2Session(form);
  38. String phoneNumber = dto.getPhoneNumber();
  39. SysUser user = remoteUserService.getByPhone(phoneNumber);
  40. String userName = user.getUserName();
  41. if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  42. remoteLogService.saveLogininfor(userName, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  43. throw new BaseException("对不起,您的账号:" + userName + " 已被删除");
  44. }
  45. if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  46. remoteLogService.saveLogininfor(userName, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  47. throw new BaseException("对不起,您的账号:" + userName + " 已停用");
  48. }
  49. remoteLogService.saveLogininfor(userName, Constants.LOGIN_SUCCESS, "登录成功");
  50. LoginUser loginUser = remoteUserService.packInfo(user);
  51. Map<String, Object> map = tokenService.createToken(loginUser);
  52. return AjaxResult.success(map);
  53. }
  54. }