用户管理

This commit is contained in:
ArnoHand 2018-09-29 17:05:26 +08:00
parent b3bfec6a46
commit 6928f594e2
8 changed files with 506 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package cn.jiangzeyin.controller.user;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.model.UserInfoMode;
import cn.jiangzeyin.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
@ -48,4 +49,9 @@ public class UpdatePwdController extends BaseController {
return JsonMessage.getString(500, e.getMessage());
}
}
}

View File

@ -0,0 +1,59 @@
package cn.jiangzeyin.controller.user;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.model.UserInfoMode;
import cn.jiangzeyin.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping(value = "/user")
public class UserInfoController extends BaseController {
@Resource
private UserService userService;
/**
* 删除用户
*
* @param id 用户id
* @return String
*/
@RequestMapping(value = "deleteUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String deleteUser(String id) {
boolean b = userService.deleteUser(id);
if (b) {
return JsonMessage.getString(200, "删除成功");
}
return JsonMessage.getString(400, "删除失败");
}
/**
* 新增用户
*
* @param user 用户
* @return String
*/
@RequestMapping(value = "addUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String addUser(UserInfoMode user) {
return userService.addUser(user);
}
/**
* 修改用户
*
* @param user 用户
* @return String
*/
@RequestMapping(value = "updateUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String updateUser(UserInfoMode user) {
return userService.updateUser(user);
}
}

View File

@ -0,0 +1,40 @@
package cn.jiangzeyin.controller.user;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.service.UserService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
@RequestMapping(value = "/user")
public class UserListController extends BaseController {
@Resource
private UserService userService;
/**
* 展示用户列表
*/
@RequestMapping(value = "list")
public String projectInfo() {
return "user/list";
}
/**
* 查询所有用户
*
*/
@RequestMapping(value = "getUserList")
@ResponseBody
public String getUserList() {
JSONArray userList = userService.getUserList();
return JsonMessage.getString(200, "",userList);
}
}

View File

