Skip to content

Commit 9a71fbd

Browse files
author
soliury
committed
refactor: add services
1 parent dbf29ad commit 9a71fbd

6 files changed

Lines changed: 257 additions & 34 deletions

File tree

package.json

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
{
2-
"name": "noder",
3-
"version": "1.0.0",
4-
"scripts": {
5-
"start": "react-native start",
6-
"clean:babelrc": "find ./node_modules -name react-packager -prune -o -name '.babelrc' -print | xargs rm -f",
7-
"postinstall": "npm run clean:babelrc"
8-
},
9-
"dependencies": {
10-
"lodash": "^4.0.1",
11-
"markdown": "0.5.0",
12-
"moment": "^2.10.6",
13-
"query-string": "^3.0.0",
14-
"react-native": "^0.18.1",
15-
"react-redux": "^4.0.6",
16-
"rebound": "0.0.13",
17-
"redux": "^3.0.5",
18-
"redux-actions": "^0.9.1",
19-
"redux-logger": "^2.4.0",
20-
"redux-promise": "^0.5.1",
21-
"redux-thunk": "^1.0.3"
22-
},
23-
"devDependencies": {
24-
"coffee-script": "^1.9.2",
25-
"dev-ip": "^1.0.1",
26-
"eslint": "^1.1.0",
27-
"eslint-plugin-react": "^3.16.1",
28-
"gulp": "^3.9.0",
29-
"gulp-replace": "^0.5.4",
30-
"gulp-util": "^3.0.4",
31-
"react-native": "^0.15.0",
32-
"redux-devtools": "^3.0.1",
33-
"run-sequence": "^1.1.0"
34-
},
35-
"bundleId": "org.reactjs.native.example.noder"
2+
"name": "noder",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"start": "react-native start",
6+
"clean:babelrc": "find ./node_modules -name react-packager -prune -o -name '.babelrc' -print | xargs rm -f",
7+
"postinstall": "npm run clean:babelrc",
8+
"android": "react-native run-android"
9+
},
10+
"dependencies": {
11+
"lodash": "^4.0.1",
12+
"markdown": "0.5.0",
13+
"moment": "^2.10.6",
14+
"query-string": "^3.0.0",
15+
"react-native": "^0.18.1",
16+
"react-redux": "^4.0.6",
17+
"rebound": "0.0.13",
18+
"redux": "^3.0.5",
19+
"redux-actions": "^0.9.1",
20+
"redux-logger": "^2.4.0",
21+
"redux-promise": "^0.5.1",
22+
"redux-thunk": "^1.0.3"
23+
},
24+
"devDependencies": {
25+
"coffee-script": "^1.9.2",
26+
"dev-ip": "^1.0.1",
27+
"eslint": "^1.1.0",
28+
"eslint-plugin-react": "^3.16.1",
29+
"gulp": "^3.9.0",
30+
"gulp-replace": "^0.5.4",
31+
"gulp-util": "^3.0.4",
32+
"react-native": "^0.15.0",
33+
"redux-devtools": "^3.0.1",
34+
"run-sequence": "^1.1.0"
35+
},
36+
"bundleId": "org.reactjs.native.example.noder"
3637
}

src/configs/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
domain: 'https://cnodejs.org',
3+
apiPath: '/api/v1'
4+
}

src/services/request.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import qs from 'query-string';
2+
import config from '../configs';
3+
4+
5+
const urlPrefix = config.domain + config.apiPath;
6+
7+
8+
function filterJSON(res) {
9+
return res.json();
10+
}
11+
12+
13+
function filterStatus(res) {
14+
if (res.status >= 200 && res.status < 300) {
15+
return res
16+
}
17+
else {
18+
let error = new Error(res.statusText);
19+
error.res = res;
20+
error.type = 'http';
21+
throw error;
22+
}
23+
}
24+
25+
26+
export function get(url, params) {
27+
if (params) {
28+
url = `${urlPrefix + url}?${qs.stringify(params)}`;
29+
}
30+
31+
return fetch(url)
32+
.then(filterStatus)
33+
.then(filterJSON);
34+
}
35+
36+
37+
export function post(url, body) {
38+
return fetch(urlPrefix + url, {
39+
method: 'POST',
40+
headers: {
41+
'Accept': 'application/json',
42+
'Content-Type': 'application/json'
43+
},
44+
body: JSON.stringify(body)
45+
})
46+
.then(filterStatus)
47+
.then(filterJSON);
48+
}
49+
50+

