本文實例為大家分享了使用Thinkphp框架開發移動端接口代碼,給原生APP提供api接口,具體內容如下
1. 使用TP框架時 放在common文件夾下文件名就叫function.php
<?php /** * Created by zhangkx * Email: [email protected] * Date: 2015/8/1 * Time: 23:15 */ /*************************** api開發輔助函數 **********************/ /** * @param null $msg 返回正確的提示信息 * @param flag success CURD 操作成功 * @param array $data 具體返回信息 * Function descript: 返回帶參數,標志信息,提示信息的json 數組 * */ function returnApiSuccess($msg = null,$data = array()){ $result = array( 'flag' => 'Success', 'msg' => $msg, 'data' =>$data ); print json_encode($result); } /** * @param null $msg 返回具體錯誤的提示信息 * @param flag success CURD 操作失敗 * Function descript:返回標志信息 ‘Error',和提示信息的json 數組 */ function returnApiError($msg = null){ $result = array( 'flag' => 'Error', 'msg' => $msg, ); print json_encode($result); } /** * @param null $msg 返回具體錯誤的提示信息 * @param flag success CURD 操作失敗 * Function descript:返回標志信息 ‘Error',和提示信息,當前系統繁忙,請稍後重試; */ function returnApiErrorExample(){ $result = array( 'flag' => 'Error', 'msg' => '當前系統繁忙,請稍後重試!', ); print json_encode($result); } /** * @param null $data * @return array|mixed|null * Function descript: 過濾post提交的參數; * */ function checkDataPost($data = null){ if(!empty($data)){ $data = explode(',',$data); foreach($data as $k=>$v){ if((!isset($_POST[$k]))||(empty($_POST[$k]))){ if($_POST[$k]!==0 && $_POST[$k]!=='0'){ returnApiError($k.'值為空!'); } } } unset($data); $data = I('post.'); unset($data['_URL_'],$data['token']); return $data; } } /** * @param null $data * @return array|mixed|null * Function descript: 過濾get提交的參數; * */ function checkDataGet($data = null){ if(!empty($data)){ $data = explode(',',$data); foreach($data as $k=>$v){ if((!isset($_GET[$k]))||(empty($_GET[$k]))){ if($_GET[$k]!==0 && $_GET[$k]!=='0'){ returnApiError($k.'值為空!'); } } } unset($data); $data = I('get.'); unset($data['_URL_'],$data['token']); return $data; } }
2. 查詢單個果品詳細信息
/** * 發布模塊 * * 獲取信息單個果品詳細信息 * */ public function getMyReleaseInfo(){ //檢查是否通過post方法得到數據 checkdataPost('id'); $where['id'] = $_POST['id']; $field[] = 'id,fruit_name,high_price,low_price,address,size,weight,fruit_pic,remark'; $releaseInfo = $this->release_obj->findRelease($where,$field); $releaseInfo['remark'] = mb_substr($releaseInfo['remark'],0,49,'utf-8').'...'; //多張圖地址按逗號截取字符串,截取後如果存在空數組則需要過濾掉 $releaseInfo['fruit_pic'] = array_filter(explode(',', $releaseInfo['fruit_pic'])); $fruit_pic = $releaseInfo['fruit_pic'];unset($releaseInfo['fruit_pic']); //為圖片添加存儲路徑 foreach($fruit_pic as $k=>$v ){ $releaseInfo['fruit_pic'][] = 'http://'.$_SERVER['HTTP_HOST'].'/Uploads/Release/'.$v; } if($releaseInfo){ returnApiSuccess('',$releaseInfo); }else{ returnApiError( '什麼也沒查到(+_+)!'); } }
3. findRelease() 方法的model
/** * 查詢一條數據 */ public function findRelease($where,$field){ if($where['status'] == '' || empty($where['status'])){ $where['status'] = array('neq','9'); } $result = $this->where($where)->field($field)->find(); return $result; }
4. app端接收到的數據(解碼json之後)
{ "flag": "success", "message": "", "responseList": { "id": "2", "fruit_name": "蘋果", "high_price": "8.0", "low_price": "5.0", "address": "天津小白樓水果市場", "size": "2.0", "weight": "2.0", "remark": "急需...", "fruit_pic": [ "http://fruit.txunda.com/Uploads/Release/201508/55599e7514815.png", "http://fruit.txunda.com/Uploads/Release/201508/554f2dc45b526.jpg" ] } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。