二、出參規(guī)范:
1.查詢:
返回參數(shù)統(tǒng)一按如下接口形式,其中cnt為ad在數(shù)據(jù)庫(kù)中的總數(shù)目,如下面數(shù)據(jù),ad下的list總數(shù)為2,area下的list總數(shù)為3。當(dāng)接口有錯(cuò)誤返回時(shí),code大于1,且msg中含有錯(cuò)誤信息,當(dāng)無(wú)錯(cuò)誤時(shí),code為1,且msg為空。
當(dāng)接口傳入當(dāng)前頁(yè)參數(shù)p ,當(dāng)前頁(yè)數(shù)據(jù)條數(shù)ps時(shí),接口返回時(shí),同樣也會(huì)反給客戶端p與ps參數(shù)。
如果有極端情況,即同一接口返回多個(gè)list,則接口分頁(yè)入?yún)⒍x為“p+下劃線+當(dāng)前l(fā)ist的下標(biāo)”,如下接口,ad下的list中,當(dāng)前頁(yè)的輸入?yún)?shù)為p_ad,ad的每頁(yè)條數(shù)輸入?yún)?shù)為ps_ad,即post或get中分頁(yè)參數(shù)為p_ad=1&ps_ad=20&p_area=1&ps_area=20
cnt不是當(dāng)前l(fā)ist返回的條數(shù),而是數(shù)據(jù)庫(kù)中,該list的總條數(shù),用于分頁(yè)。
json格式示例:
{
“code”:1,
“msg”:“”,
“ad”:{
“cnt”:22,
“p”:2,
“ps”:20,
“l(fā)ist”:[{
“adv_title”:“\u8fd9\u662f\u5e7f\u544a\u6807\u98981”,
“adv_link”:“m.xxxxxx.cn\/advert\/tbsync\/”,
“open_target”:“2”,
“img_url”:“www.xxxxxx.cn\\\/data\\\/upload\\\/shop\\\/adv\\\/04825861373036910.png”
},
{
“adv_title”:“\u8fd9\u662f\u5e7f\u544a\u6807\u98982”,
“adv_link”:“m.xxxxxx.cn\\\/advert\\\/newerDerate\\\/?430”,
“open_target”:“2”,
“img_url”:“www.xxxxxx.cn\\\/data\\\/upload\\\/shop\\\/adv\\\/04782825931526321.png”
}
]
},
“area”:{
“cnt”:3,
“p”:1,
“ps”:20,
“l(fā)ist”:[{
“c_area_id”:“16”,“c_area_name”:“\u6df1\u5733”,
“c_area_pic”:“04732773115385188.jpg”,
“english_name”:“shenzhen”,“goodsCount”:“21”,
“img_url”:“www.xxxxxx.cn\\\/data\\\/upload\\\/shop\\\/adv\\\/04825861373036910.png”
},
{
“c_area_id”:“18”,
“c_area_name”:“\u6e56\u5357”,
“c_area_pic”:“04732773115385188.jpg”,
“english_name”:“hunan”,
“goodsCount”:“33”,
“img_url”:“www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png”
},
{
“c_area_id”:“2”,
“c_area_name”:“\u5317\u4eac”,
“c_area_pic”:“04732773115385188.jpg”,
“english_name”:“beijing”,“goodsCount”:“70”,
“img_url”:“www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png”
}
]
},
“goods_info”:{
“cnt”:1,
“p”:1,
“ps”:20,
“l(fā)ist”:[{
“goods_id”:“211”,
“goods_name”:“\u5927\u4fdd\u5065\u5305\u591c”,
“goods_price”:“1000.00”,
“main_image”:“www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png”
}]
}
}
以php為示例,其中php中數(shù)組拼接格式為:
$array_data = array(
“code” =》 1,
“msg” =》 ‘’,
“ad” =》 array(
“cnt” =》 22,
“p” =》 2,
“ps” =》 20,
“l(fā)ist” =》 array(
0 =》 array(
“adv_title” =》 “這是廣告標(biāo)題1”,
“adv_link” =》 “m.xxxxxx.cn/advert/tbsync/”,
“open_target” =》 “2”,
“img_url” =》 “www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png”
),
1 =》 array(
“adv_title” =》 “這是廣告標(biāo)題2”,
“adv_link” =》 “m.xxxxxx.cn\/advert\/newerDerate\/?430”,
“open_target” =》 “2”,
“img_url” =》 “www.xxxxxx.cn\/data\/upload\/shop\/adv\/04782825931526321.png”
)
)
),
“area” =》 array(
“cnt” =》 3,
“p” =》 1,
“ps” =》 20,
“l(fā)ist” =》 array(
0 =》 array(
“c_area_id” =》 “16”,
“c_area_name” =》 “深圳”,
“c_area_pic” =》 “04732773115385188.jpg”,
“english_name” =》 “shenzhen”,
“goodsCount” =》 “21”,
“img_url” =》 “www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png”
),
1 =》 array(
“c_area_id” =》 “18”,
“c_area_name” =》 “湖南”,
“c_area_pic” =》 “04732773115385188.jpg”,
“english_name” =》 “hunan”,
“goodsCount” =》 “33”,
“img_url” =》 “www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png”
),
2 =》 array(
“c_area_id” =》 “2”,
“c_area_name” =》 “北京”,
“c_area_pic” =》 “04732773115385188.jpg”,
“english_name” =》 “beijing”,
“goodsCount” =》 “70”,
“img_url” =》 “www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png”
),
)
),
“goods_info” =》 array(
“cnt” =》 1,
“p” =》 1,
“ps” =》 20,
“l(fā)ist” =》 array(
0 =》 array(
“goods_id” =》 “211”,
“goods_name” =》 “大保健包夜”,
“goods_price” =》 “1000.00”,
“main_image” =》 “www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png”
)
)
)
);
接口盡量按一個(gè)頁(yè)面一個(gè)接口,但是公用的一些接口信息,如地區(qū)分類信息等,做單獨(dú)接口處理,但是結(jié)果參數(shù)規(guī)范,按照上面數(shù)據(jù)格式返回?cái)?shù)據(jù)。
返回的xml數(shù)據(jù)格式,如下:
《?xml version=“1.0” encoding=“utf-8”?》
《data》
《code》1《/code》
《msg/》
《ad》
《cnt》22《/cnt》
《p》2《/p》
《ps》20《/ps》
《list》
《article》
《advtitle》這是廣告標(biāo)題1《/advtitle》
《advlink》m.xxxxxx.cn/advert/tbsync/《/advlink》
《opentarget》2《/opentarget》
《imgurl》www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png《/imgurl》
《/article》
《article》
《advtitle》這是廣告標(biāo)題2《/advtitle》
《advlink》m.xxxxxx.cn\/advert\/newerDerate\/?430《/advlink》
《opentarget》2《/opentarget》
《imgurl》www.xxxxxx.cn\/data\/upload\/shop\/adv\/04782825931526321.png《/imgurl》
《/article》
《/list》
《/ad》
《area》
《cnt》3《/cnt》
《p》1《/p》
《ps》20《/ps》
《list》
《article》
《careaid》16《/careaid》
《careaname》深圳《/careaname》
《careapic》04732773115385188.jpg《/careapic》
《englishname》shenzhen《/englishname》
《goodsCount》21《/goodsCount》
《imgurl》www.xxxxxx.cn\/data\/upload\/shop\/adv\/04825861373036910.png《/imgurl》
《/article》
《article》
《careaid》18《/careaid》
《careaname》湖南《/careaname》
《careapic》04732773115385188.jpg《/careapic》
《englishname》hunan《/englishname》
《goodsCount》33《/goodsCount》
《imgurl》www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png《/imgurl》
《/article》
《article》
《careaid》2《/careaid》
《careaname》北京《/careaname》
《careapic》04732773115385188.jpg《/careapic》
《englishname》beijing《/englishname》
《goodsCount》70《/goodsCount》
《imgurl》www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png《/imgurl》
《/article》
《/list》
《/area》
《goodsinfo》
《cnt》1《/cnt》
《p》1《/p》
《ps》20《/ps》
《list》
《article》
《goodsid》211《/goodsid》
《goodsname》大保健包夜《/goodsname》
《goodsprice》1000.00《/goodsprice》
《mainimage》www.xxxxxx.cn/data/upload/shop/adv/04825861373036910.png《/mainimage》
《/article》
《/list》
《/goodsinfo》
《/data》
其中l(wèi)ist中的數(shù)字下標(biāo)用article代替,附php版本自定義的遞歸數(shù)組轉(zhuǎn)xml函數(shù):
function ArrtoXml($data, $rootNodeName = ‘data’, $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don‘t.
if (ini_get(’zend.ze1_compatibility_mode‘) == 1)
{
ini_set (’zend.ze1_compatibility_mode‘, 0);
}
if ($xml == null)
{
$xml = simplexml_load_string(“《?xml version=’1.0‘ encoding=’utf-8‘?》《$rootNodeName /》”);
}
// loop through the data passed in.
foreach($data as $key =》 $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key.。.
$key = “article”。 (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace(’/[^a-z]/i‘, ’‘, $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml-》addChild($key);
// recrusive call.
ArrtoXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml-》addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml-》asXML();
}
注意事項(xiàng):
當(dāng)返回js時(shí),即data_type=js,在json串前加入 RESULT=,即格式為RESULT={json串}。
當(dāng)返回html時(shí),即data_type=html,在返回的字符串中加入html頭部,即DOCTYPE、HEAD、BODY等信息。
2.添加,修改,刪除
當(dāng)調(diào)用增刪改數(shù)據(jù)接口時(shí),code為1時(shí)表操作成功,當(dāng)code大于1時(shí),表示操作失敗,并在msg中返回錯(cuò)誤信息,如下:
{
“code”:101,
“msg”:“操作參數(shù)丟失!”
}
當(dāng)然,若增刪改后仍然需要返回響應(yīng)數(shù)據(jù),格式同查詢。
評(píng)論