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
107 lines (85 loc) · 2.2 KB
/
topicService.js
File metadata and controls
107 lines (85 loc) · 2.2 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
var request = require('./Request')
var Storage = require('./Storage')
var config = require('../configs/config')
var tabs = ['good', 'ask', 'all', 'share', 'job']
var storage = {}
storage.get = function (tab) {
return Storage.getItem('tab_' + tab)
.then(topics=> {
if (topics) {
return topics
}
throw 'topicsInStorageIsEmpty'
})
}
storage.getAll = function () {
return Storage.multiGet(tabs.map(tab=> {
return 'tab_' + tab
}))
}
var req = {}
req.getTopicsByTab = function (params) {
var url = config.domain + '/api/v1/topics'
return request.get(url, params)
.then(data=>data.data)
.then(topics => {
if (params.page == 1 && topics) {
Storage.setItem('tab_' + params.tab, topics)
}
return topics
})
}
req.getTopicById = function (id) {
let url = config.domain + '/api/v1/topic/' + id
return request.get(url)
.then(data=>data.data)
.then(topic=> {
if (topic && topic.id) {
return topic
}
throw 'GetTopicError'
})
}
req.markTopicAsLike = function (id, token, isLiked) {
var apiUrl = config.domain + config.apiPath
if (!isLiked) {
apiUrl += '/topic/collect'
}
else {
apiUrl += '/topic/de_collect'
}
console.log(apiUrl);
return request.post(apiUrl, {
accesstoken: token,
topic_id: id
})
.then(data => {
console.log(data);
if (!data.success) {
throw 'error'
}
return data
})
}
req.reply = function (topicId, content, token, replyId) {
let apiUrl = config.domain + config.apiPath
var body = {
accesstoken: token,
content: content
}
if (replyId) {
body.reply_id = replyId
}
let url = `${apiUrl}/topic/${topicId}/replies`
return request.post(url, body)
.then(data=> {
if (data.success) {
return data.reply_id
}
else {
throw 'do reply failed'
}
})
}
exports.storage = storage
exports.req = req