Kaynağa Gözat

结构调整

徐勤民 2 ay önce
ebeveyn
işleme
af78e676b7

+ 10 - 0
src/main/java/com/xuqm/server/appmanager/common/CommonHelper.java

@@ -0,0 +1,10 @@
+package com.xuqm.server.appmanager.common;
+
+import java.util.Random;
+
+public class CommonHelper {
+
+    public static String getRandomId() {
+        return TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000);
+    }
+}

+ 61 - 0
src/main/java/com/xuqm/server/appmanager/controller/manager/v1/ManagerV1Controller.java

@@ -0,0 +1,61 @@
+package com.xuqm.server.appmanager.controller.manager.v1;
+
+import com.xuqm.server.appmanager.common.CommonHelper;
+import com.xuqm.server.appmanager.common.JWTHelper;
+import com.xuqm.server.appmanager.common.TimeHelper;
+import com.xuqm.server.appmanager.entitys.UserInfo;
+import com.xuqm.server.appmanager.entitys.sys.v1.ApplicationEntity;
+import com.xuqm.server.appmanager.http.HttpResult;
+import com.xuqm.server.appmanager.repository.sys.v1.ApplicationRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Random;
+
+@RestController
+@RequestMapping("manager/v1")
+public class ManagerV1Controller {
+
+    @Autowired
+    private ApplicationRepository applicationRepository;
+
+    @PostMapping("/manager/create")
+    public HttpResult<String> appCreate(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
+        if (null == application.getAppName() || null == application.getPackageName()) {
+            return new HttpResult<>(201, "参数错误", null);
+        } else {
+            UserInfo userInfo = JWTHelper.getUser(token);
+            if (null == userInfo) {
+                return new HttpResult<>(401, "登录失效", null);
+            }
+            ApplicationEntity app = applicationRepository.findFirstByAppNameAndTenantNo(application.getAppName(), userInfo.getTenantNo());
+            if (null != app) return new HttpResult<>(201, "已创建相关类型的同名应用", null);
+            app = new ApplicationEntity();
+            app.setTenantNo(userInfo.getTenantNo());
+            app.setAppName(application.getAppName());
+            app.setPackageName(application.getPackageName());
+            app.setDownloadUrl(application.getDownloadUrl());
+            app.setAppId(CommonHelper.getRandomId());
+            applicationRepository.save(app);
+
+            return new HttpResult<>(200, "创建成功", app.getAppId());
+        }
+
+    }
+
+    @PostMapping("/manager/list")
+    public HttpResult<List<ApplicationEntity>> appList(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
+
+        UserInfo userInfo = JWTHelper.getUser(token);
+        if (null == userInfo) {
+            return new HttpResult<>(401, "登录失效", null);
+        }
+
+
+        List<ApplicationEntity> apps = applicationRepository.findAllByTenantNo(userInfo.getTenantNo());
+
+        return new HttpResult<>(200, "成功", apps);
+    }
+
+}

+ 67 - 32
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/TenantV1Controller.java

@@ -1,20 +1,21 @@
 package com.xuqm.server.appmanager.controller.sys.v1;
 
+import com.xuqm.server.appmanager.common.CommonHelper;
 import com.xuqm.server.appmanager.common.JWTHelper;
-import com.xuqm.server.appmanager.common.TimeHelper;
+import com.xuqm.server.appmanager.controller.sys.v1.data.TenantAddUserData;
+import com.xuqm.server.appmanager.controller.sys.v1.data.TenantChangeAdminData;
+import com.xuqm.server.appmanager.controller.sys.v1.data.TenantCreateData;
+import com.xuqm.server.appmanager.controller.sys.v1.data.TenantDelUserData;
 import com.xuqm.server.appmanager.entitys.UserInfo;
-import com.xuqm.server.appmanager.entitys.sys.v1.ApplicationEntity;
 import com.xuqm.server.appmanager.entitys.sys.v1.TenantEntity;
 import com.xuqm.server.appmanager.entitys.sys.v1.TenantUserEntity;
 import com.xuqm.server.appmanager.http.HttpResult;
-import com.xuqm.server.appmanager.repository.sys.v1.ApplicationRepository;
 import com.xuqm.server.appmanager.repository.sys.v1.TenantRepository;
 import com.xuqm.server.appmanager.repository.sys.v1.TenantUserRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.List;
-import java.util.Random;
+import java.util.Objects;
 
 @RestController
 @RequestMapping("tenant/v1")
