<body>
<article class="page-container">
<form action="" method="post" class="form form-horizontal" id="form-admin-role-add">
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名稱:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="role_name" name="role_name">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3">備註:</label>
<div class="formControls col-xs-8 col-sm-9">
<input type="text" class="input-text" value="" placeholder="" id="note" name="note">
</div>
</div>
<div class="row cl">
<label class="form-label col-xs-4 col-sm-3">網站角色:</label>
<div class="formControls col-xs-8 col-sm-9">
<dl class="permission-list">
<dd>
<dl class="cl permission-list2">
<dt>
<label class="">條件查詢:</label>
</dt>
<dd>
<label class="">
<input type="checkbox" value="1"
name="checkbox" id="term_query_right">
檢視</label>
<label class="">
<input type="checkbox" value="2" name="checkbox"
id="term_add_right">
新增</label>
<label class="">
<input type="checkbox" value="3" name="checkbox"
id="term_update_right">
修改</label>
<label class="">
<input type="checkbox" value="4" name="checkbox"
id="term_delete_right">
刪除</label>
</dd>
</dl>
<dl class="cl permission-list2">
<dt>
<label class="">多條件查詢:</label>
</dt>
<dd>
<label class="">
<input type="checkbox" value="5" name="checkbox" id="termmore_query_right">
檢視</label>
<label class="">
<input type="checkbox" value="6" name="checkbox" id="termmore_add_right">
新增</label>
<label class="">
<input type="checkbox" value="7" name="checkbox" id="termmore_update_right">
修改</label>
<label class="">
<input type="checkbox" value="8" name="checkbox" id="termmore_delete_right">
刪除</label>
</dd>
</dl>
<dl class="cl permission-list2">
<dt>
<label class="">管理員管理:</label>
</dt>
<dd>
<label class="">
<input type="checkbox" value="9" name="checkbox" id="termmore_role_right">
角色管理</label>
<label class="">
<input type="checkbox" value="10" name="checkbox" id="termmore_list_right">
管理員列表</label>
</dd>
</dl>
</dd>
</dl>
</dd>
</dl>
</div>
</div>
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<input class="btn btn-primary radius" type="button" onclick="save()" value=" 提交 ">
</div>
</div>
<input type="hidden" id="id">
</form>
</article>
<!--_footer 作為公共模版分離出去-->
<script type="text/javascript" src="../lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="../lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="../static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="../static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作為公共模版分離出去-->
<!--請在下方寫此頁面業務相關的指令碼-->
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="../lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
var role_name;
<!--接收父頁面傳過來的值-->
function child(discount) {
//標題
role_name=discount;
Load();
}
var Load=function(){
var url='/api/adminroleinit';
var data={role_name:role_name};
$.ajax({
type: "POST",
async: false,
url: url,
dataType: "json",
// contentType: 'application/json',
data: data,
success:function (response) {
var list=response.list;
for(var i=0;i<list.length;i++) {
$("input[name='checkbox']").each(function () {
if ($(this).val() == list[i]) {
$(this).attr("checked", "checked");
}
});
}
}
,error:function (err) {
alert(err);
}
});
}
var save=function(){
var role_name=$("#role_name").val();
var note=$("#note").val();
var id=$("#id").val();
var list=new Array();
$.each($('input:checkbox:checked'),function(){
list.push($(this).val());
});
var url='/api/adminroleadd';
var data={id:id,role_name:role_name,note:note,list:list};
$.ajax({
type: "POST",
async: false,
url: url,
dataType: "json",
contentType: 'application/json',
data: JSON.stringify(data),
success:function (response) {
if(response.code==1){
alert("增加成功");
//關閉彈窗
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
}
if(response.code==5){
alert("修改成功");
//關閉彈窗
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
}else if(response.code==11){
alert("角色新增失敗,該角色已經存在");
}
}
,error:function (err) {
alert(err);
}
});
}
$(function(){
$(".permission-list dt input:checkbox").click(function(){
$(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
});
$(".permission-list2 dd input:checkbox").click(function(){
var l =$(this).parent().parent().find("input:checked").length;
var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
if($(this).prop("checked")){
$(this).closest("dl").find("dt input:checkbox").prop("checked",true);
$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
}
else{
if(l==0){
$(this).closest("dl").find("dt input:checkbox").prop("checked",false);
}
if(l2==0){
$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
}
}
});
$("#form-admin-role-add").validate({
rules:{
roleName:{
required:true,
},
},
onkeyup:false,
focusCleanup:true,
success:"valid",
submitHandler:function(form){
$(form).ajaxSubmit();
var index = parent.layer.getFrameIndex(window.name);
parent.layer.close(index);
}
});
});
$(function () {
})
</script>
<!--/請在上方寫此頁面業務相關的指令碼-->
</body>
</html>
後臺
@RequestMapping("/adminroleinit")
public String AdminRoleInit(String role_name) {
APIResultModel result = new APIResultModel();
result = zhangWuService.AdminRoleInit(role_name, result);
return result.toString();
}
@RequestMapping("/adminroleadd")
public String AdminRoleAdd(@RequestBody AdminRoleAddModel adminRoleAddModel) throws IOException {
APIResultModel result = new APIResultModel();
result = zhangWuService.AdminRoleAdd(adminRoleAddModel, result);
return result.toString();
}
service
public APIResultModel AdminRoleInit(String role_name, APIResultModel result) {
List<RoleRightEntity> list=zhangWuMapper.AdminRoleInit(role_name);
List liste=new ArrayList();
for (int i = 0; i <list.size(); i++) {
liste.add(list.get(i).getCheckedpower());
}
result.setList(liste);
return result;
}
public APIResultModel AdminRoleAdd(AdminRoleAddModel adminRoleAddModel, APIResultModel result) throws IOException{
response.setContentType("text/html:charset=utf-8");
HttpSession session = request.getSession(false);
String username= (String) session.getAttribute("username");
List<RoleRightEntity> listpowel=zhangWuMapper.TermShowpowel(username);
List liste=new ArrayList();
for (int i = 0; i <listpowel.size(); i++) {
liste.add(listpowel.get(i).getCheckedpower());
}
result.setList(liste);
boolean flag=liste.contains("13");//查詢
if(flag==true) {
Map map = new HashMap();
map.put("role_name", adminRoleAddModel.getRole_name());
map.put("note", adminRoleAddModel.getNote());
if (adminRoleAddModel.getId() == null) {//為增加角色
int sqlsquenceSystemRole = zhangWuMapper.SystemRole(adminRoleAddModel.getRole_name());
if (sqlsquenceSystemRole > 0) {
System.out.println("新增角色失敗,該角色已存在");
result.setCode(EnumAPIResultCode.FALSE_ROLEADD.getCode());
} else {
int sqlsquenceadd = 0;
int sqlsquenceSystemRoleAdd = zhangWuMapper.SystemRoleAdd(map);
int role_id = zhangWuMapper.SystemRoleid(adminRoleAddModel.getRole_name());
map.put("role_id", role_id);
for (int i = 0; i < adminRoleAddModel.getList().size(); i++) {
map.put("checkedpower", adminRoleAddModel.getList().get(i));
sqlsquenceadd = zhangWuMapper.AdminRoleAdd(map);
}
if (sqlsquenceadd > 0 || sqlsquenceSystemRoleAdd > 0) {
result.setCode(EnumAPIResultCode.SUCCESS_ADD.getCode());
}
}
} else {
int sqlsquenceupdate = 0;
zhangWuMapper.AdminRoledelete(adminRoleAddModel.getRole_name());
map.put("role_id", adminRoleAddModel.getId());
for (int i = 0; i < adminRoleAddModel.getList().size(); i++) {
map.put("checkedpower", adminRoleAddModel.getList().get(i));
sqlsquenceupdate = zhangWuMapper.AdminRoleAdd(map);
}
if (sqlsquenceupdate > 0) {
result.setCode(EnumAPIResultCode.SUCCESS_ALTER.getCode());
}
}
}
return result;
}
mapper
@Select("select a.checkedpower from role_right a where a.role_id=(select b.id from system_role b where b.role_name =#{role_name})")
List<RoleRightEntity> AdminRoleInit(@Param(value = "role_name") String role_name);
@Select("SELECT a.checkedpower FROM role_right a where a.role_id=(select b.role from user_login b where b.username=#{username})")
List<RoleRightEntity> TermShowpowel(@Param(value = "username") String username);
@Select("select count(*) from system_role where role_name=#{role_name}")
int SystemRole(@Param(value = "role_name") String role_name);
@Insert("INSERT INTO `sd_roulette`.`system_role` (`role_name`, `note`) VALUES ( #{role_name},#{note})")
int SystemRoleAdd(Map map);
@Select("select id from system_role where role_name=#{role_name}")
int SystemRoleid(String role_name);
@Insert("INSERT INTO `role_right` ( `role_name`, `note`, `checkedpower`,`role_id`) VALUES (#{role_name},#{note},#{checkedpower},#{role_id})")
int AdminRoleAdd(Map map);
@Delete("delete from role_right where role_name=#{role_name}")
void AdminRoledelete(@Param(value = "role_name") String role_name);
@Insert("INSERT INTO `role_right` ( `role_name`, `note`, `checkedpower`,`role_id`) VALUES (#{role_name},#{note},#{checkedpower},#{role_id})")
int AdminRoleAdd(Map map);