|
@@ -1,13 +1,29 @@
|
|
package org.jeecg.modules.system.controller;
|
|
package org.jeecg.modules.system.controller;
|
|
|
|
|
|
|
|
+import cn.com.ctop.common.module.entity.CorpFeishuUser;
|
|
|
|
+import cn.com.ctop.common.module.entity.CtopCorpWexinUser;
|
|
import cn.com.ctop.common.module.entity.SysUser;
|
|
import cn.com.ctop.common.module.entity.SysUser;
|
|
|
|
+import cn.com.ctop.common.module.service.ICorpFeishuUserService;
|
|
|
|
+import cn.com.ctop.common.module.service.ICtopCorpWexinUserService;
|
|
import cn.com.ctop.common.module.utils.AesEncryptUtil;
|
|
import cn.com.ctop.common.module.utils.AesEncryptUtil;
|
|
|
|
+import cn.com.ctop.common.module.utils.ResultMapUtils;
|
|
|
|
+import cn.com.ctop.common.module.utils.StatusCode;
|
|
|
|
+import cn.com.feishu.provider.AuthProvider;
|
|
import cn.hutool.core.util.RandomUtil;
|
|
import cn.hutool.core.util.RandomUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import lombok.val;
|
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
|
+import me.chanjar.weixin.cp.api.WxCpOAuth2Service;
|
|
|
|
+import me.chanjar.weixin.cp.api.WxCpUserService;
|
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpUserServiceImpl;
|
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
|
|
|
|
+import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
|
|
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.apache.shiro.SecurityUtils;
|
|
import org.jeecg.common.api.vo.Result;
|
|
import org.jeecg.common.api.vo.Result;
|
|
import org.jeecg.common.constant.CacheConstant;
|
|
import org.jeecg.common.constant.CacheConstant;
|
|
@@ -30,6 +46,7 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -44,7 +61,7 @@ public class LoginController {
|
|
@Autowired
|
|
@Autowired
|
|
private ISysUserService sysUserService;
|
|
private ISysUserService sysUserService;
|
|
@Autowired
|
|
@Autowired
|
|
- private ISysBaseApi sysBaseAPI;
|
|
|
|
|
|
+ private ISysBaseApi sysBaseApi;
|
|
@Autowired
|
|
@Autowired
|
|
private ISysLogService logService;
|
|
private ISysLogService logService;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -53,8 +70,148 @@ public class LoginController {
|
|
private ISysDepartService sysDepartService;
|
|
private ISysDepartService sysDepartService;
|
|
@Autowired
|
|
@Autowired
|
|
private ISysDictService sysDictService;
|
|
private ISysDictService sysDictService;
|
|
-
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICorpFeishuUserService corpFeishuUserService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICtopCorpWexinUserService ctopCorpWexinUserService;
|
|
private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";
|
|
private static final String BASE_CHECK_CODES = "qwertyuiplkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM1234567890";
|
|
|
|
+ @GetMapping("feishu/url")
|
|
|
|
+ public Map<String,Object> getUrl(){
|
|
|
|
+ Map<String,Object>result = new HashMap<>();
|
|
|
|
+ try {
|
|
|
|
+ String url = AuthProvider.getAuthUrl("");
|
|
|
|
+ result.put("url",url);
|
|
|
|
+ ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
|
|
|
|
+ return result;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ ResultMapUtils.setResultMap(result, StatusCode.COMMON_SERVER_ERROR);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ @GetMapping("/feishu/auth")
|
|
|
|
+ public Result<JSONObject> feishuAuth(HttpServletRequest request,
|
|
|
|
+ HttpServletResponse response,
|
|
|
|
+ @RequestParam(name = "code",defaultValue = "code") String code,
|
|
|
|
+ @RequestParam(name = "state",defaultValue = "_t") String state) throws IOException {
|
|
|
|
+ Result<JSONObject> result = new Result<>();
|
|
|
|
+ //1:根据code值获取用户飞书信息
|
|
|
|
+ try {
|
|
|
|
+ if(code.trim().equals("code")){
|
|
|
|
+ JSONObject setData = new JSONObject();
|
|
|
|
+ String url = AuthProvider.getAuthUrl("");
|
|
|
|
+ setData.put("url",url);
|
|
|
|
+ setData.put("type",1);
|
|
|
|
+ result.setResult(setData);
|
|
|
|
+ result.success("重定向");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ JSONObject data = AuthProvider.getFeishuUserInfo(code);
|
|
|
|
+ String accessToken = data.getJSONObject("data").getString("access_token");
|
|
|
|
+ String openId = data.getJSONObject("data").getString("open_id");
|
|
|
|
+ //根据accessToken
|
|
|
|
+ JSONObject userData = AuthProvider.getUserInfo(accessToken);
|
|
|
|
+ JSONObject dataObject = userData.getJSONObject("data");
|
|
|
|
+ String email = dataObject.getString("email");
|
|
|
|
+ if(null == email||email.trim().equals("")){
|
|
|
|
+ return result.error500(StatusCode.CTOP_FEISHU_EMAIL_NOT_EXIST.getDesc());
|
|
|
|
+ }
|
|
|
|
+ SysUser sysUser = sysUserService.getUserByEmail(email);
|
|
|
|
+ if (sysUser == null) {
|
|
|
|
+ return result.error500(StatusCode.CTOP_FEISHU_ACCOUNT_NOT_EXIST.getDesc());
|
|
|
|
+ }
|
|
|
|
+ if(sysUser.getStatus() != 1){
|
|
|
|
+ return result.error500(StatusCode.CTOP_FEISHU_ACCOUNT_NOT_EXIST.getDesc());
|
|
|
|
+ }
|
|
|
|
+ String departId = sysDepartService.selectIdByOrdCode(sysUser.getOrgCode());
|
|
|
|
+ sysUser.setDepartId(departId);
|
|
|
|
+ Result<JSONObject> getResult = sysUserService.checkUserIsEffective(sysUser);
|
|
|
|
+ if (!getResult.isSuccess()) {
|
|
|
|
+ return result.error500(StatusCode.COMMON_ACCOUNT_ERROR.getDesc());
|
|
|
|
+ }
|
|
|
|
+ //用户登录信息
|
|
|
|
+ userInfo(sysUser, getResult);
|
|
|
|
+ sysBaseApi.addLog("用户名: " + email + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
+ CorpFeishuUser feishuUser = corpFeishuUserService.getByUserId(sysUser.getId());
|
|
|
|
+ if (feishuUser == null) {
|
|
|
|
+ feishuUser = new CorpFeishuUser();
|
|
|
|
+ feishuUser.setOpenId(openId);
|
|
|
|
+ feishuUser.setUserId(sysUser.getId());
|
|
|
|
+ feishuUser.setEmail(sysUser.getEmail());
|
|
|
|
+ feishuUser.setAvatarBig(dataObject.getString("avatar_big"));
|
|
|
|
+ feishuUser.setAvatarMiddle(dataObject.getString("avatar_middle"));
|
|
|
|
+ feishuUser.setAvatarThumb(dataObject.getString("avatar_thumb"));
|
|
|
|
+ feishuUser.setAvatarUrl(dataObject.getString("avatar_url"));
|
|
|
|
+ feishuUser.setName(dataObject.getString("name"));
|
|
|
|
+ feishuUser.setMobile(dataObject.getString("mobile"));
|
|
|
|
+ feishuUser.setCreateTime(new Date());
|
|
|
|
+ feishuUser.setUpdateTime(new Date());
|
|
|
|
+ corpFeishuUserService.save(feishuUser);
|
|
|
|
+ }
|
|
|
|
+ return getResult;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return result.error500(StatusCode.COMMON_SERVER_ERROR.getDesc());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RequestMapping(value = "/wxlogin", method = RequestMethod.POST)
|
|
|
|
+ @ApiOperation("企业微信登录接口")
|
|
|
|
+ public Result<JSONObject> wxlogin(@RequestParam(name = "code") String code) throws Exception {
|
|
|
|
+ Result<JSONObject> result = new Result<>();
|
|
|
|
+ val configStorage = new WxCpDefaultConfigImpl();
|
|
|
|
+ configStorage.setCorpId("ww24b8a47826f5875f");
|
|
|
|
+ configStorage.setAgentId(1000002);
|
|
|
|
+ configStorage.setCorpSecret("MJIlySADGPlgvADnkFQPUpfZD4yV-4WN6066OgNnb0s");
|
|
|
|
+ configStorage.setToken("hcst2019");
|
|
|
|
+ configStorage.setAesKey("hcst2019");
|
|
|
|
+ val service = new WxCpServiceImpl();
|
|
|
|
+
|
|
|
|
+ service.setWxCpConfigStorage(configStorage);
|
|
|
|
+ WxCpOAuth2Service wxCpOAuth2Service = service.getOauth2Service();
|
|
|
|
+ try {
|
|
|
|
+ WxCpOauth2UserInfo userInfo = wxCpOAuth2Service.getUserInfo(1000002, code);
|
|
|
|
+ if (userInfo != null && userInfo.getUserId() != null) {
|
|
|
|
+ WxCpUserService wxCpUserService = new WxCpUserServiceImpl(service);
|
|
|
|
+ WxCpUser user = wxCpUserService.getById(userInfo.getUserId());
|
|
|
|
+ String email = user.getEmail();
|
|
|
|
+ SysUser sysUser = sysUserService.getUserByEmail(email);
|
|
|
|
+ if (sysUser == null) {
|
|
|
|
+ result.error500("该用户未注册系统账户,请联系管理员");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ if(sysUser.getStatus() != 1){
|
|
|
|
+ result.error500("该用户已冻结,请联系管理员");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ String departId = sysDepartService.selectIdByOrdCode(sysUser.getOrgCode());
|
|
|
|
+ sysUser.setDepartId(departId);
|
|
|
|
+ result = sysUserService.checkUserIsEffective(sysUser);
|
|
|
|
+ if (!result.isSuccess()) {
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ //用户登录信息
|
|
|
|
+ userInfo(sysUser, result);
|
|
|
|
+ sysBaseApi.addLog("用户名: " + email + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
+ CtopCorpWexinUser ctopCorpWexinUser = ctopCorpWexinUserService.getById(sysUser.getId());
|
|
|
|
+ if (ctopCorpWexinUser == null) {
|
|
|
|
+ ctopCorpWexinUser = new CtopCorpWexinUser();
|
|
|
|
+ ctopCorpWexinUser.setUserId(sysUser.getId());
|
|
|
|
+ ctopCorpWexinUser.setWexinEmail(user.getEmail());
|
|
|
|
+ ctopCorpWexinUser.setWexinGender(user.getGender().getGenderName());
|
|
|
|
+ ctopCorpWexinUser.setWexinName(user.getName());
|
|
|
|
+ ctopCorpWexinUser.setWexinId(user.getUserId());
|
|
|
|
+ ctopCorpWexinUser.setWexinMobile(user.getMobile());
|
|
|
|
+ ctopCorpWexinUserService.save(ctopCorpWexinUser);
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ } catch (WxErrorException e) {
|
|
|
|
+ result.error500("企业微信授权错误");
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
|
|
// @ApiOperation("登录接口")
|
|
// @ApiOperation("登录接口")
|
|
// @RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
// @RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
@@ -140,7 +297,7 @@ public class LoginController {
|
|
|
|
|
|
//用户登录信息
|
|
//用户登录信息
|
|
userInfo(sysUser, result);
|
|
userInfo(sysUser, result);
|
|
- sysBaseAPI.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
+ sysBaseApi.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -158,9 +315,9 @@ public class LoginController {
|
|
return Result.error("退出登录失败!");
|
|
return Result.error("退出登录失败!");
|
|
}
|
|
}
|
|
String username = JwtUtil.getUsername(token);
|
|
String username = JwtUtil.getUsername(token);
|
|
- LoginUser sysUser = sysBaseAPI.getUserByName(username);
|
|
|
|
|
|
+ LoginUser sysUser = sysBaseApi.getUserByName(username);
|
|
if(sysUser!=null) {
|
|
if(sysUser!=null) {
|
|
- sysBaseAPI.addLog("用户名: "+sysUser.getRealname()+",退出成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
+ sysBaseApi.addLog("用户名: "+sysUser.getRealname()+",退出成功!", CommonConstant.LOG_TYPE_1, null);
|
|
log.info(" 用户名: "+sysUser.getRealname()+",退出成功! ");
|
|
log.info(" 用户名: "+sysUser.getRealname()+",退出成功! ");
|
|
//清空用户登录Token缓存
|
|
//清空用户登录Token缓存
|
|
redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
@@ -287,7 +444,7 @@ public class LoginController {
|
|
SysUser sysUser = sysUserService.getUserByPhone(mobile);
|
|
SysUser sysUser = sysUserService.getUserByPhone(mobile);
|
|
if(sysUser!=null) {
|
|
if(sysUser!=null) {
|
|
result.error500(" 手机号已经注册,请直接登录!");
|
|
result.error500(" 手机号已经注册,请直接登录!");
|
|
- sysBaseAPI.addLog("手机号已经注册,请直接登录!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
+ sysBaseApi.addLog("手机号已经注册,请直接登录!", CommonConstant.LOG_TYPE_1, null);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.REGISTER_TEMPLATE_CODE);
|
|
b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.REGISTER_TEMPLATE_CODE);
|
|
@@ -341,7 +498,7 @@ public class LoginController {
|
|
@ApiOperation("手机号登录接口")
|
|
@ApiOperation("手机号登录接口")
|
|
@PostMapping("/phoneLogin")
|
|
@PostMapping("/phoneLogin")
|
|
public Result<JSONObject> phoneLogin(@RequestBody JSONObject jsonObject) {
|
|
public Result<JSONObject> phoneLogin(@RequestBody JSONObject jsonObject) {
|
|
- Result<JSONObject> result = new Result<JSONObject>();
|
|
|
|
|
|
+ Result<JSONObject> result = new Result<>();
|
|
String phone = jsonObject.getString("mobile");
|
|
String phone = jsonObject.getString("mobile");
|
|
|
|
|
|
//校验用户有效性
|
|
//校验用户有效性
|
|
@@ -360,7 +517,7 @@ public class LoginController {
|
|
//用户信息
|
|
//用户信息
|
|
userInfo(sysUser, result);
|
|
userInfo(sysUser, result);
|
|
//添加日志
|
|
//添加日志
|
|
- sysBaseAPI.addLog("用户名: " + sysUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
+ sysBaseApi.addLog("用户名: " + sysUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
@@ -493,7 +650,7 @@ public class LoginController {
|
|
result.setResult(obj);
|
|
result.setResult(obj);
|
|
result.setSuccess(true);
|
|
result.setSuccess(true);
|
|
result.setCode(200);
|
|
result.setCode(200);
|
|
- sysBaseAPI.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
|
|
|
|
|
|
+ sysBaseApi.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|