← 返回

应用详情

📦

Qwen

版本 v1 · 1.7 KB
ID: qwenai_chat
⬇ 下载 CIP 🔍 返回列表
📝 应用描述
与Qwen大模型进行对话
📋 详细信息
应用ID qwenai_chat
名称 Qwen
版本 v1
文件大小 1.7 KB
上传时间 2026-07-31 17:53:30
状态 ✅ 已启用
权限 network, storage, network_external
允许主机 api.sloe.de5.net
💡 界面预览 (main.lua)
📄 Lua 源码
local story_id = "chat_display"
local input_id = "chat_input"
local send_id = "send_btn"
local retry_count = 0
local max_retries = 3

local function base64_encode(str)
  local b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  local result = ""
  for i = 1, #str, 3 do
    local a = string.byte(str, i)
    local b = string.byte(str, i+1) or 0
    local c = string.byte(str, i+2) or 0
    local n = a * 65536 + b * 256 + c
    local chars = {}
    for j = 1, 4 do
      local index = math.floor(n / 2^(6*(4-j))) % 64
      table.insert(chars, b64chars:sub(index+1, index+1))
    end
    if i+1 > #str then
      chars[3] = "="
      chars[4] = "="
    elseif i+2 > #str then
      chars[4] = "="
    end
    result = result .. table.concat(chars)
  end
  return result
end

local function set_buttons_enabled(enabled)
  app.set_enabled(send_id, enabled)
end

local function send_message()
  local input_text = app.get_text(input_id)
  if not input_text or input_text == "" then
    app.toast("请输入内容")
    return
  end

  set_buttons_enabled(false)
  app.set_text(story_id, "发送中...")

  local encoded = base64_encode(input_text)
  local url = "https://api.sloe.de5.net/chat/index.php?msg=" .. encoded
  local finished = false

  app.delay(15000, function()
    if finished then return end
    finished = true
    if retry_count < max_retries then
      retry_count = retry_count + 1
      app.set_text(story_id, "请求超时,重试 " .. tostring(retry_count) .. "/" .. tostring(max_retries))
      app.delay(1000, function()
        send_message()
      end)
    else
      app.set_text(story_id, "多次超时,请检查网络")
      set_buttons_enabled(true)
      app.toast("请求超时多次")
    end
  end)

  app.http_get(url, function(body, err)
    if finished then return end
    finished = true

    if err then
      if retry_count < max_retries then
        retry_count = retry_count + 1
        app.set_text(story_id, "网络错误,重试 " .. tostring(retry_count) .. "/" .. tostring(max_retries))
        app.delay(1000, function()
          send_message()
        end)
      else
        app.set_text(story_id, "网络错误多次,请稍后")
        set_buttons_enabled(true)
        app.toast("网络错误")
      end
      return
    end

    if not body or body == "" then
      if retry_count < max_retries then
        retry_count = retry_count + 1
        app.set_text(story_id, "空响应,重试 " .. tostring(retry_count) .. "/" .. tostring(max_retries))
        app.delay(1000, function()
          send_message()
        end)
      else
        app.set_text(story_id, "服务器空响应")
        set_buttons_enabled(true)
        app.toast("空响应")
      end
      return
    end

    local data = app.json_decode(body)
    if not data then
      app.set_text(story_id, "数据解析失败")
      set_buttons_enabled(true)
      app.toast("JSON解析失败")
      return
    end

    if data.error then
      app.set_text(story_id, "错误:" .. data.error)
      set_buttons_enabled(true)
      app.toast("请求出错")
      return
    end

    retry_count = 0
    set_buttons_enabled(true)
    app.set_text(story_id, "🤖 " .. (data.reply or "无回复"))
    app.set_text(input_id, "")
  end)
end

return ui.page({
  title = "聊天助手",
  children = {
    ui.text({ id = story_id, text = "你好!有什么想聊的吗?", size = 15, margin = 8 }),
    ui.input({
      id = input_id,
      hint = "输入你想说的话...",
      single_line = true,
      margin = 8,
      on_submit = function(value)
        if value and value ~= "" then
          send_message()
        end
      end
    }),
    ui.button({
      id = send_id,
      text = "发送",
      on_click = function()
        send_message()
      end,
      margin = 8
    }),
    ui.text({ text = "Powered by 雨下¹整晚²", size = 12, color = "#888888", center = true, margin = 4 })
  }
})
⬇ 下载 CIP 文件 🔍 返回列表