In the operation of a website, the comment area of the document detail page plays an important role. It not only promotes user interaction and enhances content activity, but also brings new content and search engine visibility to the website.AnQiCMS provides a powerful and easy-to-integrate comment API, allowing you to easily implement the display and posting of comments on your website.This article will detail how to use these APIs to create a fully functional comment system for your document detail page.

The value of the comment feature and an overview of AnQi CMS comment API

The comment area is an important place for users to express their opinions and interact, which can effectively enhance users' sense of participation and belonging to the content.By commenting, you can collect user feedback, understand their opinions on the content, and even discover new directions for content creation.A security CMS that is well-versed in this field provides a dedicated comment API, allowing you to display, publish, and like core comment functions without delving into backend logic during custom development or frontend integration.

The AnQi CMS comment API mainly includes the following key interfaces, which together form the framework of the comment system:

  • Get Comment List Interface (/api/comment/list): Used to load and display existing comments on the document detail page.
  • Post Comment Interface (/api/comment/publish): Allows users to submit new comments or replies.
  • Comment Like Interface ()/api/comment/praise): Provides the function for users to like comments, increasing interactivity.

Next, we will delve deeper into how to use these interfaces.

Show existing comments: let users hear the real voice

To display comments on the document detail page, we need to call the "Get Comment List Interface" of AnQi CMS.This interface can retrieve all comments related to the document based on the document ID.

Firstly, when your document detail page loads, you need to obtain the unique identifier of the current document, that isDocument ID (archive_id). This ID is usually accessible throughthe Get Document Details API (/api/archive/detail)[en] Come and get it. Once you have this document ID, you can make a request to the comment list interface.

[en] The request address is usually{您的域名地址}/api/comment/list[en] When sending a GET request, you need to include thearchive_idAs a parameteridPass it in. For example, if your document ID is 71, the request might look like this:https://en.anqicms.com/api/comment/list?id=71.

In addition, to better control the display of comments, you can also use other optional parameters:

  • order: The sorting method of comments, for example,id descyou can display the latest comments at the top.
  • pageWhen there are many comments, you can use this parameter to implement pagination and only load comments of a specific page.
  • limitControl the number of comments displayed per page.

The returned data from the interface will be a JSON formatted list, which includes detailed information of each comment, such asid(Comment ID),parent_id(If this is a reply to another comment, then it is the parent comment ID),user_name(The nickname of the comment user),content(The comment content),created_time(The timestamp of publication),vote_count(The number of likes) etc.

In the front-end, you can use JavaScript to iterate over these data and dynamically render the comment content to the page. Forparent_idField, you can use it to implement the hierarchical display of comments, such asparent_idComments that are not zero are displayed as a reply to their parent comment with indentation to build a clear conversation structure. Don't forget tocreated_timeFormat this timestamp into a user-friendly date and time.

Post a new comment: Encourage users to actively participate

Displaying comments is the first step, but more importantly, it is necessary to provide users with a platform to express their opinions. This requires the use of the 'Comment Posting Interface' of the Anqi CMS.

When the user enters content in the comment box on the document detail page and clicks the 'Publish' button, your frontend code needs to collect the user's input and send it to the server via a POST request.{您的域名地址}/api/comment/publish.

This request requires the following key parameters:

  • archive_id: The ID of the current document, informing the system which article this comment is for.
  • user_nameCommenter's nickname.Although the comment API of Anqi CMS allows anonymous comments, it usually requires filling in a nickname.If you have integrated a user login system, you can automatically fill in the nickname of the logged-in user.
  • content: The specific content of the comment.
  • parent_id(Optional): If the user is replying to an existing comment, the replied comment'sid. This allows comments to form a conversation thread.

Please ensure that these data areapplication/jsonThe format is sent. After successful publication, the interface will return a response containing the new commentidandstatus(Status for review, may require manual review to be displayed) information JSON.After receiving a successful response, you can clear the comment input box on the frontend and refresh the comment list so that users can immediately see their posted comments (if the comment status allows direct display).

