优化前后端交互,统一ajax交互格式

This commit is contained in:
zxm 2018-01-25 15:47:00 +08:00
parent 3822bd072b
commit fac2248652
9 changed files with 466 additions and 63 deletions

View File

@ -73,10 +73,11 @@ public class RoleController extends BaseController {
@Log(desc = "添加角色")
@PostMapping(value = "addRole")
@ResponseBody
public String addRole(SysRole sysRole,String[] menus){
public JsonUtil addRole(SysRole sysRole,String[] menus){
if(StringUtils.isEmpty(sysRole.getRoleName())){
return "角色名称不能为空";
JsonUtil.error("角色名称不能为空");
}
JsonUtil j=new JsonUtil();
try{
roleService.insertSelective(sysRole);
//操作role-menu data
@ -88,10 +89,13 @@ public class RoleController extends BaseController {
sysRoleMenu.setMenuId(menu);
roleMenuService.insert(sysRoleMenu);
}
j.setMsg("保存成功");
}catch (MyException e){
j.setMsg("保存失败");
j.setFlag(false);
e.printStackTrace();
}
return "保存成功";
return j;
}
@GetMapping(value = "updateRole")
@ -136,6 +140,7 @@ public class RoleController extends BaseController {
jsonUtil.setFlag(true);
jsonUtil.setMsg("修改成功");
} catch (MyException e) {
jsonUtil.setMsg("修改失败");
e.printStackTrace();
}
return jsonUtil;
@ -146,22 +151,26 @@ public class RoleController extends BaseController {
@PostMapping(value = "del")
@ResponseBody
@RequiresPermissions("role:del")
public String del(String id) {
public JsonUtil del(String id) {
if (StringUtils.isEmpty(id)) {
return "获取数据失败";
return JsonUtil.error("获取数据失败");
}
SysRoleUser sysRoleUser=new SysRoleUser();
sysRoleUser.setRoleId(id);
JsonUtil j=new JsonUtil();
try {
int count= roleUserService.selectCountByCondition(sysRoleUser);
if(count>0){
return "已分配给用户,删除失败";
return JsonUtil.error("已分配给用户,删除失败");
}
roleService.deleteByPrimaryKey(id);
j.setMsg("删除成功");
} catch (MyException e) {
j.setMsg("删除失败");
j.setFlag(false);
e.printStackTrace();
}
return "删除成功";
return j;
}
}

View File

@ -84,24 +84,24 @@ public class UserController extends BaseController{
@Log(desc = "添加用户")
@PostMapping(value = "addUser")
@ResponseBody
public String addUser(SysUser user,String[] role) {
public JsonUtil addUser(SysUser user,String[] role) {
if (user == null) {
return "获取数据失败";
return JsonUtil.error("获取数据失败");
}
if (StringUtils.isBlank(user.getUsername())) {
return "用户名不能为空";
return JsonUtil.error("用户名不能为空");
}
if (StringUtils.isBlank(user.getPassword())) {
return "密码不能为空";
return JsonUtil.error("密码不能为空");
}
if(role==null){
return "请选择角色";
return JsonUtil.error("请选择角色");
}
int result = userService.checkUser(user.getUsername());
if (result > 0) {
return "用户名已存在";
return JsonUtil.error("用户名已存在");
}
JsonUtil j=new JsonUtil();
try {
userService.insertSelective(user);
SysRoleUser sysRoleUser=new SysRoleUser();
@ -110,10 +110,13 @@ public class UserController extends BaseController{
sysRoleUser.setRoleId(r);
roleUserService.insertSelective(sysRoleUser);
}
j.setMsg("保存成功");
} catch (MyException e) {
j.setMsg("保存失败");
j.setFlag(false);
e.printStackTrace();
}
return "保存成功";
return j;
}
@GetMapping(value = "updateUser")
@ -170,34 +173,8 @@ public class UserController extends BaseController{
@PostMapping(value = "/del")
@ResponseBody
@RequiresPermissions("user:del")
public String del(String id, boolean flag) {
if (StringUtils.isEmpty(id)) {
return "获取数据失败";
}
try {
SysUser sysUser = userService.selectByPrimaryKey(id);
if("admin".equals(sysUser.getUsername())){
return "超管无法删除";
}
SysRoleUser roleUser=new SysRoleUser();
roleUser.setUserId(id);
int count=roleUserService.selectCountByCondition(roleUser);
if(count>0){
return "账户已经绑定角色,无法删除";
}
if (flag) {
//逻辑
sysUser.setDelFlag(Byte.parseByte("1"));
userService.updateByPrimaryKeySelective(sysUser);
} else {
//物理
userService.delById(id);
}
} catch (MyException e) {
e.printStackTrace();
}
return "删除成功";
public JsonUtil del(String id, boolean flag) {
return userService.delById(id,flag);
}
@GetMapping(value = "goRePass")
@ -213,7 +190,7 @@ public class UserController extends BaseController{
/**
* 修改密码
* @param id
* @param password
* @param pass
* @param newPwd
* @return
*/

View File

@ -5,6 +5,7 @@ import com.len.base.BaseService;
import com.len.entity.SysRoleUser;
import com.len.entity.SysUser;
import com.len.util.Checkbox;
import com.len.util.JsonUtil;
import java.util.List;
/**
@ -40,7 +41,7 @@ public interface SysUserService extends BaseService<SysUser,String> {
* @param id
* @return
*/
int delById(String id);
JsonUtil delById(String id,boolean flag);
int checkUser(String username);

View File

@ -5,15 +5,19 @@ import com.len.base.impl.BaseServiceImpl;
import com.len.entity.SysRole;
import com.len.entity.SysRoleUser;
import com.len.entity.SysUser;
import com.len.exception.MyException;
import com.len.mapper.SysRoleUserMapper;
import com.len.mapper.SysUserMapper;
import com.len.service.RoleService;
import com.len.service.RoleUserService;
import com.len.service.SysUserService;
import com.len.util.Checkbox;
import com.len.util.JsonUtil;
import com.len.util.Md5Util;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -30,8 +34,12 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUser,String> implemen
@Autowired
SysRoleUserMapper sysRoleUserMapper;
@Autowired
RoleService roleService;
@Autowired
RoleUserService roleUserService;
@Override
public BaseMapper<SysUser, String> getMappser() {
return sysUserMapper;
@ -105,8 +113,38 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUser,String> implemen
}
@Override
public int delById(String id) {
return sysUserMapper.delById(id);
public JsonUtil delById(String id,boolean flag) {
if (StringUtils.isEmpty(id)) {
return JsonUtil.error("获取数据失败");
}
JsonUtil j=null;
try {
SysUser sysUser = selectByPrimaryKey(id);
if("admin".equals(sysUser.getUsername())){
return JsonUtil.error("超管无法删除");
}
SysRoleUser roleUser=new SysRoleUser();
roleUser.setUserId(id);
int count=roleUserService.selectCountByCondition(roleUser);
if(count>0){
return JsonUtil.error("账户已经绑定角色,无法删除");
}
j=new JsonUtil();
if (flag) {
//逻辑
sysUser.setDelFlag(Byte.parseByte("1"));
updateByPrimaryKeySelective(sysUser);
} else {
//物理
sysUserMapper.delById(id);
}
j.setMsg("删除成功");
} catch (MyException e) {
j.setMsg("删除失败");
j.setFlag(false);
e.printStackTrace();
}
return j;
}

View File

@ -133,12 +133,16 @@ To change this template use File | Settings | File Templates.-->
type:'post',
data:data.field,
async:false, traditional: true,
success:function(msg){
console.info('msg:'+msg);
success:function(d){
if(d.flag){
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
window.parent.layui.table.reload('roleList');
window.top.layer.msg(msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
window.top.layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
}else{
layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
}
},error:function(){
layer.alert("请求失败", {icon: 6},function () {
var index = parent.layer.getFrameIndex(window.name);

View File

@ -223,9 +223,13 @@
url: "del",
type: "post",
data: {id: id},
success: function (msg) {
layer.msg(msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
layui.table.reload('roleList');
success: function (d) {
if(d.msg){
layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
layui.table.reload('roleList');
}else{
layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
}
}
});
}

View File

@ -245,11 +245,15 @@ To change this template use File | Settings | File Templates.-->
type:'post',
data:data.field,
async:false,traditional: true,
success:function(msg){
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
window.parent.layui.table.reload('userList');
window.top.layer.msg(msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
success:function(d){
if(d.flag){
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
window.parent.layui.table.reload('userList');
window.top.layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
}else{
layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
}
},error:function(){
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);

View File

@ -244,9 +244,13 @@
url:"del",
type:"post",
data:{id:id,flag:flag},async:false,
success:function(msg){
window.top.layer.msg(msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
layui.table.reload('userList');
success:function(d){
if(d.flag){
window.top.layer.msg(d.msg,{icon:6,offset: 'rb',area:['120px','80px'],anim:2});
layui.table.reload('userList');
}else{
window.top.layer.msg(d.msg,{icon:5,offset: 'rb',area:['120px','80px'],anim:2});
}
},error:function(){
alert('error');
}
@ -258,7 +262,7 @@
title = false;
};
if (url == null || url == '') {
url = "404.html";
url = "error/404";
};
if (w == null || w == '') {
w = ($(window).width() * 0.9);

File diff suppressed because one or more lines are too long