初始化
commit
28f0293588
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="holiday03-GTP" />
|
||||||
|
<module name="holiday03-common" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
<component name="JavacSettings">
|
||||||
|
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
|
||||||
|
<module name="holiday03-GTP" options="-parameters" />
|
||||||
|
<module name="holiday03-common" options="-parameters" />
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/holiday03-GTP/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/holiday03-common/src/main/java" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,36 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="JavaDoc" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<option name="TOP_LEVEL_CLASS_OPTIONS">
|
||||||
|
<value>
|
||||||
|
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||||
|
<option name="REQUIRED_TAGS" value="" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="INNER_CLASS_OPTIONS">
|
||||||
|
<value>
|
||||||
|
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||||
|
<option name="REQUIRED_TAGS" value="" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="METHOD_OPTIONS">
|
||||||
|
<value>
|
||||||
|
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||||
|
<option name="REQUIRED_TAGS" value="@return@param@throws or @exception" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="FIELD_OPTIONS">
|
||||||
|
<value>
|
||||||
|
<option name="ACCESS_JAVADOC_REQUIRED_FOR" value="none" />
|
||||||
|
<option name="REQUIRED_TAGS" value="" />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="IGNORE_DEPRECATED" value="false" />
|
||||||
|
<option name="IGNORE_JAVADOC_PERIOD" value="true" />
|
||||||
|
<option name="IGNORE_DUPLICATED_THROWS" value="false" />
|
||||||
|
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
|
||||||
|
<option name="myAdditionalJavadocTags" value="date" />
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="RemoteRepositoriesConfiguration">
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="central" />
|
||||||
|
<option name="name" value="Central Repository" />
|
||||||
|
<option name="url" value="http://124.221.214.183:8081/repository/maven-public/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,2 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
|
@ -0,0 +1,57 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>Three</artifactId>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>holiday03-GTP</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 系统公共 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>holiday03-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Web-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Druid -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>1.2.8</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mysql Connector -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Mybatis 依赖配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Pagehelper -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
<version>1.4.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.sikadi.user;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GTPApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GTPApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.sikadi.user.controller;
|
||||||
|
|
||||||
|
import com.bwie.common.result.Result;
|
||||||
|
import com.sikadi.user.service.GTPService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gtp回答
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class GTPController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GTPService gtpService;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送问题
|
||||||
|
*
|
||||||
|
* @param ques 问题
|
||||||
|
* @return 星火大模型的回答
|
||||||
|
*/
|
||||||
|
@GetMapping("/send")
|
||||||
|
public Result send(@RequestParam("ques") String ques) {
|
||||||
|
String s = gtpService.send(ques);
|
||||||
|
return Result.success(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.sikadi.user.dto;
|
||||||
|
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*工具类
|
||||||
|
**/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class MsgDTO {
|
||||||
|
/**
|
||||||
|
* 角色
|
||||||
|
*/
|
||||||
|
private String role;
|
||||||
|
/**
|
||||||
|
* 消息内容
|
||||||
|
*/
|
||||||
|
private String content;
|
||||||
|
/**
|
||||||
|
* 响应结果字段:结果序号,取值为[0,10]; 当前为保留字段,开发者可忽略
|
||||||
|
*/
|
||||||
|
private Integer index;
|
||||||
|
|
||||||
|
public static final String ROLE_USER = "user";
|
||||||
|
public static final String ROLE_ASSISTANT = "assistant";
|
||||||
|
|
||||||
|
public static MsgDTO createUserMsg(String content) {
|
||||||
|
return new MsgDTO(ROLE_USER, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MsgDTO createAssistantMsg(String content) {
|
||||||
|
return new MsgDTO(ROLE_ASSISTANT, content, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.sikadi.user.dto.request;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.annotation.JSONField;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.sikadi.user.dto.MsgDTO;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具类
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class RequestDTO {
|
||||||
|
|
||||||
|
@JsonProperty("header")
|
||||||
|
private HeaderDTO header;
|
||||||
|
@JsonProperty("parameter")
|
||||||
|
private ParameterDTO parameter;
|
||||||
|
@JsonProperty("payload")
|
||||||
|
private PayloadDTO payload;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class HeaderDTO {
|
||||||
|
/**
|
||||||
|
* 应用appid,从开放平台控制台创建的应用中获取
|
||||||
|
*/
|
||||||
|
@JSONField(name = "app_id")
|
||||||
|
private String appId;
|
||||||
|
/**
|
||||||
|
* 每个用户的id,用于区分不同用户,最大长度32
|
||||||
|
*/
|
||||||
|
@JSONField(name = "uid")
|
||||||
|
private String uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ParameterDTO {
|
||||||
|
private ChatDTO chat;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class ChatDTO {
|
||||||
|
/**
|
||||||
|
* 指定访问的领域,general指向V1.5版本 generalv2指向V2版本。注意:不同的取值对应的url也不一样!
|
||||||
|
*/
|
||||||
|
@JsonProperty("domain")
|
||||||
|
private String domain;
|
||||||
|
/**
|
||||||
|
* 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高
|
||||||
|
*/
|
||||||
|
@JsonProperty("temperature")
|
||||||
|
private Float temperature;
|
||||||
|
/**
|
||||||
|
* 模型回答的tokens的最大长度
|
||||||
|
*/
|
||||||
|
@JSONField(name = "max_tokens")
|
||||||
|
private Integer maxTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class PayloadDTO {
|
||||||
|
@JsonProperty("message")
|
||||||
|
private MessageDTO message;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class MessageDTO {
|
||||||
|
@JsonProperty("text")
|
||||||
|
private List<MsgDTO> text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
package com.sikadi.user.dto.response;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.sikadi.user.dto.MsgDTO;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 工具类
|
||||||
|
**/
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class ResponseDTO {
|
||||||
|
|
||||||
|
@JsonProperty("header")
|
||||||
|
private HeaderDTO header;
|
||||||
|
@JsonProperty("payload")
|
||||||
|
private PayloadDTO payload;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class HeaderDTO {
|
||||||
|
/**
|
||||||
|
* 错误码,0表示正常,非0表示出错
|
||||||
|
*/
|
||||||
|
@JsonProperty("code")
|
||||||
|
private Integer code;
|
||||||
|
/**
|
||||||
|
* 会话是否成功的描述信息
|
||||||
|
*/
|
||||||
|
@JsonProperty("message")
|
||||||
|
private String message;
|
||||||
|
/**
|
||||||
|
* 会话的唯一id,用于讯飞技术人员查询服务端会话日志使用,出现调用错误时建议留存该字段
|
||||||
|
*/
|
||||||
|
@JsonProperty("sid")
|
||||||
|
private String sid;
|
||||||
|
/**
|
||||||
|
* 会话状态,取值为[0,1,2];0代表首次结果;1代表中间结果;2代表最后一个结果
|
||||||
|
*/
|
||||||
|
@JsonProperty("status")
|
||||||
|
private Integer status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class PayloadDTO {
|
||||||
|
@JsonProperty("choices")
|
||||||
|
private ChoicesDTO choices;
|
||||||
|
/**
|
||||||
|
* 在最后一次结果返回
|
||||||
|
*/
|
||||||
|
@JsonProperty("usage")
|
||||||
|
private UsageDTO usage;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class ChoicesDTO {
|
||||||
|
/**
|
||||||
|
* 文本响应状态,取值为[0,1,2]; 0代表首个文本结果;1代表中间文本结果;2代表最后一个文本结果
|
||||||
|
*/
|
||||||
|
@JsonProperty("status")
|
||||||
|
private Integer status;
|
||||||
|
/**
|
||||||
|
* 返回的数据序号,取值为[0,9999999]
|
||||||
|
*/
|
||||||
|
@JsonProperty("seq")
|
||||||
|
private Integer seq;
|
||||||
|
/**
|
||||||
|
* 响应文本
|
||||||
|
*/
|
||||||
|
@JsonProperty("text")
|
||||||
|
private List<MsgDTO> text;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class UsageDTO {
|
||||||
|
@JsonProperty("text")
|
||||||
|
private TextDTO text;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
public static class TextDTO {
|
||||||
|
/**
|
||||||
|
* 保留字段,可忽略
|
||||||
|
*/
|
||||||
|
@JsonProperty("question_tokens")
|
||||||
|
private Integer questionTokens;
|
||||||
|
/**
|
||||||
|
* 包含历史问题的总tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("prompt_tokens")
|
||||||
|
private Integer promptTokens;
|
||||||
|
/**
|
||||||
|
* 回答的tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("completion_tokens")
|
||||||
|
private Integer completionTokens;
|
||||||
|
/**
|
||||||
|
* prompt_tokens和completion_tokens的和,也是本次交互计费的tokens大小
|
||||||
|
*/
|
||||||
|
@JsonProperty("total_tokens")
|
||||||
|
private Integer totalTokens;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,145 @@
|
||||||
|
package com.sikadi.user.listener;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.sikadi.user.dto.MsgDTO;
|
||||||
|
import com.sikadi.user.dto.request.RequestDTO;
|
||||||
|
import com.sikadi.user.mapper.XfXhConfig;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.*;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.crypto.Mac;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class XfXhStreamClient {
|
||||||
|
@Resource
|
||||||
|
private XfXhConfig xfXhConfig;
|
||||||
|
|
||||||
|
@Value("${xfxh.QPS}")
|
||||||
|
private int connectionTokenCount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取令牌
|
||||||
|
*/
|
||||||
|
public static int GET_TOKEN_STATUS = 0;
|
||||||
|
/**
|
||||||
|
* 归还令牌
|
||||||
|
*/
|
||||||
|
public static int BACK_TOKEN_STATUS = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 操作令牌
|
||||||
|
*
|
||||||
|
* @param status 0-获取令牌 1-归还令牌
|
||||||
|
* @return 是否操作成功
|
||||||
|
*/
|
||||||
|
public synchronized boolean operateToken(int status) {
|
||||||
|
if (status == GET_TOKEN_STATUS) {
|
||||||
|
// 获取令牌
|
||||||
|
if (connectionTokenCount != 0) {
|
||||||
|
// 说明还有令牌,将令牌数减一
|
||||||
|
connectionTokenCount -= 1;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 放回令牌
|
||||||
|
connectionTokenCount += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送消息
|
||||||
|
*
|
||||||
|
* @param uid 每个用户的id,用于区分不同用户
|
||||||
|
* @param msgList 发送给大模型的消息,可以包含上下文内容
|
||||||
|
* @return 获取websocket连接,以便于我们在获取完整大模型回复后手动关闭连接
|
||||||
|
*/
|
||||||
|
public WebSocket sendMsg(String uid, List<MsgDTO> msgList, WebSocketListener listener) {
|
||||||
|
// 获取鉴权url
|
||||||
|
String authUrl = this.getAuthUrl();
|
||||||
|
// 鉴权方法生成失败,直接返回 null
|
||||||
|
if (authUrl == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
|
||||||
|
// 将 https/http 连接替换为 ws/wss 连接
|
||||||
|
String url = authUrl.replace("http://", "ws://").replace("https://", "wss://");
|
||||||
|
Request request = new Request.Builder().url(url).build();
|
||||||
|
// 建立 wss 连接
|
||||||
|
WebSocket webSocket = okHttpClient.newWebSocket(request, listener);
|
||||||
|
// 组装请求参数
|
||||||
|
RequestDTO requestDTO = getRequestParam(uid, msgList);
|
||||||
|
// 发送请求
|
||||||
|
webSocket.send(JSONObject.toJSONString(requestDTO));
|
||||||
|
return webSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成鉴权方法,具体实现不用关心,这是讯飞官方定义的鉴权方式
|
||||||
|
*
|
||||||
|
* @return 鉴权访问大模型的路径
|
||||||
|
*/
|
||||||
|
public String getAuthUrl() {
|
||||||
|
try {
|
||||||
|
URL url = new URL(xfXhConfig.getHostUrl());
|
||||||
|
// 时间
|
||||||
|
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
|
||||||
|
format.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
|
String date = format.format(new Date());
|
||||||
|
// 拼接
|
||||||
|
String preStr = "host: " + url.getHost() + "\n" +
|
||||||
|
"date: " + date + "\n" +
|
||||||
|
"GET " + url.getPath() + " HTTP/1.1";
|
||||||
|
// SHA256加密
|
||||||
|
Mac mac = Mac.getInstance("hmacsha256");
|
||||||
|
SecretKeySpec spec = new SecretKeySpec(xfXhConfig.getApiSecret().getBytes(StandardCharsets.UTF_8), "hmacsha256");
|
||||||
|
mac.init(spec);
|
||||||
|
|
||||||
|
byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8));
|
||||||
|
// Base64加密
|
||||||
|
String sha = Base64.getEncoder().encodeToString(hexDigits);
|
||||||
|
// 拼接
|
||||||
|
String authorizationOrigin = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", xfXhConfig.getApiKey(), "hmac-sha256", "host date request-line", sha);
|
||||||
|
// 拼接地址
|
||||||
|
HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().
|
||||||
|
addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorizationOrigin.getBytes(StandardCharsets.UTF_8))).
|
||||||
|
addQueryParameter("date", date).
|
||||||
|
addQueryParameter("host", url.getHost()).
|
||||||
|
build();
|
||||||
|
|
||||||
|
return httpUrl.toString();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("鉴权方法中发生错误:" + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取请求参数
|
||||||
|
*
|
||||||
|
* @param uid 每个用户的id,用于区分不同用户
|
||||||
|
* @param msgList 发送给大模型的消息,可以包含上下文内容
|
||||||
|
* @return 请求DTO,该 DTO 转 json 字符串后生成的格式参考 resources/demo-json/request.json
|
||||||
|
*/
|
||||||
|
public RequestDTO getRequestParam(String uid, List<MsgDTO> msgList) {
|
||||||
|
RequestDTO requestDTO = new RequestDTO();
|
||||||
|
requestDTO.setHeader(new RequestDTO.HeaderDTO(xfXhConfig.getAppId(), uid));
|
||||||
|
requestDTO.setParameter(new RequestDTO.ParameterDTO(new RequestDTO.ParameterDTO.ChatDTO(xfXhConfig.getDomain(), xfXhConfig.getTemperature(), xfXhConfig.getMaxTokens())));
|
||||||
|
requestDTO.setPayload(new RequestDTO.PayloadDTO(new RequestDTO.PayloadDTO.MessageDTO(msgList)));
|
||||||
|
return requestDTO;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.sikadi.user.listener;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.alibaba.nacos.shaded.org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
import com.sikadi.user.dto.MsgDTO;
|
||||||
|
import com.sikadi.user.dto.response.ResponseDTO;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import okhttp3.Response;
|
||||||
|
import okhttp3.WebSocket;
|
||||||
|
import okhttp3.WebSocketListener;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class XfXhWebSocketListener extends WebSocketListener {
|
||||||
|
private StringBuilder answer = new StringBuilder();
|
||||||
|
|
||||||
|
private boolean wsCloseFlag = false;
|
||||||
|
|
||||||
|
public StringBuilder getAnswer() {
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWsCloseFlag() {
|
||||||
|
return wsCloseFlag;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
|
||||||
|
super.onOpen(webSocket, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
|
||||||
|
super.onMessage(webSocket, text);
|
||||||
|
// 将大模型回复的 JSON 文本转为 ResponseDTO 对象
|
||||||
|
ResponseDTO responseData = JSONObject.parseObject(text, ResponseDTO.class);
|
||||||
|
// 如果响应数据中的 header 的 code 值不为 0,则表示响应错误
|
||||||
|
if (responseData.getHeader().getCode() != 0) {
|
||||||
|
// 日志记录
|
||||||
|
log.error("发生错误,错误码为:" + responseData.getHeader().getCode() + "; " + "信息:" + responseData.getHeader().getMessage());
|
||||||
|
// 设置回答
|
||||||
|
this.answer = new StringBuilder("大模型响应错误,请稍后再试");
|
||||||
|
// 关闭连接标识
|
||||||
|
wsCloseFlag = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 将回答进行拼接
|
||||||
|
for (MsgDTO msgDTO : responseData.getPayload().getChoices().getText()) {
|
||||||
|
this.answer.append(msgDTO.getContent());
|
||||||
|
}
|
||||||
|
// 对最后一个文本结果进行处理
|
||||||
|
if (2 == responseData.getHeader().getStatus()) {
|
||||||
|
wsCloseFlag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
|
||||||
|
super.onFailure(webSocket, t, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
|
||||||
|
super.onClosed(webSocket, code, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.sikadi.user.mapper;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 爱意遂放弃
|
||||||
|
* @create 2022-10-15 10:46
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "xfxh")
|
||||||
|
@Data
|
||||||
|
public class XfXhConfig {
|
||||||
|
/**
|
||||||
|
* 服务引擎使用 讯飞星火认知大模型V2.0,如果使用 V1.5 需要将 hostUrl 修改为 https://spark-api.xf-yun.com/v1.1/chat
|
||||||
|
*/
|
||||||
|
private String hostUrl;
|
||||||
|
/**
|
||||||
|
* 发送请求时指定的访问领域,如果是 V1.5版本 设置为 general,如果是 V2版本 设置为 generalv2
|
||||||
|
*/
|
||||||
|
private String domain;
|
||||||
|
/**
|
||||||
|
* 核采样阈值。用于决定结果随机性,取值越高随机性越强即相同的问题得到的不同答案的可能性越高。取值 [0,1]
|
||||||
|
*/
|
||||||
|
private Float temperature;
|
||||||
|
/**
|
||||||
|
* 模型回答的tokens的最大长度,V1.5取值为[1,4096],V2.0取值为[1,8192]。
|
||||||
|
*/
|
||||||
|
private Integer maxTokens;
|
||||||
|
/**
|
||||||
|
* 大模型回复问题的最大响应时长,单位 s
|
||||||
|
*/
|
||||||
|
private Integer maxResponseTime;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String appId;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String apiKey;
|
||||||
|
/**
|
||||||
|
* 用于权限验证,从服务接口认证信息中获取
|
||||||
|
*/
|
||||||
|
private String apiSecret;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.sikadi.user.service;
|
||||||
|
|
||||||
|
public interface GTPService {
|
||||||
|
|
||||||
|
String send(String ques);
|
||||||
|
}
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.sikadi.user.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.sikadi.user.dto.MsgDTO;
|
||||||
|
import com.sikadi.user.listener.XfXhStreamClient;
|
||||||
|
import com.sikadi.user.listener.XfXhWebSocketListener;
|
||||||
|
import com.sikadi.user.mapper.XfXhConfig;
|
||||||
|
import com.sikadi.user.service.GTPService;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import okhttp3.WebSocket;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
@Log4j2
|
||||||
|
public class GTPServiceImpl implements GTPService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private XfXhStreamClient xfXhStreamClient;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private XfXhConfig xfXhConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String send(String ques) {
|
||||||
|
|
||||||
|
// 如果是无效字符串,则不对大模型进行请求
|
||||||
|
if (StrUtil.isBlank(ques)) {
|
||||||
|
return "无效问题,请重新输入";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建消息对象
|
||||||
|
MsgDTO msgDTO = MsgDTO.createUserMsg(ques);
|
||||||
|
// 创建监听器
|
||||||
|
XfXhWebSocketListener listener = new XfXhWebSocketListener();
|
||||||
|
// 发送问题给大模型,生成 websocket 连接
|
||||||
|
WebSocket webSocket = xfXhStreamClient.sendMsg(UUID.randomUUID().toString().substring(0, 10), Collections.singletonList(msgDTO), listener);
|
||||||
|
if (webSocket == null) {
|
||||||
|
// 归还令牌
|
||||||
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
|
return "系统内部错误,请联系管理员";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
int count = 0;
|
||||||
|
// 为了避免死循环,设置循环次数来定义超时时长
|
||||||
|
int maxCount = xfXhConfig.getMaxResponseTime() * 5;
|
||||||
|
while (count <= maxCount) {
|
||||||
|
Thread.sleep(200);
|
||||||
|
if (listener.isWsCloseFlag()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (count > maxCount) {
|
||||||
|
return "大模型响应超时,请联系管理员";
|
||||||
|
}
|
||||||
|
// 响应大模型的答案
|
||||||
|
return listener.getAnswer().toString();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("错误:" + e.getMessage());
|
||||||
|
return "系统内部错误,请联系管理员";
|
||||||
|
} finally {
|
||||||
|
// 关闭 websocket 连接
|
||||||
|
webSocket.close(1000, "");
|
||||||
|
// 归还令牌
|
||||||
|
xfXhStreamClient.operateToken(XfXhStreamClient.BACK_TOKEN_STATUS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9001
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
main:
|
||||||
|
allow-circular-references: true
|
||||||
|
jackson:
|
||||||
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
time-zone: GMT+8
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: sikadi-user
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 182.254.222.21:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 182.254.222.21:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
namespace: Sikadi
|
||||||
|
xfxh:
|
||||||
|
hostUrl: https://spark-api.xf-yun.com/v2.1/chat
|
||||||
|
domain: generalv2
|
||||||
|
temperature: 0.5
|
||||||
|
maxTokens: 2048
|
||||||
|
maxResponseTime: 30
|
||||||
|
QPS: 2
|
||||||
|
appId: 9f22c545
|
||||||
|
apiKey: a8e17cdcbc9addd7f21e81dff71bc177
|
||||||
|
apiSecret: N2NjMDU3ZTQwYjBkODI5ZGM3ZTVjYWY3
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9001
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
main:
|
||||||
|
allow-circular-references: true
|
||||||
|
jackson:
|
||||||
|
date-format: yyyy-MM-dd HH:mm:ss
|
||||||
|
time-zone: GMT+8
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: sikadi-user
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 182.254.222.21:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 182.254.222.21:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
namespace: Sikadi
|
||||||
|
xfxh:
|
||||||
|
hostUrl: https://spark-api.xf-yun.com/v2.1/chat
|
||||||
|
domain: generalv2
|
||||||
|
temperature: 0.5
|
||||||
|
maxTokens: 2048
|
||||||
|
maxResponseTime: 30
|
||||||
|
QPS: 2
|
||||||
|
appId: 9f22c545
|
||||||
|
apiKey: a8e17cdcbc9addd7f21e81dff71bc177
|
||||||
|
apiSecret: N2NjMDU3ZTQwYjBkODI5ZGM3ZTVjYWY3
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
||||||
|
artifactId=holiday03-GTP
|
||||||
|
groupId=com.bwie
|
||||||
|
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,20 @@
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO$PayloadDTO$UsageDTO.class
|
||||||
|
com\sikadi\user\service\impl\GTPServiceImpl.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO$ParameterDTO.class
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO$PayloadDTO$ChoicesDTO.class
|
||||||
|
com\sikadi\user\GTPApplication.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO.class
|
||||||
|
com\sikadi\user\listener\XfXhStreamClient.class
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO.class
|
||||||
|
com\sikadi\user\controller\GTPController.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO$ParameterDTO$ChatDTO.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO$PayloadDTO.class
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO$PayloadDTO.class
|
||||||
|
com\sikadi\user\service\GTPService.class
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO$HeaderDTO.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO$PayloadDTO$MessageDTO.class
|
||||||
|
com\sikadi\user\mapper\XfXhConfig.class
|
||||||
|
com\sikadi\user\dto\response\ResponseDTO$PayloadDTO$UsageDTO$TextDTO.class
|
||||||
|
com\sikadi\user\listener\XfXhWebSocketListener.class
|
||||||
|
com\sikadi\user\dto\MsgDTO.class
|
||||||
|
com\sikadi\user\dto\request\RequestDTO$HeaderDTO.class
|
|
@ -0,0 +1,10 @@
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\GTPApplication.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\mapper\XfXhConfig.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\listener\XfXhStreamClient.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\dto\response\ResponseDTO.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\listener\XfXhWebSocketListener.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\service\impl\GTPServiceImpl.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\service\GTPService.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\controller\GTPController.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\dto\request\RequestDTO.java
|
||||||
|
D:\Project\Three\holiday03-GTP\src\main\java\com\sikadi\user\dto\MsgDTO.java
|
|
@ -0,0 +1,162 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>Three</artifactId>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>holiday03-common</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- bootstrap 启动器 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Sentinel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 负载均衡-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Openfeign -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- JWT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
<version>0.9.1</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Fastjson -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.80</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Boot Redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Hibernate Validator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Apache Lang3 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- lombok依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 生成验证码 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.3</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 阿里大鱼 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>dysmsapi20170525</artifactId>
|
||||||
|
<version>2.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- rabbitMQ -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||||
|
<artifactId>jackson-dataformat-xml</artifactId>
|
||||||
|
<version>2.9.10</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Oss上传 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.10.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!--图片上传 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.tobato</groupId>
|
||||||
|
<artifactId>fastdfs-client</artifactId>
|
||||||
|
<version>1.26.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun.oss</groupId>
|
||||||
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
|
<version>3.11.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.18</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.67</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.java-websocket</groupId>
|
||||||
|
<artifactId>Java-WebSocket</artifactId>
|
||||||
|
<version>1.3.8</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>4.10.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName Constants
|
||||||
|
* @Description 系统常量
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 15:05
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class Constants {
|
||||||
|
/**
|
||||||
|
* 成功标记
|
||||||
|
*/
|
||||||
|
public static final Integer SUCCESS = 200;
|
||||||
|
public static final String SUCCESS_MSG = "操作成功";
|
||||||
|
/**
|
||||||
|
* 失败标记
|
||||||
|
*/
|
||||||
|
public static final Integer ERROR = 500;
|
||||||
|
public static final String ERROR_MSG = "操作异常";
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName JwtConstants
|
||||||
|
* @Description Jwt常量
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 15:07
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class JwtConstants {
|
||||||
|
/**
|
||||||
|
* 用户ID字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USER_ID = "user_id";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USERNAME = "username";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户标识
|
||||||
|
*/
|
||||||
|
public static final String USER_KEY = "user_key";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 令牌秘钥
|
||||||
|
*/
|
||||||
|
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.bwie.common.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName TokenConstants
|
||||||
|
* @Description 令牌常量
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 15:08
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class TokenConstants {
|
||||||
|
/**
|
||||||
|
* 缓存有效期,默认720(分钟)
|
||||||
|
*/
|
||||||
|
public final static long EXPIRATION = 720;
|
||||||
|
/**
|
||||||
|
* 缓存刷新时间,默认120(分钟)
|
||||||
|
*/
|
||||||
|
public final static long REFRESH_TIME = 120;
|
||||||
|
/**
|
||||||
|
* 权限缓存前缀
|
||||||
|
*/
|
||||||
|
public final static String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||||
|
/**
|
||||||
|
* token标识
|
||||||
|
*/
|
||||||
|
public static final String TOKEN = "token";
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.bwie.common.result;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName PageResult
|
||||||
|
* @Description 列表返回结果集
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 15:09
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PageResult<T> implements Serializable {
|
||||||
|
/**
|
||||||
|
* 总条数
|
||||||
|
*/
|
||||||
|
private long total;
|
||||||
|
/**
|
||||||
|
* 结果集合
|
||||||
|
*/
|
||||||
|
private List<T> list;
|
||||||
|
public PageResult() {
|
||||||
|
}
|
||||||
|
public PageResult(long total, List<T> list) {
|
||||||
|
this.total = total;
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
public static <T> PageResult<T> toPageResult(long total, List<T> list){
|
||||||
|
return new PageResult(total , list);
|
||||||
|
}
|
||||||
|
public static <T> Result<PageResult<T>> toResult(long total, List<T> list){
|
||||||
|
return Result.success(PageResult.toPageResult(total,list));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package com.bwie.common.result;
|
||||||
|
|
||||||
|
import com.bwie.common.constants.Constants;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName Result
|
||||||
|
* @Description 响应信息主体
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 15:08
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/** 成功 */
|
||||||
|
public static final int SUCCESS = Constants.SUCCESS;
|
||||||
|
/** 失败 */
|
||||||
|
public static final int FAIL = Constants.ERROR;
|
||||||
|
private int code;
|
||||||
|
private String msg;
|
||||||
|
private T data;
|
||||||
|
public static <T> Result<T> success() {
|
||||||
|
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> success(T data) {
|
||||||
|
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> success(T data, String msg) {
|
||||||
|
return restResult(data, SUCCESS, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error() {
|
||||||
|
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(String msg) {
|
||||||
|
return restResult(null, FAIL, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(T data) {
|
||||||
|
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(T data, String msg) {
|
||||||
|
return restResult(data, FAIL, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(int code, String msg) {
|
||||||
|
return restResult(null, code, msg);
|
||||||
|
}
|
||||||
|
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||||
|
Result<T> apiResult = new Result<>();
|
||||||
|
apiResult.setCode(code);
|
||||||
|
apiResult.setData(data);
|
||||||
|
apiResult.setMsg(msg);
|
||||||
|
return apiResult;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.DirectExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class DLXQueue {
|
||||||
|
// routingKey
|
||||||
|
private static final String DEAD_ROUTING_KEY = "dead.routingkey";
|
||||||
|
private static final String ROUTING_KEY = "routingkey";
|
||||||
|
private static final String DEAD_EXCHANGE = "dead.exchange";
|
||||||
|
private static final String EXCHANGE = "common.exchange";
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送死信队列,过期后进入死信交换机,进入死信队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param deadQueueName 死信队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 过期时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendDLXQueue(String queueName, String deadQueueName, Object params, Integer expiration) {
|
||||||
|
/**
|
||||||
|
* ----------------------------------先创建一个ttl队列和死信队列--------------------------------------------
|
||||||
|
*/
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
// 队列设置存活时间,单位ms, 必须是整形数据。
|
||||||
|
map.put("x-message-ttl", expiration);
|
||||||
|
// 设置死信交换机
|
||||||
|
map.put("x-dead-letter-exchange", DEAD_EXCHANGE);
|
||||||
|
// 设置死信交换器路由
|
||||||
|
map.put("x-dead-letter-routing-key", DEAD_ROUTING_KEY);
|
||||||
|
/*参数1:队列名称 参数2:持久化 参数3:是否排他 参数4:自动删除队列 参数5:队列参数*/
|
||||||
|
Queue queue = new Queue(queueName, true, false, false, map);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
/**
|
||||||
|
* ---------------------------------创建交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange directExchange = new DirectExchange(EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(directExchange);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(ROUTING_KEY);
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
/**
|
||||||
|
* ---------------------------------在创建一个死信交换机和队列,接收死信队列---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange deadExchange = new DirectExchange(DEAD_EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(deadExchange);
|
||||||
|
|
||||||
|
Queue deadQueue = new Queue(deadQueueName, true, false, false);
|
||||||
|
rabbitAdmin.declareQueue(deadQueue);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定死信交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding deadbinding = BindingBuilder.bind(deadQueue).to(deadExchange).with(DEAD_ROUTING_KEY);
|
||||||
|
rabbitAdmin.declareBinding(deadbinding);
|
||||||
|
// 发送消息
|
||||||
|
rabbitTemplate.convertAndSend(EXCHANGE, ROUTING_KEY, params);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.CustomExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送延迟队列的工具类
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class DelayedQueue {
|
||||||
|
|
||||||
|
// routingKey
|
||||||
|
private static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
|
||||||
|
|
||||||
|
// 延迟队列交换机
|
||||||
|
private static final String DELAYED_EXCHANGE = "delayed.exchange";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送延迟队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 延迟时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendDelayedQueue(String queueName, Object params, Integer expiration) {
|
||||||
|
// 先创建一个队列
|
||||||
|
Queue queue = new Queue(queueName);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
|
||||||
|
// 创建延迟队列交换机
|
||||||
|
CustomExchange customExchange = createCustomExchange();
|
||||||
|
rabbitAdmin.declareExchange(customExchange);
|
||||||
|
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(customExchange).with(DELAYED_ROUTING_KEY).noargs();
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
|
||||||
|
// 发送延迟消息
|
||||||
|
rabbitTemplate.convertAndSend(DELAYED_EXCHANGE, DELAYED_ROUTING_KEY, params, msg -> {
|
||||||
|
// 发送消息的时候 延迟时长
|
||||||
|
msg.getMessageProperties().setDelay(expiration);
|
||||||
|
return msg;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private CustomExchange createCustomExchange() {
|
||||||
|
Map<String, Object> arguments = new HashMap<>();
|
||||||
|
/**
|
||||||
|
* 参数说明:
|
||||||
|
* 1.交换机的名称
|
||||||
|
* 2.交换机的类型
|
||||||
|
* 3.是否需要持久化
|
||||||
|
* 4.是否自动删除
|
||||||
|
* 5.其它参数
|
||||||
|
*/
|
||||||
|
arguments.put("x-delayed-type", "direct");
|
||||||
|
return new CustomExchange(DELAYED_EXCHANGE, "x-delayed-message", true, false, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
|
||||||
|
import com.github.tobato.fastdfs.service.FastFileStorageClient;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @BelongsProject: 0107day02
|
||||||
|
* @BelongsPackage: com.bw.config
|
||||||
|
* @Author: zhupengfei
|
||||||
|
* @CreateTime: 2023-02-01 08:52
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class FastUtil {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(FastUtil.class);
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FastFileStorageClient storageClient ;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*/
|
||||||
|
public String upload(MultipartFile multipartFile) throws Exception {
|
||||||
|
String originalFilename = multipartFile.getOriginalFilename().
|
||||||
|
substring(multipartFile.getOriginalFilename().
|
||||||
|
lastIndexOf(".") + 1);
|
||||||
|
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
|
||||||
|
multipartFile.getInputStream(),
|
||||||
|
multipartFile.getSize(),originalFilename , null);
|
||||||
|
return storePath.getFullPath() ;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 删除文件
|
||||||
|
*/
|
||||||
|
public String deleteFile(String fileUrl) {
|
||||||
|
if (StringUtils.isEmpty(fileUrl)) {
|
||||||
|
log.info("fileUrl == >>文件路径为空...");
|
||||||
|
return "文件路径不能为空";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
StorePath storePath = StorePath.parseFromUrl(fileUrl);
|
||||||
|
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
return "删除成功";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,310 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.NameValuePair;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
|
import org.apache.http.client.methods.HttpDelete;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.client.methods.HttpPut;
|
||||||
|
import org.apache.http.conn.ClientConnectionManager;
|
||||||
|
import org.apache.http.conn.scheme.Scheme;
|
||||||
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
|
import org.apache.http.entity.ByteArrayEntity;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.TrustManager;
|
||||||
|
import javax.net.ssl.X509TrustManager;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.cert.X509Certificate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class HttpUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get
|
||||||
|
*
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doGet(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post form
|
||||||
|
*
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @param bodys
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doPost(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys,
|
||||||
|
Map<String, String> bodys)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bodys != null) {
|
||||||
|
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||||
|
|
||||||
|
for (String key : bodys.keySet()) {
|
||||||
|
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||||
|
}
|
||||||
|
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||||
|
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||||
|
request.setEntity(formEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post StringAppGTP
|
||||||
|
*
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doPost(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys,
|
||||||
|
String body)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(body)) {
|
||||||
|
request.setEntity(new StringEntity(body, "utf-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post stream
|
||||||
|
*
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doPost(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys,
|
||||||
|
byte[] body)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body != null) {
|
||||||
|
request.setEntity(new ByteArrayEntity(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put StringAppGTP
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doPut(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys,
|
||||||
|
String body)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(body)) {
|
||||||
|
request.setEntity(new StringEntity(body, "utf-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put stream
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @param body
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doPut(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys,
|
||||||
|
byte[] body)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body != null) {
|
||||||
|
request.setEntity(new ByteArrayEntity(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete
|
||||||
|
*
|
||||||
|
* @param host
|
||||||
|
* @param path
|
||||||
|
* @param method
|
||||||
|
* @param headers
|
||||||
|
* @param querys
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static HttpResponse doDelete(String host, String path, String method,
|
||||||
|
Map<String, String> headers,
|
||||||
|
Map<String, String> querys)
|
||||||
|
throws Exception {
|
||||||
|
HttpClient httpClient = wrapClient(host);
|
||||||
|
|
||||||
|
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||||
|
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||||
|
request.addHeader(e.getKey(), e.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient.execute(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||||
|
StringBuilder sbUrl = new StringBuilder();
|
||||||
|
sbUrl.append(host);
|
||||||
|
if (!StringUtils.isBlank(path)) {
|
||||||
|
sbUrl.append(path);
|
||||||
|
}
|
||||||
|
if (null != querys) {
|
||||||
|
StringBuilder sbQuery = new StringBuilder();
|
||||||
|
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||||
|
if (0 < sbQuery.length()) {
|
||||||
|
sbQuery.append("&");
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||||
|
sbQuery.append(query.getValue());
|
||||||
|
}
|
||||||
|
if (!StringUtils.isBlank(query.getKey())) {
|
||||||
|
sbQuery.append(query.getKey());
|
||||||
|
if (!StringUtils.isBlank(query.getValue())) {
|
||||||
|
sbQuery.append("=");
|
||||||
|
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (0 < sbQuery.length()) {
|
||||||
|
sbUrl.append("?").append(sbQuery);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sbUrl.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HttpClient wrapClient(String host) {
|
||||||
|
HttpClient httpClient = new DefaultHttpClient();
|
||||||
|
if (host.startsWith("https://")) {
|
||||||
|
sslClient(httpClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sslClient(HttpClient httpClient) {
|
||||||
|
try {
|
||||||
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
|
X509TrustManager tm = new X509TrustManager() {
|
||||||
|
public X509Certificate[] getAcceptedIssuers() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||||
|
|
||||||
|
}
|
||||||
|
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
ctx.init(null, new TrustManager[] { tm }, null);
|
||||||
|
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||||
|
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
|
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||||
|
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||||
|
registry.register(new Scheme("https", 443, ssf));
|
||||||
|
} catch (KeyManagementException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
} catch (NoSuchAlgorithmException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,106 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.bwie.common.constants.JwtConstants;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName JwtUtils
|
||||||
|
* @Description Jwt工具类
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 16:10
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class JwtUtils {
|
||||||
|
public static String secret = JwtConstants.SECRET;
|
||||||
|
/**
|
||||||
|
* 从数据声明生成令牌
|
||||||
|
*
|
||||||
|
* @param claims 数据声明
|
||||||
|
* @return 令牌
|
||||||
|
*/
|
||||||
|
public static String createToken(Map<String, Object> claims){
|
||||||
|
String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 从令牌中获取数据声明
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 数据声明
|
||||||
|
*/
|
||||||
|
public static Claims parseToken(String token){
|
||||||
|
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户ID
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户ID
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户名
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户名
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取键值
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public static String getValue(Claims claims, String key){
|
||||||
|
Object obj = claims.get(key);
|
||||||
|
return obj == null ? "" : obj.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class MsgUtil {
|
||||||
|
|
||||||
|
|
||||||
|
public static void sendMsg(String phone, String code){
|
||||||
|
String host = "http://dingxin.market.alicloudapi.com";
|
||||||
|
String path = "/dx/sendSms";
|
||||||
|
String method = "POST";
|
||||||
|
//StringAppGTP appcode = "0d534f614a8647ca94d697eec974aa63";
|
||||||
|
String appcode = "dcb3dabebd3843a7a138901659ed112a";
|
||||||
|
Map<String, String> headers = new HashMap<String, String>();
|
||||||
|
headers.put("Authorization", "APPCODE " + appcode);
|
||||||
|
Map<String, String> querys = new HashMap<String, String>();
|
||||||
|
querys.put("mobile", phone);
|
||||||
|
querys.put("param", "code:"+code);
|
||||||
|
querys.put("tpl_id", "TP1711063");
|
||||||
|
Map<String, String> bodys = new HashMap<String, String>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
||||||
|
System.out.println(response.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,153 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.aliyun.oss.model.GetObjectRequest;
|
||||||
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oss服务调用
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class OssUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
|
||||||
|
*/
|
||||||
|
private static String endPoint = "oss-cn-shanghai.aliyuncs.com";
|
||||||
|
private static String accessKeyId = "LTAI5tPHcqEdn49yrDCNpLhb";
|
||||||
|
private static String accessKeySecret = "C29u7EmS0TFlgMBzVszvuPFUxsKJuW";
|
||||||
|
private static String accessPre = "https://liushiqian.oss-cn-shanghai.aliyuncs.com/";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bucket名称
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static String bucketName = "liushiqian";
|
||||||
|
|
||||||
|
private static OSS ossClient ;
|
||||||
|
|
||||||
|
static {
|
||||||
|
ossClient = new OSSClientBuilder().build(
|
||||||
|
endPoint,
|
||||||
|
accessKeyId,
|
||||||
|
accessKeySecret);
|
||||||
|
log.info("oss服务连接成功!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认路径上传本地文件
|
||||||
|
* @param filePath
|
||||||
|
*/
|
||||||
|
public static String uploadFile(String filePath){
|
||||||
|
return uploadFileForBucket(bucketName,getOssFilePath(filePath) ,filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认路径上传multipartFile文件
|
||||||
|
* @param multipartFile
|
||||||
|
*/
|
||||||
|
public static String uploadMultipartFile(MultipartFile multipartFile) {
|
||||||
|
return uploadMultipartFile(bucketName,getOssFilePath(multipartFile.getOriginalFilename()),multipartFile);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 上传 multipartFile 类型文件
|
||||||
|
* @param bucketName
|
||||||
|
* @param ossPath
|
||||||
|
* @param multipartFile
|
||||||
|
*/
|
||||||
|
public static String uploadMultipartFile(String bucketName , String ossPath , MultipartFile multipartFile){
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
inputStream = multipartFile.getInputStream();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||||
|
return accessPre+ossPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
|
||||||
|
* @param bucketName 实例名称
|
||||||
|
* @param ossPath oss存储路径
|
||||||
|
* @param filePath 本地文件路径
|
||||||
|
*/
|
||||||
|
public static String uploadFileForBucket(String bucketName , String ossPath , String filePath) {
|
||||||
|
// 创建PutObjectRequest对象。
|
||||||
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
|
||||||
|
|
||||||
|
// 上传
|
||||||
|
ossClient.putObject(putObjectRequest);
|
||||||
|
return accessPre+ossPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用文件流上传到指定的bucket实例
|
||||||
|
* @param bucketName 实例名称
|
||||||
|
* @param ossPath oss存储路径
|
||||||
|
* @param filePath 本地文件路径
|
||||||
|
*/
|
||||||
|
public static String uploadFileInputStreamForBucket(String bucketName , String ossPath , String filePath){
|
||||||
|
|
||||||
|
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
inputStream = new FileInputStream(filePath);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
|
||||||
|
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
|
||||||
|
return accessPre+ossPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void uploadFileInputStreamForBucket(String bucketName , String ossPath , InputStream inputStream ){
|
||||||
|
ossClient.putObject(bucketName, ossPath, inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载
|
||||||
|
* @param ossFilePath
|
||||||
|
* @param filePath
|
||||||
|
*/
|
||||||
|
public static void downloadFile(String ossFilePath , String filePath ){
|
||||||
|
downloadFileForBucket(bucketName , ossFilePath , filePath);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 下载
|
||||||
|
* @param bucketName 实例名称
|
||||||
|
* @param ossFilePath oss存储路径
|
||||||
|
* @param filePath 本地文件路径
|
||||||
|
*/
|
||||||
|
public static void downloadFileForBucket(String bucketName , String ossFilePath , String filePath ){
|
||||||
|
ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getOssDefaultPath(){
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
String url =
|
||||||
|
now.getYear()+"/"+
|
||||||
|
now.getMonth()+"/"+
|
||||||
|
now.getDayOfMonth()+"/"+
|
||||||
|
now.getHour()+"/"+
|
||||||
|
now.getMinute()+"/";
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getOssFilePath(String filePath){
|
||||||
|
String fileSuf = filePath.substring(filePath.indexOf(".") + 1);
|
||||||
|
return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ClassName StringUtils
|
||||||
|
* @Description 字符串处理工具类
|
||||||
|
* @Author Lichuang
|
||||||
|
* @Date 2023/10/3 16:11
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个对象是否为空
|
||||||
|
*
|
||||||
|
* @param object Object
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isNull(Object object) {
|
||||||
|
return object == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||||
|
*
|
||||||
|
* @param coll 要判断的Collection
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Collection<?> coll) {
|
||||||
|
return isNull(coll) || coll.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||||
|
*
|
||||||
|
* @param str 指定字符串
|
||||||
|
* @param strs 需要检查的字符串数组
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
public static boolean matches(String str, List<String> strs) {
|
||||||
|
if (isEmpty(str) || isEmpty(strs)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String pattern : strs) {
|
||||||
|
if (isMatch(pattern, str))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断url是否与规则配置:
|
||||||
|
* ? 表示单个字符;
|
||||||
|
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||||
|
* ** 表示任意层路径;
|
||||||
|
*
|
||||||
|
* @param pattern 匹配规则
|
||||||
|
* @param url 需要匹配的url
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(String pattern, String url) {
|
||||||
|
AntPathMatcher matcher = new AntPathMatcher();
|
||||||
|
return matcher.match(pattern, url);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.aliyun.dysmsapi20170525.Client;
|
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||||
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信工具类
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class TelSmsUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限
|
||||||
|
*/
|
||||||
|
private static String accessKeyId = "LTAIEVXszCmcd1T5";
|
||||||
|
private static String accessKeySecret = "2zHwciQXln8wExSEnkIYtRTSwLeRNd";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信访问域名
|
||||||
|
*/
|
||||||
|
private static String endpoint = "dysmsapi.aliyuncs.com";
|
||||||
|
/**
|
||||||
|
* 短信签名
|
||||||
|
*/
|
||||||
|
private static String signName = "登录验证";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实例化短信对象
|
||||||
|
*/
|
||||||
|
private static Client client;
|
||||||
|
|
||||||
|
static {
|
||||||
|
log.info("初始化短信服务开始");
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
client = initClient();
|
||||||
|
log.info("初始化短信成功:{}",signName);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
log.info("初始化短信服务结束:耗时:{}MS",(System.currentTimeMillis()-startTime));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 初始化短信对象
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private static Client initClient() throws Exception {
|
||||||
|
Config config = new Config()
|
||||||
|
// 您的AccessKey ID
|
||||||
|
.setAccessKeyId(accessKeyId)
|
||||||
|
// 您的AccessKey Secret
|
||||||
|
.setAccessKeySecret(accessKeySecret);
|
||||||
|
// 访问的域名
|
||||||
|
config.endpoint = endpoint;
|
||||||
|
return new Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送单条短信
|
||||||
|
* @param tel
|
||||||
|
* @param templateCode SMS_153991546
|
||||||
|
* @param sendDataMap
|
||||||
|
*/
|
||||||
|
public static String sendSms(String tel , String templateCode , Map<String, String> sendDataMap){
|
||||||
|
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||||
|
.setPhoneNumbers(tel)
|
||||||
|
.setSignName(signName)
|
||||||
|
.setTemplateCode(templateCode)
|
||||||
|
.setTemplateParam(JSONObject.toJSONString(sendDataMap));
|
||||||
|
SendSmsResponse sendSmsResponse = null;
|
||||||
|
try {
|
||||||
|
log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap));
|
||||||
|
sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e);
|
||||||
|
}
|
||||||
|
return JSONObject.toJSONString(sendSmsResponse.getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
package com.bwie.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.DirectExchange;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送TTL队列 设置 消息的存活时间 如果超过了存活时间
|
||||||
|
* 该条消息还没有被消费 则自动从队列中消息 ,如果配置了死信队列则消息会进入死信队列
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class TtlQueue {
|
||||||
|
// routingKey
|
||||||
|
private static final String TTL_KEY = "ttl.routingkey";
|
||||||
|
private static final String TTL_EXCHANGE = "ttl.exchange";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
RabbitAdmin rabbitAdmin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送TTL队列
|
||||||
|
*
|
||||||
|
* @param queueName 队列名称
|
||||||
|
* @param params 消息内容
|
||||||
|
* @param expiration 过期时间 毫秒
|
||||||
|
*/
|
||||||
|
public void sendTtlQueue(String queueName, Object params, Integer expiration) {
|
||||||
|
/**
|
||||||
|
* ----------------------------------先创建一个ttl队列--------------------------------------------
|
||||||
|
*/
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
// 队列设置存活时间,单位ms,必须是整形数据。
|
||||||
|
map.put("x-message-ttl", expiration);
|
||||||
|
/*参数1:队列名称 参数2:持久化 参数3:是否排他 参数4:自动删除队列 参数5:队列参数*/
|
||||||
|
Queue queue = new Queue(queueName, true, false, false, map);
|
||||||
|
rabbitAdmin.declareQueue(queue);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ---------------------------------创建交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
DirectExchange directExchange = new DirectExchange(TTL_EXCHANGE, true, false);
|
||||||
|
rabbitAdmin.declareExchange(directExchange);
|
||||||
|
/**
|
||||||
|
* ---------------------------------队列绑定交换机---------------------------------------------
|
||||||
|
*/
|
||||||
|
// 将队列和交换机绑定
|
||||||
|
Binding binding = BindingBuilder.bind(queue).to(directExchange).with(TTL_KEY);
|
||||||
|
rabbitAdmin.declareBinding(binding);
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
rabbitTemplate.convertAndSend(TTL_EXCHANGE, TTL_KEY, params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,3 @@
|
||||||
|
artifactId=holiday03-common
|
||||||
|
groupId=com.bwie
|
||||||
|
version=1.0-SNAPSHOT
|
|
@ -0,0 +1,16 @@
|
||||||
|
com\bwie\common\result\PageResult.class
|
||||||
|
com\bwie\common\utils\DelayedQueue.class
|
||||||
|
com\bwie\common\utils\MsgUtil.class
|
||||||
|
com\bwie\common\utils\TelSmsUtils.class
|
||||||
|
com\bwie\common\utils\HttpUtils.class
|
||||||
|
com\bwie\common\utils\JwtUtils.class
|
||||||
|
com\bwie\common\utils\StringUtils.class
|
||||||
|
com\bwie\common\constants\JwtConstants.class
|
||||||
|
com\bwie\common\utils\HttpUtils$1.class
|
||||||
|
com\bwie\common\constants\Constants.class
|
||||||
|
com\bwie\common\utils\TtlQueue.class
|
||||||
|
com\bwie\common\utils\DLXQueue.class
|
||||||
|
com\bwie\common\constants\TokenConstants.class
|
||||||
|
com\bwie\common\utils\OssUtil.class
|
||||||
|
com\bwie\common\result\Result.class
|
||||||
|
com\bwie\common\utils\FastUtil.class
|
|
@ -0,0 +1,15 @@
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\constants\JwtConstants.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\OssUtil.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\constants\TokenConstants.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\DelayedQueue.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\JwtUtils.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\TelSmsUtils.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\result\PageResult.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\HttpUtils.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\StringUtils.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\TtlQueue.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\DLXQueue.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\FastUtil.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\constants\Constants.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\result\Result.java
|
||||||
|
D:\Project\Three\holiday03-common\src\main\java\com\bwie\common\utils\MsgUtil.java
|
|
@ -0,0 +1,71 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>Three</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<modules>
|
||||||
|
<module>holiday03-common</module>
|
||||||
|
<module>holiday03-GTP</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<!-- 规定SpringBoot版本 -->
|
||||||
|
<!-- 父级pom文件 主要用于规定项目依赖的各个版本,用于进行项目版本约束 -->
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<version>2.6.2</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
<!-- 依赖声明 -->
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<!-- SpringCloud 微服务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.0</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!--- SpringCloud Alibaba 微服务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Nacos 配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.nacos</groupId>
|
||||||
|
<artifactId>nacos-client</artifactId>
|
||||||
|
<version>2.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bwie</groupId>
|
||||||
|
<artifactId>holiday03-common</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<!-- 私服路径-->
|
||||||
|
<distributionManagement>
|
||||||
|
<repository>
|
||||||
|
<id>menghang-releases</id>
|
||||||
|
<name>梦航-maven</name>
|
||||||
|
<url>http://124.221.214.183:8081/repository/maven-releases/</url>
|
||||||
|
</repository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
|
</project>
|
Loading…
Reference in New Issue