赞
踩
需求:由于微信token有上限每天2000次,这里使用云函数和云数据库相结合,实现token和jsapi_ticket的的缓存。
新手写代码,请观众忽略代码风格和格式哈,基本功能实现了,若有不完善的地方请自行补充。
1. 从缓存云数据库中读取token
2. 如果token无效(提前10秒失效,防止时间差导致的无效),则重新获取token和ticket
3. 如果token有效,则优先从缓存获取ticket,并判断ticket是否有效
4. 如果ticket无效,则重新获取ticket
1. 建立云数据库
在database上点右键,新建DB Schema,命令为db-wxcache
新建的json中填写内容(数据库结构及读写权限):
- // 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema
- {
- "bsonType": "object",
- "required": [],
- "permission": {
- "read": true,
- "create": true,
- "update": true,
- "delete": true
- },
- "properties": {
- "_id": {
- "description": "ID,系统自动生成",
- },
- "key": {
- "bsonType": "string",
- "label": "缓存键名",
- "errorMessage": {
- "format": "{label}不正确"
- },
- "trim": "both"
- },
- "value": {
- "bsonType": "string",
- "label": "缓存数据",
- "errorMessage": {
- "format": "{label}不正确"
- },
- "trim": "both"
- },
- "expired": {
- "bsonType": "int",
- "label": "过期时间(毫秒)",
- "errorMessage": {
- "format": "{label}不正确"
- },
- "exclusiveMinimum": true,
- "exclusiveMaximum": true,
- "defaultValue": "-1"
- }
- }
- }

2. 建立获取签名的云函数
cloudfunctions目录上点右键,新建云函数,命令为wx-signature
3. 下载微信JS-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。