|
@@ -2,9 +2,10 @@ package com.caimei365.user.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.caimei365.user.components.WeChatService;
|
|
|
-import com.caimei365.user.mapper.BaseUserDao;
|
|
|
+import com.caimei365.user.mapper.LoginMapper;
|
|
|
import com.caimei365.user.model.BaseUser;
|
|
|
import com.caimei365.user.model.JsonModel;
|
|
|
+import com.caimei365.user.model.UserLoginVo;
|
|
|
import com.caimei365.user.service.LoginService;
|
|
|
import com.caimei365.user.components.RedisService;
|
|
|
import com.caimei365.user.utils.JwtUtil;
|
|
@@ -16,6 +17,7 @@ import org.springframework.web.server.ServerWebExchange;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* Description
|
|
@@ -32,7 +34,7 @@ public class LoginServiceImpl implements LoginService {
|
|
|
@Resource
|
|
|
private WeChatService weChatService;
|
|
|
@Resource
|
|
|
- private BaseUserDao baseUserDao;
|
|
|
+ private LoginMapper loginMapper;
|
|
|
|
|
|
/**
|
|
|
* 登录(用户名,密码)
|
|
@@ -42,7 +44,7 @@ public class LoginServiceImpl implements LoginService {
|
|
|
* @return BaseUser
|
|
|
*/
|
|
|
@Override
|
|
|
- public JsonModel<BaseUser> passwordLogin(String mobileOrEmail, String password) {
|
|
|
+ public JsonModel<UserLoginVo> passwordLogin(String mobileOrEmail, String password) {
|
|
|
if (mobileOrEmail.isEmpty()) {
|
|
|
return JsonModel.error("请填写账户名", null);
|
|
|
}
|
|
@@ -50,7 +52,7 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return JsonModel.error("请填写密码", null);
|
|
|
}
|
|
|
//处理比对密码
|
|
|
- BaseUser baseUser = baseUserDao.getBaseUserByMobileOrEmail(mobileOrEmail);
|
|
|
+ UserLoginVo baseUser = loginMapper.getLoginUserByMobileOrEmail(mobileOrEmail);
|
|
|
if (baseUser != null) {
|
|
|
String md5Password = Md5Util.md5(password);
|
|
|
String dbPassword = baseUser.getPassword();
|
|
@@ -67,33 +69,6 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return JsonModel.error("输入的密码和账户名不匹配", null);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 登录校验
|
|
|
- * @param baseUser 用户信息
|
|
|
- * @return BaseUser
|
|
|
- */
|
|
|
- private JsonModel<BaseUser> logonVerify(BaseUser baseUser) {
|
|
|
- // 生成token给用户
|
|
|
- String token = JwtUtil.createToken(baseUser.getUserId());
|
|
|
- // 为了过期续签,将token存入redis,并设置超时时间
|
|
|
- redisService.set(token, token, JwtUtil.getExpireTime());
|
|
|
- baseUser.setToken(token);
|
|
|
- if (baseUser.getStatus() == 91) {
|
|
|
- //机构 / 供应商
|
|
|
- return JsonModel.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
|
|
|
- }
|
|
|
- // 供应商
|
|
|
- if (baseUser.getUserIdentity() == 3) {
|
|
|
- if (baseUser.getStatus() == 3) {
|
|
|
- return JsonModel.error(-2, "您的企业账号正在加速审核中,审核通过后即可登录", null);
|
|
|
- }
|
|
|
- if (baseUser.getStatus() == 92) {
|
|
|
- return JsonModel.error(-3, "您的企业账号审核未通过", baseUser);
|
|
|
- }
|
|
|
- }
|
|
|
- return JsonModel.success("登录成功", baseUser);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 微信授权登录(小程序)
|
|
|
*
|
|
@@ -103,12 +78,12 @@ public class LoginServiceImpl implements LoginService {
|
|
|
* @param serverWebExchange ServerWebExchange
|
|
|
*/
|
|
|
@Override
|
|
|
- public JsonModel<BaseUser> appletsAuthorization(String code, String encryptedData, String iv, ServerWebExchange serverWebExchange) {
|
|
|
+ public JsonModel<UserLoginVo> appletsAuthorization(String code, String encryptedData, String iv, ServerWebExchange serverWebExchange) {
|
|
|
if (StringUtils.isBlank(code)) {
|
|
|
return JsonModel.error("没有获取到微信授权code", null);
|
|
|
}
|
|
|
// 小程序微信授权获取登录信息
|
|
|
- JsonModel<Map<String, Object>> appletsInfo = weChatService.getAppletsInfo(code, serverWebExchange);
|
|
|
+ JsonModel<Map<String, Object>> appletsInfo = weChatService.getInfoMapByApplets(code, serverWebExchange);
|
|
|
if (appletsInfo.getCode() == -1) {
|
|
|
return JsonModel.error(appletsInfo.getMsg(), null);
|
|
|
}
|
|
@@ -129,12 +104,13 @@ public class LoginServiceImpl implements LoginService {
|
|
|
e.printStackTrace();
|
|
|
return JsonModel.error("微信解密失败", null);
|
|
|
}
|
|
|
- redisService.setMap("wxInfo:" + unionId, infoData);
|
|
|
- log.info("通过wxInfo:unionId键保存到redis,返回unionId给前端>>>>>>>>" + unionId);
|
|
|
+ // 用户数据存入Redis,key前缀:wxInfo:applets:
|
|
|
+ redisService.setMap("wxInfo:applets:" + unionId, infoData);
|
|
|
+ log.info("小程序授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:applets:" + unionId);
|
|
|
// 协销授权登录
|
|
|
- BaseUser seller = baseUserDao.getServiceProviderByOpenId(openId);
|
|
|
+ UserLoginVo seller = loginMapper.getServiceProviderByOpenId(openId);
|
|
|
if (null != seller) {
|
|
|
- baseUserDao.updateServiceProviderUnionId(seller.getUserId(), unionId);
|
|
|
+ loginMapper.updateServiceProviderUnionId(seller.getUserId(), unionId);
|
|
|
String token = JwtUtil.createToken(seller.getUserId());
|
|
|
seller.setToken(token);
|
|
|
seller.setUnionId(unionId);
|
|
@@ -142,29 +118,131 @@ public class LoginServiceImpl implements LoginService {
|
|
|
return JsonModel.success(seller);
|
|
|
}
|
|
|
// 运营人员授权登录
|
|
|
- BaseUser operation = baseUserDao.getOperationUserByUnionId(unionId);
|
|
|
+ return operationAuthLogin(openId, unionId, "mini");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信公众号授权链接(www)
|
|
|
+ *
|
|
|
+ * @param redirectUri 用于微信授权的中间页面
|
|
|
+ * @param mode 授权方式:1静默授权,其他手动同意授权
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public JsonModel<String> getAuthorizationLink(String redirectUri, Integer mode) {
|
|
|
+ String link = weChatService.getAuthorizationLink(redirectUri, mode);
|
|
|
+ String state = UUID.randomUUID().toString();
|
|
|
+ redisService.set(state, state, 1800L);
|
|
|
+ link = link.replace("STATE", state);
|
|
|
+ return JsonModel.success(link);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信公众号授权登录(www)
|
|
|
+ * <p>
|
|
|
+ * spi旧接口:user/authorizationLogin
|
|
|
+ *
|
|
|
+ * @param code 微信code
|
|
|
+ * @param state 安全认证
|
|
|
+ * @param mode 1:静默授权,2:用户手动授权
|
|
|
+ * @param serverWebExchange ServerWebExchange
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public JsonModel<UserLoginVo> websiteAuthorization(String code, String state, Integer mode, ServerWebExchange serverWebExchange) {
|
|
|
+ if (StringUtils.isBlank(code) || StringUtils.isBlank(state)) {
|
|
|
+ return JsonModel.error("参数异常", null);
|
|
|
+ }
|
|
|
+ String wxState = (String) redisService.get(state);
|
|
|
+ log.info("微信code>>>" + code + "state>>>" + wxState + "----" + state + "mode>>>" + mode);
|
|
|
+ if (wxState.equals(state)) {
|
|
|
+ try {
|
|
|
+ // 通过code获取微信用户信息
|
|
|
+ Map<String, Object> map = weChatService.getInfoMapByWeb(code, "crm");
|
|
|
+ String openId = (String) map.get("openid");
|
|
|
+ if (mode == 1) {
|
|
|
+ // 静默授权
|
|
|
+ Integer userId = loginMapper.getUserIdByOpenId(openId, "www");
|
|
|
+ if (userId > 0) {
|
|
|
+ UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
|
|
|
+ // 返回登录用户
|
|
|
+ return logonVerify(user);
|
|
|
+ } else {
|
|
|
+ return JsonModel.error(-4, "您的微信尚未绑定任何机构", null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 获取access_token
|
|
|
+ String accessToken = weChatService.getAccessToken();
|
|
|
+ // 获取微信用户信息
|
|
|
+ Map<String, Object> infoData = weChatService.getUserInfo(accessToken, openId);
|
|
|
+ String unionId = (String) infoData.get("unionid");
|
|
|
+ // 用户数据存入Redis,key前缀:wxInfo:website:
|
|
|
+ redisService.setMap("wxInfo:website:" + unionId, infoData);
|
|
|
+ log.info("移动端授权登录,返回unionId给前端,用户数据存入Redis,key:wxInfo:website:" + unionId);
|
|
|
+ // 运营人员授权登录
|
|
|
+ return operationAuthLogin(openId, unionId, "www");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return JsonModel.error("获取微信信息异常", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return JsonModel.error("请从正确的途径打开链接", null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 运营人员授权登录
|
|
|
+ * @param openId 微信openId
|
|
|
+ * @param unionId 微信unionId
|
|
|
+ * @param source 来源:www网站, mini小程序
|
|
|
+ * @return BaseUser
|
|
|
+ */
|
|
|
+ private JsonModel<UserLoginVo> operationAuthLogin(String openId, String unionId, String source) {
|
|
|
+ UserLoginVo operation = loginMapper.getOperationUserByUnionId(unionId, source);
|
|
|
if (null == operation) {
|
|
|
- operation = baseUserDao.getOperationUserByOpenId(openId, "mini");
|
|
|
- if (null == operation){
|
|
|
- operation = new BaseUser();
|
|
|
+ operation = loginMapper.getOperationUserByOpenId(openId, source);
|
|
|
+ if (null == operation) {
|
|
|
+ operation = new UserLoginVo();
|
|
|
operation.setOpenId(openId);
|
|
|
operation.setUnionId(unionId);
|
|
|
- return JsonModel.error("运营人员不存在,请登录", operation);
|
|
|
+ return JsonModel.error("您的微信尚未绑定任何机构", operation);
|
|
|
} else {
|
|
|
// 表示 openId存在, unionId不存在
|
|
|
- baseUserDao.updateOperationUnionId(operation.getUserId(), unionId);
|
|
|
+ loginMapper.updateOperationUnionId(operation.getUserId(), unionId);
|
|
|
}
|
|
|
}
|
|
|
// 如果unionId存在, openId不存在
|
|
|
if (StringUtils.isEmpty(operation.getOpenId()) || StringUtils.isBlank(operation.getOpenId())) {
|
|
|
- baseUserDao.updateOperationOpenId(operation.getUserId(), openId);
|
|
|
+ loginMapper.updateOperationOpenId(operation.getUserId(), openId);
|
|
|
}
|
|
|
operation.setOpenId(openId);
|
|
|
operation.setUnionId(unionId);
|
|
|
// 返回登录校验结果
|
|
|
return logonVerify(operation);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 登录校验
|
|
|
+ * @param baseUser 用户信息
|
|
|
+ * @return BaseUser
|
|
|
+ */
|
|
|
+ private JsonModel<UserLoginVo> logonVerify(UserLoginVo baseUser) {
|
|
|
+ // 生成token给用户
|
|
|
+ String token = JwtUtil.createToken(baseUser.getUserId());
|
|
|
+ // 为了过期续签,将token存入redis,并设置超时时间
|
|
|
+ redisService.set(token, token, JwtUtil.getExpireTime());
|
|
|
+ baseUser.setToken(token);
|
|
|
+ if (baseUser.getStatus() == 91) {
|
|
|
+ //机构 / 供应商
|
|
|
+ return JsonModel.error(-2, "您的企业账号已被冻结,请联系客服处理", null);
|
|
|
+ }
|
|
|
+ // 供应商
|
|
|
+ if (baseUser.getUserIdentity() == 3) {
|
|
|
+ if (baseUser.getStatus() == 3) {
|
|
|
+ return JsonModel.error(-2, "您的企业账号正在加速审核中,审核通过后即可登录", null);
|
|
|
+ }
|
|
|
+ if (baseUser.getStatus() == 92) {
|
|
|
+ return JsonModel.error(-3, "您的企业账号审核未通过", baseUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return JsonModel.success("登录成功", baseUser);
|
|
|
+ }
|
|
|
|
|
|
}
|