var dialog = dialogs.build({
title: "参数设置界面",
wrapInScrollView: false,
customView: <vertical padding="16">

            <text>运行模式:</text>
            <spinner id="select_runtype" entries={["单商品购买", "循环购买"]} />

      
            <text>关键词:</text>
            <input id="text_word" hint="vx群名称或好友名称包含的唯一关键词" />

          
            <text>支付密码:</text>
            <input id="text_password" inputType="numberPassword" hint="支付密码,不需要脚本输入密码填0即可" />


            <text>购买方式:</text>
            <spinner id="select_getshopfun" entries={["到店取", "送到家", "在线抽"]} />


            <text>规格选择:</text>
            <spinner id="select_type" entries={["单个盲盒随机发货", "整盒含*个盲盒"]} />


            <text>数量:</text>
            <input id="text_number" inputType="number" hint="购买数量" />


            <text>最大刷新次数:</text>
            <input id="text_timeoutcount" inputType="number" hint="刷新N次后结束或执行下一个,为0则无限刷取" />


            <text>单次刷新延时:</text>
            <input id="text_refush_timeout" inputType="number" hint="刷新后等待时间(单位ms)" />
        </vertical>
    }).on("positive_click", function (dialog) {
        // 获取用户输入的值并保存到配置对象中
        config.select_runtype = dialog.customView.select_runtype.getSelectedItem();
        config.text_word = dialog.customView.text_word.getText().toString();
        config.text_password = dialog.customView.text_password.getText().toString();
        config.select_getshopfun = dialog.customView.select_getshopfun.getSelectedItem();
        config.select_type = dialog.customView.select_type.getSelectedItem();
        config.text_number = dialog.customView.text_number.getText().toString();
        config.text_timeoutcount = dialog.customView.text_timeoutcount.getText().toString();
        config.text_refush_timeout = dialog.customView.text_refush_timeout.getText().toString();

        // 打印所有配置信息
        log("运行模式:" + config.select_runtype);
        log("关键词:" + config.text_word);
        log("支付密码:" + config.text_password);
        log("购买方式:" + config.select_getshopfun);
        log("规格选择:" + config.select_type);
        log("数量:" + config.text_number);
        log("最大刷新次数:" + config.text_timeoutcount);
        log("单次刷新延时:" + config.text_refush_timeout);

        toast("配置已保存!");
    }).show();   我想在点击配置按钮后,脚本先获取hamibot.env中对应的参数回填到对应的输入框或者选择框中,当用户输入数据完成后,点击确定按钮,脚本再读取参数然后回填到hamibot.env中,能实现么
已存档
七月CX · 2 个月前
第 1 条补充
·
2 个月前

给出的代码不完整,并且运行时点击配置按钮无响应

回答

Hamibot AI
1
· 2 个月前

可以实现你的需求。你可以通过在点击配置按钮后,从 hamibot.env 中读取已有的参数值,并将这些值回填到对话框的输入框或选择框中。当用户点击确定按钮时,脚本再获取对话框中的新值,并更新 hamibot.env

下面是完整的实现代码:

// 文档地址 https://docs.hamibot.com/

var config = {
    select_runtype: hamibot.env.select_runtype || "单商品购买",
    text_word: hamibot.env.text_word || "",
    text_password: hamibot.env.text_password || "",
    select_getshopfun: hamibot.env.select_getshopfun || "到店取",
    select_type: hamibot.env.select_type || "单个盲盒随机发货",
    text_number: hamibot.env.text_number || "",
    text_timeoutcount: hamibot.env.text_timeoutcount || "",
    text_refush_timeout: hamibot.env.text_refush_timeout || ""
};