@@ -24,11 +25,9 @@ public class TenantV1Controller {
     private TenantRepository tenantRepository;
     @Autowired
     private TenantUserRepository tenantUserRepository;
-    @Autowired
-    private ApplicationRepository applicationRepository;
 
-    @PostMapping("/tenant/create")
-    public HttpResult<String> tenantCreate(@RequestBody TenantUserEntity tenant) throws Exception {
+    @PostMapping("/create")
+    public HttpResult<String> tenantCreate(@RequestBody TenantCreateData tenant) throws Exception {
         if (null == tenant.getUserPhone() || null == tenant.getUserEmail() || null == tenant.getUserPwd()) {
             return new HttpResult<>(201, "参数错误", null);
         } else {
@@ -38,12 +37,14 @@ public class TenantV1Controller {
             u.setUserEmail(tenant.getUserEmail());
             u.setUserPhone(tenant.getUserPhone());
             u.setUserPwd(tenant.getUserPwd());
-            u.setUserId(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
-            u.setTenantNo(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
+            u.setUserName(tenant.getUserName());
+            u.setUserId(CommonHelper.getRandomId());
+            u.setTenantNo(CommonHelper.getRandomId());
             tenantUserRepository.save(u);
             TenantEntity t = new TenantEntity();
             t.setUserId(u.getUserId());
             t.setTenantNo(u.getTenantNo());
+            t.setTenantName(tenant.getTenantName());
             tenantRepository.save(t);
 
             return new HttpResult<>(200, "创建成功", "创建成功");
@@ -51,42 +52,76 @@ public class TenantV1Controller {
 
     }
 
-    @PostMapping("/manager/create")
-    public HttpResult<String> appCreate(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
-        if (null == application.getAppName() || null == application.getPackageName()) {
+    @PostMapping("/changeAdmin")
+    public HttpResult<String> changeAdmin(@RequestBody TenantChangeAdminData data, @RequestHeader(name = "token") String token) throws Exception {
+        UserInfo userInfo = JWTHelper.getUser(token);
+        if (null == userInfo) {
+            return new HttpResult<>(401, "登录失效", null);
+        }
+        if (null == data.getUserId()) {
             return new HttpResult<>(201, "参数错误", null);
         } else {
-            UserInfo userInfo = JWTHelper.getUser(token);
-            if (null == userInfo) {
-                return new HttpResult<>(401, "登录失效", null);
+            TenantEntity tenant = tenantRepository.findFirstByTenantNo(userInfo.getTenantNo());
+            if (null == tenant) return new HttpResult<>(201, "查找租户信息失败", null);
+            if (!Objects.equals(tenant.getUserId(), userInfo.getUserId())) {
+                return new HttpResult<>(201, "请用管理员账号操作", null);
             }
-            ApplicationEntity app = applicationRepository.findFirstByAppNameAndTenantNo(application.getAppName(), userInfo.getTenantNo());
-            if (null != app) return new HttpResult<>(201, "已创建相关类型的同名应用", null);
-            app = new ApplicationEntity();
-            app.setTenantNo(userInfo.getTenantNo());
-            app.setAppName(application.getAppName());
-            app.setPackageName(application.getPackageName());
-            app.setDownloadUrl(application.getDownloadUrl());
-            app.setAppId(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
-            applicationRepository.save(app);
-
-            return new HttpResult<>(200, "创建成功", app.getAppId());
+            tenant.setUserId(data.getUserId());
+            tenantRepository.save(tenant);
+
+            return new HttpResult<>(200, "修改成功", "修改成功");
         }
 
     }
 
-    @PostMapping("/manager/list")
-    public HttpResult<List<ApplicationEntity>> appList(@RequestBody ApplicationEntity application, @RequestHeader(name = "token") String token) throws Exception {
+    @PostMapping("/addUser")
+    public HttpResult<String> addUser(@RequestBody TenantAddUserData data, @RequestHeader(name = "token") String token) throws Exception {
+        UserInfo userInfo = JWTHelper.getUser(token);
+        if (null == userInfo) {
+            return new HttpResult<>(401, "登录失效", null);
+        }
+        if (null == data.getUserPhone() || null == data.getUserEmail() || null == data.getUserPwd()) {
+            return new HttpResult<>(201, "参数错误", null);
+        } else {
+            TenantUserEntity u = tenantUserRepository.findFirstByUserEmailOrUserPhone(data.getUserEmail(), data.getUserPhone());
+            if (null != u) return new HttpResult<>(201, "邮箱或手机号重复", null);
+            u = new TenantUserEntity();
+            u.setUserEmail(data.getUserEmail());
+            u.setUserPhone(data.getUserPhone());
+            u.setUserPwd(data.getUserPwd());
+            u.setUserName(data.getUserName());
+            u.setUserId(CommonHelper.getRandomId());
+            u.setTenantNo(userInfo.getTenantNo());
+            tenantUserRepository.save(u);
+
+            return new HttpResult<>(200, "添加成功", "添加成功");
+        }
+
+    }
 
+    @PostMapping("/delUser")
+    public HttpResult<String> delUser(@RequestBody TenantDelUserData data, @RequestHeader(name = "token") String token) throws Exception {
         UserInfo userInfo = JWTHelper.getUser(token);
         if (null == userInfo) {
             return new HttpResult<>(401, "登录失效", null);
         }
+        if (null == data.getUserId()) {
+            return new HttpResult<>(201, "参数错误", null);
+        } else {
+            TenantEntity tenant = tenantRepository.findFirstByTenantNo(userInfo.getTenantNo());
+            if (null == tenant) return new HttpResult<>(201, "查找租户信息失败", null);
+            if (!Objects.equals(tenant.getUserId(), userInfo.getUserId())) {
+                return new HttpResult<>(201, "请用管理员账号操作", null);
+            }
+            if (!Objects.equals(tenant.getUserId(), data.getUserId())) {
+                return new HttpResult<>(201, "无法删除管理员账号", null);
+            }
 
+            tenantUserRepository.delete(tenantUserRepository.findFirstByUserId(data.getUserId()));
 
-        List<ApplicationEntity> apps = applicationRepository.findAllByTenantNo(userInfo.getTenantNo());
+            return new HttpResult<>(200, "删除成功", "删除成功");
+        }
 
-        return new HttpResult<>(200, "成功", apps);
     }
 
 }

+ 11 - 0
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/data/TenantAddUserData.java

@@ -0,0 +1,11 @@
+package com.xuqm.server.appmanager.controller.sys.v1.data;
+
+import lombok.Data;
+
+@Data
+public class TenantAddUserData {
+    private String userName;
+    private String userPhone;
+    private String userEmail;
+    private String userPwd;
+}

+ 8 - 0
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/data/TenantChangeAdminData.java

@@ -0,0 +1,8 @@
+package com.xuqm.server.appmanager.controller.sys.v1.data;
+
+import lombok.Data;
+
+@Data
+public class TenantChangeAdminData {
+    private String userId;
+}

+ 13 - 0
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/data/TenantCreateData.java

@@ -0,0 +1,13 @@
+package com.xuqm.server.appmanager.controller.sys.v1.data;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.Data;
+
+@Data
+public class TenantCreateData {
+    private String userName;
+    private String tenantName;
+    private String userPhone;
+    private String userEmail;
+    private String userPwd;
+}

+ 8 - 0
src/main/java/com/xuqm/server/appmanager/controller/sys/v1/data/TenantDelUserData.java

@@ -0,0 +1,8 @@
+package com.xuqm.server.appmanager.controller.sys.v1.data;
+
+import lombok.Data;
+
+@Data
+public class TenantDelUserData {
+    private String userId;
+}

+ 1 - 1
src/main/java/com/xuqm/server/appmanager/entitys/sys/v1/ApplicationEntity.java

@@ -6,7 +6,7 @@ import jakarta.persistence.Table;
 import lombok.Data;
 
 @Entity
-@Table(name = "app_manager")
+@Table(name = "apps")
 @Data
 public class ApplicationEntity extends AbstractBaseTimeEntity {
     private String appId;

+ 1 - 1
src/main/java/com/xuqm/server/appmanager/repository/sys/v1/TenantRepository.java

@@ -6,6 +6,6 @@ import org.springframework.stereotype.Repository;
 
 @Repository
 public interface TenantRepository extends JpaRepository<TenantEntity, Long> {
-//    TenantEntity findFirstByUserEmailOrUserPhone(String userEmail, String userPhone);
+    TenantEntity findFirstByTenantNo(String tenantNo);
 
 }

+ 1 - 0
src/main/java/com/xuqm/server/appmanager/repository/sys/v1/TenantUserRepository.java

@@ -9,5 +9,6 @@ import org.springframework.stereotype.Repository;
 public interface TenantUserRepository extends JpaRepository<TenantUserEntity, Long> {
     TenantUserEntity findFirstByUserEmailOrUserPhone(String userEmail, String userPhone);
     TenantUserEntity findFirstByUserEmailAndUserPwd(String userEmail, String userPwd);
+    TenantUserEntity findFirstByUserId(String userId);
 
 }