简体   繁体   English

从另一个文件调用函数,并为每次调用获取随机结果

[英]call a function from another file and get a random result for each call

I have a random.js file containing this to get random fake ip 我有一个包含此文件的random.js文件,以获取随机的假ip

exports.ip = function () {
    let random = (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0); 
    return random
}

and I call the variable in the send.js file to replace string {randomip} 然后调用send.js文件中的变量来替换字符串{randomip}

let replace_tag = function (to) {

    config.message.subject = config.message.subject
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromname = config.message.fromname
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromemail = config.message.fromemail
        .replace("{randomip}", random.ip)
        .replace("{email}", to)
        .replace("{date}", random.date);

}

but it will only produce one generated ip, I want to make it generate random every time it is called will produce different values 但是它只会产生一个生成的ip,我想让它在每次被调用时生成随机值会产生不同的值

I have tried inserting it in the loop but still not working 我尝试将其插入循环中,但仍无法正常工作

I call the replace function in another function then enter it into loop like that 我在另一个函数中调用replace函数,然后像这样进入循环

let kirim = function (to) {

    replace_tag(to);

    let message = {
        from: config.message.fromname+'<'+config.message.fromemail+'>',
        to: to,
        subject: config.message.subject,
        text: config.message.text,
        html: html
    };

    transporter.sendMail(message, (error, info) => {
        if (error) {
             return console.log(error.response)
        }
        console.log('Message sent: ',info.accepted);
    });
};


(async () => {

    for (var i in list) {

        kirim(list[i]);
        await delay(config.send.delay*1000); 

    }

})();

i think it's will work. 我认为这会工作。

random.js: random.js:

exports.ip = function() {
    return (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 
        0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);
}

send.js: send.js:

let replace_tag = function (to) {

    config.message.subject = config.message.subject
        .replace("{randomip}", random.ip())
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromname = config.message.fromname
        .replace("{randomip}", random.ip())
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromemail = config.message.fromemail
        .replace("{randomip}", random.ip())
        .replace("{email}", to)
        .replace("{date}", random.date);

}
function getRandomIp() {
    return (Math.floor(Math.random() * 255) + 1)+"."+(Math.floor(Math.random() * 255) + 
    0)+"."+(Math.floor(Math.random() * 255) + 0)+"."+(Math.floor(Math.random() * 255) + 0);
}
let replace_tag = function (to) {

    config.message.subject = config.message.subject
        .replace("{randomip}", getRandomIp())
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromname = config.message.fromname
        .replace("{randomip}", getRandomIp())
        .replace("{email}", to)
        .replace("{date}", random.date);

    config.message.fromemail = config.message.fromemail
        .replace("{randomip}", getRandomIp())
        .replace("{email}", to)
        .replace("{date}", random.date);

}

I call the replace function in another function then enter it into loop 我在另一个函数中调用替换函数,然后将其输入循环

Ah, there's your problem. 嗯,你有问题。 Your replace_tag function will change the config object, and after the first call it doesn't contain the template tag any more but the replacement result. 您的replace_tag函数将更改config对象,并且在第一次调用后,它不再包含模板标记,而是替换结果。 Further calls to replace_tag won't find {randomip} in your configuration any more, so no new ips are generated. 进一步调用replace_tag将不再在您的配置中找到{randomip} ,因此不会生成新的{randomip}

You should instead keep the configuration constant (immutable), and create new message objects every time you need one. 相反,应保持配置不变(不变),并在每次需要时创建新的消息对象。 Each of those objects will then have different randomised IP addresses. 这些对象中的每一个将具有不同的随机IP地址。

// takes a string, returns a new string
function replace_tags(input, email) {
    return input
    .replace("{randomip}", random.ip)
    .replace("{email}", email)
    .replace("{date}", random.date);
}
// returns a new object, leaves config.message unaltered
function get_customised_message_template(to) {
    return {
        subject: replace_tags(config.message.subject, to),
        fromname: replace_tags(config.message.fromname, to),
        fromemail: replace_tags(config.message.fromemail, to),
    };
}

function kirim(to) {
    const random_message = get_customised_message_template(to);
//  ^^^^^^^^^^^^^^^^^^^^^^
    const message = {
        from: random_message.fromname+'<'+random_message.fromemail+'>',
        to: to,
        subject: random_message.subject,
        text: config.message.text,
        html: html
    };

    transporter.sendMail(message, (error, info) => {
        if (error) console.log(error.response);
        else console.log('Message sent: ', info.accepted);
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM