hxp
2024-10-31 402ed2e6a90a785d2fce3eca23cd324f350d54c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
include_once "/Account/User.php";
include_once "/db/DBOper.php";
include_once "/Common/Logging.php";
include_once "/Common/ServerOPS.php";
include_once "/Common/CommFunc.php";
include_once "/language/lang.php";
 
\Logging\CreateLogging("serverops.serverGroup.php");
 
$Permission = \User\Permission::P_OPSServerList;
$channel = $_SESSION['spid'];
$user = new \User\User($_SESSION['UserAccount']);
if (!$user->HavePermission($Permission)) {
    exit;
}
 
// echo "_POST:" . print_r($_POST, true), "<br/>";
\Logging\LogInfo(" _POST:" . print_r($_POST, true));
 
$alertMsg = "";
$groupJsonFile = \ServerOPS\GetServerGroup($channel);
 
$opType = $_POST["opType"];
 
if ($opType == "del") {
    $SortNum = intval($_POST["SortNum"]);
    $groupArray = json_decode(\CommFunc\GetFileContents($groupJsonFile, "[]"), true);
    \Logging\LogInfo("start to del servergroup channel:" . $channel . " SortNum:" . $SortNum . " groupInfo:" . \CommFunc\MyJson_encode($groupArray));
    foreach ($groupArray as $key => $value) {
        if ($SortNum == $value["SortNum"]) {
            unset($groupArray[$key]);
 
            $updateGroupArray = array();
            foreach ($groupArray as $value) {
                array_push($updateGroupArray, $value);
            }
            $updateGroupInfo = \CommFunc\MyJson_encode($updateGroupArray);
            \Logging\LogInfo("update servergroup channel:" . $channel . " updateGroupInfo:" . $updateGroupInfo);
            file_put_contents($groupJsonFile, $updateGroupInfo);
            $alertMsg = "删除成功!";
            break;
        }
    }
} elseif ($opType == "save") {
    \Logging\LogInfo("start to save servergroup channel:" . $channel);
    $i = 0;
    $saveGroupArray = array();
    while (true) {
        if (!array_key_exists("SortNum" . $i, $_POST)) {
            \Logging\LogInfo("post SortNum not exists:" . "SortNum" . $i);
            break;
        }
        $SortNum = intval($_POST["SortNum" . $i]);
        if ($SortNum <= 0) {
            \Logging\LogInfo("post SortNum <=0 :" . "SortNum" . $i);
            break;
        }
        $GroupTitle = $_POST["GroupTitle" . $i];
        $ServerIDList = json_decode($_POST["ServerIDList" . $i], true);
        array_push($saveGroupArray, array("SortNum" => $SortNum, "GroupTitle" => $GroupTitle, "ServerIDList" => $ServerIDList));
        $i += 1;
    }
    //排序 根据 SortNum 倒序排序  SORT_ASC 和 SORT_DESC
    array_multisort(array_column($saveGroupArray, 'SortNum'), SORT_DESC, $saveGroupArray);
    $saveGroupInfo = \CommFunc\MyJson_encode($saveGroupArray);
    \Logging\LogInfo("save servergroup channel:" . $channel . " saveGroupInfo:" . $saveGroupInfo);
    file_put_contents($groupJsonFile, $saveGroupInfo);
    $alertMsg = "保存成功!";
}
 
if ($alertMsg) {
    echo "<script>alert('" . $alertMsg . "')</script>";
}
 
$pageTitle = "区服分组";
 
$groupArray = json_decode(\CommFunc\GetFileContents($groupJsonFile, "[]"), true);
 
// echo "groupArray:" . print_r($groupArray, true);
 
//插入3个空分组待添加
for ($i = 0; $i < 3; $i++) {
    array_push($groupArray, array());
}
 
//显示表格字段配置 key-参数名,value-说明
$tableArray = array(
    "SortNum" => array("排序序号", "10%", "center", 5),
    "GroupTitle" => array("分组标题", "15%", "center", 10),
    "ServerIDList" => array("区服列表", "50%", "center", 50),
    "" => array("操作", "", "center", 0),
);
 
?>
 
<html>
 
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" type="text/css" href="/css/table.css">
</head>
 
