云开发的创建部署
1.创建项目时,后端服务选择【小程序·云函数】

2.进入项目后点击【云开发】并开通云开发,(开通过程比较简单,此处省略)

-
创建项目时,后端服务未选择【小程序·云函数】,可进行手动创建,参照步骤3、步骤4、步骤5
3.在project.config.json文件中加入以下两句代码
"cloudfunctionRoot": "cloudfunctions/",
"cloudfunctionTemplateRoot": "cloudfunctionTemplate",
4.使用此项目的appid创建一个新的项目,并执行步骤1,步骤2,将此项目的appid开通【云开发】
-
开通完毕,返回原项目,新建云函数文件夹
image.png
6.新建Node.js云函数(正常流程从此步骤开始)

5.右键云函数,在外部终端窗口打开

6.终端中输入命令,安装wx-server-sdk
npm install --save wx-server-sdk@latest
7.右键云函数,创建并部署:所有文件

8.点击云函数》云函数列表,查看云函数已创建部署成功

云函数的使用
1.在云函数的index.js实现你需要的代码
// 云函数入口函数
exports.main = async (event, context) => {
try {
const res = await cloud.openapi.security.msgSecCheck({
content: event.content
})
console.log(event)
return res;
} catch (err) {
return err;
}
}
右键云函数上传并部署

2.调用云函数
wx.cloud.callFunction({
name: 'yunhanshu',//此处为云函数名
data: {
content: '传入云函数的值'
},
success: function (res) {
},
fail: function (res) {
}
})
网友评论