-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add room manager api; but not support any more
- Loading branch information
jinqiming
committed
Mar 23, 2019
1 parent
cdd5690
commit 839a276
Showing
8 changed files
with
562 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
'use strict' | ||
|
||
let log4js = require('log4js') | ||
let logger = log4js.getLogger('chatwe') | ||
|
||
async function createChatroom (memberList, topic) { | ||
logger.debug('createChatroom()') | ||
let localTime = Date.now() | ||
|
||
let userNames = [] | ||
for (let i in memberList) { | ||
userNames.push({ UserName: memberList[i].UserName }) | ||
} | ||
|
||
let options = { | ||
uri: this.loginInfo['url'] + '/webwxcreatechatroom', | ||
qs: { | ||
'r': localTime, | ||
'pass_ticket': this.loginInfo['pass_ticket'] | ||
}, | ||
body: { | ||
'BaseRequest': this.loginInfo['BaseRequest'], | ||
'MemberCount': userNames.length, | ||
'MemberList': userNames, | ||
'Topic': topic | ||
}, | ||
json: true, | ||
method: 'POST' | ||
} | ||
|
||
logger.debug('createChatroom, option :', options) | ||
|
||
return this.s(options) | ||
.then(function (resp) { | ||
logger.debug('createChatroom resp :', resp) | ||
let issucc = resp && resp.BaseResponse && resp.BaseResponse.Ret === 0 | ||
logger.debug('createChatroom issucc :', issucc) | ||
return issucc ? resp : null | ||
}) | ||
.catch(function (err) { | ||
logger.warn(err) | ||
return null | ||
}) | ||
} | ||
|
||
async function setChatroomName (chatroom, topic) { | ||
logger.debug('SetChatroomName()') | ||
|
||
let options = { | ||
uri: this.loginInfo['url'] + '/webwxupdatechatroom', | ||
qs: { | ||
'fun': 'modtopic', | ||
'pass_ticket': this.loginInfo['pass_ticket'] | ||
}, | ||
body: { | ||
'BaseRequest': this.loginInfo['BaseRequest'], | ||
'ChatRoomName': chatroom.UserName, | ||
'NewTopic': topic | ||
}, | ||
json: true, | ||
method: 'POST' | ||
} | ||
|
||
logger.debug('createChatroom, option :', options) | ||
|
||
return this.s(options) | ||
.then(function (resp) { | ||
logger.debug('SetChatroomName resp :', resp) | ||
let issucc = resp && resp.BaseResponse && resp.BaseResponse.Ret === 0 | ||
logger.debug('SetChatroomName issucc :', issucc) | ||
return issucc ? resp : null | ||
}) | ||
.catch(function (err) { | ||
logger.warn(err) | ||
return null | ||
}) | ||
} | ||
|
||
async function deleteMemberFromChatroom (chatroom, memberList) { | ||
logger.debug('deleteMemberFromChatroom()') | ||
|
||
let userNames = '' | ||
for (let i in memberList) { | ||
userNames += memberList[i].UserName + ',' | ||
} | ||
userNames = userNames.substr(0, userNames.length - 1) | ||
|
||
let options = { | ||
uri: this.loginInfo['url'] + '/webwxupdatechatroom', | ||
qs: { | ||
'fun': 'delmember', | ||
'pass_ticket': this.loginInfo['pass_ticket'] | ||
}, | ||
body: { | ||
'BaseRequest': this.loginInfo['BaseRequest'], | ||
'ChatRoomName': chatroom.UserName, | ||
'DelMemberList': userNames | ||
}, | ||
json: true, | ||
method: 'POST' | ||
} | ||
|
||
logger.debug('deleteMemberFromChatroom, option :', options) | ||
|
||
return this.s(options) | ||
.then(function (resp) { | ||
logger.debug('deleteMemberFromChatroom resp :', resp) | ||
let issucc = resp && resp.BaseResponse && resp.BaseResponse.Ret === 0 | ||
logger.debug('deleteMemberFromChatroom issucc :', issucc) | ||
return issucc ? resp : null | ||
}) | ||
.catch(function (err) { | ||
logger.warn(err) | ||
return null | ||
}) | ||
} | ||
|
||
async function inviteMemberToChatroom (chatroom, memberList) { | ||
logger.debug('inviteMemberToChatroom()') | ||
|
||
let userNames = '' | ||
for (let i in memberList) { | ||
userNames += memberList[i].UserName + ',' | ||
} | ||
userNames = userNames.substr(0, userNames.length - 1) | ||
|
||
let options = { | ||
uri: this.loginInfo['url'] + '/webwxupdatechatroom', | ||
qs: { | ||
'fun': 'invitemember', | ||
'pass_ticket': this.loginInfo['pass_ticket'] | ||
}, | ||
body: { | ||
'BaseRequest': this.loginInfo['BaseRequest'], | ||
'ChatRoomName': chatroom.UserName, | ||
'InviteMemberList': userNames | ||
}, | ||
json: true, | ||
method: 'POST' | ||
} | ||
|
||
logger.debug('inviteMemberToChatroom, option :', options) | ||
|
||
return this.s(options) | ||
.then(function (resp) { | ||
logger.debug('inviteMemberToChatroom resp :', resp) | ||
let issucc = resp && resp.BaseResponse && resp.BaseResponse.Ret === 0 | ||
logger.debug('inviteMemberToChatroom issucc :', issucc) | ||
return issucc ? resp : null | ||
}) | ||
.catch(function (err) { | ||
logger.warn(err) | ||
return null | ||
}) | ||
} | ||
|
||
async function addMemberToChatroom (chatroom, memberList) { | ||
logger.debug('inviteMemberToChatroom()') | ||
|
||
let userNames = '' | ||
for (let i in memberList) { | ||
userNames += memberList[i].UserName + ',' | ||
} | ||
userNames = userNames.substr(0, userNames.length - 1) | ||
|
||
let options = { | ||
uri: this.loginInfo['url'] + '/webwxupdatechatroom', | ||
qs: { | ||
'fun': 'addmember', | ||
'pass_ticket': this.loginInfo['pass_ticket'] | ||
}, | ||
body: { | ||
'BaseRequest': this.loginInfo['BaseRequest'], | ||
'ChatRoomName': chatroom.UserName, | ||
'AddMemberList': userNames | ||
}, | ||
json: true, | ||
method: 'POST' | ||
} | ||
|
||
logger.debug('inviteMemberToChatroom, option :', options) | ||
|
||
return this.s(options) | ||
.then(function (resp) { | ||
logger.debug('inviteMemberToChatroom resp :', resp) | ||
let issucc = resp && resp.BaseResponse && resp.BaseResponse.Ret === 0 | ||
logger.debug('inviteMemberToChatroom issucc :', issucc) | ||
return issucc ? resp : null | ||
}) | ||
.catch(function (err) { | ||
logger.warn(err) | ||
return null | ||
}) | ||
} | ||
|
||
module.exports.Register = function (core) { | ||
core.createChatroom = createChatroom | ||
core.setChatroomName = setChatroomName | ||
core.deleteMemberFromChatroom = deleteMemberFromChatroom | ||
core.inviteMemberToChatroom = inviteMemberToChatroom | ||
core.addMemberToChatroom = addMemberToChatroom | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict' | ||
|
||
let log4js = require('log4js') | ||
let logger = log4js.getLogger('chatwe') | ||
var http = require('http') | ||
var Wechat = require('../index') | ||
var tape = require('tape') | ||
var _test = require('tape-promise').default | ||
var test = _test(tape) | ||
let Config = require('../lib/config') | ||
Config.BASE_URL = 'http://127.0.0.1:12345' | ||
Config.PUSHLOGIN_BASE_URL = 'http://127.0.0.1:12345' | ||
var wechat = new Wechat() | ||
wechat.setLogging('info') | ||
|
||
let roomName = '@@xxx' | ||
let users = [{ UserName: '1' }, { UserName: '2' }] | ||
let ret = '{"BaseResponse":{"Ret":0}}' | ||
var s = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
logger.debug(req.url) | ||
if (req.url.startsWith('/webwxupdatechatroom')) { | ||
var body = '' | ||
req.on('data', function (chunk) { | ||
body += chunk | ||
}) | ||
|
||
req.on('end', function () { | ||
let query = require('url').parse(req.url, true) | ||
|
||
// 解析参数 | ||
body = JSON.parse(body) | ||
if (query.query.fun === 'addmember' && body.AddMemberList === '1,2' && body.ChatRoomName === roomName) { | ||
res.end(ret) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
test('setup', function (t) { | ||
s.listen({ | ||
host: 'localhost', | ||
port: 12345, | ||
exclusive: true | ||
}, function () { | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('wechat addMemberToChatroom 1', async function (t) { | ||
wechat.loginInfo['url'] = Config.BASE_URL | ||
wechat.loginInfo['User'] = {} | ||
|
||
let resp = await wechat.addMemberToChatroom({ UserName: roomName }, users) | ||
|
||
t.equal(0, resp.BaseResponse.Ret) | ||
}) | ||
|
||
test('wechat addMemberToChatroom 2', async function (t) { | ||
wechat.loginInfo['url'] = Config.BASE_URL | ||
wechat.loginInfo['User'] = {} | ||
ret = '{"BaseResponse":{"Ret":1}}' | ||
let resp = await wechat.addMemberToChatroom({ UserName: roomName }, users) | ||
|
||
t.equal(null, resp) | ||
}) | ||
|
||
test('cleanup', function (t) { | ||
s.close(function () { | ||
t.end() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict' | ||
|
||
let log4js = require('log4js') | ||
let logger = log4js.getLogger('chatwe') | ||
var http = require('http') | ||
var Wechat = require('../index') | ||
var tape = require('tape') | ||
var _test = require('tape-promise').default | ||
var test = _test(tape) | ||
let Config = require('../lib/config') | ||
Config.BASE_URL = 'http://127.0.0.1:12345' | ||
Config.PUSHLOGIN_BASE_URL = 'http://127.0.0.1:12345' | ||
var wechat = new Wechat() | ||
wechat.setLogging('info') | ||
|
||
let topic = 'R1' | ||
let users = [{ UserName: '1' }, { UserName: '2' }] | ||
let ret = '{"BaseResponse":{"Ret":0}}' | ||
var s = http.createServer(function (req, res) { | ||
res.statusCode = 200 | ||
logger.debug(req.url) | ||
if (req.url.startsWith('/webwxcreatechatroom')) { | ||
var body = '' | ||
req.on('data', function (chunk) { | ||
body += chunk | ||
}) | ||
|
||
req.on('end', function () { | ||
// 解析参数 | ||
body = JSON.parse(body) | ||
if (body.Topic === topic && body.MemberCount === users.length) { | ||
res.end(ret) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
test('setup', function (t) { | ||
s.listen({ | ||
host: 'localhost', | ||
port: 12345, | ||
exclusive: true | ||
}, function () { | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('wechat createroom 1', async function (t) { | ||
wechat.loginInfo['url'] = Config.BASE_URL | ||
wechat.loginInfo['User'] = {} | ||
|
||
let resp = await wechat.createChatroom(users, topic) | ||
|
||
t.equal(0, resp.BaseResponse.Ret) | ||
}) | ||
|
||
test('wechat createroom 2', async function (t) { | ||
wechat.loginInfo['url'] = Config.BASE_URL | ||
wechat.loginInfo['User'] = {} | ||
ret = '{"BaseResponse":{"Ret":1}}' | ||
let resp = await wechat.createChatroom(users, topic) | ||
|
||
t.equal(null, resp) | ||
}) | ||
|
||
test('cleanup', function (t) { | ||
s.close(function () { | ||
t.end() | ||
}) | ||
}) |
Oops, something went wrong.