当前位置: 移动技术网 > IT编程>开发语言>PHP > Thinkphp框架开发移动端接口(1)

Thinkphp框架开发移动端接口(1)

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了使用thinkphp框架开发移动端接口代码,给原生app提供api接口,具体内容如下

1. 使用tp框架时 放在common文件夹下文件名就叫function.php    

<?php
/**
 * created by zhangkx
 * email: zkx520tnhb@163.com
 * 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"
  ]
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网