init
This commit is contained in:
32
ruoyi-yunxin/pom.xml
Normal file
32
ruoyi-yunxin/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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>ruoyi-vue-plus</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>4.8.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-yunxin</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.ruoyi</groupId>
|
||||
<artifactId>ruoyi-framework</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.dtflys.forest</groupId>
|
||||
<artifactId>forest-spring-boot-starter</artifactId>
|
||||
<version>1.5.33</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
32
ruoyi-yunxin/src/main/java/com/ruoyi/yunxin/Yunxin.java
Normal file
32
ruoyi-yunxin/src/main/java/com/ruoyi/yunxin/Yunxin.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.ruoyi.yunxin;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.yunxin.client.ImMessageClient;
|
||||
import com.ruoyi.yunxin.config.YunxinProperties;
|
||||
import com.ruoyi.yunxin.req.Option;
|
||||
import com.ruoyi.yunxin.req.SendMsgReq;
|
||||
import com.ruoyi.yunxin.resp.SendMsgResp;
|
||||
import com.ruoyi.yunxin.resp.YxDataR;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Component
|
||||
public class Yunxin {
|
||||
|
||||
@Autowired
|
||||
private YunxinProperties yunxinProperties;
|
||||
@Resource
|
||||
private ImMessageClient messageClient;
|
||||
|
||||
public YxDataR<SendMsgResp> sendTo(Long toUid,Long fromUid,Object data){
|
||||
SendMsgReq req = new SendMsgReq();
|
||||
req.setFrom(fromUid == null ? yunxinProperties.getDefaultFromUid() : fromUid+"");
|
||||
req.setTo(toUid+"");
|
||||
req.setBody(JSON.toJSONString(data));
|
||||
req.setOption(JSON.toJSONString(new Option()));
|
||||
return messageClient.sendMsg(req);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ruoyi.yunxin.client;
|
||||
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.Post;
|
||||
import com.ruoyi.yunxin.interceptor.GlodonTokenInterceptor;
|
||||
import com.ruoyi.yunxin.req.SendMsgReq;
|
||||
import com.ruoyi.yunxin.resp.SendMsgResp;
|
||||
import com.ruoyi.yunxin.resp.YxDataR;
|
||||
|
||||
@BaseRequest(baseURL = "${baseUrl}", interceptor = GlodonTokenInterceptor.class)
|
||||
public interface ImMessageClient {
|
||||
|
||||
@Post(url = "/nimserver/msg/sendMsg.action")
|
||||
YxDataR<SendMsgResp> sendMsg(SendMsgReq req);
|
||||
|
||||
// @Post(url = "/nimserver/msg/sendAttachMsg.action")
|
||||
// YxR<SendMsgResp> sendAttachMsg(SendMsgReq req);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.ruoyi.yunxin.client;
|
||||
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.Body;
|
||||
import com.dtflys.forest.annotation.Post;
|
||||
import com.ruoyi.yunxin.interceptor.GlodonTokenInterceptor;
|
||||
import com.ruoyi.yunxin.req.BlockReq;
|
||||
import com.ruoyi.yunxin.req.CreateUserReq;
|
||||
import com.ruoyi.yunxin.req.UnblockReq;
|
||||
import com.ruoyi.yunxin.req.UpdateTokenReq;
|
||||
import com.ruoyi.yunxin.resp.YxCommonR;
|
||||
|
||||
@BaseRequest(baseURL = "${baseUrl}", interceptor = GlodonTokenInterceptor.class)
|
||||
public interface ImUserClient {
|
||||
|
||||
|
||||
@Post(url = "/nimserver/user/create.action")
|
||||
YxCommonR createUser(@Body CreateUserReq req);
|
||||
|
||||
@Post(url = "/nimserver/user/update.action")
|
||||
YxCommonR updateToken(@Body UpdateTokenReq req);
|
||||
|
||||
@Post(url = "/nimserver/user/block.action")
|
||||
YxCommonR block(@Body BlockReq req);
|
||||
|
||||
@Post(url = "/nimserver/user/unblock.action")
|
||||
YxCommonR unblock(@Body UnblockReq req);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ruoyi.yunxin.client;
|
||||
|
||||
import com.dtflys.forest.annotation.BaseRequest;
|
||||
import com.dtflys.forest.annotation.Body;
|
||||
import com.dtflys.forest.annotation.Post;
|
||||
import com.ruoyi.yunxin.interceptor.GlodonTokenInterceptor;
|
||||
import com.ruoyi.yunxin.req.CreateUserReq;
|
||||
import com.ruoyi.yunxin.req.UpdateUinfoReq;
|
||||
import com.ruoyi.yunxin.resp.YxCommonR;
|
||||
import com.ruoyi.yunxin.resp.YxInfoR;
|
||||
|
||||
@BaseRequest(baseURL = "${baseUrl}", interceptor = GlodonTokenInterceptor.class)
|
||||
public interface ImUserRefClient {
|
||||
|
||||
|
||||
@Post(url = "/nimserver/user/updateUinfo.action")
|
||||
YxCommonR updateUinfo(@Body UpdateUinfoReq query);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.ruoyi.yunxin.config;
|
||||
|
||||
import com.dtflys.forest.converter.json.ForestJacksonConverter;
|
||||
import com.dtflys.forest.converter.json.ForestJsonConverter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class ForestConfig {
|
||||
|
||||
@Bean
|
||||
public ForestJsonConverter forestJacksonConverter() {
|
||||
ForestJacksonConverter converter = new ForestJacksonConverter();
|
||||
ObjectMapper mapper = converter.getMapper();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
// converter.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.ruoyi.yunxin.config;
|
||||
|
||||
import com.dtflys.forest.Forest;
|
||||
import com.dtflys.forest.config.ForestConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
|
||||
@Component
|
||||
public class ForestInit {
|
||||
|
||||
@Autowired
|
||||
private YunxinProperties yunxinProperties;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
ForestConfiguration configuration = Forest.config();
|
||||
configuration.setVariableValue("baseUrl", (method) -> yunxinProperties.getBaseUrl());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.ruoyi.yunxin.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "yunxin")
|
||||
public class YunxinProperties {
|
||||
private String baseUrl = "https://api.netease.im/nimserver";
|
||||
private String appKey;
|
||||
private String appSecret;
|
||||
|
||||
private String defaultFromUid;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.ruoyi.yunxin.interceptor;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dtflys.forest.exceptions.ForestRuntimeException;
|
||||
import com.dtflys.forest.http.ForestRequest;
|
||||
import com.dtflys.forest.http.ForestResponse;
|
||||
import com.dtflys.forest.interceptor.Interceptor;
|
||||
import com.ruoyi.yunxin.config.YunxinProperties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GlodonTokenInterceptor implements Interceptor {
|
||||
|
||||
@Autowired
|
||||
private YunxinProperties yunxinProperties;
|
||||
|
||||
@Override
|
||||
public boolean beforeExecute(ForestRequest request) {
|
||||
String nonce = UUID.fastUUID().toString();
|
||||
String curTime = DateUtil.currentSeconds() + "";
|
||||
request.addHeader("AppKey",yunxinProperties.getAppKey());
|
||||
request.addHeader("Nonce", nonce);
|
||||
request.addHeader("CurTime", curTime);
|
||||
request.addHeader("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
|
||||
log.info("onSuccess URI:{},QueryValues:{},耗时:{}ms,Param:{},RespStatus:{},Response:{}",
|
||||
request.getURI(),
|
||||
JSON.toJSONString(request.getQueryValues()),
|
||||
response.getTimeAsMillisecond(),
|
||||
JSONObject.toJSONString(request.getArguments()),
|
||||
response.getStatusCode(),
|
||||
response.getContent());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(ForestRuntimeException ex, ForestRequest request, ForestResponse response) {
|
||||
log.info("onError URI:{},QueryValues:{},耗时:{}ms,Param:{},RespStatus:{},Response:{}",
|
||||
request.getURI(),
|
||||
JSON.toJSONString(request.getQueryValues()),
|
||||
response.getTimeAsMillisecond(),
|
||||
JSONObject.toJSONString(request.getArguments()),
|
||||
response.getStatusCode(),
|
||||
response.getContent());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class BlockReq {
|
||||
private String accid;
|
||||
private boolean needkick = true;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CreateUserReq {
|
||||
private String accid;
|
||||
private String token;
|
||||
private String name;
|
||||
|
||||
}
|
||||
27
ruoyi-yunxin/src/main/java/com/ruoyi/yunxin/req/Option.java
Normal file
27
ruoyi-yunxin/src/main/java/com/ruoyi/yunxin/req/Option.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Option {
|
||||
/**
|
||||
* 该消息是否需要APNS推送或安卓系统通知栏推送
|
||||
*/
|
||||
private boolean push = true;
|
||||
/**
|
||||
* 该消息是否需要漫游(需要app开通漫游消息功能)
|
||||
*/
|
||||
private boolean roam = false;
|
||||
/**
|
||||
* 该消息是否存云端历史
|
||||
*/
|
||||
private boolean history = false;
|
||||
/**
|
||||
* 该消息是否需要发送方多端同步
|
||||
*/
|
||||
private boolean sendersync = false;
|
||||
/**
|
||||
* 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
|
||||
*/
|
||||
private boolean route = false;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SendAttachMsgReq {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SendMsgReq {
|
||||
private String from;
|
||||
private int ope = 0;
|
||||
private String to;
|
||||
private int type = 100;
|
||||
private String body;
|
||||
private String option;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UnblockReq {
|
||||
private String accid;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateTokenReq {
|
||||
private String accid;
|
||||
private String token;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.yunxin.req;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateUinfoReq {
|
||||
private String accid;
|
||||
private String name;
|
||||
private String icon;
|
||||
private String birth;
|
||||
private String mobile;
|
||||
private String gender;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ruoyi.yunxin.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SendMsgResp {
|
||||
private Long msgid;
|
||||
private Long timetag;
|
||||
private Boolean antispam;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.yunxin.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class YxCommonR {
|
||||
private Integer code;
|
||||
private String desc;
|
||||
|
||||
public boolean isSuccess(){
|
||||
return code != null && code == 200;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.ruoyi.yunxin.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class YxDataR<T> implements Serializable {
|
||||
private Integer code;
|
||||
private T data;
|
||||
|
||||
public boolean isSuccess(){
|
||||
return code != null && code == 200;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ruoyi.yunxin.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class YxInfoR<T> {
|
||||
private Integer code;
|
||||
private T info;
|
||||
|
||||
public boolean isSuccess(){
|
||||
return code != null && code == 200;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.ruoyi.yunxin.util;
|
||||
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
public class RestTemplateUtil {
|
||||
|
||||
public static RestTemplate restTemplate;
|
||||
|
||||
static {
|
||||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
||||
requestFactory.setConnectTimeout(3000);
|
||||
requestFactory.setReadTimeout(3000);
|
||||
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
|
||||
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
|
||||
//添加转换器
|
||||
for (HttpMessageConverter<?> messageConverter : messageConverters) {
|
||||
if (messageConverter instanceof StringHttpMessageConverter) {
|
||||
StringHttpMessageConverter converter = (StringHttpMessageConverter) messageConverter;
|
||||
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user