Skip to content

Commit

Permalink
Add two Demo
Browse files Browse the repository at this point in the history
Sports and breakfast in miniprogram
  • Loading branch information
wxcloudbase committed Jan 15, 2020
1 parent fea5254 commit def03dd
Show file tree
Hide file tree
Showing 86 changed files with 2,739 additions and 0 deletions.
22 changes: 22 additions & 0 deletions miniprogram/tcb-demo-breakfast/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 【外卖点餐-早餐预定】小程序云开发项目实战

## 项目介绍

- 小程序模拟一个商户对辖区内供应早餐的情景,用户通过在线预定早餐,商户实施配送,直到付款配送完成整个流程。整个流程分为预定中,已确认,已完成3个状态。在预定中状态用户可以取消预定。其他时候用户可以发起客服会话。
- 因为模拟展示,所以没有添加商户端和配送员端,状态变化由时间触发。


## 部署介绍

此处为项目完整代码,可以直接部署使用;以下是部署步骤:

- 初始化云开发环境,如果有多个云开发环境造成wx.cloud.init错误,则在app.js处进行环境定义;
- 在云开发环境中创建一个数据库,名称为food;
- 将项目目录下base文件夹中的food.json文件导入food数据库;
- 将项目目录下base文件夹中的food.jpg图片文件上传至云存储,并将CloudID填写到food数据库ADMIN中的img字段里;
- 将cloudfunctions文件夹内的6个云函数创建并部署,并将donefood、sendfood云函数上传触发器;
- 打开云开发控制台-设置tag-全局设置,添加消息推送,选择消息类型为text,环境ID选择你部署的环境ID,云函数选择contact;

## 参考文档

- [云开发文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/getting-started.html)
Binary file added miniprogram/tcb-demo-breakfast/base/food.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions miniprogram/tcb-demo-breakfast/base/food.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"_id":"ADMIN","ban":[],"tel":"13255524548","price":15.0,"food":"WAMAFO独营营养新鲜早餐全餐【手抓饼、玉米、坚果、黑豆浆、水果小食】","img":"","tips":"每个用户每天只能购买一次,在订餐时需要你的个人信息用于核验;若你点餐但不收配送也不付款则影响你后续的订餐。"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"permissions": {
"openapi": [
]
}
}
11 changes: 11 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/cancelfood/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const cloud = require('wx-server-sdk')

cloud.init();
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {
await db.collection('food').where({
userID:event.userInfo.openId
}).remove();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cancelfood",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~1.8.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"permissions": {
"openapi": [
"customerServiceMessage.send"
]
}
}
31 changes: 31 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/contact/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init();
const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
console.log(event);

let content = "收到消息,将在稍后打电话处理"

if(event.Content.indexOf('取消')!=-1){
await db.collection('food').where({
userID:event.userInfo.openId
}).remove();
content='已经成功删除'
}


await cloud.openapi.customerServiceMessage.send({
touser: event.userInfo.openId,
msgtype: 'text',
text: {
content: content,
},
})

return 'success'
}
14 changes: 14 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/contact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "contact",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~1.8.0"
}
}
14 changes: 14 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/donefood/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"permissions": {
"openapi": [
"subscribeMessage.send"
]
},
"triggers": [
{
"name": "sendMessagerTimer",
"type": "timer",
"config": "*/5 * * * * * *"
}
]
}
40 changes: 40 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/donefood/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const cloud = require('wx-server-sdk');
const doneSID = 'Xw4e3cMQ3Bp2Fm2B4xaPZ213BicUK1CeozGoBUrcBDA';

cloud.init();
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {
try {
let now = new Date(new Date().getTime()+28800000);
const messages = await db.collection('food').where({
state : 1,
donetime: _.lt(now)
}).get();

const sendPromises = messages.data.map(async message => {
try {
if(message.doneMess==true){
await cloud.openapi.subscribeMessage.send({
touser: message.userID,
page: 'index',
data: message.doneContent,
templateId: doneSID,
});
}
return db.collection('food').doc(message._id).update({
data: {
state: 2
},
});
} catch (e) {
return e;
}
});
return Promise.all(sendPromises);
} catch (err) {
console.log(err);
return err;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "donefood",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~1.8.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"permissions": {
"openapi": [
]
}
}
41 changes: 41 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/initfood/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const cloud = require('wx-server-sdk')

cloud.init();
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {
try{
const AFood = (await db.collection('food').doc('ADMIN').get()).data;

const User = (await db.collection('food').where({
userID:event.userInfo.openId
}).get()).data

if(User.length != 0){
return {
Food:AFood,
state:User[0].state,
submittime:User[0].submittime,
SID:User[0].SID,
statetime:User[0].statetime!=null?User[0].statetime:null,
donetime:User[0].donetime!=null?User[0].donetime:null
}
}
else{
//判断用户是否被禁止预定,此为手动添加
if(AFood.ban.indexOf(event.userInfo.openId)==-1){
return {
Food:AFood,
}
}
else{
return 1
}
}
}
catch(e){
console.log(e);
return -1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "initfood",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~1.8.0"
}
}
14 changes: 14 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/sendfood/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"permissions": {
"openapi": [
"subscribeMessage.send"
]
},
"triggers": [
{
"name": "sendMessagerTimer",
"type": "timer",
"config": "*/5 * * * * * *"
}
]
}
41 changes: 41 additions & 0 deletions miniprogram/tcb-demo-breakfast/cloudfunctions/sendfood/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const cloud = require('wx-server-sdk');
const stateSID = 'gkuwX69tU4YsS6Up6Lhhh3G8eSzh1SgN4zSifDfYk0c';

cloud.init();
const db = cloud.database();
const _ = db.command;

exports.main = async (event, context) => {
try {
let now = new Date(new Date().getTime()+28800000);
const messages = await db.collection('food').where({
state : 0,
statetime: _.lt(now)
}).get();

const sendPromises = messages.data.map(async message => {
try {
if(message.stateMess==true){
console.log(message.stateContent)
await cloud.openapi.subscribeMessage.send({
touser: message.userID,
page: 'index',
data: message.stateContent,
templateId: stateSID,
});
}
return db.collection('food').doc(message._id).update({
data: {
state: 1
},
});
} catch (e) {
return e;
}
});
return Promise.all(sendPromises);
} catch (err) {
console.log(err);
return err;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "sendfood",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"wx-server-sdk": "~1.8.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"permissions": {
"openapi": [
]
}
}
Loading

0 comments on commit def03dd

Please sign in to comment.