Skip to content

Commit 7dd21ca

Browse files
committed
clean code
1 parent 64cf4fb commit 7dd21ca

File tree

3 files changed

+40
-135
lines changed

3 files changed

+40
-135
lines changed

lib/api_custom_service.js

+24-71
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

3-
var util = require('./util');
4-
var wrapper = util.wrapper;
5-
var postJSON = util.postJSON;
63
var path = require('path');
74
var fs = require('fs');
85
var formstream = require('formstream');
96

7+
var util = require('./util');
8+
var wrapper = util.wrapper;
9+
var postJSON = util.postJSON;
10+
var make = util.make;
11+
1012
/**
1113
* 获取客服聊天记录
1214
* 详细请看:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1464937269_mUtmK&token=&lang=zh_CN
@@ -53,19 +55,12 @@ var formstream = require('formstream');
5355
* @param {Object} opts 查询条件
5456
* @param {Function} callback 回调函数
5557
*/
56-
exports.getRecords = function (opts, callback) {
57-
this.preRequest(this._getRecords, arguments);
58-
};
59-
60-
/*!
61-
* 获取客服聊天记录的未封装版本
62-
*/
63-
exports._getRecords = function (opts, callback) {
58+
make(exports, 'getRecords', function (opts, callback) {
6459
// https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=ACCESS_TOKEN
6560
opts.msgid = opts.msgid || 1;
6661
var url = this.endpoint + '/customservice/msgrecord/getmsglist?access_token=' + this.token.accessToken;
6762
this.request(url, postJSON(opts), wrapper(callback));
68-
};
63+
});
6964

7065
/**
7166
* 获取客服基本信息
@@ -104,18 +99,11 @@ exports._getRecords = function (opts, callback) {
10499
* ```
105100
* @param {Function} callback 回调函数
106101
*/
107-
exports.getCustomServiceList = function (callback) {
108-
this.preRequest(this._getCustomServiceList, arguments);
109-
};
110-
111-
/*!
112-
* 获取客服基本信息的未封装版本
113-
*/
114-
exports._getCustomServiceList = function (callback) {
102+
make(exports, 'getCustomServiceList', function (callback) {
115103
// https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token= ACCESS_TOKEN
116104
var url = this.endpoint + '/cgi-bin/customservice/getkflist?access_token=' + this.token.accessToken;
117105
this.request(url, {dataType: 'json'}, wrapper(callback));
118-
};
106+
});
119107

