|
|
@@ -0,0 +1,133 @@
|
|
|
+package com.zhongshu.card.server.core.service.org;
|
|
|
+
|
|
|
+import com.github.microservice.auth.client.model.LoginTokenModel;
|
|
|
+import com.github.microservice.auth.client.model.UserAuthLoginModel;
|
|
|
+import com.github.microservice.auth.client.service.UserService;
|
|
|
+import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
+import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
|
|
|
+import com.github.microservice.core.util.net.IPUtil;
|
|
|
+import com.zhongshu.card.client.model.org.LoginParam;
|
|
|
+import com.zhongshu.card.client.ret.ResultContent;
|
|
|
+import com.zhongshu.card.client.type.OrganizationState;
|
|
|
+import com.zhongshu.card.client.type.UserState;
|
|
|
+import com.zhongshu.card.server.core.dao.org.OrganizationUserDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserCountDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserLoginFailRecordDao;
|
|
|
+import com.zhongshu.card.server.core.dao.org.UserLoginRecordDao;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserLoginFailRecord;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserLoginRecord;
|
|
|
+import com.zhongshu.card.server.core.service.base.RedisService;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.ObjectUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author TRX
|
|
|
+ * @date 2024/6/4
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IndexService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserCountDao userCountDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthHelper authHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HttpServletRequest request;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisService redisService;
|
|
|
+
|
|
|
+ @Value("${oAuth.clientId}")
|
|
|
+ private String clientId;
|
|
|
+
|
|
|
+ @Value("${oAuth.clientSecret}")
|
|
|
+ private String clientSecret;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserLoginRecordDao userLoginRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserLoginFailRecordDao userLoginFailRecordDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ DBHelper dbHelper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ OrganizationUserDao organizationUserDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录--web
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent login(LoginParam param) {
|
|
|
+ String phone = param.getLoginValue();
|
|
|
+
|
|
|
+ UserAuthLoginModel userAuthLoginModel = new UserAuthLoginModel();
|
|
|
+ BeanUtils.copyProperties(param, userAuthLoginModel);
|
|
|
+ userAuthLoginModel.setDeviceIp(IPUtil.getRemoteIp(request));
|
|
|
+ userAuthLoginModel.setClientId(clientId);
|
|
|
+ userAuthLoginModel.setClientSecret(clientSecret);
|
|
|
+ userAuthLoginModel.setDeviceUserAgent(request.getHeader("user-agent"));
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<LoginTokenModel> resultContent = userService.login(userAuthLoginModel);
|
|
|
+ if (resultContent.getState() == com.github.microservice.auth.client.content.ResultState.Success) {
|
|
|
+ UserAccount userAccount = userCountDao.findTopByLoginName(phone);
|
|
|
+ if (ObjectUtils.isEmpty(userAccount)) {
|
|
|
+ return ResultContent.buildFail("账号或密码不正确");
|
|
|
+ }
|
|
|
+ // 判断用户状态
|
|
|
+ if (userAccount.getState() == UserState.Frozen) {
|
|
|
+ return ResultContent.buildFail("该账号已被冻结,不能登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (userAccount.getIsDelete() != null && userAccount.getIsDelete()) {
|
|
|
+ return ResultContent.buildFail("用户已被注销,登录失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ long orgNumber = organizationUserDao.countByUserAndState(userAccount, OrganizationState.Normal);
|
|
|
+ if (orgNumber <= 0) {
|
|
|
+ return ResultContent.buildFail("用户未加入任何机构,不能登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录已登录过
|
|
|
+ userAccount.setIsLogined(Boolean.TRUE);
|
|
|
+ userCountDao.save(userAccount);
|
|
|
+
|
|
|
+ UserLoginRecord record = new UserLoginRecord();
|
|
|
+ record.setPhone(userAccount.getPhone());
|
|
|
+ record.setUid(userAccount.getUserId());
|
|
|
+ record.setUserName(userAccount.getName());
|
|
|
+ record.setTTL(new Date(this.dbHelper.getTime() + 90 * 24L * 60 * 60 * 1000L));
|
|
|
+ userLoginRecordDao.save(record);
|
|
|
+
|
|
|
+ // 删除失败记录
|
|
|
+ userLoginFailRecordDao.deleteByUserName(param.getLoginValue());
|
|
|
+ return ResultContent.buildSuccess(resultContent.getContent());
|
|
|
+ }
|
|
|
+ // 记录登录失败信息
|
|
|
+ userLoginFailRecordDao.save(UserLoginFailRecord.builder().userName(param.getLoginValue())
|
|
|
+ .ttl(new Date(dbHelper.getTime() + 2 * 24L * 60 * 60 * 1000L)).build());
|
|
|
+ String msg = resultContent.getMsg();
|
|
|
+ if (StringUtils.isEmpty(msg)) {
|
|
|
+ msg = "账号或密码不正确.";
|
|
|
+ }
|
|
|
+ return ResultContent.buildFail(msg);
|
|
|
+ }
|
|
|
+}
|