Quellcode durchsuchen

first commit business-v1

ltt vor 2 Jahren
Ursprung
Commit
731a2f3f6b

+ 25 - 0
business-common/business-common-core/src/main/java/com/sckw/core/config/FilterConfig.java

@@ -0,0 +1,25 @@
+//package com.sckw.core.config;
+//
+//import com.sckw.core.filter.TenantFilter;
+//import org.springframework.boot.web.servlet.FilterRegistrationBean;
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//
+///**
+// * @desc:
+// * @author: Lt
+// * @date: 2024-05-22
+// */
+//@Configuration
+//public class FilterConfig {
+//    @Bean
+//    public FilterRegistrationBean<TenantFilter> tenantFilter() {
+//        FilterRegistrationBean<TenantFilter> registrationBean = new FilterRegistrationBean<>();
+//        registrationBean.setFilter(new TenantFilter());
+//        // 如果您有多个过滤器,可以设置顺序
+//        //registrationBean.setOrder(1);
+//        // 定义此过滤器适用的 URL 模式
+//        registrationBean.addUrlPatterns("/*");
+//        return registrationBean;
+//    }
+//}

+ 31 - 0
business-common/business-common-core/src/main/java/com/sckw/core/filter/TenantFilter.java

@@ -0,0 +1,31 @@
+package com.sckw.core.filter;
+
+import cn.hutool.core.util.StrUtil;
+import com.sckw.core.utils.TenantUtil;
+import com.sckw.core.utils.WebFrameworkUtils;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import java.io.IOException;
+
+/**
+ * @author xucaiqin
+ * @date 2024-05-21 09:12:35
+ */
+@Component
+public class TenantFilter extends OncePerRequestFilter {
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+        String tenantId = WebFrameworkUtils.getTenantId(request);
+        TenantUtil.setTenant(StrUtil.isBlank(tenantId) ? "1" : tenantId);
+        try {
+            filterChain.doFilter(request, response);
+        } finally {
+            TenantUtil.clear();
+        }
+    }
+}

+ 72 - 0
business-common/business-common-core/src/main/java/com/sckw/core/utils/AESUtil.java

@@ -0,0 +1,72 @@
+package com.sckw.core.utils;
+
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.SecureRandom;
+import java.util.Base64;
+
+/**
+ * @desc:
+ * @author: Lt
+ * @date: 2024-05-22
+ */
+public class AESUtil {
+    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
+    private static final int KEY_SIZE = 128;
+
+    /**
+    * @Description:
+    * @Author: Lt
+    * @Date: 2024/5/22 15:13
+    */
+    public static String encrypt(String data, String salt) throws Exception
+    {
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        SecretKeySpec secretKey = new SecretKeySpec(salt.getBytes(), "AES");
+        cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(new byte[16]));
+        byte[] encryptedBytes = cipher.doFinal(data.getBytes());
+        return Base64.getEncoder().encodeToString(encryptedBytes);
+    }
+
+
+    /**
+    * @Description: AES解码
+    * @Author: Lt
+    * @Date: 2024/5/22 15:14
+    */
+    public static String decrypt(String encryptedData, String salt) throws Exception
+    {
+        Cipher cipher = Cipher.getInstance(ALGORITHM);
+        SecretKeySpec secretKey = new SecretKeySpec(salt.getBytes(), "AES");
+        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[16]));
+        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
+        return new String(decryptedBytes);
+    }
+
+    /**
+     * 生成一个指定长度的随机盐值。
+     *
+     * @param length 盐值的长度。
+     * @return 生成的盐值。
+     */
+    public static String generateSalt(int length) {
+        if (length <= 0) {
+            throw new IllegalArgumentException("Length must be a positive number");
+        }
+
+        // 创建一个SecureRandom对象
+        SecureRandom random = new SecureRandom();
+        // 生成一个随机的字节数组
+        byte[] bytes = new byte[length];
+        random.nextBytes(bytes);
+
+        // 将字节数组编码为Base64字符串,以缩短长度并保证字符串只包含可打印字符
+        return Base64.getEncoder().encodeToString(bytes);
+    }
+
+}
+

+ 23 - 0
business-common/business-common-core/src/main/java/com/sckw/core/utils/TenantUtil.java

@@ -0,0 +1,23 @@
+package com.sckw.core.utils;
+
+/**
+ * @author xucaiqin
+ * @date 2024-05-20 17:15:04
+ */
+public class TenantUtil {
+    private final static ThreadLocal<String> tenant = new ThreadLocal<>();
+
+    private TenantUtil() {
+    }
+
+    public static void setTenant(String tenantId) {
+        tenant.set(tenantId);
+    }
+
+    public static String getTenant() {
+        return tenant.get();
+    }
+    public static void clear() {
+         tenant.remove();
+    }
+}

