diff --git a/ruoyi-site/ruoyi-h5/doc/h5_form_submission.sql b/ruoyi-site/ruoyi-h5/doc/h5_form_submission.sql new file mode 100644 index 000000000..5794cf159 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/doc/h5_form_submission.sql @@ -0,0 +1,13 @@ +CREATE TABLE `h5_form_submission` +( + id bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID', + project_id bigint NOT NULL COMMENT 'H5Project的ID', + ip_address bigint UNSIGNED DEFAULT NULL COMMENT '提交者IP地址', + user_agent varchar(512) DEFAULT NULL COMMENT '用户代理信息', + form_data JSON NOT NULL COMMENT '表单数据(JSON格式存储)', + create_time bigint not null comment '创建时间戳', + update_time bigint not null comment '更新时间戳', + PRIMARY KEY (id), + KEY `idx_project_id` (project_id) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 COMMENT ='表单提交记录表'; \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/doc/http/form.http b/ruoyi-site/ruoyi-h5/doc/http/form.http new file mode 100644 index 000000000..f75a35b03 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/doc/http/form.http @@ -0,0 +1,24 @@ +### 提交表单数据 +POST {{host}}/h5/form/submit +Content-Type: application/json + +{ + "projectId": 1, + "formData": { + "name": "张三", + "age": 18, + "email": "zhangsan@example.com" + } +} + +### 根据项目ID分页查询表单提交记录 +GET {{host}}/h5/form/page/project/1?pageNum=1&pageSize=10 + +### 根据项目ID分页查询表单提交记录(带过滤条件) +GET {{host}}/h5/form/page/project/1?pageNum=1&pageSize=10&name=张三 + +### 根据提交记录ID获取表单数据详情 +GET {{host}}/h5/form/detail/1 + +### 根据项目ID获取所有表单数据 +GET {{host}}/h5/form/list/project/1 diff --git a/ruoyi-site/ruoyi-h5/doc/http/http-client.env.json b/ruoyi-site/ruoyi-h5/doc/http/http-client.env.json new file mode 100644 index 000000000..1419b2241 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/doc/http/http-client.env.json @@ -0,0 +1,5 @@ +{ + "dev": { + "host": "http://localhost:19200" + } +} diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/FormController.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/FormController.java index f2989f503..6c50998d1 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/FormController.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/controller/FormController.java @@ -1,9 +1,10 @@ package com.luban.api.controller; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.luban.api.dto.H5FormSubmissionDTO; import com.luban.api.entity.FormSubmission; -import com.luban.api.mongo.entity.FormData; import com.luban.api.service.FormService; +import com.luban.api.util.JSON; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.servlet.http.HttpServletRequest; @@ -32,6 +33,9 @@ public class FormController { @Autowired private FormService formService; + @Autowired + private JSON json; + /** * 提交表单数据 * @@ -46,47 +50,31 @@ public class FormController { // 获取User-Agent String userAgent = httpRequest.getHeader("User-Agent"); - return formService.submitFormData( - request.getTemplateFormId(), - request.getFormId(), - request.getFormData(), - ipAddress, - userAgent - ); + H5FormSubmissionDTO dto = new H5FormSubmissionDTO(); + dto.setProjectId(request.getProjectId()); + dto.setFormData(json.toJSON(request.getFormData())); + dto.setIpAddress(JSON.ipToLong(ipAddress)); + dto.setUserAgent(userAgent); + return formService.submitFormData(dto); } /** - * 根据模板表单ID分页查询表单提交记录 + * 根据项目ID分页查询表单提交记录 * - * @param templateFormId 模板表单ID + * @param projectId 项目ID + * @param fieldFilters 字段过滤条件 * @param pageNum 页码 * @param pageSize 每页大小 * @return 表单提交记录分页数据 */ - @Operation(summary = "根据模板表单ID分页查询表单提交记录") - @GetMapping("/page/template/{templateFormId}") - public IPage pageByTemplateFormId( - @PathVariable Long templateFormId, - @RequestParam(defaultValue = "1") Integer pageNum, - @RequestParam(defaultValue = "10") Integer pageSize) { - return formService.pageFormSubmissionsByTemplateFormId(templateFormId, pageNum, pageSize); - } - - /** - * 根据表单ID分页查询表单提交记录 - * - * @param formId 表单ID - * @param pageNum 页码 - * @param pageSize 每页大小 - * @return 表单提交记录分页数据 - */ - @Operation(summary = "根据表单ID分页查询表单提交记录") - @GetMapping("/page/form/{formId}") - public IPage pageByFormId( - @PathVariable String formId, - @RequestParam(defaultValue = "1") Integer pageNum, - @RequestParam(defaultValue = "10") Integer pageSize) { - return formService.pageFormSubmissionsByFormId(formId, pageNum, pageSize); + @Operation(summary = "根据项目ID分页查询表单提交记录") + @PostMapping("/page/project/{projectId}") + public IPage pageByProjectId( + @PathVariable Long projectId, + @RequestBody(required = false) Map fieldFilters, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + return formService.pageFormSubmissionsByProjectId(projectId, fieldFilters, pageNum, pageSize); } /** @@ -97,38 +85,26 @@ public class FormController { */ @Operation(summary = "根据提交记录ID获取表单数据详情") @GetMapping("/detail/{submissionId}") - public FormData getFormDataDetail(@PathVariable Long submissionId) { + public H5FormSubmissionDTO getFormDataDetail(@PathVariable Long submissionId) { return formService.getFormDataDetail(submissionId); } /** - * 根据模板表单ID获取所有表单数据 + * 根据项目ID获取所有表单数据 * - * @param templateFormId 模板表单ID + * @param projectId 项目ID * @return 表单数据列表 */ - @Operation(summary = "根据模板表单ID获取所有表单数据") - @GetMapping("/list/template/{templateFormId}") - public List listByTemplateFormId(@PathVariable Long templateFormId) { - return formService.listFormDataByTemplateFormId(templateFormId); - } - - /** - * 根据表单ID获取所有表单数据 - * - * @param formId 表单ID - * @return 表单数据列表 - */ - @Operation(summary = "根据表单ID获取所有表单数据") - @GetMapping("/list/form/{formId}") - public List listByFormId(@PathVariable String formId) { - return formService.listFormDataByFormId(formId); + @Operation(summary = "根据项目ID获取所有表单数据") + @GetMapping("/list/project/{projectId}") + public List listByProjectId(@PathVariable Long projectId) { + return formService.listFormDataByProjectId(projectId); } /** * 获取客户端IP地址 * - * @param request HTTP请求 + * @param request HttpServletRequest对象 * @return 客户端IP地址 */ private String getClientIpAddress(HttpServletRequest request) { @@ -145,26 +121,10 @@ public class FormController { return ipAddress; } - /** - * 提交表单请求类 - */ - @Setter @Getter + @Setter public static class SubmitFormRequest { - /** - * 模板表单ID - */ - private Long templateFormId; - - /** - * 表单ID - */ - private String formId; - - /** - * 表单数据 ,key为字段id,value为字段值 - */ + private Long projectId; private Map formData; - } } diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/convert/FormSubmissionConvert.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/convert/FormSubmissionConvert.java new file mode 100644 index 000000000..324ae8ec0 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/convert/FormSubmissionConvert.java @@ -0,0 +1,67 @@ +package com.luban.api.convert; + +import com.luban.api.dto.H5FormSubmissionDTO; +import com.luban.api.entity.FormSubmission; +import org.springframework.beans.BeanUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * 表单提交转换器 + * + * @author your-name + */ +public class FormSubmissionConvert { + + /** + * 将FormSubmission实体转换为H5FormSubmissionDTO + * + * @param entity FormSubmission实体 + * @return H5FormSubmissionDTO + */ + public static H5FormSubmissionDTO toDTO(FormSubmission entity) { + if (entity == null) { + return null; + } + + H5FormSubmissionDTO dto = new H5FormSubmissionDTO(); + BeanUtils.copyProperties(entity, dto); + // 将数字形式的IP地址转换为字符串 + dto.setIpAddress(entity.getIpAddress()); + return dto; + } + + /** + * 将FormSubmission实体列表转换为H5FormSubmissionDTO列表 + * + * @param entities FormSubmission实体列表 + * @return H5FormSubmissionDTO列表 + */ + public static List toDTO(List entities) { + List dtoList = new ArrayList<>(); + if (entities == null) { + return dtoList; + } + + for (FormSubmission entity : entities) { + dtoList.add(toDTO(entity)); + } + return dtoList; + } + + /** + * 将H5FormSubmissionDTO转换为FormSubmission实体 + * + * @param dto H5FormSubmissionDTO + * @return FormSubmission实体 + */ + public static FormSubmission toEntity(H5FormSubmissionDTO dto) { + if (dto == null) { + return null; + } + FormSubmission entity = new FormSubmission(); + BeanUtils.copyProperties(dto, entity); + return entity; + } +} diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/FormSubmissionDao.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/FormSubmissionDao.java index 5b3a92093..c8df5edc8 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/FormSubmissionDao.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dao/FormSubmissionDao.java @@ -1,15 +1,30 @@ package com.luban.api.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; import com.luban.api.entity.FormSubmission; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.Map; /** - *

- * 表单提交记录 Mapper 接口 - *

+ * 表单提交记录 Mapper接口 * * @author your-name */ +@Mapper public interface FormSubmissionDao extends BaseMapper { - + /** + * 根据项目ID和字段过滤条件分页查询表单提交记录 + * + * @param page 分页对象 + * @param projectId 项目ID + * @param fieldFilters 字段过滤条件 + * @return 表单提交记录分页数据 + */ + IPage selectByProjectIdWithFieldFilters( + IPage page, + @Param("projectId") Long projectId, + @Param("fieldFilters") Map fieldFilters); } \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dto/H5FormSubmissionDTO.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dto/H5FormSubmissionDTO.java new file mode 100644 index 000000000..d8d2f9e06 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/dto/H5FormSubmissionDTO.java @@ -0,0 +1,47 @@ +package com.luban.api.dto; + +import lombok.Data; + +/** + * 表单数据传输对象 + * + * @author your-name + */ +@Data +public class H5FormSubmissionDTO { + + /** + * 主键ID + */ + private Long id; + + /** + * H5Project的ID + */ + private Long projectId; + + /** + * 提交者IP地址(IPv4为数字存储) + */ + private Long ipAddress; + + /** + * 用户代理信息 + */ + private String userAgent; + + /** + * 表单数据(JSON格式存储) + */ + private String formData; + + /** + * 创建时间戳 + */ + private Long createTime; + + /** + * 更新时间戳 + */ + private Long updateTime; +} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/entity/FormSubmission.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/entity/FormSubmission.java index 86a1aac63..2343d4c55 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/entity/FormSubmission.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/entity/FormSubmission.java @@ -31,27 +31,38 @@ public class FormSubmission implements Serializable { private Long id; /** - * 模板表单ID(对应H5Project的ID) + * H5Project的ID */ - private Long templateFormId; - - /** - * 表单ID(实例ID) - */ - private String formId; - - /** - * 提交时间戳 - */ - private Long submitTime; + @TableField("project_id") + private Long projectId; /** * 提交者IP地址 */ - private String ipAddress; + @TableField("ip_address") + private Long ipAddress; /** * 用户代理信息 */ + @TableField("user_agent") private String userAgent; -} + + /** + * 表单数据(JSON格式存储) + */ + @TableField("form_data") + private String formData; + + /** + * 创建时间戳 + */ + @TableField("create_time") + private Long createTime; + + /** + * 更新时间戳 + */ + @TableField("update_time") + private Long updateTime; +} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/dao/FormDataRepository.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/dao/FormDataRepository.java deleted file mode 100644 index aff80a748..000000000 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/dao/FormDataRepository.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.luban.api.mongo.dao; - -import com.luban.api.mongo.entity.FormData; -import org.springframework.data.mongodb.repository.MongoRepository; - -import java.util.List; - -/** - * 表单数据 Repository - * - * @author lizhw - */ -public interface FormDataRepository extends MongoRepository { - - /** - * 根据模板表单ID查询表单数据列表 - * - * @param templateFormId 模板表单ID - * @return 表单数据列表 - */ - List findByTemplateFormId(Long templateFormId); - - /** - * 根据表单ID查询表单数据列表 - * - * @param formId 表单ID - * @return 表单数据列表 - */ - List findByFormId(String formId); -} diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/entity/FormData.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/entity/FormData.java deleted file mode 100644 index 6d069952c..000000000 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/mongo/entity/FormData.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.luban.api.mongo.entity; - -import lombok.Data; -import org.springframework.data.annotation.Id; -import org.springframework.data.mongodb.core.mapping.Document; - -import java.util.Map; - -/** - * 表单数据实体类 (存储在MongoDB中) - * - * @author your-name - */ -@Data -@Document(collection = "h5_form_data") -public class FormData { - - /** - * 主键ID - */ - @Id - private Long id; - - /** - * 提交记录ID(对应MySQL中FormSubmission的ID) - */ - private Long submissionId; - - /** - * 模板表单ID(对应H5Project的ID) - */ - private Long templateFormId; - - /** - * 表单ID(实例ID) - */ - private String formId; - - /** - * 表单数据(key为formItem的prop,value为用户填写的内容) - */ - private Map formData; - - /** - * 提交时间戳 - */ - private Long submitTime; - - /** - * 提交者IP地址 - */ - private String ipAddress; - - /** - * 用户代理信息 - */ - private String userAgent; -} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/FormService.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/FormService.java index 653a48822..a78062103 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/FormService.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/FormService.java @@ -2,8 +2,8 @@ package com.luban.api.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; +import com.luban.api.dto.H5FormSubmissionDTO; import com.luban.api.entity.FormSubmission; -import com.luban.api.mongo.entity.FormData; import java.util.List; import java.util.Map; @@ -20,34 +20,21 @@ public interface FormService extends IService { /** * 提交表单数据 * - * @param templateFormId 模板表单ID - * @param formId 表单ID - * @param formData 表单数据 - * @param ipAddress IP地址 - * @param userAgent 用户代理 + * @param dto 表单数据传输对象 * @return 是否提交成功 */ - boolean submitFormData(Long templateFormId, String formId, Map formData, String ipAddress, String userAgent); + boolean submitFormData(H5FormSubmissionDTO dto); /** - * 根据模板表单ID分页查询表单提交记录 + * 根据项目ID分页查询表单提交记录,支持字段过滤 * - * @param templateFormId 模板表单ID + * @param projectId 项目ID + * @param fieldFilters 字段过滤条件 * @param pageNum 页码 * @param pageSize 每页大小 * @return 表单提交记录分页数据 */ - IPage pageFormSubmissionsByTemplateFormId(Long templateFormId, Integer pageNum, Integer pageSize); - - /** - * 根据表单ID分页查询表单提交记录 - * - * @param formId 表单ID - * @param pageNum 页码 - * @param pageSize 每页大小 - * @return 表单提交记录分页数据 - */ - IPage pageFormSubmissionsByFormId(String formId, Integer pageNum, Integer pageSize); + IPage pageFormSubmissionsByProjectId(Long projectId, Map fieldFilters, Integer pageNum, Integer pageSize); /** * 根据提交记录ID获取表单数据详情 @@ -55,21 +42,14 @@ public interface FormService extends IService { * @param submissionId 提交记录ID * @return 表单数据详情 */ - FormData getFormDataDetail(Long submissionId); + H5FormSubmissionDTO getFormDataDetail(Long submissionId); /** - * 根据模板表单ID获取所有表单数据 + * 根据项目ID获取所有表单数据 * - * @param templateFormId 模板表单ID + * @param projectId 项目ID * @return 表单数据列表 */ - List listFormDataByTemplateFormId(Long templateFormId); + List listFormDataByProjectId(Long projectId); - /** - * 根据表单ID获取所有表单数据 - * - * @param formId 表单ID - * @return 表单数据列表 - */ - List listFormDataByFormId(String formId); -} +} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/FormServiceImpl.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/FormServiceImpl.java index 133df774e..ff2a0ed88 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/FormServiceImpl.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/service/impl/FormServiceImpl.java @@ -1,15 +1,13 @@ package com.luban.api.service.impl; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.luban.api.convert.FormSubmissionConvert; import com.luban.api.dao.FormSubmissionDao; +import com.luban.api.dto.H5FormSubmissionDTO; import com.luban.api.entity.FormSubmission; -import com.luban.api.mongo.dao.FormDataRepository; -import com.luban.api.mongo.entity.FormData; import com.luban.api.service.FormService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,68 +24,43 @@ import java.util.Map; @Service public class FormServiceImpl extends ServiceImpl implements FormService { - @Autowired - private FormDataRepository formDataRepository; + private final FormSubmissionDao formSubmissionDao; + + public FormServiceImpl(FormSubmissionDao formSubmissionDao) { + this.formSubmissionDao = formSubmissionDao; + } @Override @Transactional(rollbackFor = Exception.class) - public boolean submitFormData(Long templateFormId, String formId, Map formData, String ipAddress, String userAgent) { - // 保存到MySQL - FormSubmission formSubmission = new FormSubmission(); - formSubmission.setTemplateFormId(templateFormId); - formSubmission.setFormId(formId); + public boolean submitFormData(H5FormSubmissionDTO dto) { + // 转换DTO为实体并保存到MySQL + FormSubmission entity = FormSubmissionConvert.toEntity(dto); + // 设置创建和更新时间 long currentTime = System.currentTimeMillis(); - formSubmission.setSubmitTime(currentTime); - formSubmission.setIpAddress(ipAddress); - formSubmission.setUserAgent(userAgent); - this.save(formSubmission); - - // 保存到MongoDB - FormData formDataEntity = new FormData(); - formDataEntity.setSubmissionId(formSubmission.getId()); - formDataEntity.setTemplateFormId(templateFormId); - formDataEntity.setFormId(formId); - formDataEntity.setFormData(formData); - formDataEntity.setSubmitTime(currentTime); - formDataEntity.setIpAddress(ipAddress); - formDataEntity.setUserAgent(userAgent); - formDataRepository.save(formDataEntity); - - return true; + entity.setId(null); + entity.setCreateTime(currentTime); + entity.setUpdateTime(currentTime); + return this.save(entity); } @Override - public IPage pageFormSubmissionsByTemplateFormId(Long templateFormId, Integer pageNum, Integer pageSize) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(FormSubmission::getTemplateFormId, templateFormId); - queryWrapper.orderByDesc(FormSubmission::getSubmitTime); - + public IPage pageFormSubmissionsByProjectId(Long projectId, Map fieldFilters, Integer pageNum, Integer pageSize) { IPage page = new Page<>(pageNum, pageSize); - return this.page(page, queryWrapper); + return formSubmissionDao.selectByProjectIdWithFieldFilters(page, projectId, fieldFilters); } @Override - public IPage pageFormSubmissionsByFormId(String formId, Integer pageNum, Integer pageSize) { - LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(FormSubmission::getFormId, formId); - queryWrapper.orderByDesc(FormSubmission::getSubmitTime); - - IPage page = new Page<>(pageNum, pageSize); - return this.page(page, queryWrapper); + public H5FormSubmissionDTO getFormDataDetail(Long submissionId) { + FormSubmission formSubmission = this.getById(submissionId); + return FormSubmissionConvert.toDTO(formSubmission); } @Override - public FormData getFormDataDetail(Long submissionId) { - return formDataRepository.findById(submissionId).orElse(null); + public List listFormDataByProjectId(Long projectId) { + List formSubmissions = this.lambdaQuery() + .eq(FormSubmission::getProjectId, projectId) + .list(); + return FormSubmissionConvert.toDTO(formSubmissions); } - @Override - public List listFormDataByTemplateFormId(Long templateFormId) { - return formDataRepository.findByTemplateFormId(templateFormId); - } - - @Override - public List listFormDataByFormId(String formId) { - return formDataRepository.findByFormId(formId); - } } diff --git a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/util/JSON.java b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/util/JSON.java index f756e40e8..b7842589f 100644 --- a/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/util/JSON.java +++ b/ruoyi-site/ruoyi-h5/src/main/java/com/luban/api/util/JSON.java @@ -9,6 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.List; import java.util.Map; @@ -63,4 +66,62 @@ public class JSON { } return null; } -} + + /** + * 将IP地址字符串转换为数字形式 + * + * @param ipAddress IP地址字符串,如 "192.168.1.1" + * @return IP地址的数字表示形式 + */ + public static Long ipToLong(String ipAddress) { + if (ipAddress == null || ipAddress.isEmpty()) { + return null; + } + + try { + InetAddress inetAddress = InetAddress.getByName(ipAddress); + byte[] bytes = inetAddress.getAddress(); + + if (bytes.length == 4) { + // IPv4 + long result = 0; + for (byte b : bytes) { + result = result << 8 | (b & 0xFF); + } + return result; + } else { + // IPv6 (简化处理) + return new BigInteger(bytes).longValue(); + } + } catch (UnknownHostException e) { + log.error("IP地址转换异常: " + ipAddress, e); + return null; + } + } + + /** + * 将数字形式的IP地址转换为字符串 + * + * @param ipLong 数字形式的IP地址 + * @return IP地址字符串 + */ + public static String longToIp(Long ipLong) { + if (ipLong == null) { + return null; + } + + try { + byte[] bytes = new byte[4]; + bytes[0] = (byte) ((ipLong >> 24) & 0xFF); + bytes[1] = (byte) ((ipLong >> 16) & 0xFF); + bytes[2] = (byte) ((ipLong >> 8) & 0xFF); + bytes[3] = (byte) (ipLong & 0xFF); + + InetAddress inetAddress = InetAddress.getByAddress(bytes); + return inetAddress.getHostAddress(); + } catch (UnknownHostException e) { + log.error("IP地址转换异常: " + ipLong, e); + return null; + } + } +} \ No newline at end of file diff --git a/ruoyi-site/ruoyi-h5/src/main/resources/mapper/ruoyi-h5/FormSubmissionDao.xml b/ruoyi-site/ruoyi-h5/src/main/resources/mapper/ruoyi-h5/FormSubmissionDao.xml new file mode 100644 index 000000000..922ae45c3 --- /dev/null +++ b/ruoyi-site/ruoyi-h5/src/main/resources/mapper/ruoyi-h5/FormSubmissionDao.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file