diff --git a/i_entrance/admin_userlist.php b/i_entrance/admin_userlist.php index 3459b957..55669c1e 100644 --- a/i_entrance/admin_userlist.php +++ b/i_entrance/admin_userlist.php @@ -18,8 +18,7 @@ require(__DIR__.'/../vendor/autoload.php'); - - +
diff --git a/js/admin_member.js b/js/admin_member.js deleted file mode 100644 index 7687441c..00000000 --- a/js/admin_member.js +++ /dev/null @@ -1,219 +0,0 @@ -var userFrame = '\ -\ - <%userID%>\ - <%userName%>\ - <%emailFunc(email)%>
(<%authType%>)\ - <%userGradeText%>

<%shortDate(blockUntil)%>

\ - <%nickname%>\ - \ - <%slotGeneralList%>\ - <%shortDate(joinDate)%>\ - <%shortDate(loginDate)%>\ - <%shortDate(deleteAfter)%>\ - \ -
\ - \ - \ - \ - \ - \ -
\ - \ -'; - -function convUserGrade(grade){ - var userGradeMap = { - 0:'차단', - 1:'일반', - 4:'특별', - 5:'부운영자', - 6:'운영자' - }; - - if(grade in userGradeMap){ - return userGradeMap[grade]; - } - return grade; -} - -function fillAllowJoinLogin(result){ - $('#radios_allow_join .active').removeClass('active'); - $('#radios_allow_login .active').removeClass('active'); - if(result.allowJoin){ - $('#allow_join_y').parent().addClass('active'); - } - else{ - $('#allow_join_n').parent().addClass('active'); - } - - if(result.allowLogin){ - $('#allow_login_y').parent().addClass('active'); - } - else{ - $('#allow_login_n').parent().addClass('active'); - } -} - -function fillUserList(result){ - var $user_list = $('#user_list'); - - - $user_list.empty(); - - var slotGeneralList = $.map(result.servers, function(value){ - return ''.format(value) - }).join('
'); - - var emailFunc = function(text){ - return String(text).replace('@','@
'); - } - var brFunc = function(text){ - return String(text).split(' ').join('
'); - }; - - var shortDateFunc = function(date){ - if(!date){ - return '-'; - } - return brFunc(date.substr(2)); - } - - $.each(result.users, function(idx, user){ - user.br = brFunc; - user.shortDate = shortDateFunc; - user.emailFunc = emailFunc; - if(!user.email){ - user.email = '-'; - } - user.slotGeneralList = slotGeneralList; - user.userGradeText = convUserGrade(user.userGrade); - - $user_list.append( - TemplateEngine(userFrame, user) - ) - }); - - //TODO: slotGeneralList에 값을 채워야함. ajax로 받아올 필요 있음 -} - -function changeSystem(action, param=null){ - var text = '{0}{1}을 진행합니다.'.format(action, param?(', '+param):''); - if(!confirm(text)){ - return; - } - - $.ajax({ - type:'post', - url:'j_set_userlist.php', - dataType:'json', - data:{ - 'action':action, - 'param':param - } - }).then(function(result){ - if(result.result){ - if(result.affected !== undefined){ - alert('{0}건이 처리되었습니다.'.format(result.affected)); - refreshAll(); - } - else{ - alert('완료되었습니다.'); - } - - } - else{ - alert(result.reason); - } - }); -} - -function changeUserStatus(action, userID, param=null){ - if(userID instanceof Element){ - userID = parseInt($(userID).parents('tr').data('id')); - } - if(!$.isNumeric(userID)){ - alert('userID가 올바르게 지정되지 않았습니다!'); - return; - } - userID = parseInt(userID); - - - if(action == 'set_userlevel'){ - if(!$.isNumeric(param)){ - param = prompt('원하는 등급을 입력해주세요.(1:일반, 4:특별, 5:부운영자, 6:운영자)', '1'); - } - param = parseInt(param); - - if(param < 1 || param > 6){ - alert('올바르지 않습니다.'); - return; - } - } - - if(action == 'block'){ - if(!$.isNumeric(param)){ - param = prompt('블록 기간을 입력해주세요. <= 0은 반영구(50년)입니다.', '7'); - } - param = parseInt(param); - } - - var userName = $('#userinfo_'+userID).data('username'); - - var text = '{0}에 대해서 {1}{2}을 진행합니다.'.format(userName, action, param?(', '+param):''); - if(!confirm(text)){ - return; - } - - - $.ajax({ - type:'post', - url:'j_set_userlist.php', - dataType:'json', - data:{ - 'action':action, - 'user_id':userID, - 'param':param - } - }).then(function(result){ - if(result.result){ - if(result.detail !== undefined){ - alert('완료되었습니다. : {0}'.format(result.detail)); - refreshAll(); - } - else{ - alert('완료되었습니다.'); - refreshAll(); - } - - } - else{ - alert(result.reason); - } - }); - -} - -function refreshAll(){ - $.ajax({ - type:'post', - url:'j_get_userlist.php', - dataType:'json' - }).then(function(result){ - if(!result.result){ - alert(reuslt.reason); - return; - } - - fillAllowJoinLogin(result); - fillUserList(result); - }); -} - -$(function(){ - refreshAll(); - - $('input[name=allow_join], input[name=allow_login]').on('change', function(){ - var $this = $(this); - changeSystem($this.attr('name'), $this.val()); - }) -}); diff --git a/ts/admin_member.ts b/ts/admin_member.ts new file mode 100644 index 00000000..af445897 --- /dev/null +++ b/ts/admin_member.ts @@ -0,0 +1,289 @@ +import axios from 'axios'; +import $ from 'jquery'; +import { isNumber } from 'lodash'; +import { TemplateEngine } from '../hwe/ts/util/TemplateEngine'; +import { InvalidResponse } from '../hwe/ts/defs'; +import { setAxiosXMLHttpRequest } from '../hwe/ts/util/setAxiosXMLHttpRequest'; +import { unwrap_any } from '../hwe/ts/util/unwrap_any'; +import { convertFormData } from '../hwe/ts/util/convertFormData'; + +declare global { + interface Window { + changeSystem: (action: string) => Promise; + changeUserStatus: (action: string, userID: Element | number, param?: number) => Promise; + } +} + +type UserEntry = { + userID: string, + userName: string, + email: string, + authType: string | null, + userGrade: number, + blockUntil: string | null, + nickname: string, + icon: string, + joinDate: string, + loginDate: string, + deleteAfter: string | null, +} + +type UserListResponse = { + result: true, + users: UserEntry[], + servers: string[], + allowJoin: boolean, + allowLogin: boolean, +} + +const userFrame = '\ +\ + <%userID%>\ + <%userName%>\ + <%emailFunc(email)%>
(<%authType%>)\ + <%userGradeText%>

