From 5f3f787dcfa177de0c609af746a9876af1a348a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=89=E7=A5=A5=20=E8=A9=B9?= Date: Mon, 29 Jul 2024 01:26:27 +0800 Subject: [PATCH] updates --- Controllers/AuthApiController.cs | 226 +++++++++++++++- Modals/resultClass.cs | 8 +- Views/Home/GroupList.cshtml | 110 +++++--- wwwroot/assets/javascript/custom/grouplist.js | 253 +++++++++++++++++- 4 files changed, 550 insertions(+), 47 deletions(-) diff --git a/Controllers/AuthApiController.cs b/Controllers/AuthApiController.cs index 292a283b..9b4768b2 100644 --- a/Controllers/AuthApiController.cs +++ b/Controllers/AuthApiController.cs @@ -25,6 +25,222 @@ namespace QuotationMaker.Controllers } + [Route("addGroupUser")] + public ActionResult AddGroupUser(IFormCollection obj) { + groupUserListResult ret = new groupUserListResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (token.user_perm != "system") + { + ret.ret = "no"; + ret.err_code = "90001"; + ret.message = "此帳號無此api使用權限!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string dept_uid = obj["dept_uid"].ToString(); + string users_json_str = obj["users_json"].ToString(); + string group_uid = obj["group_uid"].ToString(); + + group objGroup = conn.QueryFirstOrDefault("select * from [group] where group_uid = @group_uid", new { group_uid = group_uid }); + + if (objGroup == null) + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "無此group_uid的群組資料!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + dynamic usersJsonObj; + + try + { + usersJsonObj = JsonConvert.DeserializeObject(users_json_str); + + + + } + catch (Exception ex) + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "使用者列表資料錯誤 users json error," + ex.Message; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string nowrite_msg = ""; + int userCount = 0; + foreach (dynamic item in usersJsonObj) + { + string user_uid = item.user_uid; + string user_name = item.user_name; + groupUser chk_user = conn.QueryFirstOrDefault("select * from groupUser where user_uid = @user_uid and dept_uid = @dept_uid", new { user_uid = user_uid, dept_uid = dept_uid }); + + if (chk_user == null) + { + groupUser newGU = new groupUser(); + newGU.group_uid = group_uid; + newGU.user_uid = user_uid; + newGU.groupUser_uid = GlobalClass.CreateRandomCode(16); + newGU.dept_uid = dept_uid; + conn.Insert(newGU); + + user objUser = new user(); + objUser.user_uid=user_uid; + objUser.user_name=user_name; + ret.users.Add(objUser); + userCount++; + } + else + { + nowrite_msg += user_name + " 此用戶已經是其他群組的成員,故此員取消加入!\n"; + } + } + + + + ret.message = nowrite_msg; + ret.ret = "yes"; + + + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + [Route("delGroup")] + public ActionResult DelGroup(IFormCollection obj) { + signinResult ret = new signinResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (token.user_perm != "system") + { + ret.ret = "no"; + ret.err_code = "90001"; + ret.message = "此帳號無此api使用權限!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + + string group_uid = obj["group_uid"].ToString(); + + group objGroup = conn.QueryFirstOrDefault("select * from [group] where group_uid = @group_uid", new { group_uid = group_uid }); + + if (objGroup == null) + { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "無此group_uid的群組資料!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + conn.Execute("delete groupUser where group_uid = @group_uid", new { group_uid = group_uid}); + conn.Delete(objGroup); + + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + [Route("editGroupName")] + public ActionResult EditGroupName(IFormCollection obj) { + signinResult ret = new signinResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (token.user_perm != "system") + { + ret.ret = "no"; + ret.err_code = "90001"; + ret.message = "此帳號無此api使用權限!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string group_name = obj["group_name"].ToString(); + string group_uid = obj["group_uid"].ToString(); + + group objGroup = conn.QueryFirstOrDefault("select * from [group] where group_uid = @group_uid", new { group_uid = group_uid }); + + if (objGroup == null) { + ret.ret = "no"; + ret.err_code = "0003"; + ret.message = "無此group_uid的群組資料!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + objGroup.group_name = group_name; + + conn.Update(objGroup); + + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + [Route("delGroupUser")] + public ActionResult DelGroupUser(IFormCollection obj) { + signinResult ret = new signinResult(); + + authToken token = new authToken(this._httpContextAccessor); + if (token.user_isLogin == false) + { + HttpContext.Response.Cookies.Delete("token_key"); + ret.ret = "no"; + ret.err_code = "99999"; + ret.message = "非登入狀態!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + if (token.user_perm != "system") + { + ret.ret = "no"; + ret.err_code = "90001"; + ret.message = "此帳號無此api使用權限!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + string dept_uid = obj["dept_uid"].ToString(); + string user_uid = obj["user_uid"].ToString(); + string group_uid = obj["group_uid"].ToString(); + + groupUser objUser = conn.QueryFirstOrDefault("select * from groupUser where dept_uid = @dept_uid and user_uid = @user_uid and group_uid = @group_uid ", new { dept_uid = dept_uid, user_uid = user_uid, group_uid = group_uid }); + + if (objUser == null) { + ret.ret = "no"; + ret.err_code = "0002"; + ret.message = "此群組無此帳號可以刪除!"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + + conn.Delete(objUser); + + ret.ret = "yes"; + return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); + } + [Route("addNewGroup")] public ActionResult AddNewGroup(IFormCollection obj) { groupListResult ret = new groupListResult(); @@ -176,7 +392,13 @@ namespace QuotationMaker.Controllers return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } - ret.depts = conn.Query("select * from depts order by dept_order ").ToList(); + string sqlString = "select * from depts order by dept_order"; + + if (token.user_id != GlobalClass.appsettings("Admin:id")) { + sqlString = "select * from depts where dept_uid in (select dept_uid from groupUser where user_uid = '" + token.user_uid + "' ) order by dept_order "; + } + + ret.depts = conn.Query(sqlString).ToList(); ret.ret = "yes"; return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } @@ -213,7 +435,7 @@ namespace QuotationMaker.Controllers return Content(JsonConvert.SerializeObject(ret), "application/json;charset=utf-8"); } - List groupList = conn.Query("select * from group where dept_uid = @dept_uid", new { dept_uid = dept_uid}).ToList(); + List groupList = conn.Query("select * from [group] where dept_uid = @dept_uid", new { dept_uid = dept_uid}).ToList(); foreach (group objGroup in groupList) { diff --git a/Modals/resultClass.cs b/Modals/resultClass.cs index 81e0486f..08269308 100644 --- a/Modals/resultClass.cs +++ b/Modals/resultClass.cs @@ -12,7 +12,13 @@ public class resultClass public List depts = new List(); } - + public class groupUserListResult + { + public string ret = "no"; + public string err_code = "0000"; + public string message = ""; + public List users = new List(); + } public class groupListResult { diff --git a/Views/Home/GroupList.cshtml b/Views/Home/GroupList.cshtml index 8d93eea8..95f1bb18 100644 --- a/Views/Home/GroupList.cshtml +++ b/Views/Home/GroupList.cshtml @@ -58,36 +58,7 @@
-
- -
- -
-
Footer Menus
-
- -
-
-
- -
    -
  1. -
    - -
    Tracking Order
    -
    - -
    -
    -
  2. -
-
- - -
-
+
@@ -149,7 +120,7 @@ -
+ +
+ + +
+ +
+ +
+
\ No newline at end of file diff --git a/wwwroot/assets/javascript/custom/grouplist.js b/wwwroot/assets/javascript/custom/grouplist.js index 5e6f30a2..f077e9ae 100644 --- a/wwwroot/assets/javascript/custom/grouplist.js +++ b/wwwroot/assets/javascript/custom/grouplist.js @@ -1,10 +1,10 @@  var tmpNestableObj; var tmpNestableItem; - +var tmpGroupNameItem; $(document).ready(function () { deptList(); - + $('#memberAddSaveBtn').on('click', function () { var userArray = []; var html = ''; @@ -16,9 +16,9 @@ $(document).ready(function () { userArray.push(item); html += optionItemHtml(item); }); - + $('#nestableMember .dd-list').append(html); - + $('#memberModal').modal('toggle'); }); @@ -101,8 +101,110 @@ $(document).ready(function () { } }); }); + + $('#groupMemberAddSaveBtn').on('click', function () { + var userArray = []; + var dept_uid = $('#client_group_dept_uid').val(); + var group_uid = $('#client_group_group_uid').val(); + + $.each($('input[type=checkbox][name="memberChkList2[]"]:checked'), function () { + var item = { + user_uid: $(this).val(), + user_name: $(this).parent().find('span').eq(0).text().trim() + } + userArray.push(item); + }); + + + + if (userArray.length == 0) { + alert('沒有勾選欲加入群組的成員!'); + return; + } + + var users_json = JSON.stringify(userArray); + + + var formData = { + dept_uid: dept_uid, + group_uid: group_uid, + users_json: users_json + } + + $.ajax({ + url: "/AuthApi/addGroupUser", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + + + + var obj = data.users; + + $.each(obj, function (index, item) { + $('#' + group_uid + ' .dd-list').append(optionItemHtml(item)); + + }); + + if (data.message != '') { + alert(data.message); + } + + $('#groupNewUserModal').modal('toggle'); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + }); + + $('#groupNameEditBtn').on('click', function () { + var group_uid = $('#client_groupname_group_uid').val(); + var group_name = $('#clientGroupName').val().trim(); + + if (group_name == '') { + alert('群組名稱不得為空白!'); + return; + } + + var formData = { + group_uid: group_uid, + group_name: group_name + } + + $.ajax({ + url: "/AuthApi/editGroupName", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + tmpGroupNameItem.text(group_name); + $('#clientGroupNameEditModal').modal('toggle'); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + + }); }); + function newMemberClick(dept_uid) { var formData = { dept_uid: dept_uid @@ -163,16 +265,13 @@ function groupList() { data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { - $('#group_div').html(''); + $('#group_div').children().remove(); var obj = data.groups; var items = ""; $.each(obj, function (i, item) { - - - - - + $('#group_div').append(cardHtml(item)); + $('#' + item.group_uid).nestable(); }); } else { alert(data.message); @@ -206,6 +305,8 @@ function deptList() { }); + + groupList(); } else { alert(data.message); @@ -245,7 +346,32 @@ function delMemberBtnClick(obj) { if (group_uid == '') { delItem.remove(); } else { + var formData = { + dept_uid: $('#dept_select').val(), + group_uid: group_uid, + user_uid: user_uid + } + $.ajax({ + url: "/AuthApi/delGroupUser", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + + delItem.remove(); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); } } } @@ -256,11 +382,11 @@ function cardHtml(groupOption) { html += "
"; html += " "; html += "
"; - html += "
" + groupOption.group_name + "
"; + html += "
" + groupOption.group_name + "
"; html += "
"; html += " "; html += "
    "; - html += "
  1. "; + //html += "
  2. "; $.each(groupOption.users, function (index, item) { html += optionItemHtml(item); @@ -271,10 +397,111 @@ function cardHtml(groupOption) { html += "
"; html += " "; html += "
"; - html += " 新增成員 "; + html += " 新增成員 "; html += "
"; html += "
"; html += "
"; return html; +} + +function editGroupClick(obj) { + tmpGroupNameItem = $(obj).parent().parent(); + var group_name = $(obj).parent().parent().text().trim(); + var grid_obj = $(obj).parent().parent().parent().parent(); + var group_uid = $(obj).parent().parent().attr("data-group-uid"); + + $('#client_groupname_group_uid').val(group_uid); + $('#clientGroupName').val(group_name).trigger('change'); + + $('#clientGroupNameEditModal').modal('toggle'); + +} + +function delGroupClick(obj) { + var group_name = $(obj).parent().parent().text().trim(); + var grid_obj = $(obj).parent().parent().parent().parent(); + var group_uid = $(obj).parent().parent().attr("data-group-uid"); + + if (confirm('請確認是否要刪除此群組?')) { + var formData = { + group_uid: group_uid + } + + $.ajax({ + url: "/AuthApi/delGroup", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + + grid_obj.remove(); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); + } +} + +function addUserItem(obj) { + var group_uid = $(obj).attr("data-type"); + tmpNestableObj = $(obj).parent().parent().parent(); + + $('#client_group_group_uid').val(group_uid); + $('#client_group_dept_uid').val($('#dept_select').val()); + + var formData = { + dept_uid: $('#dept_select').val() + } + + $.ajax({ + url: "/AuthApi/noGroupUserList", + type: "post", + data: formData, + success: function (data, textStatus, jqXHR) { + if (data.ret == "yes") { + $('#lastgroup_memberlist').html('
勾選要加入群組的成員
'); + + var obj = data.userList; + var items = ""; + $.each(obj, function (i, item) { + var isExist = 'N'; + + $.each($('#' + group_uid + ' .dd-list li'), function () { + var tmp_uid = $(this).attr('data-user-uid'); + if (tmp_uid == item.user_uid) { + isExist = 'Y'; + } + }); + + if (isExist == 'N') { + items += ''; + } + + + }); + + $('#lastgroup_memberlist').append(items); + + $('#groupNewUserModal').modal('toggle'); + } else { + alert(data.message); + + if (data.err_code == "99999") { + location.href = "/Root/Login"; + } + } + }, + error: function (jqXHR, textStatus, errorThrown) { + alert('網路或伺服器發生錯誤,請稍後重試!'); + } + }); } \ No newline at end of file