无关风月
2025-05-08 9486766c806fe1d9e082b2fd02ea1cc558f1b443
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
 * 管理初始化
 */
var TQuestion = {
    id: "TQuestionTable",    //表格id
    seItem: null,        //选中的条目
    table: null,
    layerIndex: -1
};
 
/**
 * 初始化表格的列
 */
TQuestion.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: '序号', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '问题标题', field: 'content', visible: true, align: 'center', valign: 'middle'},
        {title: '添加时间', field: 'insertTime', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                var date = new Date(value);
                date.setHours(date.getHours() + 8);
                return date.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    timeZone: 'Asia/Shanghai' // 替换为您想要的时区
                });
            }},
        {title: '排序', field: 'sort', visible: true, align: 'center', valign: 'middle'},
        {title: '状态', field: 'state', visible: true, align: 'center', valign: 'middle',
            formatter: function (value, row, index) {
                return {1: "已上架", 2: "已下架", 3: "已删除"}[value]
            }
        }
    ];
};
 
/**
 * 检查是否选中
 */
TQuestion.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
 
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录!");
        return false;
    }else{
        TQuestion.seItem = selected[0];
        return true;
    }
};
TQuestion.dataCount = 0;
TQuestion.phone = "";
TQuestion.phoneId = "";
 
/**
 * 点击添加
 */
TQuestion.openAdd = function () {
    var index = layer.open({
        type: 2,
        title: '添加常见问题',
        area: ['100%', '100%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/tQuestion/add'
    });
    this.layerIndex = index;
};
 
 
/**
 * 点击编辑
 */
TQuestion.openChange = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length >1 ){
        Feng.info("只能选择一个问题进行编辑!");
    }else {
        if (this.check()){
            var selected = $('#' + this.id).bootstrapTable('getSelections');
            var index = layer.open({
                type: 2,
                title: '详情',
                area: ['100%', '100%'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/tQuestion/editQuestion/' + TQuestion.seItem.id
            });
            this.layerIndex = index;
        }
    }
};
 
 
 
/**
 * 上架
 */
 
TQuestion.onShelf = function () {
if (this.check()){
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    const data1 = {
        questionId:[],
        type:null
    };
    selected.forEach(function(obj) {
        var id = obj.id;
        data1.questionId.push(id);
    });
    data1.type = 1;
    $.ajax({
        url: Feng.ctxPath + "/tQuestion/changeState",
        type: "POST",
        contentType: "application/json", // 设置请求头的 Content-Type
        data: JSON.stringify(data1), // 将数据转换为 JSON 字符串
        success: function(response) {
            Feng.success("上架成功!");
            TQuestion.table.refresh();
        },
        error: function(xhr, status, error) {
            var errorMessage = xhr.responseText ? xhr.responseText : "上架失败!";
            Feng.error("您的网络异常!");
        }
    });
}
};
 
 
/**
 * 下架
 */
TQuestion.offShelf = function () {
 
    if (this.check()){
        var selected = $('#' + this.id).bootstrapTable('getSelections');
        const data1 = {
            questionId:[],
            type:null
        };
        selected.forEach(function(obj) {
            var id = obj.id;
            data1.questionId.push(id);
        });
        data1.type = 2;
        $.ajax({
            url: Feng.ctxPath + "/tQuestion/changeState",
            type: "POST",
            contentType: "application/json", // 设置请求头的 Content-Type
            data: JSON.stringify(data1), // 将数据转换为 JSON 字符串
            success: function(response) {
                Feng.success("下架成功!");
                TQuestion.table.refresh();
 
            },
            error: function(xhr, status, error) {
                var errorMessage = xhr.responseText ? xhr.responseText : "下架失败!";
                Feng.error("您的网络异常!");
            }
        });
    }
};
/**
 * 查看详情
 */
TQuestion.getInfo = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length >1 ){
        Feng.info("只能选择一条进行查看!");
    }else {
        if (this.check()){
            var selected = $('#' + this.id).bootstrapTable('getSelections');
 
            var index = layer.open({
                type: 2,
                title: '详情',
                area: ['100%', '100%'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/tQuestion/infoQuestion/' + TQuestion.seItem.id
            });
            this.layerIndex = index;
        }
    }
};
 
/**
 * 删除
 */
TQuestion.delete = function () {
    if (this.check()){
        var selected = $('#' + this.id).bootstrapTable('getSelections');
 
        const data1 = {
            questionId:[],
            type:null
        };
        selected.forEach(function(obj) {
            var id = obj.id;
            data1.questionId.push(id);
        });
        data1.type = 3;
 
        $.ajax({
            url: Feng.ctxPath + "/tQuestion/changeState",
            type: "POST",
            contentType: "application/json", // 设置请求头的 Content-Type
            data: JSON.stringify(data1), // 将数据转换为 JSON 字符串
            success: function(response) {
                Feng.success("删除成功!");
                TQuestion.table.refresh();
            },
            error: function(xhr, status, error) {
                var errorMessage = xhr.responseText ? xhr.responseText : "删除失败!";
                Feng.error(errorMessage);
            }
        });
    }
};
/**
 * 打开查看详情
 */
TQuestion.openDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '详情',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tQuestion/editQuestion/' + TQuestion.seItem.id
        });
        this.layerIndex = index;
    }
};
 
 
/**
 * 查询列表
 */
