背景介绍
在Web开发过程中,往往需要获取一些数据,比如用户登录后的用户名。以往前后端不分离的开发过程中,可以把这些数据放在session中获取,在HTML5中引入了LocalStorage,可以把数据存储在LocalStorage中。
Ant Design Pro是一款基于DVA的前端框架,当每个Web页面都需要获取一些不变的数据量时,使用DVA模式在每个页面connect对应的model比较繁琐,特别是类似用户名这些基本不变的数据,可以在网页读取LocalStoage直接获取。
实现功能
用户登录后,存储用户名、用户权限、区域列表、站点列表、当前站点这些数据到LocalStorage,格式类似:
{
user: 'admin',
ownAreas: [1, 2, 3],
currentSubstation: {
id: '#1-substation', desc: '站点1' },
substationList: [
{
id: '#1-substation', desc: '站点1' },
{
id: '#2-substation', desc: '站点2' },
],
authority: 'admin',
type: 'account',
};
后端实现(Spring)
后端API:http://localhost:8080/api/login/account
代码:
@RequestMapping(value = "/login/account", method = RequestMethod.POST)
public ResponseEntity<Map<String, Object>> LoginAction(@RequestBody LoginAction loginAction) {
Map<String, Object> data = new HashMap<>();
String userName =