Browse Source

新增教研分析单点登录

wangliang 3 năm trước cách đây
mục cha
commit
5da3b39941

+ 2 - 0
distributed-print/src/main/java/com/qmth/distributed/print/api/TSAuthController.java

@@ -137,8 +137,10 @@ public class TSAuthController {
         Map<String, Object> map = new HashMap<>();
         map.computeIfAbsent("loginName", v -> "admin");
         map.computeIfAbsent("role", v -> RoleTypeEnum.OFFICE_TEACHER);
+        map.computeIfAbsent("orgName", v -> "本科生院");
         map.computeIfAbsent("realName", v -> "admin");
         map.computeIfAbsent("mobileNumber", v -> "18008659001");
+        map.computeIfAbsent("returnUrl", v -> "https://www.baidu.com");
         String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getSsoLoginApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
         String result = HttpUtil.postJson(dictionaryConfig.reportOpenDomain().getHostUrl() + dictionaryConfig.reportOpenDomain().getSsoLoginApi(), JacksonUtil.parseJson(map), accessToken, timestamp);
         if (Objects.nonNull(result)) {

+ 15 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/entity/SysUser.java

@@ -124,6 +124,21 @@ public class SysUser extends BaseEntity implements Serializable {
         this.historicName = historicName;
     }
 
+    public SysUser() {
+
+    }
+
+    public SysUser(Long schoolId, String loginName, String realName, String mobileNumber, Long orgId) {
+        setId(SystemConstant.getDbUuid());
+        this.schoolId = schoolId;
+        this.loginName = loginName;
+        this.realName = realName;
+        this.mobileNumber = mobileNumber;
+        this.pwdCount = 1;
+        this.password = SystemConstant.DEFAULT_PASSWORD;
+        this.orgId = orgId;
+    }
+
     @Override
     public void insertInfo(Long userId) {
         super.insertInfo(userId);

+ 3 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/enums/AppSourceEnum.java

@@ -13,7 +13,9 @@ public enum AppSourceEnum {
 
     SYSTEM("自身应用系统"),
 
-    WHU_THIRD("武汉大学");
+    WHU_THIRD("武汉大学"),
+
+    PRINT_THIRD("知学知考-分布式印刷");
 
     private String title;
 

+ 94 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/bean/params/LoginThirdParam.java

@@ -0,0 +1,94 @@
+package com.qmth.teachcloud.report.business.bean.params;
+
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import com.qmth.teachcloud.common.enums.RoleTypeEnum;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+import java.util.Optional;
+
+/**
+ * @Description: 课程参数
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/5/27
+ */
+public class LoginThirdParam implements Serializable {
+
+    @ApiModelProperty(value = "登录名")
+    private String loginName;
+
+    @ApiModelProperty(value = "角色枚举")
+    private RoleTypeEnum role;
+
+    @ApiModelProperty(value = "真实名")
+    private String realName;
+
+    @ApiModelProperty(value = "手机号码")
+    private String mobileNumber;
+
+    @ApiModelProperty(value = "学院名称")
+    private String orgName;
+
+    @ApiModelProperty(value = "返回url")
+    private String returnUrl;
+
+    /**
+     * 参数校验
+     */
+    public void validParams() {
+        Optional.ofNullable(this.getLoginName()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("登录名为空"));
+        Optional.ofNullable(this.getRole()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("角色为空"));
+        Optional.ofNullable(this.getReturnUrl()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("返回url为空"));
+        Optional.ofNullable(this.getOrgName()).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学院名称为空"));
+    }
+
+    public String getOrgName() {
+        return orgName;
+    }
+
+    public void setOrgName(String orgName) {
+        this.orgName = orgName;
+    }
+
+    public String getLoginName() {
+        return loginName;
+    }
+
+    public void setLoginName(String loginName) {
+        this.loginName = loginName;
+    }
+
+    public RoleTypeEnum getRole() {
+        return role;
+    }
+
+    public void setRole(RoleTypeEnum role) {
+        this.role = role;
+    }
+
+    public String getRealName() {
+        return realName;
+    }
+
+    public void setRealName(String realName) {
+        this.realName = realName;
+    }
+
+    public String getMobileNumber() {
+        return mobileNumber;
+    }
+
+    public void setMobileNumber(String mobileNumber) {
+        this.mobileNumber = mobileNumber;
+    }
+
+    public String getReturnUrl() {
+        return returnUrl;
+    }
+
+    public void setReturnUrl(String returnUrl) {
+        this.returnUrl = returnUrl;
+    }
+}

+ 66 - 5
teachcloud-report/src/main/java/com/qmth/teachcloud/report/api/SsoApiController.java

@@ -1,13 +1,20 @@
 package com.qmth.teachcloud.report.api;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.teachcloud.common.SignatureEntityTest;
+import com.qmth.teachcloud.common.bean.dto.UserAuthenticationDto;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
-import com.qmth.teachcloud.common.entity.BasicSchool;
+import com.qmth.teachcloud.common.entity.*;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.AuthThirdUtil;
+import com.qmth.teachcloud.common.util.JacksonUtil;
+import com.qmth.teachcloud.report.business.bean.params.LoginThirdParam;
+import com.qmth.teachcloud.report.business.cache.WhuUserAuthCacheUtil;
 import io.swagger.annotations.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -22,6 +29,9 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.text.MessageFormat;
+import java.util.Objects;
 import java.util.Optional;
 
 /**
@@ -42,6 +52,18 @@ public class SsoApiController {
     @Resource
     DictionaryConfig dictionaryConfig;
 
+    @Resource
+    SysUserService sysUserService;
+
+    @Resource
+    SysUserRoleService sysUserRoleService;
+
+    @Resource
+    SysOrgService sysOrgService;
+
+    @Resource
+    SysRoleService sysRoleService;
+
     @ApiOperation(value = "单点登录接口")
     @ApiResponses({@ApiResponse(code = 200, message = "单点登录接口", response = Object.class)})
     @RequestMapping(value = "/login", method = RequestMethod.POST)
@@ -50,11 +72,50 @@ public class SsoApiController {
         Optional.ofNullable(result).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("数据为空"));
         String decodeJson = URLDecoder.decode(result, SystemConstant.CHARSET_NAME);
         log.info("login进来了,result:{}", decodeJson);
-//        BasicSemesterParams basicSemesterParams = JacksonUtil.readJson(decodeJson, BasicSemesterParams.class);
-//        Optional.ofNullable(basicSemesterParams).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
-//        basicSemesterParams.validParams();
+        LoginThirdParam loginThirdParam = JacksonUtil.readJson(decodeJson, LoginThirdParam.class);
+        Optional.ofNullable(loginThirdParam).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("转换后的数据为空"));
+        loginThirdParam.validParams();
         BasicSchool basicSchool = AuthThirdUtil.hasPermission();
+        //插入或者更新用户
+        //查询用户是否存在
+        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
+        sysUserQueryWrapper.lambda().eq(SysUser::getSchoolId, basicSchool.getId())
+                .eq(SysUser::getLoginName, loginThirdParam.getLoginName())
+                .eq(SysUser::getEnable, true);
+        SysUser sysUser = sysUserService.getOne(sysUserQueryWrapper);
+        if (Objects.isNull(sysUser)) {//新增
+            //查询学院id
+            QueryWrapper<SysOrg> sysOrgQueryWrapper = new QueryWrapper<>();
+            sysOrgQueryWrapper.lambda().eq(SysOrg::getSchoolId, basicSchool.getId())
+                    .eq(SysOrg::getName, loginThirdParam.getOrgName())
+                    .eq(SysOrg::getEnable, true);
+            SysOrg sysOrg = sysOrgService.getOne(sysOrgQueryWrapper);
+            Optional.ofNullable(sysOrg).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("学院数据为空"));
+
+            sysUser = new SysUser(basicSchool.getId(),
+                    loginThirdParam.getLoginName(),
+                    Objects.nonNull(loginThirdParam.getRealName()) ? loginThirdParam.getRealName() : loginThirdParam.getLoginName(),
+                    Objects.nonNull(loginThirdParam.getMobileNumber()) ? loginThirdParam.getMobileNumber() : null,
+                    sysOrg.getId());
+            sysUserService.save(sysUser);
+
+            //新增用户和角色关系
+            QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
+            sysRoleQueryWrapper.lambda().eq(SysRole::getType, loginThirdParam.getRole())
+                    .eq(SysRole::getEnable, true);
+            SysRole sysRole = sysRoleService.getOne(sysRoleQueryWrapper);
+            Optional.ofNullable(sysRole).orElseThrow(() -> ExceptionResultEnum.PARAMS_ERROR.exception("角色数据为空"));
+
+            SysUserRole sysUserRole = new SysUserRole(sysUser.getId(), sysRole.getId());
+            sysUserRoleService.save(sysUserRole);
+        }
+
+        UserAuthenticationDto userAuthenticationDto = new UserAuthenticationDto(String.valueOf(sysUser.getId()), SystemConstant.getUuid());
+        WhuUserAuthCacheUtil.setAuthCode(userAuthenticationDto);
+        String pattern = "{0}{1}{2}";
+        String code = URLEncoder.encode(MessageFormat.format(pattern, userAuthenticationDto.getUid(), SignatureEntityTest.FIELD_JOINER, SignatureEntityTest.encrypt(userAuthenticationDto.getCode())), SystemConstant.CHARSET_NAME);
+
         response.setHeader("Access-Control-Allow-Origin", "*");
-        response.sendRedirect(dictionaryConfig.sysDomain().getLoginAuthenUrl() + SystemConstant.getUuid());
+        response.sendRedirect(dictionaryConfig.sysDomain().getLoginAuthenUrl() + code);
     }
 }