From 4f3a98f19143865cdc1de4791e8a95d96bd40c65 Mon Sep 17 00:00:00 2001 From: maven <2163098428@qq.com> Date: 星期五, 01 八月 2025 13:27:59 +0800 Subject: [PATCH] yys 密码已重置 --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/PersonnelHeaderApi.java | 191 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 191 insertions(+), 0 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/PersonnelHeaderApi.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/PersonnelHeaderApi.java new file mode 100644 index 0000000..3043d93 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/api/PersonnelHeaderApi.java @@ -0,0 +1,191 @@ +package com.ruoyi.web.controller.api; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.log.Log; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.ruoyi.common.config.PersonnelProperties; +import com.ruoyi.common.core.domain.entity.Custom; +import com.ruoyi.common.core.domain.entity.User; +import com.ruoyi.common.utils.SecurityUtils; +import com.ruoyi.system.mapper.UserMapper; +import com.ruoyi.system.service.CustomService; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.DigestUtils; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * 鑾峰彇浜轰簨绯荤粺 + */ +@Component +public class PersonnelHeaderApi { + + @Resource + private PersonnelProperties personnelProperties; + @Resource + private CustomService customService; + @Resource + private UserMapper userMapper; + + public String fetchNewAccessToken() { + HttpRequest request = HttpRequest.post(personnelProperties.getCode()) + .header("Content-Type", "application/x-www-form-urlencoded") + .form("grant_type", "client_credentials") + .form("client_id", personnelProperties.getAppId()) + .form("client_secret", personnelProperties.getAppSecret()); + HttpResponse response = request.execute(); + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode; + try { + jsonNode = objectMapper.readTree(response.body()); + String accessToken = jsonNode.get("access_token").asText(); + return accessToken; + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + } + + //鍒ゆ柇鏄惁瀛樺湪 + public String getAccessToken() { + String accessToken; + accessToken = fetchNewAccessToken(); + return accessToken; + } + + //璋冪敤 + public List<Company> companyUrl() { + String accessToken = getAccessToken(); + HttpRequest request = HttpRequest.get(personnelProperties.getCompanies()) + .header("Authorization", "Bearer " + accessToken); + List<Company> companies; + try { + companies = JSON.parseArray(request.execute().body(), Company.class); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + return companies.stream().filter(ob -> { + if (Objects.equals(ob.getStatus(), "enabled")) return true; + return false; + }).collect(Collectors.toList()); + } + + public List<Person> userUrl(String companyId) { + String accessToken = getAccessToken(); + HttpRequest request = HttpRequest.get(personnelProperties.getSimple() + companyId) + .header("Authorization", "Bearer " + accessToken) + .header("Content-Type", "application/form-data"); + List<Person> person; + try { + person = JSON.parseArray(request.execute().body(), Person.class); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + List<JSONObject> department = getDepartment(companyId); + return person.stream().filter(ob -> { + if (Objects.equals(ob.getStatus(), "enabled")) { + ob.setIsLive(userMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getAccount, ob.getEmployeeID()))); + ob.setDepartment(getDepartmentStr(department, ob.getDepartmentCode()).replaceFirst("/", "")); + return true; + } + return false; + }).collect(Collectors.toList()); + } + + public String getDepartmentStr(List<JSONObject> department, String code) { + String str = ""; + Optional<JSONObject> depart = department.stream().filter(a -> code.equals(a.get("departmentCode"))).findFirst(); + str = "/" + depart.get().get("departmentName") + str; + if (depart.get().get("parentDepartmentCode") != null) { + str = getDepartmentStr(department, depart.get().get("parentDepartmentCode").toString()) + str; + } + return str; + } + + public List<JSONObject> getDepartment(String companyId) { + String accessToken = getAccessToken(); + HttpRequest request = HttpRequest.get(personnelProperties.getDepartment().replace("companyId", companyId)) + .header("Authorization", "Bearer " + accessToken) + .header("Content-Type", "application/form-data"); + List<JSONObject> list; + try { + list = JSON.parseArray(request.execute().body()); + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + return list.stream().filter(ob -> { + if (Objects.equals(ob.get("status"), "enabled")) return true; + return false; + }).collect(Collectors.toList()); + } + + public Person selectPersonUser(String code) { + String accessToken = getAccessToken(); + HttpRequest request = HttpRequest.get(personnelProperties.getPerson() + code) + .header("Authorization", "Bearer " + accessToken) + .header("Content-Type", "application/form-data"); + Person person; + try { + person = JSON.parseObject(request.execute().body(), Person.class); + if (BeanUtil.isEmpty(person)) return null; + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } + List<JSONObject> department = getDepartment(person.getCompanyId()); + person.setDepartment(getDepartmentStr(department, person.getDepartmentCode()).replaceFirst("/", "")); + person.setIsLive(userMapper.selectCount(Wrappers.<User>lambdaQuery().eq(User::getAccount, person.getEmployeeID()))); + return person; + } + + /** + * 灏嗕汉浜嬬郴缁熷嬀閫夌殑鍐呭杞Щ鍒版湰绯荤粺 + * @param personDto + * @return + */ + @Transactional(rollbackFor = Exception.class) + public Object addPersonUser(PersonDto personDto) { + personDto.getPerson().forEach(person -> { + User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, person.getEmployeeID())); + List<Company> company = personDto.getCompany(); + String companyName = ""; + Custom custom = null; + // 濉厖宸ュ巶淇℃伅 + companyName = company.stream().filter(a -> a.getCompanyId().equals(person.getCompanyId())).findFirst().get().getCompanyName(); + custom = customService.getCustomId(companyName); + + if (BeanUtil.isEmpty(user)) { + user = new User(); + user.setName(person.getName()); + user.setNameEn("not write"); + user.setAccount(person.getEmployeeID()); + user.setPhone(person.getPhoneNumber()); + user.setEmail(person.getCompanyEmail()); + user.setIsCustom(0); + user.setPassword(SecurityUtils.encryptPassword("zttZTT123!")); + user.setCompany(BeanUtil.isNotEmpty(custom) ? (custom.getId() + "") : companyName); + userMapper.insert(user); + } else { + user.setName(person.getName()); + user.setPhone(person.getPhoneNumber()); + user.setEmail(person.getCompanyEmail()); + user.setIsCustom(0); + user.setCompany(BeanUtil.isNotEmpty(custom) ? (custom.getId() + "") : companyName); + userMapper.updateById(user); + } + }); + return 1; + } +} -- Gitblit v1.9.3