云开发文章评论功能

150 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情

今天继续分享我的毕设吧。

微信小程序ios和安卓获取本机时间时格式不同,ios时间读取的时候,不能使用2022-1-1,要使用2022/1/1,所以要做字符串替换,这一步要进行格式转换day.replace(/-/g, "/") 转成 //2022/1/1的格式后可以正常进行判断了。

这次说一下文章的评论和点赞功能如何实现。

因为我用的是云开发,所以传输数据时会有问题。目前云开发的数据库操作是不能对嵌套数组进行增删改查操作的,所以我要存一个json格式的数组时就会有一个数据修改的问题。我要存的评论是数组对象,这里其实不适合用数组对象的,因为uni云数据库中有模板是评论表,表结构是这样的,

{
  "bsonType": "object",
  "required": [
    "article_id",
    "user_id",
    "comment_content",
    "like_count",
    "comment_type",
    "comment_user_name",
    "reply_user_id",
    "comment_user_avatar",
    "reply_comment_id"
  ],
  "permission": {
    "read": true,
    "create": "auth.uid != null",
    "update": "doc.user_id == auth.uid",
    "delete": "doc.user_id == auth.uid"
  },
  "properties": {
    "_id": {
      "description": "存储文档 ID(帖子 ID),系统自动生成"
    },
    "article_id": {
      "bsonType": "string",
      "description": "帖子ID,opendb-news-posts 表中的`_id`字段",
      "title": "帖子Id",
      "foreignKey": "chat._id"
    },
    "user_id": {
      "bsonType": "string",
      "description": "评论者ID,参考`uni-id-users` 表",
      "title": "评论用户id",
      "forceDefaultValue": {
        "$env": "uid"
      },
      "foreignKey": "uni-id-users._id"
    },
    "comment_content": {
      "bsonType": "string",
      "description": "评论内容",
      "title": "评论内容",
      "trim": "right"
    },
    "comment_type": {
      "bsonType": "int",
      "title": "回复类型 1为回复 0为评论",
      "description": "回复类型: 0 针对文章的回复  1 针对评论的回复"
    },
    "reply_user_id": {
      "bsonType": "string",
      "description": "被回复的评论用户ID,comment_type为1时有效",
      "title": "被回复用户Id",
      "foreignKey": "uni-id-users._id"
    },
    "reply_comment_id": {
      "bsonType": "string",
      "title": "回复人的评论id",
      "description": "回复人的评论id,comment_type为1时有效",
      "foreignKey": "opendb-news-comments._id"
    },
    "comment_user_name": {
      "bsonType": "string",
      "title": "回复人的用户名",
      "description": "回复人的用户名,comment_type为1时有效"
    },
    "comment_user_avatar": {
      "bsonType": "string",
      "title": "回复人的头像",
      "description": "回复人的头像,comment_type为1时有效"
    },
    "comment_date": {
      "bsonType": "timestamp",
      "description": "评论发表时间",
      "title": "评论发表时间",
      "forceDefaultValue": {
        "$env": "now"
      }
    },
    "comment_ip": {
      "bsonType": "string",
      "description": "评论发表时 IP 地址",
      "forceDefaultValue": {
        "$env": "clientIP"
      }
    }
  }
}

这里我推断uniapp云开发是推荐我们用这样的表来做评论功能的,但是由于之前使用的微信小程序云开发是可以对嵌套数组进行操作的,所以我还是选择了在一张表里完成评论功能,这就导致了我只能将数据取出来修改好再重新更新到数据库中去,数据量比较大,这又导致了另一个问题。云函数中数据传输似乎有限制,我尝试将其放到前端编写数据库操作语句,解决了此问题。