var dialog = dialogs.build({
    title: "参数设置界面",
    wrapInScrollView: false,
    customView: <vertical padding="16">
        <text>运行模式:</text>
        <spinner id="select_runtype" entries={["单商品购买", "循环购买"]} />

        <text>关键词:</text>
        <input id="text_word" hint="vx群名称或好友名称包含的唯一关键词" />

        <text>支付密码:</text>
        <input id="text_password" inputType="numberPassword" hint="支付密码,不需要脚本输入密码填0即可" />

        <text>购买方式:</text>
        <spinner id="select_getshopfun" entries={["到店取", "送到家", "在线抽"]} />

        <text>规格选择:</text>
        <spinner id="select_type" entries={["单个盲盒随机发货", "整盒含*个盲盒"]} />

        <text>数量:</text>
        <input id="text_number" inputType="number" hint="购买数量" />

        <text>最大刷新次数:</text>
        <input id="text_timeoutcount" inputType="number" hint="刷新N次后结束或执行下一个,为0则无限刷取" />

        <text>单次刷新延时:</text>
        <input id="text_refush_timeout" inputType="number" hint="刷新后等待时间(单位ms)" />
    </vertical>
}).on("show", function (dialog) {
    // 回填已有参数到对话框控件
    dialog.customView.select_runtype.setSelection(["单商品购买", "循环购买"].indexOf(config.select_runtype));
    dialog.customView.text_word.setText(config.text_word);
    dialog.customView.text_password.setText(config.text_password);
    dialog.customView.select_getshopfun.setSelection(["到店取", "送到家", "在线抽"].indexOf(config.select_getshopfun));
    dialog.customView.select_type.setSelection(["单个盲盒随机发货", "整盒含*个盲盒"].indexOf(config.select_type));
    dialog.customView.text_number.setText(config.text_number);
    dialog.customView.text_timeoutcount.setText(config.text_timeoutcount);
    dialog.customView.text_refush_timeout.setText(config.text_refush_timeout);
}).on("positive_click", function (dialog) {
    // 获取用户输入的值并保存到配置对象中
    config.select_runtype = dialog.customView.select_runtype.getSelectedItem();
    config.text_word = dialog.customView.text_word.getText().toString();
    config.text_password = dialog.customView.text_password.getText().toString();
    config.select_getshopfun = dialog.customView.select_getshopfun.getSelectedItem();
    config.select_type = dialog.customView.select_type.getSelectedItem();
    config.text_number = dialog.customView.text_number.getText().toString();
    config.text_timeoutcount = dialog.customView.text_timeoutcount.getText().toString();
    config.text_refush_timeout = dialog.customView.text_refush_timeout.getText().toString();

    // 更新 hamibot.env 中的参数
    hamibot.env.select_runtype = config.select_runtype;
    hamibot.env.text_word = config.text_word;
    hamibot.env.text_password = config.text_password;
    hamibot.env.select_getshopfun = config.select_getshopfun;
    hamibot.env.select_type = config.select_type;
    hamibot.env.text_number = config.text_number;
    hamibot.env.text_timeoutcount = config.text_timeoutcount;
    hamibot.env.text_refush_timeout = config.text_refush_timeout;

    // 打印所有配置信息
    log("运行模式:" + config.select_runtype);
    log("关键词:" + config.text_word);
    log("支付密码:" + config.text_password);
    log("购买方式:" + config.select_getshopfun);
    log("规格选择:" + config.select_type

***

# 补充回答 1

你说得对,之前的代码示例确实不完整,并且没有处理对话框按钮点击逻辑。以下是完整的、经过修正的脚本代码,确保你可以在点击“配置”按钮时弹出对话框,并在点击“确定”后更新 `hamibot.env` 中的参数。

### ✅ 完整可运行代码如下:

```javascript
// 文档地址 https://docs.hamibot.com/

// 初始化配置对象,优先从 hamibot.env 获取已有值
var config = {
    select_runtype: hamibot.env.select_runtype || "单商品购买",
    text_word: hamibot.env.text_word || "",
    text_password: hamibot.env.text_password || "",
    select_getshopfun: hamibot.env.select_getshopfun || "到店取",
    select_type: hamibot.env.select_type || "单个盲盒随机发货",
    text_number: hamibot.env.text_number || "",
    text_timeoutcount: hamibot.env.text_timeoutcount || "",
    text_refush_timeout: hamibot.env.text_refush_timeout || ""
};

// 创建一个按钮触发配置界面
auto.waitFor();
var ui = floaty.window(
    <vertical padding="10">
        <button id="btn_config">配置参数</button>
    </vertical>
);

ui.btn_config.click(function () {
    showConfigDialog();
});

// 显示配置对话框
function showConfigDialog() {
    var dialog = dialogs.build({
        title: "参数设置界面",
        wrapInScrollView: false,
        customView: <vertical padding="16">
            <text>运行模式:</text>
            <spinner id="select_runtype" entries={["单商品购买", "循环购买"]} />

            <text>关键词:</text>
            <input id="text_word" hint="vx群名称或好友名称包含的唯一关键词" />

            <text>支付密码:</text>
            <input id="text_password" inputType="numberPassword" hint="支付密码,不需要脚本输入密码填0即可" />

            <text>购买方式:</text>
            <spinner id="select_getshopfun" entries={["到店取", "送到家", "在线抽"]} />

            <text>规格选择:</text>
            <spinner id="select_type" entries={["单个盲盒随机发货", "整盒含*个盲盒"]} />

            <text>数量:</text>
            <input id="text_number" inputType="number" hint="购买数量" />

            <text>最大刷新次数:</text>
            <input id="text_timeoutcount" inputType="number" hint="刷新N次后结束或执行下一个,为0则无限刷取" />

            <text>单次刷新延时:</text>
            <input id="text_refush_timeout" inputType="number" hint="刷新后等待时间(单位ms)" />
        </vertical>,
        positive: "确定",
        negative: "取消"
    }).on("show", function (dialog) {
        // 回填已有参数到控件
        dialog.customView.select_runtype.setSelection(["单商品购买", "循环购买"].indexOf(config.select_runtype));
        dialog.customView.text_word.setText(config.text_word);
        dialog.customView.text_password.setText(config.text_password);
        dialog.customView.select_getshopfun.setSelection(["到店取", "送到家", "在线抽"].indexOf(config.select_getshopfun));
        dialog.customView.select_type.setSelection(["单个盲盒随机发货", "整盒含*个盲盒"].indexOf(config.select_type));
        dialog.customView.text_number.setText(config.text_number);
        dialog.customView.text_timeoutcount.setText(config.text_timeoutcount);
        dialog.customView.text_refush_timeout.setText(config.text_refush_timeout);
    }).on("positive_click", function (dialog) {
        // 读取用户输入并更新配置
        config.select_runtype = dialog.customView.select_runtype.getSelectedItem();
        config.text_word = dialog.customView.text_word.getText().toString();
        config.text_password = dialog.customView.text_password.getText().toString();
        config.select_getshopfun = dialog.customView.select_getshopfun.getSelectedItem();
        config.select_type = dialog.customView.select_type.getSelectedItem();
        config.text_number = dialog.customView.text_number.getText().toString();
        config.text_timeoutcount = dialog.customView.text_timeoutcount.getText().toString();
        config.text_refush_timeout = dialog.customView.text_refush_timeout.getText().toString();

        // 更新 hamibot.env
        hamibot.env.select_runtype = config.select_runtype;
        hamibot.env.text_word = config.text_word;
        hamibot.env.text_password = config.text_password;
        hamibot.env.select_getshopfun = config.select_getshopfun;
        hamibot.env.select_type = config.select_type;
        hamibot.env.text_number = config.text_number;
        hamibot
ℹ️ 本回答由 AI 生成,内容仅供参考