下面以PHP为例子,给出一个实例。程序包括三个文件: spirit.php为定义相关的函数, list.php显示模板列表, edit.php编辑模板,下面分别讲解。

spirit.php

<?php
define("SPIRIT_HOST", "https://www.printspirit.cn");
define("UID",  "third_test");
define("PASS", "third_test");

function getAccessToken($uid, $pass) {

	$apcuAvailabe = function_exists('apcu_enabled') && apcu_enabled();
	
	if($apcuAvailabe){
		$access_token = apcu_fetch('access_token');
		$expirt_time = apcu_fetch('expirt_time');
		if ( $access_token && $expirt_time > time() ) return $access_token;
	}

	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-access-token?userid=$uid&passwd=$pass"));

	if ($rc!=NULL && $rc->rc=='OK') {
		if($apcuAvailabe){
			apcu_store('access_token', $rc->token);
			apcu_store('expirt_time',  time() + $rc->expirt);
		}
		return $rc->token;
	}	
	die("无法获取TOKEN:".$rc->errmsg);
}

function getList($subclass="") {
	$token = getAccessToken(UID, PASS);
	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-list?token=${token}&subclass=${subclass}"), true);
	if ($rc!=NULL && $rc['rc']=='OK') 	return $rc['data'];
	return [];
}

function getContent($tpid) {
	$token = getAccessToken(UID, PASS);
	$rc=json_decode(file_get_contents(SPIRIT_HOST . "/api/get-label-content?token=${token}&tpid=${tpid}"), true);
	if ($rc!=NULL && $rc['rc']=='OK') return $rc['data'];
	return "";
}

function get_edit_url($subclass, $tpid="") {
	$token = getAccessToken(UID, PASS);
	return SPIRIT_HOST . "/third-edit?subclass=${subclass}&tpid=${tpid}&token=${token}";	
}

该文件定义了4个函数:

  • getAccessToken()获取访问API的token, 调用任何API都需要access_token 需要注意的是,应将access_token缓存起来,在有效期内避免重复获取, 在示例程序中如果安装了PHP-APCu将自动缓存token。
  • getList($subclass) 返回$subclass指定类别的打印模板列表。如果不设定类别,返回该账户下的全部打印模板。
  • get_edit_url($subclass, $tpid) 获取编辑模板的url地址,如$tpid为空,创建新模板。$subclass指定保存类别。可以直接跳转到该地址,或嵌入IFRAME。
  • getContent($tpid) 获取模板内容

list.php

list.php调用getList()获取当前用户的全部打印模板,并列表显示出来,显示缩略图使用了/utils/thumb。每个模板后面有一个编辑按钮,最下面有一个新增按钮就调用edit.php

<?php
require_once("spirit.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>A Test Client for Spirit</title>
  </head>
  <body>
  	<table style="width:50%; margin: 0 auto; border:1px solid #888">
  	<tr><th>标签名</th><th>子分类</th><th>缩略图</th></tr>
  	<?php 
			$lst = getList();
  		foreach( $lst as $l) {
	  		echo "<tr>";
	  		echo "<td>${l['name']}</td>";
	  		echo "<td>${l['subclass']}</td>";
	  		/*显示缩略图*/
	  		echo "<td><img height='100px' src='" . SPIRIT_HOST . "/utils/thumb?id=${l[id]}' /></td>";
	  		echo "<td><a href='edit.php?subclass=${l['subclass']}&tpid=${l['id']}"'><button>编辑(嵌入)</button></a>"
	      	echo "<a href='<?="edit.php?subclass=${l['subclass']}&tpid=${l['id']}&target=new"'><button>编辑(跳转)</button></a></td>"
	  		echo "</tr>\n";
	  	}
  	?>	
  	</table>
  	<div style="width:50%;margin:0 auto;padding:5px;text-align:right">
  		<a href='edit.php?subclass="user1"'><button>新增标签</button></a>
  	<div>
  </body>
</html> 

edit.php

编辑打印模板,通过get_edit_url(subclass, tpid)获取编辑url, 可以直接跳转,或嵌入IFRAME中。如tpid为空,创建新模板。 subclass指定保存的子类,例子中全部保存到user1,实际程序可以设置为第三方网站当前登录用户。

<?php
require_once("spirit.php");
$url=get_edit_url($_GET['subclass'], $_GET['tpid']);

if (!empty($_GET['target']) && $_GET['target']=='new') {
    header("Location: " . $url);
    return;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
  <div class="iframe">
     <iframe src="<?php echo $url?>" />
  </div>
  </body>
</html> 

留言

登录