mirror of
				https://github.com/Kakune55/ComiPy.git
				synced 2025-11-04 06:24:40 +08:00 
			
		
		
		
	feat:添加评论删除功能
This commit is contained in:
		@@ -2,6 +2,14 @@ import time
 | 
				
			|||||||
import db.util as util
 | 
					import db.util as util
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 查找评论
 | 
				
			||||||
 | 
					def getById(id: str):
 | 
				
			||||||
 | 
					    "通过id查找所有评论"
 | 
				
			||||||
 | 
					    conn = util.getConn()
 | 
				
			||||||
 | 
					    c = conn.cursor()
 | 
				
			||||||
 | 
					    cursor = c.execute("SELECT * FROM Comments WHERE id = ?", (id,))
 | 
				
			||||||
 | 
					    return cursor.fetchone()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 查找评论
 | 
					# 查找评论
 | 
				
			||||||
def listByBookid(id: str):
 | 
					def listByBookid(id: str):
 | 
				
			||||||
    "通过bookid查找所有评论"
 | 
					    "通过bookid查找所有评论"
 | 
				
			||||||
@@ -74,3 +82,16 @@ def new(bookid: str, from_uid: int, score: str, content=""):
 | 
				
			|||||||
    conn.commit()
 | 
					    conn.commit()
 | 
				
			||||||
    conn.close()
 | 
					    conn.close()
 | 
				
			||||||
    return
 | 
					    return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 查找评论
 | 
				
			||||||
 | 
					def remove(id:int)->bool:
 | 
				
			||||||
 | 
					    "通过id删除评论"
 | 
				
			||||||
 | 
					    conn = util.getConn()
 | 
				
			||||||
 | 
					    c = conn.cursor()
 | 
				
			||||||
 | 
					    c.execute("DELETE FROM Comments WHERE id = ?", (id,))
 | 
				
			||||||
 | 
					    conn.commit()
 | 
				
			||||||
 | 
					    changes = conn.total_changes
 | 
				
			||||||
 | 
					    conn.close()
 | 
				
			||||||
 | 
					    if changes == 0:
 | 
				
			||||||
 | 
					        return False
 | 
				
			||||||
 | 
					    return True
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -74,32 +74,32 @@
 | 
				
			|||||||
<body>
 | 
					<body>
 | 
				
			||||||
    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
 | 
					    <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
 | 
				
			||||||
        <div class="modal-dialog">
 | 
					        <div class="modal-dialog">
 | 
				
			||||||
          <div class="modal-content">
 | 
					            <div class="modal-content">
 | 
				
			||||||
            <div class="modal-header">
 | 
					                <div class="modal-header">
 | 
				
			||||||
              <h1 class="modal-title fs-5" id="exampleModalLabel">评论</h1>
 | 
					                    <h1 class="modal-title fs-5" id="exampleModalLabel">评论</h1>
 | 
				
			||||||
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
 | 
					                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					                <div class="modal-body">
 | 
				
			||||||
 | 
					                    <form id="comment" action="/api/comment/upload" method="post">
 | 
				
			||||||
 | 
					                        <select class="form-select" aria-label="Default select example" name="score">
 | 
				
			||||||
 | 
					                            <option value="none">无评价</option>
 | 
				
			||||||
 | 
					                            <option value="like">赞👍</option>
 | 
				
			||||||
 | 
					                            <option value="dislike">踩👎</option>
 | 
				
			||||||
 | 
					                        </select>
 | 
				
			||||||
 | 
					                        <div class="input-group" style="margin-top: 10px;">
 | 
				
			||||||
 | 
					                            <span class="input-group-text">评论</span>
 | 
				
			||||||
 | 
					                            <textarea class="form-control" name="text" aria-label="With textarea"></textarea>
 | 
				
			||||||
 | 
					                        </div>
 | 
				
			||||||
 | 
					                        <input type="hidden" name="bookid" value="{{data[0][1]}}" />
 | 
				
			||||||
 | 
					                    </form>
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
 | 
					                <div class="modal-footer">
 | 
				
			||||||
 | 
					                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
 | 
				
			||||||
 | 
					                    <button type="button" class="btn btn-primary" onclick="commentSubmit()">提交</button>
 | 
				
			||||||
 | 
					                </div>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <div class="modal-body">
 | 
					 | 
				
			||||||
              <form id="comment" action="/api/comment/upload" method="post">
 | 
					 | 
				
			||||||
                <select class="form-select" aria-label="Default select example" name="score">
 | 
					 | 
				
			||||||
                    <option value="none">无评价</option>
 | 
					 | 
				
			||||||
                    <option value="like">赞👍</option>
 | 
					 | 
				
			||||||
                    <option value="dislike">踩👎</option>
 | 
					 | 
				
			||||||
                  </select>
 | 
					 | 
				
			||||||
                  <div class="input-group" style="margin-top: 10px;" >
 | 
					 | 
				
			||||||
                    <span class="input-group-text">评论</span>
 | 
					 | 
				
			||||||
                    <textarea class="form-control" name="text" aria-label="With textarea"></textarea>
 | 
					 | 
				
			||||||
                  </div>
 | 
					 | 
				
			||||||
                  <input type="hidden" name="bookid" value="{{data[0][1]}}"/>
 | 
					 | 
				
			||||||
              </form>
 | 
					 | 
				
			||||||
            </div>
 | 
					 | 
				
			||||||
            <div class="modal-footer">
 | 
					 | 
				
			||||||
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
 | 
					 | 
				
			||||||
              <button type="button" class="btn btn-primary" onclick="commentSubmit()">提交</button>
 | 
					 | 
				
			||||||
            </div>
 | 
					 | 
				
			||||||
          </div>
 | 
					 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
      </div>
 | 
					    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <div class="container">
 | 
					    <div class="container">
 | 
				
			||||||