+ 32 - 0
business-common/business-common-core/src/main/java/com/sckw/core/utils/WebFrameworkUtils.java

@@ -0,0 +1,32 @@
+package com.sckw.core.utils;
+
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+/**
+ * 专属于 web 包的工具类
+ *
+ * @author 芋道源码
+ */
+public class WebFrameworkUtils {
+
+    public static final String HEADER_TENANT_ID = "tenant-id";
+
+
+    public static String getTenantId(HttpServletRequest request) {
+        return request.getHeader(HEADER_TENANT_ID);
+    }
+
+
+    public static HttpServletRequest getRequest() {
+        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
+        if (!(requestAttributes instanceof ServletRequestAttributes)) {
+            return null;
+        }
+        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
+        return servletRequestAttributes.getRequest();
+    }
+
+}

+ 38 - 0
business-modules/business-user/src/main/java/com/sckw/user/controller/UserController.java

@@ -0,0 +1,38 @@
+package com.sckw.user.controller;
+
+import com.sckw.core.utils.TenantUtil;
+import com.sckw.core.web.response.HttpResult;
+import com.sckw.user.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @desc:
+ * @author: Lt
+ * @date: 2024-05-22
+ */
+@RestController
+@RequestMapping("/user")
+public class UserController {
+
+    @Autowired
+    UserService userService;
+
+    /**
+    * @Description: 根据tenantId生成用户信息
+    * @Author: Lt
+    * @Date: 2024/5/22 0022 13:58
+    */
+    @PostMapping(value = "/generateUserByTenantId")
+    public HttpResult generateUserByTenantId() throws Exception {
+        String tenant = TenantUtil.getTenant();
+        if (tenant.isBlank()){
+            return HttpResult.error("租户为空");
+        }
+        return HttpResult.ok(userService.generateUserByTenantId(tenant));
+    }
+
+
+}

+ 79 - 0
business-modules/business-user/src/main/java/com/sckw/user/entity/KwBusinessUser.java

@@ -0,0 +1,79 @@
+package com.sckw.user.entity;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * @desc:
+ * @author: Lt
+ * @date: 2024-05-22
+ */
+@Data
+public class KwBusinessUser implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 自增长id
+     */
+    private Long id;
+
+    /**
+     * 姓名
+     */
+    private String name;
+
+    /**
+     * 电话
+     */
+    private String mobile;
+
+    /**
+     * 登录名
+     */
+    private String username;
+
+    /**
+     * 密码
+     */
+    private String password;
+
+    /**
+     * 盐
+     */
+    private String slat;
+
+    /**
+     * 状态,1:启用,0:禁用
+     */
+    private Integer status;
+
+    /**
+     * token
+     */
+    private String token;
+
+    /**
+     * 0:普通,1是卡车司机
+     */
+    private Integer type;
+
+    private String tenantId;
+
+    /**
+     * 新增时间
+     */
+    private Date createTime;
+
+    /**
+     * 修改时间
+     */
+    private Date updateTime;
+
+    /**
+     * 删除时间
+     */
+    private Integer delFlag;
+}

+ 24 - 0
business-modules/business-user/src/main/java/com/sckw/user/mapper/UserMapper.java

@@ -0,0 +1,24 @@
+package com.sckw.user.mapper;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.sckw.user.entity.KwBusinessUser;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * @desc:
+ * @author: Lt
+ * @date: 2024-05-22
+ */
+@Mapper
+@DS("user")
+public interface UserMapper extends BaseMapper<KwBusinessUser> {
+
+    /**
+     * 查询 根据主键 id 查询
+     * @author Lt
+     * @date 2024/05/22
+     **/
+    KwBusinessUser load(String id);
+    KwBusinessUser selectOneByTenantId(String tenantId);
+}

+ 85 - 0
business-modules/business-user/src/main/java/com/sckw/user/service/UserService.java

