Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
重构缓存更新代码
Browse files Browse the repository at this point in the history
  • Loading branch information
reverland committed Jan 29, 2016
1 parent c83d377 commit e39503a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
66 changes: 32 additions & 34 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,26 @@ function logPrivateMsg(o, obj) {
function handlePrivate(username, replyContent, obj) {
// 如果没找到,请求啊
// 查看Object Array中是否有UserName属性为username的Object
function _has(list, Property, username) {
for (let l of list) {
if (l[Property] == username) {
return true;
}
// FIXME_TEST: find替换

var p = new Promise((resolve, reject)=>{
if (obj.memberList.findIndex(m=>m['UserName']==username) < 0) {
// memberList中不存在
var contactP = _requestUserInfo(username, obj);
} else {
var contactP = Promise.resolve(obj);
}
return false;
}

contactP.then(_logPrivateTextMsg).catch(reject);

var p = new Promise((resolve, reject)=>{
if (!_has(obj.memberList, 'UserName', username)) {
var contactP = new Promise((resolve, reject)=>{
function _logPrivateTextMsg(obj) {
// FIXME_TEST: 用find替换
var m = obj.memberList.find(m=>m.UserName==username);
resolve("[" + m.NickName + "说]" + replyContent);
}

function _requestUserInfo(username, obj) {
return new Promise((resolve, reject)=>{
var postData = {
BaseRequest: obj.BaseRequest,
Count: 1,
Expand Down Expand Up @@ -82,19 +89,6 @@ function handlePrivate(username, replyContent, obj) {
resolve(obj);
});
});
} else {
var contactP = Promise.resolve(obj);
}

contactP.then(_logPrivateTextMsg).catch(reject);

function _logPrivateTextMsg(obj) {
for (var i = 0; i < obj.memberList.length; i++) {
if (obj.memberList[i]['UserName'] == username) {
console.log('[' + obj.memberList[i]['NickName'] + ' 说]', replyContent);
return;
}
}
}
});
return p;
Expand All @@ -116,7 +110,21 @@ function handleGroup(groupUserName, replyContent, obj) {
}
// 查看是否缓存中有
if (!(groupUserName in obj.groupContact)) {
var contactP = new Promise((resolve, reject)=>{
var contactP = _requestGroupInfo(groupUserName, obj)
} else {
var contactP = Promise.resolve(obj);
}

contactP.then(_logGroupTextMsg);
// 记录群消息函数
function _logGroupTextMsg(obj) {
var groupRealName = obj.groupContact[groupUserName]['nickName'];
var m = obj.groupContact[groupUserName]['memberList'].find(m=>m.UserName==fromUserName)
resolve("[" + groupRealName + "]" + m.NickName + replyContent.replace(fromUserName, '').replace("<br/>", ""));
}

function _requestGroupInfo(groupUserName, obj) {
return new Promise((resolve, reject)=>{
var postData = {
BaseRequest: obj.BaseRequest,
Count: 1,
Expand Down Expand Up @@ -155,16 +163,6 @@ function handleGroup(groupUserName, replyContent, obj) {
resolve(obj);
}); // request
}); //promise
} else {
var contactP = Promise.resolve(obj);
}

contactP.then(_logGroupTextMsg);
// 记录群消息函数
function _logGroupTextMsg(obj) {
var groupRealName = obj.groupContact[groupUserName]['nickName'];
var m = obj.groupContact[groupUserName]['memberList'].find(m=>m.UserName==fromUserName)
resolve("[" + groupRealName + "]" + m.NickName + replyContent.replace(fromUserName, '').replace("<br/>", ""));
}
});
return p;
Expand Down
2 changes: 1 addition & 1 deletion webwx.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var getUUID = new Promise((resolve, reject)=>{
function checkAndParseUUID(text) {
var result = /window.QRLogin.code = (\d+); window.QRLogin.uuid = "([^"]+)";/.exec(text);
//debug("checkAndParseUUID");
if (result[1] != '200') {
if (!result || result[1] != '200') {
return false;
}
return result[2];
Expand Down

0 comments on commit e39503a

Please sign in to comment.