小程序开发-几句话推荐
项目设计
项目来源
技术选型
uniapp + 微信云开发, uniapp 只开发微信端, 微信云开发省去搭建后端项目的繁琐。
数据库设计
按照非关系型数据库设计,可以参考
Mongodb 数据库表格设计原则
数据库ER图基础概念整理
表设计
云数据库跟 mongodb很相似,云函数只有两个集合, item 和 user
item 集合"publish": "-1/1", // 发布状态
1
2
3
4
5
6
7
8
9
10{
"_id": "",
"author":"",
"content": "",
"createTime": "",
"updateTime": "",
"publish": "-1/1",
}
user集合
1 | { |
难点
uniapp
uniapp 的熟悉度
uniapp 和 Vue 的 熟悉程度还是不够,磕磕碰碰的,尤其是对接 微信云开发这段,而且官方文档也有省略。
uniapp 插件使用
使用到一个uniapp的插件mescroll
看了好久,不是很明白,上手了发现它封装的很好,我使用了它给的一个例子就写完了。
Vue
Vue 中写节流函数
这里提供一个Vue 中写节流函数的写法1
2
3
4
5
6
7import { throttle } from '@/utils/utils.js'
export default{
methods:{
handleClickLove: throttle(function(){}, 1000)
}
}
云开发
对接云函数
在 user.likes 数组中 删除对应的 id
请注意这个方法在微信云数据库的控制台测试的时候是报异常的,只能在云函数中使用
Command.pull
1 | const db = cloud.database() |
随机集合中的几个数据
因为 mongodb 和 云数据库都是 nosql 类型的数据库, 所以概念上类似
当你想遇到不会的 sql 语法,查一下 mongodb 是如何实现的,然后再找找看微信的文档
1 | const db = cloud.database() |
后台管理系统请求
每隔2小时获取新微信请求 token
微信文档-获取接口调用凭据,
需要APPID和APPSECRET1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60const axios = require("axios");
const { APPID, APPSECRET } = require("../config");
const logger = require("./log4j");
const fs = require("fs");
const path = require("path");
const URL = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${APPID}&secret=${APPSECRET}`;
const fName = path.resolve(__dirname, "./accessToken.json");
// 请求并更新 accessToken
const updateAccessToken = async () => {
return axios
.get(URL)
.then(({ data }) => {
const { access_token } = data;
if (access_token) {
fs.writeFileSync(
fName,
JSON.stringify({
access_token,
createTime: new Date(),
})
);
} else {
throw new Error("access token 请求不成功");
}
})
.catch(async (err) => {
logger.error(err);
await updateAccessToken();
});
};
// 从文件中读 accessToken
const getAccessToken = async () => {
try {
// const readRes = fs.readFileSync(fName, "utf-8");
const readObj = JSON.parse(fs.readFileSync(fName, "utf-8"));
const createTime = new Date(readObj.createTime).getTime();
const nowTime = new Date().getTime();
// accessToken 过期
if ((nowTime - createTime) / (3600 * 1000) >= 2) {
await updateAccessToken();
await getAccessToken();
}
return readObj.access_token;
} catch (error) {
logger.error(error);
await updateAccessToken();
await getAccessToken();
}
};
// 定时任务更新 accessToken
setInterval(async () => {
await updateAccessToken();
}, 7200 * 1000);
module.exports = getAccessToken;
通过HTTP API 访问 云数据库
1 | // axios 实例 |