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