120108
/**
121109
* 获取在线客服接待信息
@@ -154,17 +142,16 @@ exports._getCustomServiceList = function (callback) {
154142
* ```
155143
* @param {Function} callback 回调函数
156144
*/
157-
exports.getOnlineCustomServiceList = function (callback) {
158-
this.preRequest(this._getOnlineCustomServiceList, arguments);
159-
};
160-
161-
/*!
162-
* 获取在线客服接待信息的未封装版本
163-
*/
164-
exports._getOnlineCustomServiceList = function (callback) {
145+
make(exports, 'getOnlineCustomServiceList', function (callback) {
165146
// https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token= ACCESS_TOKEN
166147
var url = this.endpoint + '/cgi-bin/customservice/getonlinekflist?access_token=' + this.token.accessToken;
167148
this.request(url, {dataType: 'json'}, wrapper(callback));
149+
});
150+
151+
var md5 = function (input) {
152+
var crypto = require('crypto');
153+
var hash = crypto.createHash('md5');
154+
return hash.update(input).digest('hex');
168155
};
169156

170157
/**
@@ -193,20 +180,7 @@ exports._getOnlineCustomServiceList = function (callback) {
193180
* @param {String} password 密码,可以直接传递明文,wechat模块自动进行md5加密
194181
* @param {Function} callback 回调函数
195182
*/
196-
exports.addKfAccount = function (account, nick, password, callback) {
197-
this.preRequest(this._addKfAccount, arguments);
198-
};
199-
200-
var md5 = function (input) {
201-
var crypto = require('crypto');
202-
var hash = crypto.createHash('md5');
203-
return hash.update(input).digest('hex');
204-
};
205-
206-
/*!
207-
* 添加客服账号的未封装版本
208-
*/
209-
exports._addKfAccount = function (account, nick, password, callback) {
183+
make(exports, 'addKfAccount', function (account, nick, password, callback) {
210184
// https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
211185
var url = this.endpoint + '/customservice/kfaccount/add?access_token=' + this.token.accessToken;
212186
var data = {
@@ -216,7 +190,7 @@ exports._addKfAccount = function (account, nick, password, callback) {
216190
};
217191

218192
this.request(url, postJSON(data), wrapper(callback));
219-
};
193+
});
220194

221195
/**
222196
* 设置客服账号
@@ -244,14 +218,7 @@ exports._addKfAccount = function (account, nick, password, callback) {
244218
* @param {String} password 密码,可以直接传递明文,wechat模块自动进行md5加密
245219
* @param {Function} callback 回调函数
246220
*/
247-
exports.updateKfAccount = function (account, nick, password, callback) {
248-
this.preRequest(this._updateKfAccount, arguments);
249-
};
250-
251-
/*!
252-
* 设置客服账号的未封装版本
253-
*/
254-
exports._updateKfAccount = function (account, nick, password, callback) {
221+
make(exports, 'updateKfAccount', function (account, nick, password, callback) {
255222
// https://api.weixin.qq.com/customservice/kfaccount/add?access_token=ACCESS_TOKEN
256223
var url = this.endpoint + '/customservice/kfaccount/update?access_token=' + this.token.accessToken;
257224
var data = {
@@ -261,7 +228,7 @@ exports._updateKfAccount = function (account, nick, password, callback) {
261228
};
262229

263230
this.request(url, postJSON(data), wrapper(callback));
264-
};
231+
});
265232

266233
/**
267234
* 删除客服账号
@@ -287,18 +254,11 @@ exports._updateKfAccount = function (account, nick, password, callback) {
287254
* @param {String} account 账号名字,格式为:前缀@公共号名字
288255
* @param {Function} callback 回调函数
289256
*/
290-
exports.deleteKfAccount = function (account, callback) {
291-
this.preRequest(this._deleteKfAccount, arguments);
292-
};
293-
294-
/*!
295-
* 删除客服账号的未封装版本
296-
*/
297-
exports._deleteKfAccount = function (account, callback) {
257+
make(exports, 'deleteKfAccount', function (account, callback) {
298258
// https://api.weixin.qq.com/customservice/kfaccount/del?access_token=ACCESS_TOKEN
299259
var url = this.endpoint + '/customservice/kfaccount/del?access_token=' + this.token.accessToken + '&kf_account=' + account;
300260
this.request(url, {dataType: 'json'}, wrapper(callback));
301-
};
261+
});
302262

303263
/**
304264
* 设置客服头像
@@ -325,14 +285,7 @@ exports._deleteKfAccount = function (account, callback) {
325285
* @param {String} filepath 头像路径
326286
* @param {Function} callback 回调函数
327287
*/
328-
exports.setKfAccountAvatar = function (account, filepath, callback) {
329-
this.preRequest(this._setKfAccountAvatar, arguments);
330-
};
331-
332-
/*!
333-
* 上传多媒体文件的未封装版本
334-
*/
335-
exports._setKfAccountAvatar = function (account, filepath, callback) {
288+
make(exports, 'setKfAccountAvatar', function (account, filepath, callback) {
336289
// http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=ACCESS_TOKEN&kf_account=KFACCOUNT
337290
var that = this;
338291
fs.stat(filepath, function (err, stat) {
@@ -351,4 +304,4 @@ exports._setKfAccountAvatar = function (account, filepath, callback) {
351304
};
352305
that.request(url, opts, wrapper(callback));
353306
});
354-
};
307+
});

lib/api_group.js

+13-55
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var util = require('./util');
44
var wrapper = util.wrapper;
55
var postJSON = util.postJSON;
6+
var make = util.make;
67

78
/**
89
* 获取分组列表
@@ -27,18 +28,11 @@ var postJSON = util.postJSON;
2728
* ```
2829
* @param {Function} callback 回调函数
2930
*/
30-
exports.getGroups = function (callback) {
31-
this.preRequest(this._getGroups, arguments);
32-
};
33-
34-
/*!
35-
* 获取分组列表的未封装版本
36-
*/
37-
exports._getGroups = function (callback) {
31+
make(exports, 'getGroups', function (callback) {
3832
// https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
3933
var url = this.endpoint + '/cgi-bin/groups/get?access_token=' + this.token.accessToken;
4034
this.request(url, {dataType: 'json'}, wrapper(callback));
41-
};
35+
});
4236

4337
/**
4438
* 查询用户在哪个分组
@@ -61,21 +55,14 @@ exports._getGroups = function (callback) {
6155
* @param {String} openid Open ID
6256
* @param {Function} callback 回调函数
6357
*/
64-
exports.getWhichGroup = function (openid, callback) {
65-
this.preRequest(this._getWhichGroup, arguments);
66-
};
67-
68-
/*!
69-
* 查询用户在哪个分组未分组版本
70-
*/
71-
exports._getWhichGroup = function (openid, callback) {
58+
make(exports, 'getWhichGroup', function (openid, callback) {
7259
// https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=ACCESS_TOKEN
7360
var url = this.endpoint + '/cgi-bin/groups/getid?access_token=' + this.token.accessToken;
7461
var data = {
7562
'openid': openid
7663
};
7764
this.request(url, postJSON(data), wrapper(callback));
78-
};
65+
});
7966

8067
/**
8168
* 创建分组
@@ -96,14 +83,7 @@ exports._getWhichGroup = function (openid, callback) {
9683
* @param {String} name 分组名字
9784
* @param {Function} callback 回调函数
9885
*/
99-
exports.createGroup = function (name, callback) {
100-
this.preRequest(this._createGroup, arguments);
101-
};
102-
103-
/*!
104-
* 创建分组的未封装版本
105-
*/
106-
exports._createGroup = function (name, callback) {
86+
make(exports, 'createGroup', function (name, callback) {
10787
// https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
10888
// POST数据格式:json
10989
// POST数据例子:{"group":{"name":"test"}}
@@ -112,7 +92,7 @@ exports._createGroup = function (name, callback) {
11292
'group': {'name': name}
11393
};
11494
this.request(url, postJSON(data), wrapper(callback));
115-
};
95+
});
11696

11797
/**
11898
* 更新分组名字
@@ -134,14 +114,7 @@ exports._createGroup = function (name, callback) {
134114
* @param {String} name 新的分组名字
135115
* @param {Function} callback 回调函数
136116
*/
137-
exports.updateGroup = function (id, name, callback) {
138-
this.preRequest(this._updateGroup, arguments);
139-
};
140-
141-
/*!
142-
* 更新分组名字的未封装版本
143-
*/
144-
exports._updateGroup = function (id, name, callback) {
117+
make(exports, 'updateGroup', function (id, name, callback) {
145118
// http请求方式: POST(请使用https协议)
146119
// https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN
147120
// POST数据格式:json
@@ -151,7 +124,7 @@ exports._updateGroup = function (id, name, callback) {
151124
'group': {'id': id, 'name': name}
152125
};
153126
this.request(url, postJSON(data), wrapper(callback));
154-
};
127+
});
155128

156129
/**
157130
* 移动用户进分组
@@ -173,14 +146,7 @@ exports._updateGroup = function (id, name, callback) {
173146
* @param {Number} groupId 分组ID
174147
* @param {Function} callback 回调函数
175148
*/
176-
exports.moveUserToGroup = function (openid, groupId, callback) {
177-
this.preRequest(this._moveUserToGroup, arguments);
178-
};
179-
180-
/*!
181-
* 移动用户进分组的未封装版本
182-
*/
183-
exports._moveUserToGroup = function (openid, groupId, callback) {
149+
make(exports, 'moveUserToGroup', function (openid, groupId, callback) {
184150
// http请求方式: POST(请使用https协议)
185151
// https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=ACCESS_TOKEN
186152
// POST数据格式:json
@@ -191,7 +157,7 @@ exports._moveUserToGroup = function (openid, groupId, callback) {
191157
'to_groupid': groupId
192158
};
193159
this.request(url, postJSON(data), wrapper(callback));
194-
};
160+
});
195161

196162
/**
197163
* 删除分组
@@ -212,18 +178,10 @@ exports._moveUserToGroup = function (openid, groupId, callback) {
212178
* @param {Number} groupId 分组ID
213179
* @param {Function} callback 回调函数
214180
*/
215-
exports.removeGroup = function (groupId, callback) {
216-
this.preRequest(this._removeGroup, arguments);
217-
};
218-
219-
/*!
220-
* 移动用户进分组的未封装版本
221-
*/
222-
exports._removeGroup = function (groupId, callback) {
181+
make(exports, 'removeGroup', function (groupId, callback) {
223182
var url = this.endpoint + '/cgi-bin/groups/delete?access_token=' + this.token.accessToken;
224183
var data = {
225184
'group': { id: groupId}
226185
};
227186
this.request(url, postJSON(data), wrapper(callback));
228-
};
229-
187+
});

lib/api_ip.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var util = require('./util');
44
var wrapper = util.wrapper;
5+
var make = util.make;
56

67
/**
78
* 获取微信服务器IP地址
@@ -23,15 +24,8 @@ var wrapper = util.wrapper;
2324
* ```
2425
* @param {Function} callback 回调函数
2526
*/
26-
exports.getIp = function (callback) {
27-
this.preRequest(this._getIp, arguments);
28-
};
29-
30-
/*!
31-
* 获取微信服务器IP地址的未封装版本
32-
*/
33-
exports._getIp = function (callback) {
27+
make(exports, 'getIp', function (callback) {
3428
// https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=ACCESS_TOKEN
3529
var url = this.endpoint + '/cgi-bin/getcallbackip?access_token=' + this.token.accessToken;
3630
this.request(url, {dataType: 'json'}, wrapper(callback));
37-
};
31+
});

0 commit comments

Comments
 (0)