uniapp云开发

13 min read

云开发概念

云函数

云数据库

云存储和cdn

云开发步骤

  1. 选择阿里云 创建 关联

  2. 创建云函数 上传部署云函数

  3. 打开云空间网页版

  4. 创建云数据库

  5. 解决H5云函数调用跨域问题

云函数基本结构

'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	//context 包含调用信息和运行状态
	//返回数据给客户端
	return event

};

调用云函数

uniCloud.callFunction({
	name:"views",  // 函数名
	data:{         // 参数 
		articleId:1,
		views:0
	},
	success() {
		// 成功回调
	},
	fail() {
		// 失败回调
	}
})

使用云数据库

  1. 初始化云数据库 const db = uniCloud.database();

  2. 获取集合对象collection const collection = db.collection(“article”);

  3. CURD

  • 增: await collection.add({});

  • 删: await collection.doc(“id号”).remove();

  • 改: await collection.doc(“id号”).update(); await collection.doc(“id号”).set();

update/set 区别; update只能更新存在的记录; set在没有记录的情况下会进行创建;

  • 查:

await collection.doc(“id号”).get() await collection.where({name:“xx”}).get(); // 根据条件进行查询

云函数url化

阿里云暂不支持修改响应头中的content-disposition,即无法返回html并在浏览器中展示,只可以触发下载