为dede增加安卓客户端接口的基本流程

2019-11-25,,

开发一个网站客户端有很多中办法,最简单的就是后台什么也不便,安卓客户端只是根据URL地址获得整个网页放到webview中,但这种没什么意义,可以更进一步,仍然只是显示完整网页,但是为客户端专门布局html。

但真正做的好的客户端不是这么敷衍了事,php服务端只提供数据,而不负责数据的显示,所有显示在手机上完成。而服务器数据一般都是返回xml或者是 json。

遗憾的是dedecms到现在为止都没有推出任何平台的客户端,服务端根本连个接口都没有。

本文介绍如何为安卓客户端提供后台支持,返回xml数据。

这里以获取文章详情为例。

思路:其实为客户端提供数据我们只需改变返回的数据格式,将原本的html改成xml格式,也就是说我们只需要一个xml格式的模版,其他代码可以不变。因为不管是安卓客户端还是网页,对于一篇文章而言,我们需要的信息基本是一致的。

问题是,既然我们需要为安卓客户采用专门的模版,如何让dede识别到请求来自安卓客户端,且在什么地方加载这个模版呢?

识别来自安卓客户端只需在客户端的请求里面加入一个标志位就行了,我这里的标志名称为isMobile,然后在dede的/plus/view.php文件中获得这个变量:

if($isMobile==1)
$arc->setMobileVisitFlag(true);
else{
$arc->setMobileVisitFlag(false);
}

isMobile变量可以直接获取到,不需要自己处理,dede对所有请求做了处理,将每个请求的值映射到了和请求名称完全一致的变量中。

上面的代码获得了这个变量,判断是否为1,如果为一表示来自于客户端,这时我们调用一个我自己在/include/arc.archives.class.php文件中加入的函数setMobileVisitFlag()。setMobileVisitFlag()函数是这样定义的

public function setMobileVisitFlag($flag){
$this->mobileVisitFlag = $flag;
}

其中$mobileVisitFlag为我们给Archives类增加的一个变量。

 

现在我们需要找到Archives类中加载模版文件的地方,在这里对mobileVisitFlag的情况做判断,如果为真,则模板换成我们的xml模版,也就是客户端需要的数据格式。代码在GetTempletFile()函数中。下面是GetTempletFile原来的代码:

function GetTempletFile()
{
global $cfg_basedir,$cfg_templets_dir,$cfg_df_style;
$cid = $this->ChannelUnit->ChannelInfos['nid'];
if(!empty($this->Fields['templet']))
{
$filetag = MfTemplet($this->Fields['templet']);
if( !preg_match("#\/#", $filetag) ) $filetag = $GLOBALS['cfg_df_style'].'/'.$filetag;
}
else
{
$filetag = MfTemplet($this->TypeLink->TypeInfos["temparticle"]);
}
$tid = $this->Fields['typeid'];
$filetag = str_replace('{cid}', $cid,$filetag);
$filetag = str_replace('{tid}', $tid,$filetag);
$tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;

if($cid=='spec')
{
if( !empty($this->Fields['templet']) )
{
$tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;
}
else
{
$tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_spec.htm";
}
}
if(!file_exists($tmpfile))
{
$tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/".($cid=='spec' ? 'article_spec.htm' : 'article_default.htm');
}
if (!preg_match("#.htm$#", $tmpfile)) return FALSE;
return $tmpfile;
}

 

改成这样

 

  function GetTempletFile()    { global $cfg_basedir,$cfg_templets_dir,$cfg_df_style;        $cid = $this->ChannelUnit->ChannelInfos['nid'];        if(!empty($this->Fields['templet']))        {            $filetag = MfTemplet($this->Fields['templet']);            if( !preg_match("#\/#", $filetag) ) $filetag = $GLOBALS['cfg_df_style'].'/'.$filetag;        }        else        {            $filetag = MfTemplet($this->TypeLink->TypeInfos["temparticle"]);        }        $tid = $this->Fields['typeid'];        $filetag = str_replace('{cid}', $cid,$filetag);        $filetag = str_replace('{tid}', $tid,$filetag);        /*HEJIE_MODIFY 客户端 @www.jcodecraeer.com*/        if($this->mobileVisitFlag ){            header("Content-Type:text/xml; charset=GBK");            $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_detail_mobile.xml";            return $tmpfile;        }        $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;        if($cid=='spec')        {            if( !empty($this->Fields['templet']) )            {                $tmpfile = $cfg_basedir.$cfg_templets_dir.'/'.$filetag;            }            else            {                $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/article_spec.htm";            }        }        if(!file_exists($tmpfile))        {            $tmpfile = $cfg_basedir.$cfg_templets_dir."/{$cfg_df_style}/".($cid=='spec' ? 'article_spec.htm' : 'article_default.htm');        }        if (!preg_match("#.htm$#", $tmpfile)) return FALSE;        return $tmpfile;    }

红色部分为加入的代码。

最后是这个xml模版的写法了,这个模版基本上是按照一种约定来写的,跟客户端如何解析xml相关,这里只是给出一个我自己的例子

<?xml version="1.0" encoding="UTF-8"?>
<jcodecraeer>
<blog>
<id>{dede:field.id/}</id>
<title><![CDATA[{dede:field.title/}]]></title>
<author>{dede:field.authoranme/}</author>
<url>http://www.jcodecraeer.com{dede:field.arcurl/}</url>
<body>
<![CDATA[
<h4> {dede:field.title/} </h4>
<div>
<span style='color:#333333;font-size:12px;padding-right:15px;'>{dede:field.authoranme/}</span>
<span style='color:#333333;font-size:12px;padding-right:15px;'> {dede:field.pubdate function="MyDate('Y-m-d H:i:s',@me)"/}</span></div>
{dede:field.body /}
]]>
</body>
<pubDate>{dede:field.pubdate function="MyDate('Y-m-d H:i:s',@me)"/}</pubDate>
<commentCount>{dede:field.id runphp=’yes’}$dsql = new dedesql(false);$dsql -> SetQuery(“Select count(id) as c from dede_feedback where aid=”.@me);$row = $dsql -> getone();@me=$row['c'];{/dede:field.id}</commentCount>
</blog>
</jcodecraeer>

《为dede增加安卓客户端接口的基本流程.doc》

下载本文的Word格式文档,以方便收藏与打印。