<%shortDate(blockUntil)%>

\ + <%nickname%>\ + \ + <%slotGeneralList%>\ + <%shortDate(joinDate)%>\ + <%shortDate(loginDate)%>\ + <%shortDate(deleteAfter)%>\ + \ +
\ + \ + \ + \ + \ + \ +
\ + \ +'; + +function convUserGrade(grade: number): string { + const userGradeMap = { + 0: '차단', + 1: '일반', + 4: '특별', + 5: '부운영자', + 6: '운영자' + }; + + if (grade in userGradeMap) { + return userGradeMap[grade as (keyof typeof userGradeMap)]; + } + return grade.toString(); +} + +function fillAllowJoinLogin(result: UserListResponse) { + if (result.allowJoin) { + $('#allow_join_y').trigger('click'); + } + else { + $('#allow_join_n').trigger('click'); + } + + if (result.allowLogin) { + $('#allow_login_y').trigger('click'); + } + else { + $('#allow_login_n').trigger('click'); + } +} + +function fillUserList(result: UserListResponse) { + const $user_list = $('#user_list'); + + + $user_list.empty(); + + const slotGeneralList = $.map(result.servers, function (value) { + return ``; + }).join('
'); + + const emailFunc = function (text: string) { + return String(text).replace('@', '@
'); + } + const brFunc = function (text: string) { + return String(text).split(' ').join('
'); + }; + + const shortDateFunc = function (date: string | null) { + if (!date) { + return '-'; + } + return brFunc(date.substr(2)); + } + + $.each(result.users, function (idx, user) { + const templateItem = { + ...user, + email: user.email ?? '-', + br: brFunc, + shortDate: shortDateFunc, + emailFunc: emailFunc, + slotGeneralList: slotGeneralList, + userGradeText: convUserGrade(user.userGrade), + } + + $user_list.append( + TemplateEngine(userFrame, templateItem) + ) + }); + + //TODO: slotGeneralList에 값을 채워야함. ajax로 받아올 필요 있음 +} + +async function changeSystem(action: string, param?: string): Promise { + const text = `${action}${param ? (', ' + param) : ''}을 진행합니다.`; + if (!confirm(text)) { + return; + } + + let result: InvalidResponse | { + result: true, + affected?: number, + }; + + try { + const response = await axios({ + url: 'j_set_userlist.php', + method: 'post', + responseType: 'json', + data: convertFormData({ + 'action': action, + 'param': param ?? null + }) + }) + result = response.data; + } + catch (e) { + console.error(e); + alert(`실패했습니다: ${e}`); + return; + } + + if (!result.result) { + alert(result.reason); + return; + } + + if (result.affected) { + alert(`${result.affected}건이 처리되었습니다.`); + await refreshAll(); + } + else { + alert('완료되었습니다.'); + } +} + +async function changeUserStatus(action: string, userID: Element | number, param?: number): Promise { + if (userID instanceof Element) { + userID = parseInt($(userID).parents('tr').data('id')); + } + if (!isNumber(userID)) { + alert('userID가 올바르게 지정되지 않았습니다!'); + return; + } + + + if (action == 'set_userlevel') { + if (!isNumber(param)) { + param = parseInt(prompt('원하는 등급을 입력해주세요.(1:일반, 4:특별, 5:부운영자, 6:운영자)', '1') ?? '0'); + } + + if (param < 1 || param > 6) { + alert('올바르지 않습니다.'); + return; + } + } + + if (action == 'block') { + if (!isNumber(param)) { + param = parseInt(prompt('블록 기간을 입력해주세요. <= 0은 반영구(50년)입니다.', '7') ?? '7'); + } + } + + const userName = unwrap_any($('#userinfo_' + userID).data('username')); + + const text = `${userName}에 대해서 ${action}${param ? (', ' + param) : ''}을 진행합니다.`; + if (!confirm(text)) { + return; + } + + let result: InvalidResponse | { + result: true, + detail?: string, + }; + + try { + const response = await axios({ + url: 'j_set_userlist.php', + method: 'post', + responseType: 'json', + data: convertFormData({ + 'action': action, + 'user_id': userID, + 'param': param ?? null + }) + }) + result = response.data; + } + catch (e) { + console.error(e); + alert(`실패했습니다: ${e}`); + return; + } + + if (!result.result) { + alert(result.reason); + return; + } + + if (result.detail) { + alert(`완료되었습니다: ${result.detail}`); + } + else { + alert('완료되었습니다.'); + } + + await refreshAll(); +} + +async function refreshAll() { + let result: InvalidResponse | UserListResponse; + + try { + const response = await axios({ + url: 'j_get_userlist.php', + method: 'post', + responseType: 'json', + }); + result = response.data; + } + catch (e) { + console.error(e); + alert(`실패했습니다: ${e}`); + return; + } + + if (!result.result) { + alert(result.reason); + return; + } + + fillAllowJoinLogin(result); + fillUserList(result); +} + +$(async function () { + setAxiosXMLHttpRequest(); + window.changeSystem = changeSystem; + window.changeUserStatus = changeUserStatus; + + await refreshAll(); + + $('input[name=allow_join], input[name=allow_login]').on('change', async function () { + const $this = $(this); + await changeSystem(unwrap_any($this.attr('name')), unwrap_any($this.val())); + }) +}); diff --git a/webpack.config.js b/webpack.config.js index f2febb42..2d07a20a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -115,6 +115,7 @@ module.exports = (env) => { 'common': './ts/common_deprecated.ts', 'entrance': './ts/entrance.ts', 'user_info': './ts/user_info.ts', + 'admin_member': './ts/admin_member.ts', }, output: { filename: '[name].js',