已有小程序加入云开发步骤
1、按如下新建、移动文件夹
2、修改project.config.json 文件
更改项目appid
"appid": "wxb51fd021075b8f28",
新增云函数路径:
"cloudfunctionRoot": "cloud/functions/",
更新dist路径:
"miniprogramRoot": "znkf-xcx/dist",
3、在云函数functions/login目录下安装wx-server-sdk
npm install --save wx-server-sdk@latest
Npm install --save crypto@latest
//(有可能会报错)在云函数目录functions/login目录下cnpm install
4、app.js中初始化云
componentDidMount() {
//将redux状态挂载到 Taro 对象上,方便使用
Taro.$store = store;
//初始化云cloud
if (process.env.TARO_ENV === 'weapp') {
Taro.cloud.init({
env:'znkf-xcx-shengchan-4cd1w',
traceUser: true
})
}
}
// config中添加配置:cloud: true,
5、创建云函数cloud/functions/login0513.js
const cloud = require('wx-server-sdk')
// 初始化 cloud
cloud.init({
env: 'cupdata-yjs8m',
// secretId: 'AKIDNVM5XndCUUElqe9tXtyUiiSy1oEOtFG0',
// secretKey: 'bR0ECzLRdZU7pIe7LXi37xWNjnalfMmO',
});
const db = cloud.database();
const userCollection = db.collection("user");
const _ = db.command
/**
* 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端
*
* event 参数包含小程序端调用传入的 data
*
*/
exports.main = async event => {
const { OPENID } = cloud.getWXContext();
const { name, gender, avatarUrl } = event;
console.log('event', event);
console.log("name1:", name);
// 可执行其他自定义逻辑
//查询该openid用户是否存在
try {
//查询出所有结果再做filter筛选,效率不高
// const allUser = (await userCollection.get()).data
// const [userInfo] = allUser.filter(v => v.openId === OPENID)
// console.log('查到的userInfo', userInfo)
const [userRecord] = (await userCollection.where({
openId: _.eq(OPENID)
}).get()).data
console.log('查到的userInfo1', userRecord)
//如无该openid用户信息,则插入该用户信息
if (!userRecord) {
console.log("用户不存在");
await userCollection.add({
data: {
name: name,
gender: gender,
avatarUrl: avatarUrl,
openId: OPENID,
createdTime: db.serverDate(),
},
})
console.log("已新增该用户");
return {
code: 1,
message: "用户不存在,已新增该用户",
userInfo:{
name: name || null,
avatarUrl: avatarUrl || null,
gender: gender || null,
openId: OPENID,
}
};
} else {
console.log("用户已存在");
await userCollection.doc(userRecord._id).update({
data: {
name,
gender,
avatarUrl
}
});
console.log("已更新该用户信息");
return {
code: 2,
message: "用户已存在,已更新该用户信息",
userInfo:{
name: name || null,
avatarUrl: avatarUrl || null,
gender: gender || null,
openId: OPENID,
}
};
}
// console.log 的内容可以在云开发云函数调用日志查看
// 获取 WX Context (微信调用上下文),包括 OPENID、APPID、及 UNIONID(需满足 UNIONID 获取条件)
return {
event,
// openid: wxContext.OPENID,
// appid: wxContext.APPID,
// unionid: wxContext.UNIONID,
};
} catch (e) {
console.log(e);
return {
code: 500,
message: "服务器错误",
}
}
};
6、创建package.json
{
"name": "sbuscribe0617",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "^2.1.2"
}
}
6、小程序端调用云函数
Taro.cloud
.callFunction({
name: 'login0514',
data: {
name: nickName,
gender:gender,
avatarUrl:avatarUrl
},
}).then(cloudres =>{
console.log("cloudres",cloudres);
})
Project.config.json
{
"miniprogramRoot": "znkf-xcx/dist/",
"cloudfunctionRoot": "cloud/functions/",
"projectname": "znkf-xcx(redux)",
"description": "答题小程序,redux版本",
"appid": "wxb51fd021075b8f28",
"setting": {
"urlCheck": true,
"es6": false,
"postcss": false,
"minified": false
},
"compileType": "miniprogram",
"simulatorType": "wechat",
"simulatorPluginLibVersion": {},
"cloudfunctionTemplateRoot": "cloudfunctionTemplate",
"condition": {}
}
const db = cloud.database();
const userCollection = db.collection("user");
const [userRecord] = (await userCollection.where({openId: OPENID}).get()).data;
await userCollection.doc(userRecord._id).update({data: {name, gender,avatarUrl }});
//event包含了传参和userInfo中的appid+openid
event { name: '宋力文',
userInfo:
{ appId: 'wxb51fd021075b8f28',
openId: 'oJxib5fj7dGxWclHAOD2xrp3X8zo' } }