(六)、引入uni-id用户体系,基于opendb创建schema,向数据库添加新数据【uniapp+uinicloud多用户社区博客实战项目(完整开发文档-从零到完整项目)】

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 (六)、引入uni-id用户体系,基于opendb创建schema,向数据库添加新数据【uniapp+uinicloud多用户社区博客实战项目(完整开发文档-从零到完整项目)】,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

1,uni-id-pages

uni-id-pages链接
提供了一批现成的、开源的、登录注册账户管理相关的前端页面和云端云对象。

1.1 引入uni-id-pages

从dcloud插件市场通过hbuilder导入的方式引入
在这里插入图片描述
在这里插入图片描述

1.2 云端配置config.json

uni-id的云端配置文件在uniCloud/cloudfunctions/common/uni-config-center/uni-id/config.json中。
在这里插入图片描述
config.json

{
  "passwordSecret": "tigrehhzz",
  "passwordStrength": "medium",
  "tokenSecret": "tigrehhzz",
  "tokenExpiresIn": 7200,
  "tokenExpiresThreshold": 3600,
  "passwordErrorLimit": 6,
  "passwordErrorRetryTime": 3600,
  "autoSetInviteCode": false,
  "forceInviteCode": false,
  "app": {
    "tokenExpiresIn": 2592000,
    "tokenExpiresThreshold": 864000,
    "oauth": {
      "weixin": {
        "appid": "",
        "appsecret": ""
      },
      "qq": {
        "appid": "",
        "appsecret": ""
      },
      "apple": {
        "bundleId": ""
      }
    }
  },
  "web": {
    "tokenExpiresIn": 7200,
    "tokenExpiresThreshold": 3600,
    "oauth": {
      "weixin-h5": {
        "appid": "",
        "appsecret": ""
      },
      "weixin-web": {
        "appid": "",
        "appsecret": ""
      }
    }
  },
  "mp-weixin": {
    "tokenExpiresIn": 259200,
    "tokenExpiresThreshold": 86400,
    "oauth": {
      "weixin": {
        "appid": "",
        "appsecret": ""
      }
    }
  },
  "mp-qq": {
    "tokenExpiresIn": 259200,
    "tokenExpiresThreshold": 86400,
    "oauth": {
      "qq": {
        "appid": "",
        "appsecret": ""
      }
    }
  },
  "mp-alipay": {
    "tokenExpiresIn": 259200,
    "tokenExpiresThreshold": 86400,
    "oauth": {
      "alipay": {
        "appid": "",
        "privateKey": "",
        "keyType": "PKCS8"
      }
    }
  },
  "service": {
    "sms": {
      "name": "",
      "codeExpiresIn": 180,
      "smsKey": "",
      "smsSecret": "",
      "scene": {
        "bind-mobile": {
          "templateId": "",
          "codeExpiresIn": 240
        }
      }
    },
    "univerify": {
      "appid": "",
      "apiKey": "",
      "apiSecret": ""
    }
  }
}

1.3 切换到登录地址–注册新账号

http://localhost:8080/#/uni_modules/uni-id-pages/pages/login/login-withpwd
在这里插入图片描述
注册新账号

在这里插入图片描述

1.4 云数据库中查看账号信息(表明已经注册成功)

在这里插入图片描述

2,利用opendb创建文章的schema文件

2.1 新建文章schema

在这里插入图片描述在这里插入图片描述

2.2 quanzi_articles.schema.json

{
  "bsonType": "object",
  "required": [
    "user_id",
    "title"
  ],
  "permission": {
    "read": true,
    "create": "auth.uid != null",
    "update": "doc.user_id == auth.uid",
    "delete": "doc.user_id == auth.uid"
  },
  "properties": {
    "_id": {
      "description": "存储文档 ID(用户 ID),系统自动生成"
    },
    "user_id": {
      "bsonType": "string",
      "description": "文章作者ID, 参考`uni-id-users` 表",
      "foreignKey": "uni-id-users._id",
      "defaultValue": {
        "$env": "uid"
      }
    },
    "title": {
      "bsonType": "string",
      "title": "标题",
      "description": "标题",
      "label": "标题",
      "trim": "both"
    },
    "description": {
      "bsonType": "string",
      "title": "文章摘要",
      "description": "文章摘要",
      "label": "文章摘要",
      "trim": "both"
    },
    "province": {
      "bsonType": "string",
      "title": "发布省份",
      "description": "发布省份",
      "label": "发布省份",
      "trim": "both"
    },
    "content": {
      "bsonType": "string",
      "title": "文章内容",
      "description": "文章内容",
      "label": "文章内容",
      "trim": "right"
    },
    "article_status": {
      "bsonType": "int",
      "title": "文章状态",
      "description": "文章状态:0 草稿箱 1 已发布",
      "defaultValue": 0,
      "enum": [{
          "value": 0,
          "text": "草稿箱"
        },
        {
          "value": 1,
          "text": "已发布"
        }
      ]
    },
    "view_count": {
      "bsonType": "int",
      "title": "阅读数量",
      "description": "阅读数量",
      "defaultValue": 20,
      "permission": {
        "write": false
      }
    },
    "like_count": {
      "bsonType": "int",
      "description": "喜欢数、点赞数",
      "defaultValue": 0,
      "permission": {
        "write": false
      }
    },
    "comment_count": {
      "bsonType": "int",
      "description": "评论数量",
      "defaultValue": 0,
      "permission": {
        "write": false
      }
    },
    "last_comment_user_id": {
      "bsonType": "string",
      "description": "最后回复用户 id,参考`uni-id-users` 表",
      "foreignKey": "uni-id-users._id"
    },
    "picurls": {
      "bsonType": "array",
      "title": "封面大图",
      "description": "缩略图地址",
      "label": "封面大图"
    },
    "publish_date": {
      "bsonType": "timestamp",
      "title": "发表时间",
      "description": "发表时间",
      "defaultValue": {
        "$env": "now"
      }
    },
    "publish_ip": {
      "bsonType": "string",
      "title": "发布文章时IP地址",
      "description": "发表时 IP 地址",
      "forceDefaultValue": {
        "$env": "clientIP"
      }
    },
    "last_modify_date": {
      "bsonType": "timestamp",
      "title": "最后修改时间",
      "description": "最后修改时间",
      "defaultValue": {
        "$env": "now"
      }
    },
    "last_modify_ip": {
      "bsonType": "string",
      "description": "最后修改时 IP 地址",
      "forceDefaultValue": {
        "$env": "clientIP"
      }
    }
  },
  "version": "0.0.1"
}

3,向云数据库添加新数据

3.1 添加数据方法

在script中,声明db实列对象

  const db = uniCloud.database()

添加数据方法

      //添加数据到云数据库quanzi_articles
      async addData() {
        await db.collection("quanzi_articles").add({
          ...this.artObj
        }).then(res => {
          uni.hideLoading();
          uni.showToast({
            title: "发布成功"
          })
          setTimeout(() => {
            uni.reLaunch({
              url: "/pages/index/index"
            })
          }, 800)
        })
      },

3.2 测试

添加数据
在这里插入图片描述
数据库中查看:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135737.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!