如何使用tp开发webapp

发布网友 发布时间:2022-04-24 11:05

我来回答

1个回答

热心网友 时间:2023-10-10 18:34

方案一:给原生APP提供api接口

使用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 <a href="https://www.baidu.com/s?wd=json_encode&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9ryc1uyckrj-WmHN9Pj9b0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWbsnjm1PWcdnW04nWmLP1mY" target="_blank" class="-highlight">json_encode</a>($result);
}
  
/**
* @param null $msg 返回具体错误的提示信息
* @param flag success CURD 操作失败
* Function descript:返回标志信息 ‘Error',和提示信息的json 数组
*/
function returnApiError($msg = null){
$result = array(
'flag' => 'Error',
'msg' => $msg,
);
print <a href="https://www.baidu.com/s?wd=json_encode&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9ryc1uyckrj-WmHN9Pj9b0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWbsnjm1PWcdnW04nWmLP1mY" target="_blank" class="-highlight">json_encode</a>($result);
}
  
/**
* @param null $msg 返回具体错误的提示信息
* @param flag success CURD 操作失败
* Function descript:返回标志信息 ‘Error',和提示信息,当前系统繁忙,请稍后重试;
*/
function returnApiErrorExample(){
$result = array(
'flag' => 'Error',
'msg' => '当前系统繁忙,请稍后重试!',
);
print <a href="https://www.baidu.com/s?wd=json_encode&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9ryc1uyckrj-WmHN9Pj9b0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWbsnjm1PWcdnW04nWmLP1mY" target="_blank" class="-highlight">json_encode</a>($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;
}
}
    
 
查询单个果品详细信息
  
 
  
    
/**
* 发布模块
*
* 获取信息单个果品详细信息
*
*/
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'] = <a href="https://www.baidu.com/s?wd=mb_substr&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9ryc1uyckrj-WmHN9Pj9b0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWbsnjm1PWcdnW04nWmLP1mY" target="_blank" class="-highlight">mb_substr</a>($releaseInfo['remark'],0,49,'utf-8').'...';
//多张图地址按逗号截取字符串,截取后如果存在空数组则需要过滤掉
$releaseInfo['fruit_pic'] = <a href="https://www.baidu.com/s?wd=array_filter&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9ryc1uyckrj-WmHN9Pj9b0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWbsnjm1PWcdnW04nWmLP1mY" target="_blank" class="-highlight">array_filter</a>(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( '什么也没查到(+_+)!');
}
}
    
 
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;
}
    
 
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"
]
}
}
    
 
app端接收到的数据(原生json串)
代码如下:
 
{"flag":"success","message":"","responseList":{"id":"2","fruit_name":"\u82f9\u679c","high_price":"8.0","low_price":"5.0","address":"\u5929\u6d25\u5c0f\u767d\u697c\u6c34\u679c\u5e02\u573a","size":"2.0","weight":"2.0","remark":"\u6025\u9700...","fruit_pic":["http:\/\/fruit.txunda.com\/Uploads\/Release\/201508\/55599e7514815.png","http:\/\/fruit.txunda.com\/Uploads\/Release\/201508\/554f2dc45b526.jpg"]}}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com