@@ -112,10 +112,12 @@
 | 
				
			|||||||
                <!-- 详细信息 -->
 | 
					                <!-- 详细信息 -->
 | 
				
			||||||
                <h1>{{ data[0][2] }}</h1>
 | 
					                <h1>{{ data[0][2] }}</h1>
 | 
				
			||||||
                <h3>更新时间: {{time}}</h3>
 | 
					                <h3>更新时间: {{time}}</h3>
 | 
				
			||||||
                <h2>👍{{socre["like"]}}  👎{{socre["dislike"]}}</h2>
 | 
					                <h2>👍{{socre["like"]}} 👎{{socre["dislike"]}}</h2>
 | 
				
			||||||
                <button class="btn btn-primary" onclick="window.location.href='/view/{{ id }}'">在线浏览</button>
 | 
					                <button class="btn btn-primary" onclick="window.location.href='/view/{{ id }}'">在线浏览</button>
 | 
				
			||||||
                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">撰写评论</button>
 | 
					                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">撰写评论</button>
 | 
				
			||||||
 | 
					                {%if islogin == "admin"%}
 | 
				
			||||||
                <button class="btn btn-danger">删除资源</button>
 | 
					                <button class="btn btn-danger">删除资源</button>
 | 
				
			||||||
 | 
					                {% endif %}
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
@@ -130,18 +132,20 @@
 | 
				
			|||||||
            {% for item in comments %}
 | 
					            {% for item in comments %}
 | 
				
			||||||
            <div class="comment">
 | 
					            <div class="comment">
 | 
				
			||||||
                <h3>{{item["from"]}}:
 | 
					                <h3>{{item["from"]}}:
 | 
				
			||||||
                {%if item["socre"] == "like"%}
 | 
					                    {%if item["socre"] == "like"%}
 | 
				
			||||||
                <small class="text-muted">觉得很赞👍</small>
 | 
					                    <small class="text-muted">觉得很赞👍</small>
 | 
				
			||||||
                {%endif%}
 | 
					                    {%endif%}
 | 
				
			||||||
                {%if item["socre"] == "dislike"%}
 | 
					                    {%if item["socre"] == "dislike"%}
 | 
				
			||||||
                <small class="text-muted">点了个踩👎</small>
 | 
					                    <small class="text-muted">点了个踩👎</small>
 | 
				
			||||||
                {%endif%}
 | 
					                    {%endif%}
 | 
				
			||||||
                </h3>
 | 
					                </h3>
 | 
				
			||||||
                <h3>{{item["text"]}}</h3>
 | 
					                <h3>{{item["text"]}}</h3>
 | 
				
			||||||
                <small class="text-muted">id:{{item["id"]}}  {{item["time"]}}</small>
 | 
					                <small class="text-muted">id:{{item["id"]}} {{item["time"]}}</small>
 | 
				
			||||||
 | 
					                {% if islogin == item["from"] %}
 | 
				
			||||||
 | 
					                <button class="btn btn-danger" id="{{item['id']}}" onclick="delComment(id)">删除</button>
 | 
				
			||||||
 | 
					                {% endif %}
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            {% endfor %}
 | 
					            {% endfor %}
 | 
				
			||||||
            <!-- 在此添加更多评论 -->
 | 
					 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -151,9 +155,29 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    <script>
 | 
					    <script>
 | 
				
			||||||
        const comment = document.getElementById("comment")
 | 
					        const comment = document.getElementById("comment")
 | 
				
			||||||
        function commentSubmit(){
 | 
					        function commentSubmit() {
 | 
				
			||||||
            comment.submit()
 | 
					            comment.submit()
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        function delComment(id) {
 | 
				
			||||||
 | 
					            fetch("/api/comment/remove?id=" + id, {
 | 
				
			||||||
 | 
					                method: 'GET'
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					                .then(response => {
 | 
				
			||||||
 | 
					                    // 首先检查HTTP状态码
 | 
				
			||||||
 | 
					                    if (response.ok) {
 | 
				
			||||||
 | 
					                        // 请求成功,刷新页面
 | 
				
			||||||
 | 
					                        location.reload();
 | 
				
			||||||
 | 
					                    } else {
 | 
				
			||||||
 | 
					                        // 请求失败,抛出一个错误
 | 
				
			||||||
 | 
					                        throw new Error('网络请求失败,状态码:' + response.status);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					                .catch(error => {
 | 
				
			||||||
 | 
					                    // 处理任何在请求过程中发生的错误
 | 
				
			||||||
 | 
					                    alert('请求失败:' + error.message);
 | 
				
			||||||
 | 
					                });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    </script>
 | 
					    </script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,3 +25,20 @@ def comment_api():  # 概览
 | 
				
			|||||||
        request.form["text"],
 | 
					        request.form["text"],
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
    return redirect("/book/" + request.form["bookid"])
 | 
					    return redirect("/book/" + request.form["bookid"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@comment_api_bp.route("/api/comment/remove")
 | 
				
			||||||
 | 
					def remove():  # 删除api
 | 
				
			||||||
 | 
					    if request.cookies.get("islogin") is None:  # 验证登录状态
 | 
				
			||||||
 | 
					        return abort(403)
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        id = int(request.args.get("id"))
 | 
				
			||||||
 | 
					    except:
 | 
				
			||||||
 | 
					        return abort(400)
 | 
				
			||||||
 | 
					    commentInfo = db.comments.getById(id)
 | 
				
			||||||
 | 
					    if commentInfo is None:
 | 
				
			||||||
 | 
					        return abort(404)
 | 
				
			||||||
 | 
					    if int(request.cookies.get("uid")) == commentInfo[3]:
 | 
				
			||||||
 | 
					        if db.comments.remove(id):
 | 
				
			||||||
 | 
					            return "OK"
 | 
				
			||||||
 | 
					        return abort(404)
 | 
				
			||||||
 | 
					    return abort(400)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,6 +48,7 @@ def book(bookid):  # 接口
 | 
				
			|||||||
    raw_com = db.comments.listByBookid(bookid)
 | 
					    raw_com = db.comments.listByBookid(bookid)
 | 
				
			||||||
    comments = []
 | 
					    comments = []
 | 
				
			||||||
    for i in raw_com:
 | 
					    for i in raw_com:
 | 
				
			||||||
 | 
					        print(request.cookies.get("islogin"))
 | 
				
			||||||
        comments.append(
 | 
					        comments.append(
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                "id": i[0],
 | 
					                "id": i[0],
 | 
				
			||||||
@@ -65,6 +66,7 @@ def book(bookid):  # 接口
 | 
				
			|||||||
        time=time.strftime("%Y-%m-%d %H:%M:%S", local_time),
 | 
					        time=time.strftime("%Y-%m-%d %H:%M:%S", local_time),
 | 
				
			||||||
        socre=db.comments.getScore(bookid),
 | 
					        socre=db.comments.getScore(bookid),
 | 
				
			||||||
        comments=comments,
 | 
					        comments=comments,
 | 
				
			||||||
 | 
					        islogin=request.cookies.get("islogin")
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user