@@ -0,0 +1,85 @@
+package com.sckw.user.service;
+
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.sckw.core.utils.AESUtil;
+import com.sckw.core.utils.IdWorker;
+import com.sckw.user.entity.KwBusinessUser;
+import com.sckw.user.mapper.UserMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.security.SecureRandom;
+import java.util.Base64;
+import java.util.Locale;
+import java.util.Random;
+import java.util.UUID;
+
+/**
+ * @desc:
+ * @author: Lt
+ * @date: 2024-05-22
+ */
+@Service
+public class UserService {
+
+    @Autowired
+    UserMapper userMapper;
+
+    public String generateUserByTenantId(String tenantId) throws Exception {
+        KwBusinessUser user = userMapper.selectOneByTenantId(tenantId);
+        if (ObjectUtils.isNotNull(user)) {
+            throw new Exception("用户已存在");
+        } else {
+            KwBusinessUser kwBusinessUser = new KwBusinessUser();
+            kwBusinessUser.setId(new IdWorker(1L).nextId());
+            String username = generateRandomAccount(6,12);
+            kwBusinessUser.setName(username);
+            kwBusinessUser.setUsername(username);
+            //生成盐
+            String salt = AESUtil.generateSalt(16);
+            kwBusinessUser.setSlat(salt);
+            String encryptPwd = AESUtil.encrypt("123456", salt);
+            kwBusinessUser.setPassword(encryptPwd);
+            kwBusinessUser.setType(0);
+            kwBusinessUser.setTenantId(tenantId);
+            final int insert = userMapper.insert(kwBusinessUser);
+            if (insert > 0) {
+                return "创建成功";
+            }else {
+                return "创建失败";
+            }
+        }
+
+    }
+
+    /**
+     * 生成介于最小长度和最大长度之间的随机账号。
+     *
+     * @param minLen 最小长度。
+     * @param maxLen 最大长度。
+     * @return 生成的随机账号。
+     */
+    public static String generateRandomAccount(int minLen, int maxLen) {
+        if (minLen <= 0 || maxLen <= 0 || minLen > maxLen) {
+            throw new IllegalArgumentException("Invalid length range");
+        }
+
+        // 创建Random对象
+        Random random = new Random();
+        // 确定账号的长度,随机选择在最小长度和最大长度之间
+        int length = random.nextInt(maxLen - minLen + 1) + minLen;
+        // 定义可能包含的字符
+        String possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+        // 使用StringBuilder构建随机账号
+        StringBuilder sb = new StringBuilder(length);
+        for (int i = 0; i < length; i++) {
+            // 从可能的字符中随机选择一个字符
+            sb.append(possibleChars.charAt(random.nextInt(possibleChars.length())));
+        }
+        return sb.toString();
+    }
+
+
+
+
+}

+ 47 - 0
business-modules/business-user/src/main/resources/mapper/UserMapper.xml

@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.sckw.user.mapper.UserMapper">
+    <resultMap id="BaseResultMap" type="com.sckw.user.entity.KwBusinessUser" >
+        <result column="id" property="id" />
+        <result column="name" property="name" />
+        <result column="mobile" property="mobile" />
+        <result column="username" property="username" />
+        <result column="password" property="password" />
+        <result column="slat" property="slat" />
+        <result column="status" property="status" />
+        <result column="token" property="token" />
+        <result column="type" property="type" />
+        <result column="create_time" property="createTime" />
+        <result column="update_time" property="updateTime" />
+        <result column="del_flag" property="delFlag" />
+    </resultMap>
+
+    <sql id="Base_Column_List">
+                id,
+                `name`,
+                mobile,
+                username,
+                `password`,
+                slat,
+                `status`,
+                token,
+                type,
+                create_time,
+                update_time,
+                del_flag
+    </sql>
+
+
+
+    <select id="load" resultMap="BaseResultMap">
+        SELECT <include refid="Base_Column_List" />
+        FROM kw_business_user
+        WHERE id = #{id} and del_flag = 0
+    </select>
+
+    <select id="selectOneByTenantId" resultMap="BaseResultMap">
+        SELECT <include refid="Base_Column_List" />
+        FROM kw_business_user
+        WHERE tenant_id = #{tenantId} and del_flag = 0
+    </select>
+</mapper>

+ 5 - 0
business-modules/business-usual-transport/src/main/java/com/sckw/usual/service/OrderActionService.java

@@ -89,6 +89,11 @@ public class OrderActionService {
      */
     public PageRes<KwOrderRes> orderPageList(OrderListParam orderListParam)
     {
+
+        String tenant = TenantUtil.getTenant();
+        if (StringUtils.isNotBlank(tenant)) {
+            orderListParam.setTenantId(tenant);
+        }
         PageHelper.startPage(orderListParam.getPage(), orderListParam.getPageSize());
         List<KwOrderRes> kwOrders = orderGeneralTransportMapper.orderList(orderListParam);