|
|
- # 小程序模块配置说明
-
- ## 概述
- 本文档说明小程序登录服务需要配置的内容,包括数据库、Redis、微信配置等。
-
- ## 1. 数据库配置
-
- ### 1.1 数据库表结构
- 需要确保 `applet_user` 表已创建,表结构如下:
-
- ```sql
- CREATE TABLE `applet_user` (
- `id` varchar(32) NOT NULL COMMENT '主键',
- `create_by` varchar(50) DEFAULT NULL COMMENT '创建人',
- `create_time` datetime DEFAULT NULL COMMENT '创建日期',
- `update_by` varchar(50) DEFAULT NULL COMMENT '更新人',
- `update_time` datetime DEFAULT NULL COMMENT '更新日期',
- `sys_org_code` varchar(64) DEFAULT NULL COMMENT '所属部门',
- `name` varchar(100) DEFAULT NULL COMMENT '昵称',
- `openid` varchar(100) DEFAULT NULL COMMENT '第三方认证id',
- `phone` varchar(20) DEFAULT NULL COMMENT '手机号',
- `bmi` decimal(10,2) DEFAULT NULL COMMENT '体总指数',
- `fat` decimal(10,2) DEFAULT NULL COMMENT '脂肪',
- `avatar` varchar(500) DEFAULT NULL COMMENT '头像',
- PRIMARY KEY (`id`),
- UNIQUE KEY `uk_openid` (`openid`),
- UNIQUE KEY `uk_phone` (`phone`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='小程序用户表';
- ```
-
- ### 1.2 数据库连接配置
- 在 `application.yml` 中配置数据库连接:
-
- ```yaml
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/jeecg-boot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
- username: root
- password: root
- driver-class-name: com.mysql.cj.jdbc.Driver
- ```
-
- ## 2. Redis配置
-
- ### 2.1 Redis连接配置
- 在 `application.yml` 中配置Redis:
-
- ```yaml
- spring:
- redis:
- host: localhost
- port: 6379
- password:
- database: 0
- timeout: 10000ms
- lettuce:
- pool:
- max-active: 8
- max-wait: -1ms
- max-idle: 8
- min-idle: 0
- ```
-
- ### 2.2 Redis配置类
- 需要创建Redis配置类:
-
- ```java
- @Configuration
- public class RedisConfig {
-
- @Bean
- public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
- RedisTemplate<String, Object> template = new RedisTemplate<>();
- template.setConnectionFactory(factory);
-
- // 设置key的序列化方式
- template.setKeySerializer(new StringRedisSerializer());
- // 设置value的序列化方式
- template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
- // 设置hash key的序列化方式
- template.setHashKeySerializer(new StringRedisSerializer());
- // 设置hash value的序列化方式
- template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
-
- template.afterPropertiesSet();
- return template;
- }
- }
- ```
-
- ## 3. 微信小程序配置
-
- ### 3.1 微信配置
- 在 `application.yml` 中配置微信小程序信息:
-
- ```yaml
- wechat:
- mpAppId: your_applet_appid
- mpAppSecret: your_applet_secret
- merchantId: your_merchant_id # 如果需要支付功能
- ```
-
- ### 3.2 微信配置类
- 确保 `WxHttpUtils` 类能正确读取配置:
-
- ```java
- @Component
- public class WxHttpUtils {
- @Value("${wechat.mpAppId}")
- private String appid;
-
- @Value("${wechat.mpAppSecret}")
- private String secret;
- }
- ```
-
- ## 4. JWT配置
-
- ### 4.1 JWT密钥配置
- 在 `application.yml` 中配置JWT密钥:
-
- ```yaml
- jeecg:
- jwt:
- secret: your_jwt_secret_key
- expire: 604800 # 7天,单位秒
- ```
-
- ### 4.2 JWT工具类配置
- 确保 `JwtUtil` 类能正确读取配置:
-
- ```java
- @Component
- public class JwtUtil {
- @Value("${jeecg.jwt.secret}")
- private String secret;
-
- @Value("${jeecg.jwt.expire}")
- private long expire;
- }
- ```
-
- ## 5. 日志配置
-
- ### 5.1 日志级别配置
- 在 `application.yml` 中配置日志级别:
-
- ```yaml
- logging:
- level:
- org.jeecg.modules.applet: DEBUG
- org.jeecg.modules.common.wxUtils: DEBUG
- ```
-
- ## 6. 安全配置
-
- ### 6.1 CORS配置
- 如果需要跨域访问,需要配置CORS:
-
- ```java
- @Configuration
- public class CorsConfig {
-
- @Bean
- public CorsFilter corsFilter() {
- CorsConfiguration config = new CorsConfiguration();
- config.addAllowedOrigin("*");
- config.addAllowedHeader("*");
- config.addAllowedMethod("*");
-
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", config);
-
- return new CorsFilter(source);
- }
- }
- ```
-
- ## 7. 依赖配置
-
- ### 7.1 Maven依赖
- 确保 `pom.xml` 中包含以下依赖:
-
- ```xml
- <!-- Redis -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
-
- <!-- HTTP客户端 -->
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
-
- <!-- JSON处理 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>fastjson</artifactId>
- </dependency>
-
- <!-- MyBatis Plus -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- </dependency>
- ```
-
- ## 8. 环境变量配置
-
- ### 8.1 生产环境配置
- 在生产环境中,建议使用环境变量或配置中心来管理敏感信息:
-
- ```yaml
- wechat:
- mpAppId: ${WECHAT_MP_APPID:default_appid}
- mpAppSecret: ${WECHAT_MP_SECRET:default_secret}
-
- spring:
- redis:
- host: ${REDIS_HOST:localhost}
- port: ${REDIS_PORT:6379}
- password: ${REDIS_PASSWORD:}
- ```
-
- ## 9. 监控配置
-
- ### 9.1 健康检查
- 可以添加健康检查端点:
-
- ```yaml
- management:
- endpoints:
- web:
- exposure:
- include: health,info,metrics
- endpoint:
- health:
- show-details: always
- ```
-
- ## 10. 注意事项
-
- 1. **安全性**:生产环境中不要将敏感信息硬编码在代码中
- 2. **性能**:Redis连接池配置要根据实际负载调整
- 3. **监控**:建议添加应用监控和日志收集
- 4. **备份**:定期备份数据库和Redis数据
- 5. **测试**:在部署前进行充分的测试
-
- ## 11. 常见问题
-
- ### 11.1 Redis连接失败
- - 检查Redis服务是否启动
- - 检查网络连接和防火墙设置
- - 检查Redis配置是否正确
-
- ### 11.2 微信API调用失败
- - 检查微信小程序配置是否正确
- - 检查网络连接是否正常
- - 检查微信API调用频率限制
-
- ### 11.3 数据库连接失败
- - 检查数据库服务是否启动
- - 检查数据库连接配置
- - 检查数据库用户权限
-
- ## 12. 部署检查清单
-
- - [ ] 数据库表已创建
- - [ ] Redis服务已启动
- - [ ] 微信小程序配置正确
- - [ ] JWT密钥已配置
- - [ ] 日志配置正确
- - [ ] 网络连接正常
- - [ ] 安全配置已设置
- - [ ] 监控已配置
- - [ ] 备份策略已制定
|