TQuestion.search = function () {
    var queryData = {};
    queryData['content'] = $("#content").val();
    queryData['state'] = $("#state").val();
    TQuestion.table.refresh({query: queryData});
};
 
 
/**
 * 重置搜索
 */
TQuestion.resetSearch = function () {
    $("#content").val('');
    $("#state").val('');
    TQuestion.search();
};
 
$(function () {
 
    const data1 = {
        id:null,
        phone:null,
        insertTime:null
    };
    // 获取客服电话
    $.ajax({
        url: Feng.ctxPath + "/tPhone/list",
        type: "POST",
        contentType: "application/json", // 设置请求头的 Content-Type
        success: function(response) {
            var phoneValue = null;
            var split = [];
            // 遍历列表数据使用 forEach() 方法
            response.forEach(function (item) {
                TQuestion.phoneId = item.id;
                phoneValue = item.phone;
                split = phoneValue.split(",");
            });
            if (response[0].phone !== "" && response[0].phone!== null) {
                var dataContainer = document.getElementById('phone');
                split.forEach(function (item) {
                    if (item !== ''){
                        // 创建新的数据项
                        var dataItem = document.createElement('div');
                        dataItem.className = 'data-item';
                        dataItem.innerText = item;
                        // 创建删除按钮
                        var deleteButton = document.createElement('div');
                        deleteButton.className = 'delete-button';
                        deleteButton.innerHTML = '×';
                        // 为删除按钮添加点击事件监听器
                        deleteButton.addEventListener('click', function () {
                            TQuestion.dataCount--;
                            // 在点击事件处理程序中删除数据项
                            dataContainer.removeChild(dataItem);
                        });
                        // 将删除按钮添加到数据项
                        dataItem.appendChild(deleteButton);
                        // 将数据项添加到容器中
                        dataContainer.appendChild(dataItem);
                        TQuestion.dataCount++;
                        // 获取item元素的文本内容(不包含"x")
                        var itemValues = Array.from(dataContainer.getElementsByClassName('data-item')).map(function (item) {
                            return item.innerText.replace(/[\u00D7]/g, '').trim();
                        });
                    }
 
                });
            }
            var addButton = document.getElementById('addButton');
            addButton.addEventListener('click', function () {
                var phoneRegex = /^1[0-9]{10}$/;
                var landlineRegex = /^\d{4}-\d{7}$/;
 
                var inputField = document.getElementById('addPhone');
                var dataContainer = document.getElementById('phone');
                var inputData = inputField.value.trim();
                if (TQuestion.dataCount>=2){
                    Feng.info("最多添加两个客服电话!");
                    // todo 添加正则表达式验证电话号码合法性
                }else if(!phoneRegex.test(inputData)&&!landlineRegex.test(inputData)){
                    Feng.info("客服电话不合法,请重新输入!");
                }else {
                    if (inputData !== '' || document.querySelectorAll('.data-item').length !==0) {
                        TQuestion.phone = "";
                        // 创建新的数据项
                        var dataItem = document.createElement('div');
                        if (inputData !== ''){
                            dataItem.className = 'data-item';
                            dataItem.innerText = inputData;
                            // 创建删除按钮
                            var deleteButton = document.createElement('div');
                            deleteButton.className = 'delete-button';
                            deleteButton.innerHTML = '×';
                            // 为删除按钮添加点击事件监听器
                            deleteButton.addEventListener('click', function () {
                                TQuestion.dataCount--;
                                // 在点击事件处理程序中删除数据项
                                dataContainer.removeChild(dataItem);
                            });
                            // 将删除按钮添加到数据项
                            dataItem.appendChild(deleteButton);
                            // 将数据项添加到容器中
                            dataContainer.appendChild(dataItem);
                            // 清空输入框的内容
                            inputField.value = '';
                            TQuestion.dataCount++;
                        }
                        var dataItems = document.querySelectorAll('.data-item');
                        for(var i = 0; i < dataItems.length; i++) {
                            var dataItem1 = dataItems[i];
                            TQuestion.phone = TQuestion.phone+dataItem1.textContent.slice(0, -1)+",";
                        }
                        data1.phone = TQuestion.phone;
                        data1.id = TQuestion.phoneId;
                        $.ajax({
                            url: Feng.ctxPath + "/tPhone/update",
                            type: "POST",
                            contentType: "application/json", // 设置请求头的 Content-Type
                            data: JSON.stringify(data1), // 将数据转换为 JSON 字符串
                            success: function(response) {
                                Feng.success("添加成功!");
                                TQuestion.table.refresh();
                                // window.parent.TQuestion.table.refresh();
                                TQuestion.phone = "";
                            },
                            error: function(xhr, status, error) {
                                TQuestion.phone = "";
                                var errorMessage = xhr.responseText ? xhr.responseText : "添加失败!";
                                Feng.error(errorMessage);
                            }
                        });
                    } else {
                        Feng.info("请输入客服电话!");
                    }
                }
 
            });
            // window.parent.TQuestion.table.refresh();
        },
        error: function(xhr, status, error) {
            var errorMessage = xhr.responseText ? xhr.responseText : "删除失败!";
            Feng.error(errorMessage);
        }
    });
    var defaultColunms = TQuestion.initColumn();
    var table = new BSTable(TQuestion.id, "/tQuestion/list", defaultColunms);
    table.setPaginationType("client");
    TQuestion.table = table.init();
});