-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathapi.js
69 lines (63 loc) · 1.88 KB
/
api.js
1
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
60
61
62
63
64
65
66
67
68
69
import config from '../config.js'
import util from "./util.js"
const keyCategories = 'categories';
const keyCacheExpire = 'categories-expire';
const expire = 1800; // 1800 seconds
// 获取书籍分类
const getCategories = () => {
if (config.debug) console.log(config.api.categories);
// 从缓存中读取,判断缓存存不存在,并且有没有过期
return new Promise((resolve, reject) => {
let categories = {};
let now = util.now();
let value
let cacheExpire = parseInt(uni.getStorageSync(keyCacheExpire))
if (cacheExpire > now) {
try {
value = uni.getStorageSync(keyCategories)
if (value) value = JSON.parse(value)
} catch (e) {
console.log(e)
}
}
if (value) {
if (config.debug) console.log("从缓存中获取分类数据");
resolve(value);
} else {
if (config.debug) console.log("从接口获取数据");
util.request(config.api.categories).then(function(res) {
let tree = util.menuToTree(res.data.categories)
uni.setStorageSync(keyCategories, JSON.stringify(tree))
uni.setStorageSync(keyCacheExpire, now + expire)
resolve(tree)
}).catch(function(e) {
reject(e)
})
}
});
}
const getCategoryByCid = (cid) => {
return getCategories().then((categories) => {
let found = false
let category = {}
for (let i = 0; found == false && i < categories.length; i++) {
if (cid == categories[i].id) {
found = true
category = categories[i]
} else if (categories[i].children.length>0){
let children = categories[i].children
for (let i = 0; found == false && i < children.length; i++) {
if (cid == children[i].id) {
found = true
category = children[i]
}
}
}
}
return category
})
}
module.exports = {
getCategories,
getCategoryByCid,
}