unicloud云开发进阶30-项目14引入uni-id用户体系基于openDB创建Schema

117 阅读3分钟

将获取到的省市信息赋值到data

  export default {
    data() {
      return {
        // 是否显示工具条
        toolShow:false,
        // 是否选中工具条其中一项
        headShow:false,
        // 字体加粗
        boldShow:false,
        // 字体倾斜
        italicShow:false,
        // 用户提交内容
        artObj:{
          title:"",
          content:"",
          description:"",
          picurls:"",
          province:""
        }
      };
    },
    onLoad() {
      getProvince().then(res=>{
        this.artObj.province = res
      })
    }, 

image.png

现在该有的信息都拿到了 image.png

接下来就是往数据库中添加数据了,这就需要采集用户信息了,接下来就是导入uni-id用户体系

文档位置: uniapp.dcloud.net.cn/uniCloud/un…

在文档中进入插件市场,安装uni-id-pages插件

在pages.json中找到登录页的地址 uni_modules/uni-id-pages/pages/login/login-withpwd

image.png

点击登录后弹出这个提示框说明依赖已经安装成功

image.png

剩下的就是配置uni-id 在云函数、公共文件、uni-config-center目录中,新建一个uni-id文件夹,将config.json文件复制进来

image.png

config.json这个文件不知道哪里来的,视频中直接提供了,全部代码如下:

{
  "passwordSecret": "", 
  "passwordStrength": "medium", 
  "tokenSecret": "", 
  "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": ""
    }
  }
}

然后将第一项和第三项改成这样

{
  "passwordSecret": "quanzi", 
  "passwordStrength": "medium", 
  "tokenSecret": "quanzi@#z4", 

设置完成后,再点击登陆

image.png

点击注册,注册一个admin111的用户后,就进入了之前的页面

image.png

现在数据表un-id-users中,已经有刚注册的用户信息了

新建schema

在云函数、database中创建一个叫quanzi_article的schema 使用内建的文章schema,这样就不用自己写了,之前都是使用默认的空模板 image.png

image.png

在这个schema中,title和id是必填的,content不是,所以给他删掉

{
	"bsonType": "object",
	"required": [
		"user_id",
		"title",
		"content"
	],

接下来,读的权限改成所有人都能读,把原本的权限验证改成true

	"permission": {
		"read": "doc.user_id == auth.uid && doc.article_status == 0 || doc.article_status == 1",
		"create": "auth.uid != null",
		"update": "doc.user_id == auth.uid",
		"delete": "doc.user_id == auth.uid"
	},

就把第一个字段读改成true就行了

	"permission": {
		"read": true,
		"create": "auth.uid != null",
		"update": "doc.user_id == auth.uid",
		"delete": "doc.user_id == auth.uid"
	},

接下来是这个schema的foreignKey还是绑定的用户表的id,这里不做改变

"user_id": {
    "bsonType": "string",
    "description": "文章作者ID, 参考`uni-id-users` 表",
    "foreignKey": "uni-id-users._id",
    "defaultValue": {
        "$env": "uid"
    }
},

项目没有分类,所以把分类删掉

		"category_id": {
			"bsonType": "string",
			"title": "分类",
			"description": "分类 id,参考`uni-news-categories`表",
			"foreignKey": "opendb-news-categories._id",
			"enum": {
				"collection": "opendb-news-categories",
				"field": "name as text, _id as value"
			}
		},

摘要字段也删掉

"excerpt": {
    "bsonType": "string",
    "title": "文章摘录",
    "description": "文章摘录",
    "label": "摘要",
    "trim": "both"
},

文章状态article_status字段是用来等待管理员审核的,这里没有做这个功能,暂时放着不动

view_count字段是阅读量,这个保留

like_count是点赞数,

is_sticky置顶字段项目中也没做,也删掉

"is_sticky": {
    "bsonType": "bool",
    "title": "是否置顶",
    "description": "是否置顶",
    "permission": {
        "write": false
    }
},

阅读加精字段is_essence也删掉

"is_essence": {
    "bsonType": "bool",
    "title": "阅读加精",
    "description": "阅读加精",
    "permission": {
        "write": false
    }
},

开放评论字段comment_status也是默认开放的吗,所以删掉

"comment_status": {
    "bsonType": "int",
    "title": "开放评论",
    "description": "评论状态:0 关闭  1 开放",
    "enum": [
        {
            "value": 0,
            "text": "关闭"
        },
        {
            "value": 1,
            "text": "开放"
        }
    ]
},

评论数量comment_count字段保留

最后评论用户的id字段last_comment_user_id也保留

封面大图(缩略图)把原本的字段名avatar改成本项目中使用的picurls,类型改成数组,空格是没有的,可以删掉

"avatar": {
    "bsonType": "string",
    "title": "封面大图",
    "description": "缩略图地址",
    "label": "封面大图",
    "trim": "both"
},

改成这样

"picurls": {
    "bsonType": "array",
    "title": "封面大图",
    "description": "缩略图地址",
    "label": "封面大图"
},

发布时间字段有自动获取当前发布时间,也不用改

"publish_date": {
    "bsonType": "timestamp",
    "title": "发表时间",
    "description": "发表时间",
    "defaultValue": {
        "$env": "now"
    }
},

获取服务端的IP地址字段publish_ip不动

最后修改时间字段last_modify_date也保留

最后修改时的IP字段last_modify_ip留不留都可以

排版显示模式字段mode删除

"mode": {
    "bsonType": "number",
    "title": "排版显示模式",
    "description": "排版显示模式,如左图右文、上图下文等"
}

缺少一个描述信息字段description和一个省份字段province,写在title字段下面

"title": {
    "bsonType": "string",
    "title": "标题",
    "description": "标题",
    "label": "标题",
    "trim": "both"
    },
    "description": {
    	"bsonType": "string",
    	"title": "文章摘要",
    	"description": "文章摘要",
    	"label": "文章摘要",
    	"trim": "both"
    },
        "province": {
    	"bsonType": "string",
    	"title": "发布省份",
    	"description": "发布省份",
    	"label": "发布省份",
    	"trim": "both"
    },