From adce0f5120be8375919c60b40843d3267f459c90 Mon Sep 17 00:00:00 2001 From: wbb <1975655432@qq.com> Date: Wed, 16 Nov 2022 21:54:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=85=A8=E5=B1=80=E5=8F=98?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E5=85=A8=E5=B1=80=E5=8F=AF=E8=B0=83=E7=94=A8?= =?UTF-8?q?openid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scr/food/app.js | 30 ++++++---- scr/food/cloudfunctions/get/config.json | 6 ++ scr/food/cloudfunctions/get/index.js | 16 ++++++ scr/food/cloudfunctions/get/package.json | 14 +++++ .../quickstartFunctions/config.json | 7 +++ .../createCollection/index.js | 56 +++++++++++++++++++ .../getMiniProgramCode/index.js | 20 +++++++ .../quickstartFunctions/getOpenId/index.js | 17 ++++++ .../quickstartFunctions/index.js | 25 +++++++++ .../quickstartFunctions/package.json | 14 +++++ .../quickstartFunctions/selectRecord/index.js | 12 ++++ .../quickstartFunctions/sumRecord/index.js | 18 ++++++ .../quickstartFunctions/updateRecord/index.js | 32 +++++++++++ scr/food/pages/detail/detail.js | 13 +++-- 14 files changed, 266 insertions(+), 14 deletions(-) create mode 100644 scr/food/cloudfunctions/get/config.json create mode 100644 scr/food/cloudfunctions/get/index.js create mode 100644 scr/food/cloudfunctions/get/package.json create mode 100644 scr/food/cloudfunctions/quickstartFunctions/config.json create mode 100644 scr/food/cloudfunctions/quickstartFunctions/createCollection/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/getMiniProgramCode/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/getOpenId/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/package.json create mode 100644 scr/food/cloudfunctions/quickstartFunctions/selectRecord/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/sumRecord/index.js create mode 100644 scr/food/cloudfunctions/quickstartFunctions/updateRecord/index.js diff --git a/scr/food/app.js b/scr/food/app.js index 95643d4..88e4722 100644 --- a/scr/food/app.js +++ b/scr/food/app.js @@ -15,15 +15,25 @@ App({ traceUser: true, }); } - +this.getOpenid() this.globalData = {}; - },//触底函数 - onReachBottom(){ - console.log("上拉加载...."); - }, - //上拉函数 - onPullDownRefresh(){ - console.log("下拉刷新..."); - }, + // 获取用户openid + + +}, +getOpenid() { + let that = this; + wx.cloud.callFunction({ + name: 'get', + complete: res => { + console.log('openid: ', res.result.openid) + console.log('appid: ', res.result.appid) + // var openid = res.result.openId; + // that.setData({ + // openid: openid + // }) + } + }) -}); + } +}) diff --git a/scr/food/cloudfunctions/get/config.json b/scr/food/cloudfunctions/get/config.json new file mode 100644 index 0000000..5ecc33e --- /dev/null +++ b/scr/food/cloudfunctions/get/config.json @@ -0,0 +1,6 @@ +{ + "permissions": { + "openapi": [ + ] + } +} \ No newline at end of file diff --git a/scr/food/cloudfunctions/get/index.js b/scr/food/cloudfunctions/get/index.js new file mode 100644 index 0000000..3f4e3c1 --- /dev/null +++ b/scr/food/cloudfunctions/get/index.js @@ -0,0 +1,16 @@ +// 云函数入口文件 +const cloud = require('wx-server-sdk') + +cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境 + +// 云函数入口函数 +exports.main = async (event, context) => { + const wxContext = cloud.getWXContext() + + return { + event, + openid: wxContext.OPENID, + appid: wxContext.APPID, + unionid: wxContext.UNIONID, + } +} \ No newline at end of file diff --git a/scr/food/cloudfunctions/get/package.json b/scr/food/cloudfunctions/get/package.json new file mode 100644 index 0000000..e7978fe --- /dev/null +++ b/scr/food/cloudfunctions/get/package.json @@ -0,0 +1,14 @@ +{ + "name": "get", + "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.6.3" + } +} \ No newline at end of file diff --git a/scr/food/cloudfunctions/quickstartFunctions/config.json b/scr/food/cloudfunctions/quickstartFunctions/config.json new file mode 100644 index 0000000..41a485c --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/config.json @@ -0,0 +1,7 @@ +{ + "permissions": { + "openapi": [ + "wxacode.get" + ] + } +} \ No newline at end of file diff --git a/scr/food/cloudfunctions/quickstartFunctions/createCollection/index.js b/scr/food/cloudfunctions/quickstartFunctions/createCollection/index.js new file mode 100644 index 0000000..75a40b9 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/createCollection/index.js @@ -0,0 +1,56 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: cloud.DYNAMIC_CURRENT_ENV +}); + +const db = cloud.database(); + +// 创建集合云函数入口函数 +exports.main = async (event, context) => { + try { + // 创建集合 + await db.createCollection('sales'); + await db.collection('sales').add({ + // data 字段表示需新增的 JSON 数据 + data: { + region: '华东', + city: '上海', + sales: 11 + } + }); + await db.collection('sales').add({ + // data 字段表示需新增的 JSON 数据 + data: { + region: '华东', + city: '南京', + sales: 11 + } + }); + await db.collection('sales').add({ + // data 字段表示需新增的 JSON 数据 + data: { + region: '华南', + city: '广州', + sales: 22 + } + }); + await db.collection('sales').add({ + // data 字段表示需新增的 JSON 数据 + data: { + region: '华南', + city: '深圳', + sales: 22 + } + }); + return { + success: true + }; + } catch (e) { + // 这里catch到的是该collection已经存在,从业务逻辑上来说是运行成功的,所以catch返回success给前端,避免工具在前端抛出异常 + return { + success: true, + data: 'create collection success' + }; + } +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/getMiniProgramCode/index.js b/scr/food/cloudfunctions/quickstartFunctions/getMiniProgramCode/index.js new file mode 100644 index 0000000..08e1a82 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/getMiniProgramCode/index.js @@ -0,0 +1,20 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: cloud.DYNAMIC_CURRENT_ENV +}); + +// 获取小程序二维码云函数入口函数 +exports.main = async (event, context) => { + // 获取小程序二维码的buffer + const resp = await cloud.openapi.wxacode.get({ + path: 'pages/index/index' + }); + const { buffer } = resp; + // 将图片上传云存储空间 + const upload = await cloud.uploadFile({ + cloudPath: 'code.png', + fileContent: buffer + }); + return upload.fileID; +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/getOpenId/index.js b/scr/food/cloudfunctions/quickstartFunctions/getOpenId/index.js new file mode 100644 index 0000000..edaf198 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/getOpenId/index.js @@ -0,0 +1,17 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: "cloud1-8g5wmepxce8a3b8a" +}); + +// 获取openId云函数入口函数 +exports.main = async (event, context) => { + // 获取基础信息 + const wxContext = cloud.getWXContext(); + + return { + openid: wxContext.OPENID, + appid: wxContext.APPID, + unionid: wxContext.UNIONID, + }; +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/index.js b/scr/food/cloudfunctions/quickstartFunctions/index.js new file mode 100644 index 0000000..d137f69 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/index.js @@ -0,0 +1,25 @@ +const getOpenId = require('./getOpenId/index'); +const getMiniProgramCode = require('./getMiniProgramCode/index'); +const createCollection = require('./createCollection/index'); +const selectRecord = require('./selectRecord/index'); +const updateRecord = require('./updateRecord/index'); +const sumRecord = require('./sumRecord/index'); + + +// 云函数入口函数 +exports.main = async (event, context) => { + switch (event.type) { + case 'getOpenId': + return await getOpenId.main(event, context); + case 'getMiniProgramCode': + return await getMiniProgramCode.main(event, context); + case 'createCollection': + return await createCollection.main(event, context); + case 'selectRecord': + return await selectRecord.main(event, context); + case 'updateRecord': + return await updateRecord.main(event, context); + case 'sumRecord': + return await sumRecord.main(event, context); + } +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/package.json b/scr/food/cloudfunctions/quickstartFunctions/package.json new file mode 100644 index 0000000..4350dbb --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/package.json @@ -0,0 +1,14 @@ +{ + "name": "quickstartFunctions", + "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.4.0" + } +} diff --git a/scr/food/cloudfunctions/quickstartFunctions/selectRecord/index.js b/scr/food/cloudfunctions/quickstartFunctions/selectRecord/index.js new file mode 100644 index 0000000..c7848a7 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/selectRecord/index.js @@ -0,0 +1,12 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: cloud.DYNAMIC_CURRENT_ENV +}); +const db = cloud.database(); + +// 查询数据库集合云函数入口函数 +exports.main = async (event, context) => { + // 返回数据库查询结果 + return await db.collection('sales').get(); +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/sumRecord/index.js b/scr/food/cloudfunctions/quickstartFunctions/sumRecord/index.js new file mode 100644 index 0000000..dfdc293 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/sumRecord/index.js @@ -0,0 +1,18 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: cloud.DYNAMIC_CURRENT_ENV +}); +const db = cloud.database(); +const $ = db.command.aggregate; + +// 聚合记录云函数入口函数 +exports.main = async (event, context) => { + // 返回数据库聚合结果 + return db.collection('sales').aggregate() + .group({ + _id: '$region', + sum: $.sum('$sales') + }) + .end(); +}; diff --git a/scr/food/cloudfunctions/quickstartFunctions/updateRecord/index.js b/scr/food/cloudfunctions/quickstartFunctions/updateRecord/index.js new file mode 100644 index 0000000..2ce3ad0 --- /dev/null +++ b/scr/food/cloudfunctions/quickstartFunctions/updateRecord/index.js @@ -0,0 +1,32 @@ +const cloud = require('wx-server-sdk'); + +cloud.init({ + env: cloud.DYNAMIC_CURRENT_ENV +}); +const db = cloud.database(); + +// 修改数据库信息云函数入口函数 +exports.main = async (event, context) => { + try { + // 遍历修改数据库信息 + for (let i = 0; i < event.data.length; i++) { + await db.collection('sales').where({ + _id: event.data[i]._id + }) + .update({ + data: { + sales: event.data[i].sales + }, + }); + } + return { + success: true, + data: event.data + }; + } catch (e) { + return { + success: false, + errMsg: e + }; + } +}; diff --git a/scr/food/pages/detail/detail.js b/scr/food/pages/detail/detail.js index 312e437..e39b79d 100644 --- a/scr/food/pages/detail/detail.js +++ b/scr/food/pages/detail/detail.js @@ -13,18 +13,23 @@ Page({ clockresult:[], userid:null, num:null + }, /** * 生命周期函数--监听页面加载 */ - onLoad: function (options) { - let foodid =options.id - var userid=options.aid - console.log('1111wb'); + onLoad: function (options) { + + const app=getApp() + let foodid =options.id + var userid=app.globalData.openid + console.log('1111w '); + console.log(userid) + cont.doc(foodid).get({ success: res => { console.log(res.data)