src/services/storage.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React,{
2+
AsyncStorage
3+
} from 'react-native';
4+
5+
6+
export function setItem(key, value) {
7+
if (value == null) return Promise.reject('StorageService Error: value should not be null or undefined');
8+
return AsyncStorage.setItem(key, JSON.stringify(value));
9+
}
10+
11+
12+
export function getItem(key) {
13+
return AsyncStorage.getItem(key)
14+
.then(function (value) {
15+
return JSON.parse(value)
16+
});
17+
}
18+
19+
20+
export const clear = AsyncStorage.clear;
21+
22+
23+
export const removeItem = AsyncStorage.removeItem;
24+
25+
26+
export function multiGet(keys) {
27+
return AsyncStorage.multiGet(keys)
28+
.then(results=> {
29+
return results.map(item=> {
30+
return [item[0], JSON.parse(item[1])]
31+
})
32+
});
33+
}
34+
35+
36+
export function multiRemove(keys) {
37+
return AsyncStorage.multiRemove(keys)
38+
}

src/services/token.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function getToken() {
2+
return global.token;
3+
}
4+
5+
6+
export function setToken(token) {
7+
global.token = token;
8+
}

src/services/topicService.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import * as requestService from './request';
2+
import * as storageService from './storage';
3+
import { getToken, setToken } from './token';
4+
5+
const tabs = ['good', 'ask', 'all', 'share', 'job'];
6+
7+
function filterData(data) {
8+
return data.data;
9+
}
10+
11+
12+
// 读取缓存
13+
export const storage = {
14+
getTopicsByTab: function (tab) {
15+
return storageService.getItem('tab_' + tab)
16+
.then(topics=> {
17+
if (topics) {
18+
return topics
19+
}
20+
throw 'topicsInStorageIsEmpty'
21+
})
22+
},
23+
24+
25+
getAllTopics: function () {
26+
storageService.multiGet(tabs.map(tab=> {
27+
return 'tab_' + tab
28+
}))
29+
},
30+
31+
32+
removeAllTopics: function () {
33+
return storageService.multiRemove(tabs.map(tab=> {
34+
return 'tab_' + tab
35+
}))
36+
}
37+
};
38+
39+
40+
// 从远程api获取数据
41+
export const req = {
42+
getTopicsByTab: function (params) {
43+
return requestService.get('/topics', params)
44+
.then(filterData)
45+
.then(topics => {
46+
if (params.page == 1 && topics) {
47+
storageService.setItem('tab_' + params.tab, topics)
48+
}
49+
return topics
50+
})
51+
},
52+
53+
54+
getTopicById: function (id) {
55+
return requestService.get('/topic/' + id)
56+
.then(data=>data.data)
57+
.then(topic=> {
58+
if (topic && topic.id) {
59+
return topic
60+
}
61+
throw 'getTopicById Error'
62+
})
63+
},
64+
65+
66+
reply: function (topicId, content, replyId) {
67+
let body = {
68+
accesstoken: getToken(),
69+
content: content
70+
};
71+
if (replyId) {
72+
body.reply_id = replyId
73+
}
74+
let url = `/topic/${topicId}/replies`;
75+
76+
return requestService.post(url, body)
77+
.then(data=> {
78+
if (data.success) {
79+
return data.reply_id
80+
}
81+
else {
82+
throw 'do reply failed'
83+
}
84+
})
85+
},
86+
87+
88+
upComment: function (replyId) {
89+
let body = {
90+
accesstoken: getToken()
91+
};
92+
93+
let url = `/reply/${replyId}/ups`;
94+
95+
return requestService.post(url, body)
96+
.then(data=> {
97+
if (data.success) {
98+
return data.action == 'up'
99+
}
100+
else {
101+
throw 'do reply failed'
102+
}
103+
})
104+
},
105+
106+
107+
publish: function (title, tab, content) {
108+
const body = {
109+
title: title,
110+
tab: tab,
111+
content: content,
112+
accesstoken: getToken()
113+
};
114+
return requestService.post('/topics', body)
115+
.then(data=> {
116+
if (data.success) {
117+
return data.topic_id
118+
}
119+
throw 'publish failed'
120+
})
121+
}
122+
};

0 commit comments

Comments
 (0)