<body>
    <center>
        <p><b><?php echo $pageTitle; ?></b></P>
    </center>
    <hr />
    <form action="serverGroup.php" method="post">
        <table width="50%">
            <caption>
                <p>
                    <input type="submit" value="保存分组" onclick="return CheckSubmit()" />
                    <input type="hidden" name="opType" id="opType" value="save" />
                </p>
                <p>
                    直接修改或在空分组直接输入新分组内容<br />
                    调整分组排序或区间后建议刷新选服列表<br />
                </p>
            </caption>
            <?php
            echo "<thead><tr>";
            foreach ($tableArray as $value) {
                echo "<th width=\"" . $value[1] . "\">" . $value[0] . "</td>";
            }
            echo "</tr></thead>";
 
            foreach ($groupArray as $i => $groupInfo) {
                echo "<tr class='trc'>";
                $SortNum = $groupInfo["SortNum"];
                $GroupTitle = $groupInfo["GroupTitle"];
                foreach ($tableArray as $key => $value) {
                    if (!$key) {
                        echo "<td align='center'>";
                        if ($groupInfo) {
                            echo " <input type=\"button\" value=\"删\" onclick=\"return suredel('$channel', '$SortNum', '$GroupTitle')\"/>&nbsp;";
                        }
                        echo "</td>";
                        continue;
                    }
 
                    $inputType = "text";
                    $tdContent = $groupInfo[$key];
                    if ($key == "ServerIDList" && $tdContent) {
                        $tdContent = json_encode($tdContent);
                    } else if ($key == "SortNum") {
                        // $inputType = "number";
                    }
                    $name = $key . $i;
                    echo "<td align='" . $value[2] . "'>" .
                        "<input type=\"" . $inputType . "\" name=\"" . $name . "\" id=\"" . $name . "\" value=\"" . $tdContent . "\" size=\""
                        . $value[3]  . "\" style=\"text-align: center;\" placeholder=\"空分组\"/>" .
                        "</td>";
                }
                echo "</tr>";
            }
            ?>
        </table>
 
    </form>
 
    <script type="text/javascript">
        function CheckSubmit() {
            var i = 0;
            var sortNumList = [];
            while (document.getElementById("SortNum" + i)) {
                if (!document.getElementById("SortNum" + i).value) {
                    i += 1;
                    continue;
                }
                var SortNum = Number(document.getElementById("SortNum" + i).value);
                if (!SortNum || SortNum <= 0) {
                    alert("排序序号需要大于0的值!");
                    document.getElementById("SortNum" + i).focus();
                    return false;
                }
                if (sortNumList.indexOf(SortNum) != -1) {
                    alert("排序序号不能重复!");
                    document.getElementById("SortNum" + i).focus();
                    return false;
                }
                sortNumList.push(SortNum);
 
                // alert(i + "=" + SortNum);
                if (!document.getElementById("GroupTitle" + i).value) {
                    alert("区服标题不能为空!");
                    document.getElementById("GroupTitle" + i).focus();
                    return false;
                }
 
                if (!document.getElementById("ServerIDList" + i).value) {
                    alert("区服列表不能为空!");
                    document.getElementById("ServerIDList" + i).focus();
                    return false;
                }
 
                try {
                    JSON.parse(document.getElementById("ServerIDList" + i).value)
                } catch (error) {
                    alert("区服列表格式错误!");
                    document.getElementById("ServerIDList" + i).focus();
                    return false;
                }
                i += 1;
            }
            return true;
        }
 
        function doPost(to, p) { // to:提交动作(action),p:参数 
            var myForm = document.createElement("form");
            myForm.method = "post";
            myForm.action = to;
            for (var i in p) {
                var myInput = document.createElement("input");
                myInput.setAttribute("name", i); // 为input对象设置name 
                myInput.setAttribute("value", p[i]); // 为input对象设置value 
                myForm.appendChild(myInput);
            }
            document.body.appendChild(myForm);
            myForm.submit();
            document.body.removeChild(myForm); // 提交后移除创建的form 
        }
 
        function suredel(Channel, SortNum, GroupTitle) {
            if (!window.confirm("确定删除" + Channel + "服务器分组[" + GroupTitle + "]?")) {
                return false;
            }
            doPost("serverGroup.php", {
                "opType": "del",
                "SortNum": SortNum,
            });
            return true;
        }
    </script>
</body>
 
</html>