@ -0,0 +1,53 @@
package cn.jiangzeyin.model;
/**
* 用户信息
**/
public class UserInfoMode {
private String id;
private String name;
private Boolean role;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getRole() {
return role;
}
public void setRole(Boolean role) {
this.role = role;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfoMode{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", role='" + role + '\'' +
", password='" + password + '\'' +
'}';
}
}

View File

@ -1,11 +1,19 @@
package cn.jiangzeyin.service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.model.UserInfoMode;
import cn.jiangzeyin.util.JsonUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
/**
* @author Administrator
@ -20,7 +28,7 @@ public class UserService extends BaseService {
*
* @param name 用户名
* @param pwd 密码
* @return
* @return 登录
*/
public boolean login(String name, String pwd) throws Exception {
JSONObject userInfo = getJsonObject(FILENAME, name);
@ -48,6 +56,31 @@ public class UserService extends BaseService {
return false;
}
/**
* 获取用户列表
*
* @return 用户列表
*/
public JSONArray getUserList() {
JSONObject jsonObject = null;
try {
jsonObject = getJsonObject(FILENAME);
} catch (IOException e) {
e.printStackTrace();
}
if (jsonObject == null) {
return null;
}
Set<Map.Entry<String, Object>> set = jsonObject.entrySet();
JSONArray array = new JSONArray();
for (Map.Entry entry : set) {
String key = (String) entry.getKey();
JSONObject value = (JSONObject) entry.getValue();
value.remove("password");
array.add(value);
}
return array;
}
/**
* 修改密码
@ -73,4 +106,82 @@ public class UserService extends BaseService {
return "olderror";
}
}
/**
* 删除用户
*
* @param id 用户id
* @return String
*/
public boolean deleteUser(String id) {
try {
deleteJson(FILENAME, id);
return true;
} catch (Exception e) {
DefaultSystemLog.LOG().error(e.getMessage(), e);
}
return false;
}
/**
* 新增用户
*
* @param user 用户
*/
public String addUser(UserInfoMode user) {
if (user == null) {
return JsonMessage.getString(400, "用户信息为空");
}
String password = user.getPassword();
if (StrUtil.isEmpty(password)) {
return JsonMessage.getString(400, "密码不能为空");
}
try {
JSONObject jsonData = getJsonObject(FILENAME);
String id = user.getId();
if (jsonData.containsKey(id)) {
return JsonMessage.getString(400, "该用户已存在");
}
JSONObject object = (JSONObject) JSON.toJSON(user);
saveJson(FILENAME, object);
return JsonMessage.getString(200, "添加成功");
} catch (Exception e) {
DefaultSystemLog.LOG().error(e.getMessage(), e);
}
return JsonMessage.getString(400, "添加失败");
}
/**
* 修改用户
*
* @param user 用户
* @return String
*/
public String updateUser(UserInfoMode user) {
if (user == null) {
return JsonMessage.getString(400, "用户信息为空");
}
System.out.println(user.toString());
String id = user.getId();
if (StrUtil.isEmpty(id)) {
return JsonMessage.getString(400, "修改失败,获取id失败");
}
String name = StrUtil.nullToEmpty(user.getName());
String password = StrUtil.nullToEmpty(user.getPassword());
Boolean role = user.getRole();
try {
JSONObject jsonData = getJsonObject(FILENAME);
JSONObject jsonObject = jsonData.getJSONObject(id);
jsonObject.put("name", name);
if (!StrUtil.isEmpty(password)) {
jsonObject.put("password", password);
}
jsonObject.put("role", role);
updateJson(FILENAME, jsonObject);
return JsonMessage.getString(200, "修改成功");
} catch (Exception e) {
DefaultSystemLog.LOG().error(e.getMessage(), e);
}
return JsonMessage.getString(400, "修改失败");
}
}

View File

@ -0,0 +1,20 @@
body {
padding: 10px 10px 0px 10px;
}
.div-project {
display: none;
width: 100%;
margin: 10px 0;
}
.div-project .layui-inline {
width: calc(50% - 5px);
margin-right: 0px;
}
.layui-form-label{
width: 100px;
}
.layui-input-block{
margin-left: 135px;
}

View File

@ -34,6 +34,9 @@
<li class="layui-nav-item">
<a href="javascript:;" data-options="{'id':'manage', 'title':'项目管理', 'url':'/manage/projectInfo'}">项目管理</a>
</li>
<li class="layui-nav-item">
<a href="javascript:;" data-options="{'id':'user', 'title':'用户管理', 'url':'/user/list'}">用户管理</a>
</li>
</ul>
</div>
</div>

View File

@ -0,0 +1,213 @@
<!DOCTYPE html>
<html>
<head>
#parse("./common/head.vm")
<title>项目管理系统</title>
<link rel="stylesheet" href="/static/css/manage/list.css" media="all">
</head>
<body>
<div class="layui-row">
<button id="addProject" class="layui-btn layui-btn-sm">新增用户</button>
<button id="refresh" class="layui-btn layui-btn-sm">刷新表格</button>
</div>
<table class="layui-table" id="tab_project" lay-filter="tab_project" style="margin: 0;"></table>
<div class="layui-container div-project" id="div-project">
<form action="" class="layui-form" id="form_project">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">登录名</label>
<div class="layui-input-block">
<input type="text" name="id" placeholder="登录名(设置后将不能修改)" required lay-verify="required"
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="text" name="password" placeholder=""
class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">昵称</label>
<div class="layui-input-block">
<input type="text" name="name" placeholder="昵称" required
lay-verify="required" class="layui-input">
</div>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">是否是管理员</label>
<div class="layui-input-block">
<input type="checkbox" value="true" name="role" lay-skin="switch" lay-text="是|否">
</div>
</div>
</div>
<input type="hidden" lay-submit lay-filter="submitProject" id="project_submit">
</form>
</div>
</body>
<script type="text/html" id="bar_projects">
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-warm" lay-event="update">编辑</a>
<a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
</script>
<script type="text/javascript">
layui.use(['layer', 'element', 'table', 'form'], function () {
var $ = layui.$;
var table = layui.table;
var form = layui.form;
var layer = layui.layer;
table.render({
id: 'table_project',
elem: '#tab_project',
url: '/user/getUserList',
height: 'full-52',
even: true,
cols: [[
{field: 'id', title: 'id'},
{field: 'name', title: '昵称'},
{field: 'role', title: '是否是管理员'},
{field: 'op', title: '操作', align: 'center', toolbar: '#bar_projects', fixed: 'right'}
]],
loading: true,
response: {
statusCode: 200
}
});
// '添加用户'点击事件
$('#addProject').on('click', function () {
// 重置表单
document.getElementById('form_project').reset();
$('#form_project [name="id"]').attr('readonly', false).removeClass('layui-disabled');
$('#form_project [name="role"]').attr('checked', false);
// 弹出
layer.open({
type: 1,
title: '新增项目',
content: $('#div-project'),
area: ['60%', '60%'],
btnAlign: 'c',
btn: ['提交'],
yes: function (index, layero) {
$('#form_project').attr('action', '/user/addUser');
$('#project_submit').click();
}
});
});
// '刷新表格'点击事件
$('#refresh').on('click', function () {
table.reload('table_project', {height: 'full-52'});
});
// 提交项目表单
form.on('submit(submitProject)', function (data) {
$.ajax({
url: data.form.action,
type: 'POST',
dataType: 'json',
data: data.field,
success: function (data) {
if (200 == data.code) {
layer.closeAll('page');
// 刷新项目列表
table.reload('table_project', {height: 'full-52'});
} else {
layer.msg(data.msg);
}
},
error: function (err) {
layer.msg('操作失败!');
}
});
return false;
});
// 表格工具条事件
table.on('tool(tab_project)', function (obj) {
var data = obj.data;
var event = obj.event;
if ('update' === event) {
// 修改
updateApplication(data);
} else if ('delete' === event) {
// 删除
deleteApplication(data);
}
});
// 编辑用户信息
function updateApplication(data) {
// 重置表单
document.getElementById('form_project').reset();
$('#form_project [name="id"]').attr('readonly', true).addClass('layui-disabled');
// 设置表单值
for (var key in data) {
if ("role" == key) {
if (data[key])
$('#form_project [name="role"]').attr('checked', true);
} else {
$('#form_project [name="' + key + '"]').val(data[key]);
}
}
// 弹出
layer.open({
type: 1,
title: '编辑',
content: $('#div-project'),
area: ['60%', '60%'],
btnAlign: 'c',
btn: ['提交'],
yes: function (index, layero) {
$('#form_project').attr('action', '/user/updateUser');
$('#project_submit').click();
}
});
}
// 删除用户
function deleteApplication(data) {
layer.confirm('确定删除用户 ' + data.id + '', {title: '系统提示'}, function (index) {
layer.close(index);
$.ajax({
url: '/user/deleteUser',
type: 'POST',
dataType: 'json',
data: {id: data.id},
success: function (data) {
if (200 == data.code) {
layer.msg('删除成功!');
// 刷新项目列表
table.reload('table_project', {height: 'full-52'});
} else {
layer.msg(data.msg);
}
},
error: function (err) {
layer.msg('删除失败!');
}
});
});
}
});
</script>
</html>