use of org.jeecg.modules.system.entity.SysUser in project jeecg-boot by jeecgboot.
the class ThirdLoginController method oauth2LoginCallback.
/**
* 企业微信/钉钉 OAuth2登录回调
*
* @param code
* @param state
* @param response
* @return
*/
@ResponseBody
@GetMapping("/oauth2/{source}/callback")
public String oauth2LoginCallback(@PathVariable("source") String source, // 企业微信返回的code
@RequestParam(value = "code", required = false) String code, // 钉钉返回的code
@RequestParam(value = "authCode", required = false) String authCode, @RequestParam("state") String state, HttpServletResponse response) {
SysUser loginUser;
if (ThirdAppConfig.WECHAT_ENTERPRISE.equalsIgnoreCase(source)) {
log.info("【企业微信】OAuth2登录进入callback:code=" + code + ", state=" + state);
loginUser = thirdAppWechatEnterpriseService.oauth2Login(code);
if (loginUser == null) {
return "登录失败";
}
} else if (ThirdAppConfig.DINGTALK.equalsIgnoreCase(source)) {
log.info("【钉钉】OAuth2登录进入callback:authCode=" + authCode + ", state=" + state);
loginUser = thirdAppDingtalkService.oauth2Login(authCode);
if (loginUser == null) {
return "登录失败";
}
} else {
return "不支持的source";
}
try {
String token = saveToken(loginUser);
state += "/oauth2-app/login?oauth2LoginToken=" + URLEncoder.encode(token, "UTF-8");
state += "&thirdType=" + "wechat_enterprise";
log.info("OAuth2登录重定向地址: " + state);
try {
response.sendRedirect(state);
return "ok";
} catch (IOException e) {
e.printStackTrace();
return "重定向失败";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "解码失败";
}
}
use of org.jeecg.modules.system.entity.SysUser in project jeecg-boot by jeecgboot.
the class LoginController method selectDepart.
/**
* 登陆成功选择用户当前部门
* @param user
* @return
*/
@RequestMapping(value = "/selectDepart", method = RequestMethod.PUT)
public Result<JSONObject> selectDepart(@RequestBody SysUser user) {
Result<JSONObject> result = new Result<JSONObject>();
String username = user.getUsername();
if (oConvertUtils.isEmpty(username)) {
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
username = sysUser.getUsername();
}
String orgCode = user.getOrgCode();
this.sysUserService.updateUserDepart(username, orgCode);
SysUser sysUser = sysUserService.getUserByName(username);
JSONObject obj = new JSONObject();
obj.put("userInfo", sysUser);
result.setResult(obj);
return result;
}
use of org.jeecg.modules.system.entity.SysUser in project jeecg-boot by jeecgboot.
the class LoginController method userInfo.
/**
* 用户信息
*
* @param sysUser
* @param result
* @return
*/
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
String syspassword = sysUser.getPassword();
String username = sysUser.getUsername();
// 获取用户部门信息
JSONObject obj = new JSONObject();
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
obj.put("departs", departs);
if (departs == null || departs.size() == 0) {
obj.put("multi_depart", 0);
} else if (departs.size() == 1) {
sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
obj.put("multi_depart", 1);
} else {
// 查询当前是否有登录部门
// update-begin--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
SysUser sysUserById = sysUserService.getById(sysUser.getId());
if (oConvertUtils.isEmpty(sysUserById.getOrgCode())) {
sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
}
// update-end--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
obj.put("multi_depart", 2);
}
// update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息
String tenantIds = sysUser.getRelTenantIds();
if (oConvertUtils.isNotEmpty(tenantIds)) {
List<Integer> tenantIdList = new ArrayList<>();
for (String id : tenantIds.split(",")) {
tenantIdList.add(Integer.valueOf(id));
}
// 该方法仅查询有效的租户,如果返回0个就说明所有的租户均无效。
List<SysTenant> tenantList = sysTenantService.queryEffectiveTenant(tenantIdList);
if (tenantList.size() == 0) {
result.error500("与该用户关联的租户均已被冻结,无法登录!");
return result;
} else {
obj.put("tenantList", tenantList);
}
}
// update-end--Author:sunjianlei Date:20210802 for:获取用户租户信息
// 生成token
String token = JwtUtil.sign(username, syspassword);
// 设置token缓存有效时间
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
obj.put("token", token);
obj.put("userInfo", sysUser);
obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
result.setResult(obj);
result.success("登录成功");
return result;
}
use of org.jeecg.modules.system.entity.SysUser in project jeecg-boot by jeecgboot.
the class LoginController method getUserInfo.
/**
* 【vue3专用】获取用户信息
*/
@GetMapping("/user/getUserInfo")
public Result<JSONObject> getUserInfo(HttpServletRequest request) {
Result<JSONObject> result = new Result<JSONObject>();
String username = JwtUtil.getUserNameByToken(request);
if (oConvertUtils.isNotEmpty(username)) {
// 根据用户名查询用户信息
SysUser sysUser = sysUserService.getUserByName(username);
// 用户登录信息
Result<JSONObject> resultObj = userInfo(sysUser, result);
JSONObject jsonObject = resultObj.getResult();
JSONObject obj = new JSONObject();
obj.put("userInfo", jsonObject.get("userInfo"));
obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
result.setResult(obj);
result.success("");
}
return result;
}
use of org.jeecg.modules.system.entity.SysUser in project jeecg-boot by jeecgboot.
the class LoginController method sms.
/**
* 短信登录接口
*
* @param jsonObject
* @return
*/
@PostMapping(value = "/sms")
public Result<String> sms(@RequestBody JSONObject jsonObject) {
Result<String> result = new Result<String>();
String mobile = jsonObject.get("mobile").toString();
// 手机号模式 登录模式: "2" 注册模式: "1"
String smsmode = jsonObject.get("smsmode").toString();
log.info(mobile);
if (oConvertUtils.isEmpty(mobile)) {
result.setMessage("手机号不允许为空!");
result.setSuccess(false);
return result;
}
Object object = redisUtil.get(mobile);
if (object != null) {
result.setMessage("验证码10分钟内,仍然有效!");
result.setSuccess(false);
return result;
}
// 随机数
String captcha = RandomUtil.randomNumbers(6);
JSONObject obj = new JSONObject();
obj.put("code", captcha);
try {
boolean b = false;
// 注册模板
if (CommonConstant.SMS_TPL_TYPE_1.equals(smsmode)) {
SysUser sysUser = sysUserService.getUserByPhone(mobile);
if (sysUser != null) {
result.error500(" 手机号已经注册,请直接登录!");
baseCommonService.addLog("手机号已经注册,请直接登录!", CommonConstant.LOG_TYPE_1, null);
return result;
}
b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.REGISTER_TEMPLATE_CODE);
} else {
// 登录模式,校验用户有效性
SysUser sysUser = sysUserService.getUserByPhone(mobile);
result = sysUserService.checkUserIsEffective(sysUser);
if (!result.isSuccess()) {
String message = result.getMessage();
if ("该用户不存在,请注册".equals(message)) {
result.error500("该用户不存在或未绑定手机号");
}
return result;
}
/**
* smsmode 短信模板方式 0 .登录模板、1.注册模板、2.忘记密码模板
*/
if (CommonConstant.SMS_TPL_TYPE_0.equals(smsmode)) {
// 登录模板
b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.LOGIN_TEMPLATE_CODE);
} else if (CommonConstant.SMS_TPL_TYPE_2.equals(smsmode)) {
// 忘记密码模板
b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.FORGET_PASSWORD_TEMPLATE_CODE);
}
}
if (b == false) {
result.setMessage("短信验证码发送失败,请稍后重试");
result.setSuccess(false);
return result;
}
// 验证码10分钟内有效
redisUtil.set(mobile, captcha, 600);
// update-begin--Author:scott Date:20190812 for:issues#391
// result.setResult(captcha);
// update-end--Author:scott Date:20190812 for:issues#391
result.setSuccess(true);
} catch (ClientException e) {
e.printStackTrace();
result.error500(" 短信接口未配置,请联系管理员!");
return result;
}
return result;
}
Aggregations