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