# diboot-mobile 使用说明
# 1、引入依赖
Maven
<dependency>
<groupId>com.diboot</groupId>
<artifactId>diboot-mobile-spring-boot-starter</artifactId>
<version>{latestVersion}</version>
</dependency>
<!-- 微信公众号工具包 (按需引入) -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>{latestVersion}</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 微信小程序工具包 (按需引入) -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>{latestVersion}</version>
<exclusions>
<exclusion>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</exclusion>
</exclusions>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
或Gradle:
compile("com.diboot:diboot-mobile-spring-boot-starter:{latestVersion}")
compile("com.github.binarywang:weixin-java-mp:{latestVersion}")
compile("com.github.binarywang:weixin-java-miniapp:{latestVersion}")
1
2
3
2
3
组件依赖的数据表 iam_member,在组件starter初次启动时将自动初始化。
如果使用diboot-devtools,还可一键生成该组件相关的controller等基础代码到本地项目下。
# 2、参数配置:
diboot-mobile组件有以下配置项,用于初始化设置 配置参数:
# 是否初始化sql,默认true,初始化之后(或非开发环境)可以关闭
diboot.component.mobile.init-sql=false
# 配置匿名接口
diboot.iam.anon-urls=/*/auth/**
# 微信小程序相关配置(按需配置)
diboot.mobile.wx-miniapp.appid=必须
diboot.mobile.wx-miniapp.secret=必须
diboot.mobile.wx-miniapp.token=
diboot.mobile.wx-miniapp.aes-key=
diboot.mobile.wx-miniapp.msg-data-format=JSON
# 微信公众号相关配置(按需配置)
diboot.mobile.wx-mp.appid=必须
diboot.mobile.wx-mp.secret=必须
diboot.mobile.wx-mp.token=必须
diboot.mobile.wx-mp.aes-key=必须
diboot.mobile.wx-mp.msg-data-format=JSON
diboot.mobile.wx-mp.state=必须
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
⚠️ 引入微信小程序(公众号)依赖,diboot.mobile.wx-miniapp(wx-mp),必须配置,否则会造成相关类无法使用
# 3. 使用说明
- 因移动端接口不同于PC端,建议为移动端接口单独创建一个子项目(模块),在该模块下依赖diboot-mobile-spring-boot-starter组件
- 依赖devtools,启动项目进入devtools组件初始化页面,生成移动端等组件的基础代码
@since2.3.1
如您的移动端场景为需要账号密码登录的H5形式,需自行实现创建IamMember用户及IamAccount登录账号。@since2.4.0
启动默认的用户登陆信息(admin/123456)
# 4、使用配置
@since2.4.0
配置 默认情况下是使用IamMember用户进行登陆,此时您不需要做任何配置即可直接使用,如果您想使用IamUser作为登陆用户,需要手动进行配置
# IamUser作为登陆用户的配置方式
相关的登陆接口方法参数AuthCredential子类的
userTypeClass属性
设置为IamUser.class
小程序配置
@Bean
public WxMaAuthService wxMaAuthService(WxMaService wxMaService, IamMemberService iamMemberService, IamAccountService iamAccountService) {
return new WxMaUserAuthServiceImpl(wxMaService, iamMemberService, iamAccountService);
}
1
2
3
4
2
3
4
- 公众号配置
@Autowired
private MobileProperties mobileProperties;
@Bean
public WxMpAuthService wxMpAuthService(WxMpService wxMpService, IamAccountService iamAccountService, IamMemberService iamMemberService) {
return new WxMpUserAuthServiceImpl(wxMpService, iamAccountService, iamMemberService, mobileProperties.getWxMp().getState())
}
1
2
3
4
5
6
7
2
3
4
5
6
7
以下配置仅在
@since2.3.1
生效
- 小程序配置
public class WxConfig {
/**
* 注入WxMaMemberAuthService
*
* @param wxMaService
* @param iamMemberService
* @param iamAccountService
* @return
*/
@Bean
public WxMaMemberAuthService wxMaMemberAuthService(WxMaService wxMaService, IamMemberService iamMemberService, IamAccountService iamAccountService) {
return new WxMaMemberAuthServiceImpl(wxMaService, iamMemberService, iamAccountService);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- 公众号配置
public class WxConfig {
/**
* 设置微信网页授权state
*/
@Value("${wechat.state}")
private String STATE;
/**
* 注入WxMpMemberAuthService
*
* @param wxMpService
* @param iamMemberService
* @param iamAccountService
* @return
*/
@Bean
public WxMpMemberAuthService wxMpMemberAuthService(WxMpService wxMpService, IamMemberService iamMemberService, IamAccountService iamAccountService) {
WxMpMemberAuthServiceImpl wxMpMemberAuthService = new WxMpMemberAuthServiceImpl(wxMpService, iamMemberService, iamAccountService);
try {
Field state = wxMpMemberAuthService.getClass().getDeclaredField("STATE");
state.setAccessible(true);
state.set(wxMpMemberAuthService, STATE);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return wxMpMemberAuthService;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
使用过程中遇到问题,可加群交流。