| | |
| | | package com.ruoyi.tide.controller; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
| | | import com.ruoyi.common.constant.Constants; |
| | |
| | | import com.ruoyi.framework.aspectj.lang.annotation.Anonymous; |
| | | import com.ruoyi.framework.security.service.SysLoginService; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.project.system.domain.SysDept; |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.service.ISysDeptService; |
| | | import com.ruoyi.project.system.service.ISysUserService; |
| | | import com.ruoyi.tide.pojo.TidePojo; |
| | | import com.ruoyi.tide.utils.TideUtils; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Isolation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.PreDestroy; |
| | | import javax.annotation.security.PermitAll; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | |
| | | @Autowired |
| | | private ISysDeptService deptService; |
| | | |
| | | @ApiOperation(value = "登录") |
| | | @PostMapping("/tideLogin") |
| | | public AjaxResult login(@RequestBody TidePojo tidePojo) { |
| | | String token = TideUtils.getToken(tidePojo.getCode()); |
| | | JSONObject userInfo = TideUtils.getUserInfo(token); |
| | | JSONObject tenant = JSONObject.parseObject(userInfo.get("tenant").toString()); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | // 生成令牌 |
| | | String generateToken = loginService.login(tenant.get("tenantAccount").toString(), tenant.get("tenantPassword").toString(), null, |
| | | IdWorker.get32UUID()); |
| | | ajax.put(Constants.TOKEN, generateToken); |
| | | return ajax; |
| | | if (tidePojo == null || ObjectUtils.isEmpty(tidePojo.getCode())) { |
| | | return AjaxResult.error("缺少浪潮授权码(code),请从浪潮门户重新进入系统"); |
| | | } |
| | | // 浪潮门户下发的授权码可能带固定后缀,做一次兼容处理 |
| | | String code = String.valueOf(tidePojo.getCode()).trim(); |
| | | if (code.endsWith("suanwangcode")) { |
| | | code = code.substring(0, code.length() - "suanwangcode".length()); |
| | | } |
| | | String token; |
| | | try { |
| | | token = TideUtils.getToken(code); |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 换取token异常, code={}", code, e); |
| | | return AjaxResult.error("浪潮授权失败:换取令牌异常,请从门户重新进入系统"); |
| | | } |
| | | if (ObjectUtils.isEmpty(token)) { |
| | | return AjaxResult.error("浪潮授权失败:授权码校验未通过(可能已过期或已被使用),请从门户重新进入系统"); |
| | | } |
| | | JSONObject userInfo; |
| | | try { |
| | | userInfo = TideUtils.getUserInfo(token); |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 获取用户信息异常, token={}", token, e); |
| | | return AjaxResult.error("浪潮授权失败:获取用户信息异常,请联系管理员"); |
| | | } |
| | | log.info("[浪潮SSO] 用户信息: {}", userInfo); |
| | | if (userInfo == null) { |
| | | return AjaxResult.error("浪潮未返回用户信息,请联系平台管理员"); |
| | | } |
| | | // 兼容浪潮不同版本/不同平台的租户字段命名 |
| | | JSONObject tenant = null; |
| | | for (String key : new String[]{"tenant", "tenantInfo", "enterprise", "company", "org"}) { |
| | | Object v = userInfo.get(key); |
| | | if (v instanceof JSONObject) { |
| | | tenant = (JSONObject) v; |
| | | break; |
| | | } |
| | | if (v instanceof String && ((String) v).trim().startsWith("{")) { |
| | | try { |
| | | tenant = JSONObject.parseObject((String) v); |
| | | break; |
| | | } catch (Exception ignored) { |
| | | } |
| | | } |
| | | } |
| | | String account = tenant == null ? null : firstText(tenant, "tenantAccount", "account", "userName", "loginName", "mobile", "phone", "name"); |
| | | String password = tenant == null ? null : firstText(tenant, "tenantPassword", "password", "pwd", "passWord"); |
| | | if (ObjectUtils.isEmpty(account)) { |
| | | account = firstText(userInfo, "tenantAccount", "account", "userName", "loginName", "mobile", "phone"); |
| | | } |
| | | if (ObjectUtils.isEmpty(password)) { |
| | | password = firstText(userInfo, "tenantPassword", "password", "pwd"); |
| | | } |
| | | if (ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(password)) { |
| | | log.error("[浪潮SSO] 未取到租户账号/密码, userInfo={}", userInfo); |
| | | return AjaxResult.error("浪潮未返回企业租户账号信息,请联系平台管理员检查该账号的企业绑定"); |
| | | } |
| | | try { |
| | | String generateToken = loginService.login(account, password, null, IdWorker.get32UUID()); |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put(Constants.TOKEN, generateToken); |
| | | return ajax; |
| | | } catch (Exception e) { |
| | | log.error("[浪潮SSO] 租户账号登录失败, account={}", account, e); |
| | | return AjaxResult.error("浪潮单点登录失败:租户账号[" + account + "]在系统中登录失败(" + e.getMessage() + ")"); |
| | | } |
| | | } |
| | | |
| | | /** 依次尝试多个键,返回第一个非空字符串值 */ |
| | | private String firstText(JSONObject obj, String... keys) { |
| | | if (obj == null || keys == null) { |
| | | return null; |
| | | } |
| | | for (String key : keys) { |
| | | Object v = obj.get(key); |
| | | if (v == null) { |
| | | continue; |
| | | } |
| | | String s = String.valueOf(v).trim(); |
| | | if (!s.isEmpty() && !"null".equalsIgnoreCase(s)) { |
| | | return s; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @PostMapping("/applicationOrdering") |
| | | @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED) |
| | | public JSONObject order (@RequestBody TidePojo tidePojo) { |
| | | SysUser user = userService.selectUserByUserName(tidePojo.getPltAccountLogin()); |
| | | String defaultPwd = "I73Kj+Mn$+SI";//默认开通密码固定写死 |
| | | // String randomString = TideUtils.getRandomString(12); |
| | | //账号不存在,执行新增操作 |
| | | if(Objects.isNull(user)){ |
| | | if(ObjectUtils.isEmpty(user)){ |
| | | //1.先新增用户对应的公司 |
| | | SysDept dept = new SysDept(); |
| | | dept.setParentId(100L);//父公司id |
| | | dept.setDeptName(tidePojo.getEnterpriseName()); |
| | | dept.setDeptNick(tidePojo.getEnterpriseName()); |
| | | dept.setOrderNum(0); |
| | | boolean deptNameUnique = deptService.checkDeptNameUnique(dept); |
| | | if (deptNameUnique){ |
| | | deptService.insertDept(dept); |
| | | } |
| | | //查询公司 |
| | | SysDept newSysDept = deptService.selectDeptByDeptName(dept.getDeptName()); |
| | | dept.setDeptId(ObjectUtils.isEmpty(newSysDept)?100L:newSysDept.getDeptId()); |
| | | dept.setTenantId(dept.getDeptId()); |
| | | deptService.updateDept(dept); |
| | | user = new SysUser(); |
| | | String password = SecurityUtils.encryptPassword(defaultPwd); |
| | | user.setPassword(password); |
| | |
| | | user.setDelFlag("0"); |
| | | user.setPostIds(new Long[]{1L}); |
| | | user.setRoleId(1L); |
| | | user.setRoleIds(new Long[]{2L}); |
| | | user.setDeptIds(new Long[]{100L}); |
| | | user.setRoleIds(new Long[]{2L});//默认普通角色 |
| | | user.setDeptIds(new Long[]{dept.getDeptId()});//绑定公司 |
| | | user.setTenantId(dept.getDeptId()); |
| | | userService.insertUser(user); |
| | | } |
| | | Map<String, Object> map = new HashMap<>(); |