From dda268997ca8f8a364f7c19b45d7a43a50a98efe Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 25 十月 2022 18:00:11 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java |  126 ++++++++++++++++++++++++++++++-----------
 1 files changed, 92 insertions(+), 34 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
index 4374972..481de34 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
@@ -1,6 +1,7 @@
 package com.sandu.ximon.admin.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.lang.Snowflake;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
 import com.sandu.common.enums.RoleLevelStatus;
@@ -12,13 +13,11 @@
 import com.sandu.ximon.admin.param.PwdParam;
 import com.sandu.ximon.admin.param.UserPwsParm;
 import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.security.authcode.MD5Util;
 import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
 import com.sandu.ximon.dao.bo.AdminBo;
 import com.sandu.ximon.dao.bo.MenuNode;
-import com.sandu.ximon.dao.domain.Admin;
-import com.sandu.ximon.dao.domain.AdminRoleRelation;
-import com.sandu.ximon.dao.domain.Role;
-import com.sandu.ximon.dao.domain.RoleMenuRelation;
+import com.sandu.ximon.dao.domain.*;
 import com.sandu.ximon.dao.enums.AdministratorEnums;
 import com.sandu.ximon.dao.enums.OrderByEnums;
 import com.sandu.ximon.dao.mapper.AdminMapper;
@@ -44,6 +43,7 @@
     private final AdminRoleRelationService adminRoleRelationService;
     private final RoleService roleService;
     private AdminMapper adminMapper;
+    private Snowflake snowflake;
 
     public Admin findByUserName(String username) {
         return getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, username).last("limit 1"));
@@ -53,9 +53,14 @@
     public boolean register(AdminParam param) {
         Admin admin = findByUserName(param.getUsername());
         if (admin != null) {
-            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡瀛樺湪");
+            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡鍦ㄧ鐞嗗憳涓瓨鍦�");
+        }
+        Client one = SpringContextHolder.getBean(ClientService.class).getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, param.getUsername()));
+        if (one != null) {
+            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡鍦ㄥ鎴蜂腑瀛樺湪");
         }
         Admin save = new Admin();
+        save.setId(snowflake.nextId());
         save.setUsername(param.getUsername());
         save.setPassword(passwordEncoder.encode(param.getPassword()));
         save.setNickName(param.getNickName());
@@ -173,7 +178,7 @@
         if (admin == null) {
             throw new BusinessException("鎵句笉鍒扮鐞嗗憳");
         }
-        if(!AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getUserDetails().getAdministratorType())){
+        if (!AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getUserDetails().getAdministratorType())) {
             throw new BusinessException("鍙湁瓒呯骇绠$悊鍛樻墠鑳藉垹闄ょ敤鎴�");
         }
 
@@ -182,6 +187,9 @@
         //鏈夌敤鎴峰繀瀹氭湁鐢ㄦ埛涓庤鑹茬殑鍏崇郴鏁版嵁
         AdminRoleRelation one = adminRoleRelationService.getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, admin.getId()));
         Role role = SpringContextHolder.getBean(RoleService.class).getOne(Wrappers.lambdaQuery(Role.class).eq(Role::getId, one.getRoleId()));
+        if (role == null) {
+            throw new BusinessException("鏁版嵁寮傚父锛�");
+        }
 
         if (role.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
             throw new BusinessException("瓒呯骇绠$悊鍛樻棤娉曞垹闄�");
@@ -196,6 +204,15 @@
                 }
             }
         }
+
+        /**
+         * 鍒犻櫎绠$悊鍛� 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "鍒犻櫎绠$悊鍛橈細" + admin.getUsername() + "鐢ㄦ埛id锛�" + admin;
+        StoreOperationRecordsUtils.storeOperationData(null, null, "鍒犻櫎绠$悊鍛�", content);
+        /**
+         * 鍒犻櫎绠$悊鍛� 鏃ュ織璁板綍缁撴潫
+         */
 
         return flag;
     }
