Skip to content

Commit

Permalink
新增 springboot小程序工具类。 整合成功
Browse files Browse the repository at this point in the history
  • Loading branch information
zenofung committed Mar 14, 2022
1 parent 9aacdbd commit f038c8a
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.
24 changes: 22 additions & 2 deletions mall-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>

<!-- redis默认使用lettuce客户端,排除这个引入jedis,解决堆外内存溢出 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.0.1</version>
</dependency>


<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
Expand Down Expand Up @@ -63,10 +83,10 @@
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
<!-- 小程序组件库-->
<!-- 小程序组件库-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<!-- &lt;!&ndash; 服务注册与发现 &ndash;&gt;-->
Expand Down
67 changes: 67 additions & 0 deletions wine/src/main/java/com/wine/game/wine/config/MyRedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wine.game.wine.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
* @description: # ┏┓   ┏┓+ +
* #   ┏┛┻━━━┛┻┓ + +
* #   ┃       ┃
* #   ┃   ━   ┃ ++ + + +
* #   ████━████ ┃+
* #   ┃       ┃ +
* #   ┃   ┻   ┃
* #   ┃       ┃ + +
* #   ┗━┓   ┏━┛
* #     ┃   ┃
* #     ┃   ┃ + + + +
* #     ┃   ┃    Codes are far away from bugs with the animal protecting
* #     ┃   ┃ +     神兽保佑,代码无bug
* #     ┃   ┃
* #     ┃   ┃  +
* #     ┃    ┗━━━┓ + +
* #     ┃        ┣┓
* #     ┃        ┏┛
* #     ┗┓┓┏━┳┓┏┛ + + + +
* #      ┃┫┫ ┃┫┫
* #      ┗┻┛ ┗┻┛+ + + +
* #
* @author: zeno fung
* @create: 2022-03-14 14:51
*/

@Configuration
public class MyRedisConfig {

@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);

// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

// 设置value的序列化规则和 key的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Arrays;
import java.util.Map;

import me.chanjar.weixin.common.bean.WxAccessToken;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand Down Expand Up @@ -60,6 +60,8 @@ public R info(@PathVariable("id") Integer id){
@RequestMapping("/save")
//@RequiresPermissions("wine:user:save")
public R save(@RequestBody UserEntity user){
// wxMaUserServiceImpl.getUserInfo();

userService.save(user);

return R.ok();
Expand Down
24 changes: 23 additions & 1 deletion wine/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@

## \u516C\u4F17\u53F7\u914D\u7F6E(\u5FC5\u586B)
#wx.miniapp.appid = wx1665a2834d959989
#wx.miniapp.secret = 3ac50cbfd17e19046d352ea560c6bd14
#wx.miniapp.token = @token
#wx.miniapp.aesKey = @aesKey
## \u6D88\u606F\u683C\u5F0F\uFF0CXML\u6216\u8005JSON.
#wx.miniapp.msgDataFormat = XML
## \u5B58\u50A8\u914D\u7F6Eredis(\u53EF\u9009)
## \u6CE8\u610F: \u6307\u5B9Aredis.host\u503C\u540E\u4E0D\u4F1A\u4F7F\u7528\u5BB9\u5668\u6CE8\u5165\u7684redis\u8FDE\u63A5(JedisPool)
## \u914D\u7F6E\u7C7B\u578B: Memory(\u9ED8\u8BA4), Jedis, RedisTemplate
#wx.miniapp.config-storage.type = redistemplate
# # \u76F8\u5173redis\u524D\u7F00\u914D\u7F6E: wa(\u9ED8\u8BA4)
#wx.miniapp.config-storage.key-prefix = wa
#wx.miniapp.config-storage.redis.host = 120.26.163.91
#wx.miniapp.config-storage.redis.password=123456
#wx.miniapp.config-storage.redis.port = 6379
#
## http\u5BA2\u6237\u7AEF\u914D\u7F6E # http\u5BA2\u6237\u7AEF\u7C7B\u578B: HttpClient(\u9ED8\u8BA4), OkHttp, JoddHttp
#wx.miniapp.config-storage.http-client-type=HttpClient
#wx.miniapp.config-storage.http-proxy-host=
#wx.miniapp.config-storage.http-proxy-port=
#wx.miniapp.config-storage.http-proxy-username=
#wx.miniapp.config-storage.http-proxy-password=
26 changes: 25 additions & 1 deletion wine/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ spring:
store-type: redis
redis:
host: 120.26.163.91
password: 123456
password:
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
cloud:
alicloud:
access-key: LTAI4FnPLQQ1sDJqMJbcn9Nq
Expand Down Expand Up @@ -98,3 +104,21 @@ mybatis-plus:
image:
targetPath: /mydata/nginx/html/images/
imageUrl: http://120.26.163.91/images/
wx:
miniapp:
appid: wx1665a2834d959989
secret: 3ac50cbfd17e19046d352ea560c6bd14
token:
aes-key:
msg-data-format: XML
config-storage:
type: redistemplate
redis:
host: 120.26.163.91
password:
port: 6379
http-client-type: httpclient
http-proxy-host:
http-proxy-port:
http-proxy-password:
http-proxy-username:
23 changes: 23 additions & 0 deletions wine/src/test/java/com/wine/game/wine/WineApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
package com.wine.game.wine;

import cn.binarywang.wx.miniapp.api.WxMaSchemeService;
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
class WineApplicationTests {

@Autowired
private RedisTemplate redisTemplate;

@Autowired
private WxMaService wxMaService;

@Test
void contextLoads() {
// redisTemplate.opsForValue().set("1","3");
try {
// String accessToken = wxMaService.getAccessToken();
boolean aa = wxMaService.getSecCheckService().checkMessage("特3456书yuuo莞6543李zxcz蒜7782法fgnv级");
boolean bb = wxMaService.getSecCheckService().checkMessage("草拟吗");
System.out.println("aa---->"+aa+"bb---->"+bb);
} catch (WxErrorException e) {
e.printStackTrace();
}

}

}

0 comments on commit f038c8a

Please sign in to comment.