Commit b78c26c8 authored by 赵啸非's avatar 赵啸非

添加h5钉钉用户授权登录与注册绑定

parent 71bc4695
...@@ -11,4 +11,10 @@ public class DingTalkReq extends BaseReq { ...@@ -11,4 +11,10 @@ public class DingTalkReq extends BaseReq {
*/ */
private String code; private String code;
private String userName;
private String phone;
private String openId;
} }
...@@ -2,6 +2,7 @@ package com.mortals.xhx.busiz.h5.web; ...@@ -2,6 +2,7 @@ package com.mortals.xhx.busiz.h5.web;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.ap.CookieService; import com.mortals.framework.ap.CookieService;
import com.mortals.framework.ap.GlobalSysInfo; import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.ap.SysConstains; import com.mortals.framework.ap.SysConstains;
...@@ -31,6 +32,7 @@ import com.mortals.xhx.module.dept.model.DeptQuery; ...@@ -31,6 +32,7 @@ import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.service.DeptService; import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.dingding.IDingTalkService; import com.mortals.xhx.module.dingding.IDingTalkService;
import com.mortals.xhx.module.dingding.personal.service.IDingPersonService; import com.mortals.xhx.module.dingding.personal.service.IDingPersonService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery; import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService; import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -46,8 +48,7 @@ import java.math.BigDecimal; ...@@ -46,8 +48,7 @@ import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED; import static com.mortals.xhx.common.key.ErrorCode.*;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED_CONTENT;
/** /**
...@@ -80,18 +81,20 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U ...@@ -80,18 +81,20 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U
@Autowired @Autowired
private DeptService deptService; private DeptService deptService;
@Autowired @Autowired
private IDingPersonService dingPersonService; private IDingPersonService dingPersonService;
@RequestMapping("authlogin") @RequestMapping("authlogin")
@UnAuth
public String login(@RequestBody DingTalkReq req) throws Exception { public String login(@RequestBody DingTalkReq req) throws Exception {
JSONObject ret = new JSONObject(); JSONObject ret = new JSONObject();
UserEntity userEntity = null; UserEntity userEntity = null;
try { try {
if (ObjectUtils.isEmpty(req.getCode())) { if (ObjectUtils.isEmpty(req.getCode())) {
throw new AppException("钉钉 code 不能为空!"); throw new AppException("钉钉code不能为空!");
} }
//根据code 查询钉钉用户id //根据code 查询钉钉用户id
Rest<String> personRest = dingPersonService.getPersonByCode(req.getCode()); Rest<String> personRest = dingPersonService.getPersonByCode(req.getCode());
...@@ -102,15 +105,9 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U ...@@ -102,15 +105,9 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U
UserQuery userQuery = new UserQuery(); UserQuery userQuery = new UserQuery();
userQuery.setDingUserId(personRest.getData()); userQuery.setDingUserId(personRest.getData());
userEntity = userService.selectOne(userQuery); userEntity = userService.selectOne(userQuery);
if (ObjectUtils.isEmpty(userEntity)) throw new AppException("不存在当前匹配的钉钉关联用户!"); if (ObjectUtils.isEmpty(userEntity))
throw new AppException(REGISTER_AUTH_FAIL, "不存在当前匹配的钉钉关联用户!");
String currUserName = userEntity.getRealName();
if (currUserName == null || currUserName.trim().length() == 0) {
currUserName = "管理员";
}
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("currUserName", currUserName);
data.put("id", userEntity.getId()); data.put("id", userEntity.getId());
data.put("userType", userEntity.getUserType()); data.put("userType", userEntity.getUserType());
userEntity.setLoginTime(System.currentTimeMillis()); userEntity.setLoginTime(System.currentTimeMillis());
...@@ -136,8 +133,61 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U ...@@ -136,8 +133,61 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U
} }
} }
@RequestMapping("register")
@UnAuth
public String resgister(@RequestBody DingTalkReq req) {
JSONObject ret = new JSONObject();
UserEntity userEntity = null;
try {
if (ObjectUtils.isEmpty(req.getUserName()))
throw new AppException("用户姓名不能为空!");
if (ObjectUtils.isEmpty(req.getPhone()))
throw new AppException("手机号码不能为空!");
if (ObjectUtils.isEmpty(req.getOpenId()))
throw new AppException("openId不能为空!");
StaffEntity staffEntity = staffService.selectOne(new StaffQuery().phoneNumber(req.getPhone()).name(req.getUserName()));
if (ObjectUtils.isEmpty(staffEntity))
throw new AppException(String.format("系统未找到当前用户,姓名:%s,手机:%s,请联系管理员!", req.getUserName(), req.getPhone()));
UserQuery userQuery = new UserQuery();
userQuery.setCustomerId(staffEntity.getId());
userEntity = userService.selectOne(userQuery);
if (ObjectUtils.isEmpty(userEntity))
throw new AppException(String.format("员工未与系统用户绑定,staffId:%s", staffEntity.getId()));
userEntity.setDingUserId(req.getOpenId());
JSONObject data = new JSONObject();
data.put("id", userEntity.getId());
data.put("userType", userEntity.getUserType());
userEntity.setLoginTime(System.currentTimeMillis());
userEntity.setToken(IdUtil.fastSimpleUUID());
userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
String token = authTokenService.createToken(userEntity);
data.put("token", token);
ret.put(KEY_RESULT_DATA, data);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
ret.put(KEY_RESULT_MSG, "钉钉用户登录系统成功!");
recordSysLog(request, userEntity, "钉钉用户登录系统成功!");
return ret.toJSONString();
} catch (AppException e) {
log.error("dinding register error ", e);
ret.put(KEY_RESULT_CODE, e.getCode());
ret.put(KEY_RESULT_MSG, super.convertException(e));
return ret.toJSONString();
} catch (Exception e) {
log.error("系统异常 error ", e);
ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
ret.put(KEY_RESULT_MSG, super.convertException(e));
return ret.toJSONString();
}
}
@RequestMapping("logout") @RequestMapping("logout")
public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception { public void logout(HttpServletRequest request, HttpServletResponse response) {
recordSysLog(request, "退出登录"); recordSysLog(request, "退出登录");
super.removeCurrUser(request); super.removeCurrUser(request);
this.deleteBlackCookie(request, response); this.deleteBlackCookie(request, response);
...@@ -151,13 +201,4 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U ...@@ -151,13 +201,4 @@ public class DingTalkLoginController extends BaseCRUDJsonBodyMappingController<U
} }
@RequestMapping("parseToken")
public IUser parseToken() throws Exception {
IUser userEntity = authTokenService.getLoginUser(request);
if (!ObjectUtils.isEmpty(userEntity)) {
authTokenService.verifyToken(userEntity);
return userEntity;
}
return null;
}
} }
...@@ -112,6 +112,10 @@ public interface ErrorCode { ...@@ -112,6 +112,10 @@ public interface ErrorCode {
public static final String TOKEN_AUTH_FAIL_CONTENT = "token认证失败!"; public static final String TOKEN_AUTH_FAIL_CONTENT = "token认证失败!";
public static final int REGISTER_AUTH_FAIL = 1011;
public static final String REGISTER_AUTH_FAIL_CONTENT = "token认证失败!";
public static final int ERROR_TOKEN_EXPIRED = 9001; public static final int ERROR_TOKEN_EXPIRED = 9001;
public static final String ERROR_TOKEN_EXPIRED_CONTENT = "用户登录过期,请重新登录!"; public static final String ERROR_TOKEN_EXPIRED_CONTENT = "用户登录过期,请重新登录!";
public static final int ERROR_TOKEN_UNAUTHORIZED = 9002; public static final int ERROR_TOKEN_UNAUTHORIZED = 9002;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment