.
├── README.md
├── project.config.json // 项目配置文件
├── cloudfunctions | 云环境 // 存放云函数的目录
│ ├── login // 用户登陆云函数
│ │ ├── index.js
│ │ └── package.json
│ └── collection_get // 数据库查询云函数
│ │ ├── index.js
│ │ └── package.json
│ └── collection_update // 数据库更新云函数
│ ├── index.js
│ └── package.json
└── miniprogram
├── images // 存放小程序图片
├── lib // 配置文件
├── pages // 小程序各类页面
| ├── index // 首页
| └── menu // 分类页
| └── user // 用户中心
| └── search // 搜索页
| └── list // 列表页 搜索结果页
| └── detail // 详情页
| └── collection // 收藏页
| └── find // 发现页
| └── idiom-jielong // 成语接龙页
| └── poet // 做者页
| └── baijiaxing // 百家姓
| └── xiehouyu // 歇后语
| └── poet // 做者页
| └── suggest // 建议反馈
| └── ... // 其余
├── style // 样式文件目录
├── app.js // 小程序入口文件
├── app.json // 全局配置
└── app.wxss // 全局样式
复制代码
本项目是使用的小程序云开发。云开发提供了一个 JSON 数据库,用户能够直接在云端进行数据库增删改查,可是,小程序对用户操做数据的权限进行了必定的限制(例如数据update、一次性get记录的条数限制等),因此,这里主要采用云函数来操做数据库。html
函数根目录上右键,在右键菜单中,选择建立一个新的 Node.js 云函数,咱们将该云函数命名为 collection_get。git
编辑 index.js:github
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
/**
* page: 第几页
* num: 每页几条数据
* condition: 查询条件,例如 { name: '李白' }
*/
const {database, page, num, condition} = event
console.log(event)
try {
return await db.collection(database)
.where(condition)
.skip(num * (page - 1))
.limit(num)
.get()
} catch (err) {
console.log(err)
}
}
复制代码
例如,按照查询条件{tags: '唐诗三百首'}
查询诗词列表,每页num = 10
条数据:正则表达式
let {list, page, num} = this.data
let that = this
this.setData({
loading: true
})
wx.cloud.callFunction({
name: 'collection_get',
data: {
database: 'gushici',
page,
num,
condition: {
tags: '唐诗三百首'
}
},
}).then(res => {
if(!res.result.data.length) { // 没搜索到
that.setData({
loading: false,
isOver: true
})
} else {
let res_data = res.result.data
list.push(...res_data)
that.setData({
list,
page: page + 1, // 页面加1
loading: false
})
}
})
.catch(console.error)
}
复制代码
注意,当咱们向数据库中添加记录时,系统会自动帮咱们为每条记录添加上用户的 openid
字段,但若是,数据表是本身用 json/csv 文件导入的,就不存在 openid
字段,此时,当更新这个数据表时,系统会认为你不是建立者,因此也就没法更新。数据库
此时,就须要经过云函数更新数据库,新建云函数 collection_update, 编辑 index.js:json
// 更新数据 - 根据 _id 更新已打开人数
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
const { id } = event
console.log(event)
try {
return await db.collection('gushici').doc(id)
.update({
data: {
opened: _.inc(1)
},
})
} catch (e) {
console.error(e)
}
}
复制代码
更新某_id数据的打开人数:小程序
let _id = e.currentTarget.dataset.id
wx.cloud.callFunction({
name: 'collection_update',
data: {
id: _id
},
}).then(res => {
console.log(res.data)
})
.catch(console.error)
复制代码
小程序云开发可使用正则表达式进行模糊查询。例如, 根据用户输入关键词,查询标题中存在改关键词的古诗词。微信小程序
let database = 'gushici'
let condition = {
name: {
$regex:'.*'+ inputValue,
$options: 'i'
}
}
let { list, page, num } = this.data
let that = this
this.setData({
loading: true
})
// 模糊查询
wx.cloud.callFunction({
name: 'collection_get',
data: {
database,
page,
num,
condition
},
}).then(res => {
if (!res.result.data.length) { // 没搜索到
that.setData({
loading: false,
isOver: true
})
} else {
let res_data = res.result.data
list.push(...res_data)
that.setData({
list,
loading: false
})
}
})
.catch(console.error)
复制代码
小程序中页面触发转发的方式有两种:bash
button
组件设置属性 open-type="share"
,能够在用户点击按钮后触发 Page.onShareAppMessage 事件,若是当前页面没有定义此事件,则点击后无效果。用户还能够在 Page.onShareAppMessage 事件中自定义转发后显示的标题、图片、路径:服务器
onShareAppMessage(res) {
let id = wx.getStorageSync('shareId')
if (res.from === 'button') {
// 来自页面内转发按钮
console.log(res.target)
}
return {
title: `跟我一块儿挑战最长的成语接龙吧!`,
path: `pages/find/find`,
imageUrl: '/images/img.jpg',
}
},
复制代码
详情请参考文章:微信小程序之受权
数据表中明明有数据,可是 collection.get 到的却为空。解决:能够在云开发控制台中打开数据库权限设置,设置权限。
collection.update 函数调用成功单返回的倒是0行记录被更新,由于小程序端不容许更新没有 openid 字段的数据。解决:能够经过云函数更新数据库。
解决:1:将图片上传到服务器,填写服务器上的图片路径地址。2:将图片转为 base64 编码。
缘由:请看文档:developers.weixin.qq.com/miniprogram…
解决:去掉json数据 {}
之间的逗号, 若是最外层为 []
,也必须去掉, 最终形如:
{
"index": "做者_1",
"type": "做者",
"poet": "李白",
"abstract": "李白(701年-762年),字太白,号青莲居士,唐朝浪漫主义诗人,被后人誉为“诗仙”..."
}
{
"index": "做者_2",
"type": "做者",
"poet": "白居易",
"abstract": "白居易(772年-846年),字乐天,号香山居士..."
}
复制代码
若是你有关于使用云开发CloudBase相关的技术故事/技术实战经验想要跟你们分享,欢迎留言联系咱们哦~比心!