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

问题描述 投票:0回答:3

我有一个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
}

我调用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);

}

但它只会产生一个生成的ip,我想让它每次调用时都会生成随机的,会产生不同的值

我已经尝试将它插入循环但仍然无法正常工作

我在另一个函数中调用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); 

    }

})();
javascript node.js function random call
3个回答
0
投票

我在另一个函数中调用replace函数然后将其输入循环

啊,这是你的问题。你的replace_tag函数将改变config对象,并且在第一次调用之后它不再包含模板标记,而是替换结果。进一步调用replace_tag将不再在您的配置中找到{randomip},因此不会生成新的ips。

您应该保持配置不变(不可变),并在每次需要时创建新的消息对象。然后,每个对象将具有不同的随机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);
    });
};

0
投票

我认为它会起作用。

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:

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);

}

0
投票
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);

}
© www.soinside.com 2019 - 2024. All rights reserved.