@@ -262,36 +279,77 @@
      */
     public List<MenuNode> getUserPermissionList() {
         List<MenuNode> resultList;
-        //鍒ゆ柇瓒呯涓庢櫘閫氱鐞嗗憳
-        if (SecurityUtils.getAdministratorIdentity().equals(AdministratorEnums.ADMIN.getCode())) {
-            //鍙傛暟涓簄ull鏃讹紝鑾峰彇鎵�鏈夌殑鑿滃崟鏉冮檺
-            resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(null);
-        } else {
-            //閫氳繃鐢ㄦ埛UserID鑾峰彇鐢ㄦ埛瑙掕壊
-            AdminRoleRelation one = SpringContextHolder.getBean(AdminRoleRelationService.class).
-                    getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, SecurityUtils.getUserId()));
-            //鍒ょ┖
-            if (one == null) {
-                throw new BusinessException("璇ョ敤鎴锋湭缁戝畾瑙掕壊");
-            }
-
-            //閫氳繃RoleID鑾峰彇MeunId鍒楄〃
-            List<RoleMenuRelation> menuIdList = SpringContextHolder.getBean(RoleMenuRelationService.class)
-                    .list(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, one.getRoleId()));
-
-            //鍒ょ┖
-            if (menuIdList.isEmpty()) {
-                return new ArrayList<>();
-            }
-            List<Long> menuIds = new ArrayList<>(menuIdList.size());
-            for (RoleMenuRelation bean : menuIdList) {
-                menuIds.add(bean.getMenuId());
-            }
-
-            resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds);
-
+        //閫氳繃鐢ㄦ埛UserID鑾峰彇鐢ㄦ埛瑙掕壊
+        AdminRoleRelation one = SpringContextHolder.getBean(AdminRoleRelationService.class).
+                getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, SecurityUtils.getUserId()));
+        //鍒ょ┖
+        if (one == null) {
+            throw new BusinessException("璇ョ敤鎴锋湭缁戝畾瑙掕壊");
         }
 
+        //閫氳繃RoleID鑾峰彇MeunId鍒楄〃
+        List<RoleMenuRelation> menuIdList = SpringContextHolder.getBean(RoleMenuRelationService.class)
+                .list(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, one.getRoleId()));
+
+        //鍒ょ┖
+        if (menuIdList.isEmpty()) {
+            return new ArrayList<>();
+        }
+        List<Long> menuIds = new ArrayList<>(menuIdList.size());
+        for (RoleMenuRelation bean : menuIdList) {
+            menuIds.add(bean.getMenuId());
+        }
+
+        resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds);
+
         return resultList;
     }
+
+
+    /**
+     * 鐢熸垚key
+     *
+     * @param userId
+     * @return
+     */
+    public String creatUserKey(Long userId, Integer type) {
+        if (userId == null) {
+            throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓虹┖锛�");
+        }
+        //0锛氬厖閲嶇疆key,1锛氳幏鍙杒ey
+        if (type == null || (type != 0 && type != 1)) {
+            type = 1;
+        }
+
+        ClientService clientService = SpringContextHolder.getBean(ClientService.class);
+        Client client = clientService.getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (client == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦紝璇烽噸鏂扮‘璁わ紒");
+        }
+
+        /**
+         * key鎿嶄綔鏃ュ織
+         */
+        String content = "鎿嶄綔鐢ㄦ埛key锛�" + SecurityUtils.getUsername() + "  琚搷浣滅敤鎴穒d锛�" + userId;
+
+
+        //鑾峰彇
+        if (type == 1) {
+            content += "鎿嶄綔绫诲瀷:鑾峰彇key";
+            StoreOperationRecordsUtils.storeOperationData(null, null, "鎿嶄綔鐢ㄦ埛key", content);
+            return "鐢ㄦ埛鐨勪究鎹风櫥褰昸ey涓猴細" + client.getUserKey();
+        } else {
+            //鏇存柊
+            String md5 = MD5Util.md5("uesrId:" + userId + "_AdministratorType:" + 2 + "time:" + System.currentTimeMillis());
+            client.setUserKey(md5);
+            boolean flag = clientService.updateById(client);
+            if (flag) {
+                content += "鎿嶄綔绫诲瀷:鐢熸垚key,鏂扮殑key涓猴細" + md5;
+                StoreOperationRecordsUtils.storeOperationData(null, null, "鎿嶄綔鐢ㄦ埛key", content);
+                return "鐢熸垚渚挎嵎鐧诲綍key鎴愬姛锛岃鐢ㄦ埛key涓猴細" + md5 + "锛岃濡ュ杽淇濈锛�";
+            } else {
+                throw new BusinessException("鐢熸垚渚挎嵎鐧诲綍key澶辫触锛�");
+            }
+        }
+    }
 }

--
Gitblit v1.9.3