一键玩(LUA)代码示例
<p>将以下代码复制到脚本代码的顶部,替换<code>CooCheckUrl</code>为你的面板API地址。<code>CooCheckCode</code>保存控制器code,控制器code通常由用户提供。</p>
<pre><code class="language-lua">--面板API
local CooCheckUrl = &quot;http://api.coocheck.com/check?panel=pn-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx&amp;code=&quot;
--控制器code,通常由用户输入提供
local CooCheckCode = &quot;cxxxxxx&quot;
--API返回的数据
local CooCheckData = {}
--拉取远程参数数据到变量CooCheckData。需要注意:远程参数有缓存,缓存失效后才能拿到用户填写的最新参数。
local function LoadCooCheck()
local apiUrl = CooCheckUrl .. CooCheckCode .. &quot;&amp;type=json&quot;
local result = httpGet(apiUrl)
--print(&quot;Coocheck API 网址 : &quot; .. apiUrl)
--print(&quot;Coocheck API 返回 : &quot; .. result)
CooCheckData = jsonLib.decode(result)
end
--读取远程参数key的值,如果key不存在则返回defValue
local function CooCheck(key, defValue)
if CooCheckData[key] ~= nil then
return CooCheckData[key]
end
return defValue
end</code></pre>
<p>拉取远程参数时,通过<code>LoadCooCheck()</code>方式,远程参数数据会保存到变量CooCheckData。
读取远程参数时,通过<code>CooCheck(key, defValue)</code>方式读取指定key的参数;特殊情况下我们会担心指定key的参数获取失败而影响脚本运行,比如发布新面板时候不慎删除某个有用的key,而defValue参数的作用是发现key参数不存在时,以defValue值作为参数值返回。</p>
<pre><code class="language-lua">--返回key为submited_at的参数值,如果不存在该参数,则返回0
print(&quot;submited_at : &quot; .. CooCheck(&quot;submited_at&quot;, 0))
--返回key为key1的参数值,如果不存在该参数,则返回123
print(&quot;key1 : &quot; .. CooCheck(&quot;key1&quot;, 123))</code></pre>