Эх сурвалжийг харах

创建租户,创建应用

xuqm 1 жил өмнө
parent
commit
96da12992b

+ 34 - 0
src/main/java/cn/org/bjca/trust/java/imserver/common/RabbitMQHelper.java

@@ -0,0 +1,34 @@
+package cn.org.bjca.trust.java.imserver.common;
+
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class RabbitMQHelper {
+    public static void requestByGetAndParams(String requestUrl, String param) throws Exception {
+        System.out.println(requestUrl + ":\n" + param);
+        URL url = new URL(requestUrl);
+        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
+        //设置请求方式,请求参数类型
+        httpURLConnection.setRequestMethod("PUT");
+        httpURLConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
+        httpURLConnection.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
+        httpURLConnection.setDoOutput(true);
+        OutputStream outputStream = httpURLConnection.getOutputStream();
+        //将参数写入输出流,param必须是JSON格式
+        outputStream.write(param.getBytes());
+        outputStream.flush();
+        InputStream inputStream = httpURLConnection.getInputStream();
+        ByteArrayOutputStream bout = new ByteArrayOutputStream();
+        byte[] bytes = new byte[1024];
+        int len = 0;
+        while ((len = inputStream.read(bytes)) >= 0) {
+            bout.write(bytes, 0, len);
+        }
+        inputStream.close();
+        bout.close();
+        bout.toByteArray();
+    }
+}

+ 81 - 0
src/main/java/cn/org/bjca/trust/java/imserver/common/TimeHelper.java

