useBookList.js
3.33 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-07 17:46:54
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2022-06-09 11:26:42
* @FilePath: /tswj/src/composables/useBookList.js
* @Description:
*/
import { ref } from 'vue'
import axios from '@/utils/axios';
import { Toast } from 'vant';
import { useRoute } from 'vue-router';
import { kgBookListAPI } from '@/api/C/kg'
export const useBookList = () => {
const $route = useRoute();
const emptyStatus = ref(false);
// tslint:disable-next-line: variable-name
const kg_id = $route.query.kg_id ? $route.query.kg_id : '';
const kgInfo = ref({
id: '',
logo: '',
name: '',
multi_name: '',
book_list: []
});
if (kg_id) { // 从学校列表进入
axios.get('/srv/?a=kg_book_list', {
params: {
kg_id
}
})
.then(res => {
if (res.data.code === 1) {
kgInfo.value = res.data.data;
kgInfo.value.book_list.forEach(item => {
item.show = true; // 默认显示所有,给搜索功能留的hook
});
// 有空格分割name
if (kgInfo.value.name.indexOf(' ') > -1) {
kgInfo.value.multi_name = kgInfo.value.name.split(' ');
}
if (!kgInfo.value.book_list.length) {
emptyStatus.value = true;
}
} else {
// tslint:disable-next-line: no-console
console.warn(res);
if (!res.data.show) return false;
Toast({
icon: 'close',
message: res.data.msg
});
}
})
.catch(err => {
// tslint:disable-next-line: no-console
console.error(err);
})
} else { // 从访客进入
axios.get('/srv/?a=book_list')
.then(res => {
if (res.data.code === 1) {
res.data.data.forEach(item => {
item.show = true; // 默认显示所有,给搜索功能留的hook
});
kgInfo.value = {
book_list: res.data.data
}
if (!kgInfo.value.book_list.length) {
emptyStatus.value = true;
}
} else {
// tslint:disable-next-line: no-console
console.warn(res);
if (!res.data.show) return false;
Toast({
icon: 'close',
message: res.data.msg
});
}
})
.catch(err => {
// tslint:disable-next-line: no-console
console.error(err);
})
}
return {
kg_id,
kgInfo,
emptyStatus
}
}
// !!废弃方法
// 前端使用方式过于复杂
export const useAsyncBookList = async () => {
const $route = useRoute();
const emptyStatus = ref(false);
// tslint:disable-next-line: variable-name
const kg_id = $route.query.kg_id ? $route.query.kg_id : '';
const kgInfo = ref({
id: '',
logo: '',
name: '',
multi_name: [],
book_list: []
});
const { data } = await kgBookListAPI({ kg_id });
if (data) kgInfo.value = data;
kgInfo.value.book_list.forEach(item => {
item.show = true; // 默认显示所有,给搜索功能留的hook
});
// 有空格分割name
if (kgInfo.value.name.indexOf(' ') > -1) {
kgInfo.value.multi_name = kgInfo.value.name.split(' ');
}
if (!kgInfo.value.book_list.length) {
emptyStatus.value = true;
}
return {
kg_id,
kgInfo,
emptyStatus
}
}