Skip to content

Commit

Permalink
fix(h5test): support batch request to resolve #188
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinzZH committed Jul 20, 2018
1 parent c121e89 commit 7a88e9d
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 86 deletions.
83 changes: 56 additions & 27 deletions bin/tsw/util/h5-test/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ const OALogin = require('util/oa-login/index.js');
const gzipHttp = require('util/gzipHttp.js');
const canIuse = /^[0-9a-zA-Z_-]{0,64}$/;

module.exports = function(request, response) {
OALogin.checkLoginForTSW(request, response, function() {
module.exports = (request, response) => {
OALogin.checkLoginForTSW(request, response, () => {
module.exports.go(request, response);
});
};

module.exports.go = async function(request, response) {
module.exports.go = async (request) => {

const uin = request.param('uin');
const val = request.param('val');
const map = request.param('uinval');
let data;

const data = await module.exports.addTestUser(uin, val).toES6Promise().catch(function() {
return null;
});
if (map && typeof map === 'object') {
data = await module.exports.addTestUsers(map).toES6Promise().catch(() => null);
} else {
data = await module.exports.addTestUser(uin, val).toES6Promise().catch(() => null);
}

const result = { code: 0, data: data };

returnJson(result);
};

const returnJson = function(json) {
const returnJson = json => {
const gzip = gzipHttp.create({
contentType: 'application/json; charset=UTF-8',
code: 200
Expand All @@ -46,11 +50,35 @@ const returnJson = function(json) {
gzip.end();
};

module.exports.addTestUser = function(uin, val) {
logger.debug('addTestUser:' + uin);
val = val || true;
const memcached = isTest.cmem();
/**
* 设置测试环境
* @param uin
* @param val
* @returns {void | * | Promise<any> | Promise<never>}
*/
module.exports.addTestUser = (uin, val) => {
if (!uin) return Deferred.create().reject();
const users = {};
users[uin] = val;
return module.exports.addTestUsers(users);
};

/**
* 批量设置测试环境
* @param map 批量设置列表 key 是 uin,map[key] 是环境
* @returns {void | * | Promise<any> | Promise<never>}
*/
module.exports.addTestUsers = (map) => {
const uins = Object.keys(map);
logger.debug('addTestUsers:' + uins);
const defer = Deferred.create();

const memcached = isTest.cmem();

if (!memcached) {
return defer.reject('memcached not exists');
}

const appid = context.appid || '';
const appkey = context.appkey;
let keyText = isTest.keyBitmap();
Expand All @@ -60,24 +88,21 @@ module.exports.addTestUser = function(uin, val) {
keyText = `${keyText}.${appid}`;
}

if (!uin) {
if (!uins.length) {
return defer.reject();
}

if (!canIuse.test(uin)) {
return defer.reject();
}

if (!val) {
return defer.reject();
}
for (let i = 0; i < uins.length; i++) {
if (!canIuse.test(uins[i])) {
return defer.reject();
}

if (!memcached) {
return defer.reject('memcached not exists');
if (!map[uins[i]]) {
return defer.reject();
}
}


memcached.get(keyText, function(err, data) {
memcached.get(keyText, (err, data) => {

if (appid && typeof data === 'string') {
// 解密
Expand All @@ -99,25 +124,29 @@ module.exports.addTestUser = function(uin, val) {
text = {};
}

text[uin] = val;
for (let i = 0; i < uins.length; i++) {
const uin = uins[i];
const val = map[uin];
text[uin] = val;

logger.debug(`setKeyText: ${uin}; value: ${val}`);
logger.debug(`setKeyText: ${uin}; value: ${val}`);
}

if (appid) {
// 加密
text = post.encode(appid, appkey, text);
}

memcached.set(keyText, text, expire, function(err, ret) {
memcached.set(keyText, text, expire, (err) => {
if (err) {
defer.reject('memcache set data error');
} else {
logger.debug('setKeyText success');
defer.resolve();
}
});

});

return defer;
};

51 changes: 31 additions & 20 deletions bin/tsw/util/h5-test/del.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,26 @@ const gzipHttp = require('util/gzipHttp.js');
const canIuse = /^[0-9a-zA-Z_-]{0,64}$/;


module.exports = function(request, response) {
OALogin.checkLoginForTSW(request, response, function() {
module.exports = (request, response) => {
OALogin.checkLoginForTSW(request, response, () => {
module.exports.go(request, response);
});
};

module.exports.go = async function(request, response) {
module.exports.go = async request => {

const uin = request.param('uin');

const data = await module.exports.deleteTestUser(
uin
).toES6Promise().catch(function() {
return null;
});
).toES6Promise().catch(() => null);

const result = { code: 0, data: data };

returnJson(result);
};

const returnJson = function(json) {
const returnJson = json => {
const gzip = gzipHttp.create({
contentType: 'application/json; charset=UTF-8',
code: 200
Expand All @@ -48,20 +46,25 @@ const returnJson = function(json) {
gzip.end();
};

module.exports.deleteTestUser = uin => {
uin = Array.isArray(uin) ? uin : [uin];
return module.exports.deleteTestUsers([uin]);
};

module.exports.deleteTestUser = function(uin) {
logger.debug('deleteTestUser:' + uin);
/**
* 批量删除测试环境
* @param uins
* @returns {void | * | Promise<any> | Promise<never>}
*/
module.exports.deleteTestUsers = uins => {
logger.debug('deleteTestUser:' + uins);
const memcached = isTest.cmem();
const defer = Deferred.create();
const appid = context.appid || '';
const appkey = context.appkey;
let keyText = isTest.keyBitmap();

if (!uin) {
return defer.reject();
}

if (!canIuse.test(uin)) {
if (!uins || uins.length === 0) {
return defer.reject();
}

Expand All @@ -74,7 +77,13 @@ module.exports.deleteTestUser = function(uin) {
return defer.reject('memcached not exists');
}

memcached.get(keyText, function(err, data) {
for (let i = 0; i < uins.length; i++) {
if (!uins[i] || !canIuse.test(uins[i])) {
return defer.reject();
}
}

memcached.get(keyText, (err, data) => {

if (appid && typeof data === 'string') {
// 解密
Expand All @@ -96,18 +105,20 @@ module.exports.deleteTestUser = function(uin) {
return defer.resolve();
}

if (text[uin]) {
delete text[uin];
for (let i = 0; i < uins.length; i++) {
const uin = uins[i];
if (text[uin]) {
delete text[uin];
}
logger.debug('deleteKeyText:' + uin);
}

logger.debug('deleteKeyText:' + uin);

if (appid) {
// 加密
text = post.encode(appid, appkey, text);
}

memcached.set(keyText, text, expire, function(err, ret) {
memcached.set(keyText, text, expire, err => {
if (err) {
defer.reject('memcache del data error');
} else {
Expand Down
76 changes: 39 additions & 37 deletions bin/tsw/util/h5-test/page/src/new.tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@

return defer;
};
_api.addTestUsers = function(params){
var defer =$.Deferred();
$.ajax({
url: '/api/h5test/add',
type: 'POST',
contentType : "application/json",
dataType : "json",
data: JSON.stringify($.extend({},commomData,{
uinval: params
})),
success: function(data){
if(data && data.code === 0 ) {
defer.resolve(data);
}else {
defer.reject( data && data.msg || "系统繁忙,请稍后再试");
}
},
fail: function(err){
defer.reject( err && err.msg || "系统繁忙,请稍后再试");
}
});

return defer;
};

/*
* 删除白名单
Expand Down Expand Up @@ -122,57 +146,35 @@
<script type="text/javascript">
(function() {
var handlerNums = function(val){
return val.split(/(\b[0-9a-zA-Z_\-]{1,64}\b)/g);
}
var arr = [];
val.replace(/(\b[0-9a-zA-Z_\-]{1,64}\b)/g, function(uin){arr.push(uin); return uin;});
return arr;
};
//批量加入
$('#join-submit2').click(function(event){
var deferArr = [];
$('#join-submit2').click(function(e){
var val = $('#join-textarea').val();
// var arr = val.split(/(\b[0-9A-Za-z]+\b)/g);
var arr = handlerNums(val);
var param = {};
var selectVal = $('.selected')[0].value;
for(var i = 1; i < arr.length; i++) {
(function(uin){
var request = api.addTestUser({
uin : uin,
val : selectVal
});
deferArr.push(request);
}(arr[i]));
i++;
for(var i = 0; i < arr.length; i++) {
param[arr[i]] = selectVal;
}
$.when.apply(this, deferArr).done(function(){
api.addTestUsers(param).done(function(){
alert("你的号码,现已加入H5测试环境");
}).fail(function(errMsg){
errMsg = errMsg || "出了点小问题,请联系系统管理员处理";
alert(errMsg);
}).always(function () {
//$('#number-get').trigger('click');
alert(errMsg || "出了点小问题,请联系系统管理员处理");
});
return false;
});
//移除
$('#remove-submit2').click(function(event){
var deferArr = [];
$('#remove-submit2').click(function(e){
var val = $('#join-textarea').val();
var arr = handlerNums(val);

for(var i = 1; i < arr.length; i++) {

var request = api.delTestUser({
uin : arr[i]
});
deferArr.push(request);

i++;
}
$.when.apply(this, deferArr).done(function(){
api.delTestUser({
uin : handlerNums(val)
}).done(function(){
alert("H5测试环境删除成功");
}).fail(function(errMsg){
errMsg = errMsg || "出了点小问题,请联系系统管理员处理";
alert(errMsg);
}).always(function () {
//$('#number-get').trigger('click');
alert(errMsg || "出了点小问题,请联系系统管理员处理");
});
return false;
});
Expand Down
Loading

0 comments on commit 7a88e9d

Please sign in to comment.