@@ -0,0 +1,81 @@
+package cn.org.bjca.trust.java.imserver.common;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+public class TimeHelper {
+
+    /**
+     * 获取当前时间戳
+     *
+     * @return 时间戳
+     */
+    public static long getTimeMillis() {
+        return System.currentTimeMillis();
+    }
+
+    /**
+     * 获取当前时间,指定返回样式
+     *
+     * @param formats 指定样式
+     * @return 时间字符串
+     */
+    public static String getTimeString(String formats) {
+        return getStringFormMillis(System.currentTimeMillis(), formats);
+    }
+
+    /**
+     * 根据给定时间戳和样式,返回字符串
+     *
+     * @param millis  时间戳
+     * @param formats 指定字符串格式
+     * @return 时间字符串
+     */
+    public static String getStringFormMillis(long millis, String formats) {
+        Date date = new Date(millis);
+        return new SimpleDateFormat(formats, Locale.getDefault()).format(date);
+    }
+
+    /**
+     * 根据Date 返回指定格式的时字符串
+     *
+     * @param date    数据
+     * @param formats 格式
+     * @return 指定格式的字符串
+     */
+    public static String getStringFromDate(Date date, String formats) {
+        SimpleDateFormat formatter = new SimpleDateFormat(formats, Locale.getDefault());
+        return formatter.format(date);
+    }
+
+    /**
+     * 根据给定字符串和格式,获取时间戳
+     *
+     * @param dateString 时间字符串
+     * @param formats    时间格式
+     * @return 时间戳
+     */
+    public static long getTimeMillisForType(String dateString, String formats) {
+        SimpleDateFormat format = new SimpleDateFormat(formats, Locale.getDefault());
+        Date date = null;
+        try {
+            date = format.parse(dateString);
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+
+        return date != null ? date.getTime() : 0;
+
+    }
+
+    /**
+     * 获取以秒为单位的时间戳
+     *
+     * @return 秒
+     */
+    public static long getTimeFromSecond() {
+        return getTimeMillis() / 1000;
+    }
+}

+ 0 - 14
src/main/java/cn/org/bjca/trust/java/imserver/controller/HelloController.java

@@ -1,11 +1,5 @@
 package cn.org.bjca.trust.java.imserver.controller;
 
-import cn.org.bjca.trust.java.imserver.entitys.Message;
-import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
-import cn.org.bjca.trust.java.imserver.entitys.message.TextMessage;
-import cn.org.bjca.trust.java.imserver.repository.MessageRepository;
-import cn.org.bjca.trust.java.imserver.repository.UserRepository;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,18 +8,10 @@ import org.springframework.web.bind.annotation.RestController;
 @RestController
 @RequestMapping("hello")
 public class HelloController {
-    @Autowired
-    private MessageRepository messageRepository;
 
     @GetMapping("/{id}")
     public String getById(@PathVariable String id) throws Exception {
         System.out.println("id ==> " + id);
-        Message message = new Message();
-        message.setType("111");
-        TextMessage textMessage = new TextMessage();
-        textMessage.setText("ssss");
-        message.setTextMessage(textMessage);
-        messageRepository.save(message);
         return "{\"msg\":\"qdxorigin\",\"code\":200,\"data\":\"Sbfuiaefhaikufhcsauik\"}";
     }
 }

+ 71 - 0
src/main/java/cn/org/bjca/trust/java/imserver/controller/sys/v1/TenantV1Controller.java

@@ -0,0 +1,71 @@
+package cn.org.bjca.trust.java.imserver.controller.sys.v1;
+
+import cn.org.bjca.trust.java.imserver.HttpResult;
+import cn.org.bjca.trust.java.imserver.common.RabbitMQHelper;
+import cn.org.bjca.trust.java.imserver.common.TimeHelper;
+import cn.org.bjca.trust.java.imserver.entitys.sys.ApplicationEntity;
+import cn.org.bjca.trust.java.imserver.entitys.sys.TenantEntity;
+import cn.org.bjca.trust.java.imserver.repository.tenant.ApplicationRepository;
+import cn.org.bjca.trust.java.imserver.repository.tenant.TenantRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+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 java.util.Random;
+
+@RestController
+@RequestMapping("tenant/v1")
+public class TenantV1Controller {
+
+    @Autowired
+    private TenantRepository tenantRepository;
+    @Autowired
+    private ApplicationRepository applicationRepository;
+
+    @PostMapping("/tenant/create")
+    public HttpResult<String> tenantCreate(@RequestBody TenantEntity tenant) throws Exception {
+        if (null == tenant.getTenantName() ||
+                null == tenant.getUserPhone() ||
+                null == tenant.getUserEmail() ||
+                null == tenant.getUserName()) {
+            return new HttpResult<>(201, "参数错误", "");
+        } else {
+            TenantEntity t = tenantRepository.findFirstByUserEmailOrUserPhone(tenant.getUserEmail(), tenant.getUserPhone());
+            if (null != t) return new HttpResult<>(201, "当前联系人已注册", "");
+            t = new TenantEntity();
+            t.setTenantName(tenant.getTenantName());
+            t.setUserEmail(tenant.getUserEmail());
+            t.setUserName(tenant.getUserName());
+            t.setUserPhone(tenant.getUserPhone());
+            t.setTenantNo(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
+            tenantRepository.save(t);
+
+//            RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/vhosts/" + appid, "");
+            return new HttpResult<>(200, "创建成功", "创建成功");
+        }
+
+    }
+
+    @PostMapping("/app/create")
+    public HttpResult<String> appCreate(@RequestBody ApplicationEntity application) throws Exception {
+        if (null == application.getAppName() ||
+                null == application.getTenantNo()) {
+            return new HttpResult<>(201, "参数错误", "");
+        } else {
+            ApplicationEntity app = applicationRepository.findFirstByAppNameAndTenantNo(application.getAppName(), application.getTenantNo());
+            if (null != app) return new HttpResult<>(201, "已创建相关类型的同名应用", "");
+            app = new ApplicationEntity();
+            app.setTenantNo(application.getTenantNo());
+            app.setAppName(application.getAppName());
+            app.setAppId(TimeHelper.getTimeString("yyyyMMddHHmm") + (new Random().nextInt(899999999) + 100000000));
+            applicationRepository.save(app);
+
+            RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/vhosts/" + app.getAppId(), "");
+            return new HttpResult<>(200, "创建成功", "创建成功");
+        }
+
+    }
+
+}

+ 14 - 35
src/main/java/cn/org/bjca/trust/java/imserver/controller/user/v1/UserV1Controller.java

@@ -1,18 +1,16 @@
 package cn.org.bjca.trust.java.imserver.controller.user.v1;
 
 import cn.org.bjca.trust.java.imserver.HttpResult;
+import cn.org.bjca.trust.java.imserver.common.RabbitMQHelper;
 import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
+import cn.org.bjca.trust.java.imserver.entitys.sys.ApplicationEntity;
 import cn.org.bjca.trust.java.imserver.repository.UserRepository;
+import cn.org.bjca.trust.java.imserver.repository.tenant.ApplicationRepository;
 import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpHeaders;
 import org.springframework.web.bind.annotation.*;
 
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
 import java.util.UUID;
 
 @RestController
@@ -23,14 +21,19 @@ public class UserV1Controller {
     private HttpServletRequest request;
     @Autowired
     private UserRepository userRepository;
+    @Autowired
+    private ApplicationRepository applicationRepository;
 
     @PostMapping("/login")
-    public HttpResult<LoginData> getById(@RequestBody LoginBean user, @RequestHeader HttpHeaders headers) throws Exception {
+    public HttpResult<LoginData> login(@RequestBody LoginBean user, @RequestHeader HttpHeaders headers) throws Exception {
         String appid = headers.getFirst("appid");
         String userid = headers.getFirst("userid");
         String ostype = headers.getFirst("ostype");
-        requestByGetAndParams("http://114.115.203.60:15672/api/vhosts/"+appid, "");
         if (null != appid && null != userid && null != ostype) {
+
+            ApplicationEntity app = applicationRepository.findFirstByAppId(appid);
+            if (null == app) return new HttpResult<>(201, "appId不存在", new LoginData());
+
             UserInfo userInfo = userRepository.findUserInfoByUserIdAndOsTypeAndAppId(userid, ostype, appid);
             if (null == userInfo) {
                 System.out.println("-----------------用户不存在------------------");
@@ -42,10 +45,10 @@ public class UserV1Controller {
                 userInfo.setPassword(UUID.randomUUID().toString());
                 userRepository.save(userInfo);
 
-                requestByGetAndParams("http://114.115.203.60:15672/api/users/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"password\":\"" + userInfo.getPassword() + "\",\"tags\":\"\"}");
-                requestByGetAndParams("http://114.115.203.60:15672/api/permissions/%2F/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
-                requestByGetAndParams("http://114.115.203.60:15672/api/permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
-                requestByGetAndParams("http://114.115.203.60:15672/api/topic-permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"exchange\":\"\",\"write\":\".*\",\"read\":\".*\"}");
+                RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/users/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"password\":\"" + userInfo.getPassword() + "\",\"tags\":\"\"}");
+                RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/permissions/%2F/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
+                RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}");
+                RabbitMQHelper.requestByGetAndParams("http://114.115.203.60:15672/api/topic-permissions/" + appid + "/" + userInfo.getUserName(), "{\"username\":\"" + userInfo.getUserName() + "\",\"vhost\":\"" + appid + "\",\"exchange\":\"\",\"write\":\".*\",\"read\":\".*\"}");
 
             }
 
@@ -56,28 +59,4 @@ public class UserV1Controller {
 
     }
 
-    private void requestByGetAndParams(String requestUrl, String param) throws Exception {
-        System.out.println(requestUrl + ":\n" + param);
-        URL url = new URL(requestUrl);
-        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
-        //设置请求方式,请求参数类型
-        httpURLConnection.setRequestMethod("PUT");
-        httpURLConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
-        httpURLConnection.setRequestProperty("Authorization", "Basic YWRtaW46YWRtaW4=");
-        httpURLConnection.setDoOutput(true);
-        OutputStream outputStream = httpURLConnection.getOutputStream();
-        //将参数写入输出流,param必须是JSON格式
-        outputStream.write(param.getBytes());
-        outputStream.flush();
-        InputStream inputStream = httpURLConnection.getInputStream();
-        ByteArrayOutputStream bout = new ByteArrayOutputStream();
-        byte[] bytes = new byte[1024];
-        int len = 0;
-        while ((len = inputStream.read(bytes)) >= 0) {
-            bout.write(bytes, 0, len);
-        }
-        inputStream.close();
-        bout.close();
-        bout.toByteArray();
-    }
 }

+ 16 - 0
src/main/java/cn/org/bjca/trust/java/imserver/entitys/sys/ApplicationEntity.java

@@ -0,0 +1,16 @@
+package cn.org.bjca.trust.java.imserver.entitys.sys;
+
+import cn.org.bjca.trust.java.imserver.entitys.AbstractBaseTimeEntity;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Table;
+import lombok.Data;
+
+@Entity
+@Table(name = "applications")
+@Data
+public class ApplicationEntity extends AbstractBaseTimeEntity {
+    private String appId;
+    private String appName;
+    private String tenantNo;
+    private String userId;
+}

+ 18 - 0
src/main/java/cn/org/bjca/trust/java/imserver/entitys/sys/TenantEntity.java

@@ -0,0 +1,18 @@
+package cn.org.bjca.trust.java.imserver.entitys.sys;
+
+import cn.org.bjca.trust.java.imserver.entitys.AbstractBaseTimeEntity;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Table;
+import lombok.Data;
+
+@Entity
+@Table(name = "tenant")
+@Data
+public class TenantEntity extends AbstractBaseTimeEntity {
+    private String tenantNo;
+    private String tenantName;
+    private String userId;
+    private String userName;
+    private String userPhone;
+    private String userEmail;
+}

+ 11 - 0
src/main/java/cn/org/bjca/trust/java/imserver/repository/tenant/ApplicationRepository.java

@@ -0,0 +1,11 @@
+package cn.org.bjca.trust.java.imserver.repository.tenant;
+
+import cn.org.bjca.trust.java.imserver.entitys.sys.ApplicationEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ApplicationRepository extends JpaRepository<ApplicationEntity, Long> {
+    ApplicationEntity findFirstByAppNameAndTenantNo(String appName, String tenantNo);
+    ApplicationEntity findFirstByAppId(String appId);
+}

+ 11 - 0
src/main/java/cn/org/bjca/trust/java/imserver/repository/tenant/TenantRepository.java

@@ -0,0 +1,11 @@
+package cn.org.bjca.trust.java.imserver.repository.tenant;
+
+import cn.org.bjca.trust.java.imserver.entitys.sys.TenantEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface TenantRepository extends JpaRepository<TenantEntity, Long> {
+    TenantEntity findFirstByUserEmailOrUserPhone(String userEmail, String userPhone);
+
+}

+ 2 - 0
src/main/java/lombok.config

@@ -0,0 +1,2 @@
+config.stopBubbling=true
+lombok.equalsAndHashCode.callSuper=call