|
@@ -1,9 +1,11 @@
|
|
|
package com.qs.mp.framework.web.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.qs.mp.channel.domain.Channel;
|
|
|
import com.qs.mp.channel.service.IChannelService;
|
|
|
import com.qs.mp.common.constant.Constants;
|
|
|
import com.qs.mp.common.core.redis.RedisCache;
|
|
|
+import com.qs.mp.common.enums.ChannelRoleEnum;
|
|
|
import com.qs.mp.common.enums.UserIdentityEnum;
|
|
|
import com.qs.mp.common.exception.ServiceException;
|
|
|
import com.qs.mp.common.exception.user.CaptchaException;
|
|
@@ -22,6 +24,8 @@ import com.qs.mp.system.domain.SysUser;
|
|
|
import com.qs.mp.system.service.ISysConfigService;
|
|
|
import com.qs.mp.system.service.ISysUserService;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import javax.annotation.Resource;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -30,6 +34,7 @@ import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
@@ -61,6 +66,9 @@ public class SysLoginService
|
|
|
@Autowired
|
|
|
private IChannelService channelService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserDetailsService userDetailsService;
|
|
|
+
|
|
|
/**
|
|
|
* 登录验证
|
|
|
*
|
|
@@ -71,7 +79,7 @@ public class SysLoginService
|
|
|
* @param identity 唯一标识
|
|
|
* @return 结果
|
|
|
*/
|
|
|
- public String login(String username, String password, String code, String uuid, int identity)
|
|
|
+ public Map<String, String> login(String username, String password, String code, String uuid, int identity)
|
|
|
{
|
|
|
// 用户验证
|
|
|
Authentication authentication = null;
|
|
@@ -97,84 +105,35 @@ public class SysLoginService
|
|
|
}
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
+ return buildUserToken(identity, loginUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> buildUserToken(int identity, LoginUser loginUser) {
|
|
|
recordLoginInfo(loginUser.getUser());
|
|
|
+ HashMap<String, String> result = new HashMap<>();
|
|
|
|
|
|
loginUser.setIdentity(UserIdentityEnum.valueOf(identity));
|
|
|
- if (!loginUser.getUsername().equals("admin") && UserIdentityEnum.isChannel(loginUser.getIdentity())) {
|
|
|
- fillChannelInfo(loginUser);
|
|
|
+ if (!loginUser.getUsername().equals("admin") && UserIdentityEnum.isChannel(
|
|
|
+ loginUser.getIdentity().ordinal())) {
|
|
|
+ ChannelRoleEnum roleEnum = fillChannelInfo(loginUser);
|
|
|
+ result.put(Constants.CHANNEL_ROLE, roleEnum.getValue());
|
|
|
}
|
|
|
-/*
|
|
|
- // 根据用户身份,设置对应的属性
|
|
|
- loginUser.setIdentity(UserIdentityEnum.valueOf(identity));
|
|
|
|
|
|
- if(loginUser.isCustomer()){
|
|
|
- fillCustomerInfo(loginUser);
|
|
|
- }*/
|
|
|
-// if(loginUser.isMerchant()){
|
|
|
-// fillMerchantInfoByStaffs(loginUser);
|
|
|
-// }
|
|
|
// 生成token
|
|
|
- return tokenService.createToken(loginUser);
|
|
|
+ String token = tokenService.createToken(loginUser);
|
|
|
+ result.put(Constants.TOKEN, token);
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 登录验证
|
|
|
- *
|
|
|
- * @param username 用户名
|
|
|
- * @param code 验证码
|
|
|
- * @param uuid 唯一标识
|
|
|
- * @param identity 唯一标识
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- public String loginBySms(String username, String code, String uuid, int identity)
|
|
|
+ public Map<String, String> wxAuthLogin(String username, int identity)
|
|
|
{
|
|
|
- // 校验短信验证码
|
|
|
- validateSMSCaptcha(username, code, uuid);
|
|
|
-
|
|
|
- // 用户验证
|
|
|
- Authentication authentication = null;
|
|
|
- try
|
|
|
- {
|
|
|
- // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
- authentication = authenticationManager
|
|
|
- .authenticate(new UsernamePasswordAuthenticationToken(username, code));
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- if (e instanceof BadCredentialsException)
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
- throw new UserPasswordNotMatchException();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
- throw new ServiceException(e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- recordLoginInfo(loginUser.getUser());
|
|
|
-
|
|
|
- loginUser.setIdentity(UserIdentityEnum.valueOf(identity));
|
|
|
- if (!loginUser.getUsername().equals("admin") && UserIdentityEnum.isChannel(loginUser.getIdentity())) {
|
|
|
- fillChannelInfo(loginUser);
|
|
|
- }
|
|
|
-/*
|
|
|
- // 根据用户身份,设置对应的属性
|
|
|
- loginUser.setIdentity(UserIdentityEnum.valueOf(identity));
|
|
|
-
|
|
|
- if(loginUser.isCustomer()){
|
|
|
- fillCustomerInfo(loginUser);
|
|
|
- }*/
|
|
|
-// if(loginUser.isMerchant()){
|
|
|
-// fillMerchantInfoByStaffs(loginUser);
|
|
|
-// }
|
|
|
- // 生成token
|
|
|
- return tokenService.createToken(loginUser);
|
|
|
+ LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username);
|
|
|
+ return buildUserToken(identity, loginUser);
|
|
|
}
|
|
|
|
|
|
- public String login(SmsCode smsCode)
|
|
|
+ public Map<String, String> login(SmsCode smsCode)
|
|
|
{
|
|
|
// 用户验证
|
|
|
Authentication authentication = null;
|
|
@@ -201,29 +160,7 @@ public class SysLoginService
|
|
|
}
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(smsCode.getMobile(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
- recordLoginInfo(loginUser.getUser());
|
|
|
-
|
|
|
- loginUser.setIdentity(UserIdentityEnum.valueOf(smsCode.getIdentity()));
|
|
|
- if (!loginUser.getUsername().equals("admin") && UserIdentityEnum.isChannel(loginUser.getIdentity())) {
|
|
|
- fillChannelInfo(loginUser);
|
|
|
- }
|
|
|
-/*
|
|
|
- // 根据用户身份,设置对应的属性
|
|
|
- loginUser.setIdentity(UserIdentityEnum.valueOf(smsCode.getIdentity()));
|
|
|
- logger.info("login user info :"+ JSONObject.toJSONString(loginUser));
|
|
|
-
|
|
|
- if(loginUser.isCustomer()){
|
|
|
- fillCustomerInfo(loginUser);
|
|
|
- }*/
|
|
|
-// if(loginUser.isMerchant()){
|
|
|
-// fillMerchantInfoByStaffs(loginUser);
|
|
|
-// }
|
|
|
-
|
|
|
- logger.info("prepare create token 。。。");
|
|
|
- String token = tokenService.createToken(loginUser);
|
|
|
- logger.info("token:"+token);
|
|
|
- // 生成token
|
|
|
- return token;
|
|
|
+ return buildUserToken(smsCode.getIdentity(), loginUser);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -251,31 +188,6 @@ public class SysLoginService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 校验验证码
|
|
|
- *
|
|
|
- * @param username 用户名
|
|
|
- * @param code 短信验证码
|
|
|
- * @param uuid 唯一标识
|
|
|
- * @return 结果
|
|
|
- */
|
|
|
- public void validateSMSCaptcha(String username, String code, String uuid)
|
|
|
- {
|
|
|
- String verifyKey = Constants.SMS_CAPTCHA_CODE_KEY + uuid;
|
|
|
- String captcha = redisCache.getCacheObject(verifyKey);
|
|
|
- redisCache.deleteObject(verifyKey);
|
|
|
- if (captcha == null)
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
|
|
|
- throw new CaptchaExpireException();
|
|
|
- }
|
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
|
- {
|
|
|
- AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
|
|
|
- throw new CaptchaException();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 记录登录信息
|
|
|
*/
|
|
@@ -290,21 +202,19 @@ public class SysLoginService
|
|
|
* 填充注入Channel 渠道/经销商 信息
|
|
|
* @param loginUser
|
|
|
*/
|
|
|
- public void fillChannelInfo(LoginUser loginUser){
|
|
|
+ public ChannelRoleEnum fillChannelInfo(LoginUser loginUser){
|
|
|
Channel channel = channelService.getChannelByUserId(loginUser.getUserId());
|
|
|
if (null != channel && null != channel.getChannelId()){
|
|
|
- // 校验渠道身份标识
|
|
|
- if (loginUser.getIdentity() == UserIdentityEnum.CHANNEL && channel.getLevel() <= 0) {
|
|
|
- throw new ServiceException("此账号的渠道不存在");
|
|
|
- } else if (loginUser.getIdentity() == UserIdentityEnum.SALESITE && channel.getLevel() != 0) {
|
|
|
- throw new ServiceException("此账号的经销商不存在");
|
|
|
- }
|
|
|
loginUser.setChannelId(channel.getChannelId());
|
|
|
loginUser.setChannelNo(channel.getChannelNo());
|
|
|
+ if (channel.getLevel() == 0) {
|
|
|
+ return ChannelRoleEnum.SALESITE;
|
|
|
+ } else {
|
|
|
+ return ChannelRoleEnum.CHANNEL;
|
|
|
+ }
|
|
|
} else {
|
|
|
throw new ServiceException("登录账号不存在");
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
|