To enhance user experience, you can also consider adding client-side input validation to ensure that the user has entered a valid nickname and comment content.

Comment likes: enhance interaction and fun.

To make the comment section more lively, you can use the 'like comment interface' to allow users to express agreement with other people's comments.

When the user clicks the 'like' button next to the comment, your front-end code should send{您的域名地址}/api/comment/praisea POST request with the liked comment'sid.

The request body is also simple, it just needs to contain oneidThe field, which is the ID of the liked comment.After successfully liking, the interface will return the latest number of likes for the comment, and you can update the like display on the front-end page.To prevent users from liking repeatedly, you may also need to handle it accordingly on the frontend or through backend logic (such as recording users' liking behavior).

Integrate key points and enhance user experience

Integrate the CMS comment API into the document detail page, not just a simple call to the interface, but more importantly, to build a smooth and friendly user experience.

  1. [en] Strategy to obtain document IDin loading the document details page, througharchiveDetail.mdinterface to obtain the current document'sidis the crucial first step. ThisidIt will serve as the core parameter for the comment list and comment publishing interface.
  2. Front-end framework and data bindingWhether you use Vue, React, or native JavaScript, you need a mechanism to manage and render comment data.Consider using data binding to dynamically update the comment list, especially when new comments are posted or the number of likes on comments changes.
  3. User identity recognition: Although it is a required item in the comment publishing interface ofuser_nameis a required item, butuser_id[en]It is optional. If you want to distinguish between logged-in users and anonymous users, you can consider integrating the user login and registration interface of the Anqi CMS (/api/loginand/api/register)。The logged-in user can automatically fill in the nickname and may have more comment management permissions (such as deleting their own comments, if this feature is provided by the backend).
  4. Comment review mechanismPlease note.commentPublishReturned by the interface.statusField. If your security CMS backend is configured with comment review, new comments may not be displayed immediately, but will be in a pending review status (status: 0)。The front-end should be able to handle this situation, for example, displaying a prompt such as 'Comment submitted, pending review'.
  5. Error handling and user feedbackIn all API calls, there should be a sound error handling mechanism.If the API request fails, clear error messages should be displayed to the user instead of the page freezing or showing unknown errors.
  6. Preventing malicious commentsThe 'AnQi CMS' may provide comment spam filtering or sensitive word review features in the backend. You can also add simple validations on the frontend, such as word count limits, to improve comment quality.
  7. Asynchronous Loading and Performance OptimizationThe comment area can consider asynchronous loading to avoid hindering the display of the main document content, thereby improving page loading speed.

Through careful design and integration, you will be able to bring an active and interactive comment community to your website powered by your security CMS, greatly enriching your content ecosystem.


Common Questions and Answers (FAQ)

Does the Anqi CMS comment API support multi-level replies? How to implement it?Yes, the Anqi CMS comment API supports multi-level replies. When posting a comment, you can go throughparent_idThis parameter specifies which existing comment this reply is in response to. When you get the list of comments, the returned data containsparent_idThe field will indicate the ID of the parent comment. During frontend rendering, you can use it according toparent_idTo build the hierarchical structure of comments, for example, by using recursive components or specific DOM operations to achieve nested display of replies.

2. Whether the comment published by the user needs to be reviewed before it is displayed?It depends on your security CMS backend settings.commentPublishin the returned data of the interface,statusThe field will display the review approval status. IfstatusThe value 0 indicates that the comment is under review; if it is 1, it means the comment has passed the review and can be displayed.If your website requires strict content control, you can enable the comment review feature in the background, and all new comments will enter the review queue first.

3. How to prevent users from maliciously commenting or repeatedly liking?For comment spamming, the Anqi CMS backend usually has some built-in anti-spam mechanisms, such as comment frequency restrictions based on IP address or user ID.In addition, you can also limit the frequency of user comments on the front end.commentPraiseInterface design usually handles the logic of duplicate likes.In the frontend, you can use JavaScript to record whether a user has liked a comment and disable the like button for a short period of time to provide a more friendly user experience.To be more strict, the backend can check the user's like history when liking to prevent malicious liking.