PageSourceSearch

https://lingyuzhao.top/js/comment.js

js lingyuzhao.top collected 2026-10-06 13:16:00 UTC 15,636 bytes, 367 lines download raw bytes

1/**
2 * 删除一个评论
3 * @param id 需要被删除的评论数据
4 * @param controller 需要使用的控制器
5 * @return {Promise<any>}
6 */
7function deleteComment(id, controller) {
8    if (controller === undefined || controller === '') {
9        console.error("评论处理器不明确,评论删除失败!", controller)
10        const temp = {}
11        temp[WEB_CONF.RES_RETURN] = "评论处理器不明确,评论删除失败!";
12        return Promise.reject(temp)
13    }
14    return axios(
15        {
16            url: backEnd_host_href() + '/' + controller + WEB_CONF.COMMENT_DELETE,
17            params: {
18                id: id,
19                userId: my.id
20            }
21        }
22    ).then(
23        (res) => {
24            return res.data;
25        }
26    ).catch(
27        (e) => {
28            const res = {}
29            res[WEB_CONF.RES_RETURN] = e.message
30            return res;
31        }
32    )
33}
34
35/**
36 *
37 * @param e 指定的评论数据容器
38 * @param json {{id, user, userId, date, data, ip}}
39 *     所有的子评论数据 json 对象
40 * @param fun1 点击子评论头像之后的回调函数
41 * @param fun2 删除评论成功之后的回调函数
42 * @param controller 指定的控制器
43 */
44function addCommentSub(e, json, fun1 = undefined, fun2 = undefined, controller = WEB_CONF['CONTROLLER_TRENDS']) {
45    // 获取当前用户
46    const user = json.user;
47    if (user !== undefined) {
48        const comment_div_reply = document.createElement("div");
49        comment_div_reply.className = "comment_div comment_div_reply pre_div";
50        if (isMobileDevice()) {
51            comment_div_reply.classList.add("comment_div_Mobile")
52        }
53        const img = document.createElement("img");
54        img.className = 'avatar headSculpture'
55        getUserConfById(json.userId).then(
56            (res) => {
57                img.src = res.headSculpture[1]
58                const user_a = document.createElement("a");
59                user_a.className = "headSculpture_a user_a user_a_id_num"
60                // 尝试获取到用户的 tempName 并赋值给 a
61                LY_Utils.getUserJsonByKeyById(user.id, 'tempName', (res) => user_a.innerText = res, (_) => user_a.innerText = user.name);
62                if (fun1 !== undefined) {
63                    img.onclick = fun1;
64                    user_a.onclick = fun1;
65                }
66                const reqDate = document.createElement("div");
67                reqDate.innerText = " ";
68                const reqDate_span = document.createElement("span");
69                reqDate_span.innerText = json.date;
70                reqDate.appendChild(reqDate_span);
71                const data_pre = document.createElement("pre");
72                data_pre.className = "Trends_data_comment";
73                data_pre.innerText = json.data;
74                const area_span = document.createElement("a");
75                area_span.className = "area_span";
76                area_span.innerText = "î§§ " + json.ip
77                area_span.href = 'https://www.baidu.com/s?ie=UTF-8&wd=' + json.ip;
78                area_span.target = "_blank";
79                const buttonArea = document.createElement("div");
80                // 开始添加
81                comment_div_reply.appendChild(img)
82                comment_div_reply.appendChild(user_a);
83                comment_div_reply.appendChild(reqDate);
84                comment_div_reply.appendChild(data_pre);
85                if ((!isNotLogin()) && user.id.toString() === my.id) {
86                    // 代表是自己的评论 可以删除
87                    // 设置此评论的删除按钮
88                    const delete_comment_button = document.createElement("button");
89                    delete_comment_button.setAttribute("class", "delete_comment_button user_self");
90                    delete_comment_button.innerText = " 删除此回复"
91                    delete_comment_button.addEventListener("click", () => {
92                        if (confirm("您确定要删除此回复评论吗?")) {
93                            deleteComment(json.id, controller).then(
94                                (res) => {
95                                    if (WEB_CONF.RES_RETURN_OK_VALUE === res[WEB_CONF.RES_RETURN]) {
96                                        if (fun2 !== undefined) {
97                                            fun2()
98                                        }
99                                    } else {
100                                        jokerBoxPopUp.show("评论删除失败:" + res[WEB_CONF.RES_RETURN])
101                                    }
102                                }
103                            )
104                        }
105                    })
106                    delete_comment_button.style.margin = '0'
107                    area_span.style.lineHeight = delete_comment_button.style.height
108                    buttonArea.appendChild(delete_comment_button)
109                }
110
111                buttonArea.style.width = '100%'
112                area_span.style.display = 'inline-block'
113                area_span.style.height = '100%'
114                comment_div_reply.style.height = '100%'
115                buttonArea.appendChild(area_span);
116                comment_div_reply.appendChild(buttonArea)
117                e.appendChild(comment_div_reply)
118            }
119        )
120    }
121}
122
123/**
124 * 像指定元素添加一个评论数据
125 * @param showArea 信息输出点
126 * @param e 指定的评论数据容器
127 * @param json {
128 *     {
129 *     id, user, userId, date, data, ip, sub : [
130 *         {
131 *                     id, user, userId, date, data
132 *         }
133 *     ]
134 * }
135 * }
136 * @param link 当前页面的链接
137 * @param fun1 回复评论成功之后的回调
138 * @param fun2 评论删除成功之后的回调
139 * @param controller 控制器的名称!
140 */
141function addComment(showArea, e, json, link, fun1 = undefined, fun2 = undefined, c
141ontroller = WEB_CONF['CONTROLLER_TRENDS']) {
142    // 创建出评论数据的 标签
143    const comment_div = document.createElement("div");
144    const img = document.createElement("img");
145    const user_a = document.createElement("a");
146    const date_div = document.createElement("div");
147    const date_span = document.createElement("span");
148    const data_pre = document.createElement("pre");
149    const reply_comment_button = document.createElement("button");
150    const reply_comment_div = document.createElement("div");
151    // 设置此评论数据的 id
152    comment_div.setAttribute("class", "comment_div pre_div");
153    if (isMobileDevice()) {
154        comment_div.classList.add("comment_div_Mobile")
155    }
156    comment_div.id = json.id;
157    // 设置此评论数据的 发送者
158    img.className = 'avatar headSculpture'
159    getUserConfById(json.userId).then(
160        (res) => {
161            // 获取到用户配置数据
162            const userJson = json.user;
163            const toHome = () => toPersonHomePage(userJson);
164            img.src = res.headSculpture[1]
165            // 设置点击头像跳转
166            img.onclick = toHome
167            user_a.setAttribute("class", "headSculpture_a user_a")
168            // 尝试获取到头像 如果出错了就直接用 userJson.name
169            LY_Utils.getUserJsonByKeyById(userJson.id, 'tempName', (res) => user_a.innerText = res, (_) => user_a.innerText = userJson.name);
170            // 设置点击用户名跳转
171            user_a.onclick = toHome
172            comment_div.appendChild(img);
173            comment_div.appendChild(user_a);
174            // 设置此评论的日期
175            date_div.innerText = " ";
176            date_span.innerText = json.date;
177            date_div.appendChild(date_span)
178            comment_div.appendChild(date_div);
179            // 设置此评论的文本数据
180            data_pre.className = "Trends_data_comment";
181            data_pre.innerText = json.data;
182            comment_div.appendChild(data_pre);
183            // 设置此评论的地区位置
184            const area_span = document.createElement("a");
185            area_span.className = "area_span";
186            area_span.innerText = "î§§ " + json.ip;
187            area_span.href = 'https://www.baidu.com/s?ie=UTF-8&wd=' + json.ip;
188            area_span.target = "_blank";
189            comment_div.appendChild(area_span);
190            // 设置此评论的回复数据按钮
191            reply_comment_button.setAttribute("class", "reply_comment_button");
192            reply_comment_button.innerText = " 回复此评论"
193            reply_comment_button.addEventListener("click", () => {
194                let subData = '';
195                do {
196                    subData = window.prompt("请输入您要回复的评论.", subData)
197                    if (subData != null) {
198                        if (subData.length > 256) {
199                            jokerBoxPopUp.show("您的评论字数不能超过 256!!!")
200                        } else {
201                            break;
202                        }
203                    } else {
204                        return;
205                    }
206                } while (true);
207                addComment_button(
208                    showArea,
209                    link,
210                    userJson.id,
211                    json.id,
212                    subData,
213                    fun1, controller)
214            })
215            comment_div.appendChild(reply_comment_button)
216            if ((!isNotLogin()) && userJson.id.toString() === my.id) {
217                // 设置此评论的删除按钮
218                const delete_comment_button = document.createElement("button");
219                delete_comment_button.setAttribute("class", "delete_comment_button");
220                delete_comment_button.innerText = " 删除此评论"
221                delete_comment_button.addEventListener("click", () => {
222                    if (confirm("您确定要删除此评论吗?")) {
223                        deleteComment(json.id, controller).then(
224                            (res) => {
225                                if (WEB_CONF.RES_RETURN_OK_VALUE === res[WEB_CONF.RES_RETURN]) {
226                                    if (fun2 !== undefined) {
227                                        fun2()
228                                    }
229                                } else {
230                                    jokerBoxPopUp.show("评论删除失败:" + res[WEB_CONF.RES_RETURN])
231                                }
232                            }
233                        )
234                    }
235                })
236                comment_div.appendChild(delete_comment_button)
237            }
238            // 设置此评论的子回复数据
239            const sub = json.sub;
240            for (let i = 0; i < sub.length; i++) {
241                const subElement = sub[i];
242                subElement.date = getDate(subElement.date);
243                addCommentSub(reply_comment_div, subElement, () => toPersonHomePage(subElement.user), fun2, controller)
244            }
245            comment_div.appendChild(reply_comment_div);
246            e.appendChild(comment_div)
247        }
248    )
249
250}
251
252
253/**
254 * 加载所有的评论数据
255 * @param show_area 加载期间需要使用到的信息输出点
256 * @param controller 评论所属的控制器
257 * @param link 当前页面的链接
258 * @param fun1 回复评论成功之后的回调
259 * @param fun2 评论删除成功之后的回调
260 */
261function loadAllComment(show_area, controller = WEB_CONF['CONTROLLER_TRENDS'], link, fun1 = undefined, fun2 = undefined) {
262    return axios(
263        {
264            url: backEnd_host_href() + '/' + controller + WEB_CONF.COMMENT_GET,
265            method: 'post',
266            params: {
267                father: document.getElementsByClassName("TrendsId_span")[0].innerText
268            }
269        }
270    ).then(
271        (res) => {
272            res = res.data
273            if (WEB_CONF.RES_RETURN_OK_VALUE === res[WEB_CONF.RES_RETURN]) {
274                const json_date = res[WEB_CONF.COMMENT_ALL_KEY]
275                const comment_div_pHome = document.getElementById("comment_div_PHome");
276                for (let i = json_date.length - 1; i >= 0; i--) {
277                    const jsonDateElement = json_date[i];
278                    jsonDateElement.date = getDate(jsonDateElement.date);
279                    addComment(show_area, comment_div_pHome, jsonDateElement, link, fun1, fun2, controller)
280                }
281            } else {
282                show_area.innerText = res[WEB_CONF.RES_RETURN]
283            }
284        }
285    )
286}
287
288/**
289 * 回复一个评论
290 * @param showArea 信息输出点
291 * @param father 当前评论回复的对象id
292 * @param data 评论的内容
293 * @param address 评论地区
294 * @param link 当前界面的链接
295 * @param recId 评论信息的通知人员 一般是被评论的用户
296 * @param controller 评论所属的控制器
297 * @param fun 评论成功之后的回调函数
298 */
299function addCommentFunByFatherDataAddress(showArea, father, data, address, link, recId, controller, fun = undefined) {
300    if (controller === undefined || controller === '') {
301        console.error("评论处理器不明确,评论发送失败!", controller)
302        return;
303    }
304    axios(
305        {
306            url: backEnd_host_href() + '/' + controller + WEB_CONF.COMMENT_ADD,
307            method: 'post',
308            params: {
309                father: father,
310                data: data,
311                ipAddress: address,
312                link: link,
313                recId: recId
314            }
315        }
316    ).then(
317        (res) => {
318            res = res.data;
319            if (WEB_CONF.RES_RETURN_OK_VALUE === res[WEB_CONF.RES_RETURN]) {
320                if (fun !== undefined) {
321                    fun()
322                }
323            } else {
324                show_area.innerText = new Date().toUTCString() + ": " + res[WEB_CONF.RES_RETURN];
325            }
326        }
327    )
328}
329
330/**
331 * 添加评论
332 * @param showArea 信息输出点
333 * @param father 当前评论回复的对象id
334 * @param data 评论的内容
335 * @param link 当前界面的链接
336 * @param recId 评论信息的通知人员 一般是被评论的用户
337 * @param fun 评论成功之后的回调函数
338 * @param controller 评论需要使用的控制器
339 */
340function addComment_button(showArea, link, recId,
341                           father = TRENDS_DATA_TEMP.id,
342                           data = document.getElementById("commentTrends_input_data").value,
343                           fun = undefined, controller = WEB_CONF['CONTROLLER_TRENDS']
344) {
345    if (data === undefined) {
346        return;
347    }
348    if (data.length <= 0) {
349        jokerBoxPopUp.show("评论内容不能为空!!")
350        addComment_button(showArea, link, recId, father, window.prompt("请修改您的评论.", data), fun, controller)
351        return;
352    }
353    if (data.length > 256) {
354        jokerBoxPopUp.show("评论的字数不得超过256个字!!")
355        addComment_button(showArea, link, recId, father, window.prompt("请修改您的评论.", data), fun, controller)
356        return;
357    }
358    if (isNotLogin()) {
359        jokerBoxPopUp.show("请先进行登录操作");
360        return;
361    }
362    getIPLocationAndCache(my.ip).then(
363        (json) => {
364            addCommentFunByFatherDataAddress(showArea, father, data, json.address, link, recId, controller, fun)
365        }
366    )
367}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.