已有小程序加入云开发步骤

1、按如下新建、移动文件夹

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

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' } }

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值