forked from osdio/noder-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopicService.js
More file actions
131 lines (110 loc) · 2.27 KB
/
topicService.js
File metadata and controls
131 lines (110 loc) · 2.27 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
127
128
129
130
131
import * as requestService from './request';
import * as storageService from './storage';
import { getToken, setToken } from './token';
const tabs = ['good', 'ask', 'all', 'share', 'job'];
function filterData(data) {
return data.data;
}
// 读取缓存
export const storage = {
getTopicsByTab: function (tab) {
return storageService.getItem('tab_' + tab)
.then(topics=> {
if (topics) {
return topics
}
throw 'topicsInStorageIsEmpty'
})
},
getAllTopics: function () {
return storageService.multiGet(tabs.map(tab=> {
return 'tab_' + tab
}))
.then(arr=> {
let ob = {};
arr.forEach((item)=> {
if (!item[1]) {
throw 'topicsIsEmpty';
}
ob[item[0].replace('tab_', '')] = item[1];
});
return ob;
});
},
removeAllTopics: function () {
return storageService.multiRemove(tabs.map(tab=> {
return 'tab_' + tab
}))
}
};
// 从远程api获取数据
export const req = {
getTopicsByTab: function (tab = 'all', params = {}) {
return requestService.get('/topics', {
tab,
page: 1,
limit: 10,
...params
})
.then(filterData)
},
getTopicById: function (id) {
return requestService.get('/topic/' + id)
.then(filterData)
.then(topic=> {
if (topic && topic.id) {
return topic
}
throw 'getTopicById Error'
})
},
reply: function ({topicId, content, replyId}) {
let body = {
accesstoken: getToken(),
content: content
};
if (replyId) {
body.reply_id = replyId
}
let url = `/topic/${topicId}/replies`;
return requestService.post(url, body)
.then(data=> {
if (data.success) {
return data.reply_id
}
else {
throw 'do reply failed'
}
})
},
upReply: function ({replyId}) {
let body = {
accesstoken: getToken()
};
let url = `/reply/${replyId}/ups`;
return requestService.post(url, body)
.then(data=> {
if (data.success) {
return data.action == 'up'
}
else {
throw 'up reply failed'
}
})
},
publish: function ({title, tab, content}) {
const body = {
title: title,
tab: tab,
content: content,
accesstoken: getToken()
};
return requestService.post('/topics', body)
.then(data=> {
if (data.success) {
return data.topic_id
}
throw 'publish failed'
})
}
};