方案一:给原生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 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;
}
}
查询单个果品详细信息
/**
* 发布模块
*
* 获取信息单个果品详细信息
*
*/
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( '什么也没查到(+_+)!');
}
}
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":"u82f9u679c","high_price":"8.0","low_price":"5.0","address":"u5929u6d25u5c0fu767du697cu6c34u679cu5e02u573a","size":"2.0","weight":"2.0","remark":"u6025u9700...","fruit_pic":["http://fruit.txunda.com/uploads/release/201508/55599e7514815.png","http://fruit.txunda.com/uploads/release/201508/554f2dc45b526.jpg"]}}
方案二:另外我们还可以通过thinkphp实现移动端访问自动切换主题模板,这样也可以做到移动端访问
thinkphp的模板主题机制,如果只是在pc,只要需修改 default_theme (新版模板主题默认是空,表示不启用模板主题功能)配置项就可以方便的实现多模板主题切换。
但对于移动端和pc端,也许你会设计完全不同的主题风格,且针对不同的来路提供不同的渲染方式,其中一种比较流行的方法是“响应式设计”,但就本人经历而言,要实现完全的“响应式设计”并不是那么容易,且解决兼容问题也是个难题,假设是大型站点,比如:淘宝、百度、拍拍这些,响应式设计肯定是满足不了需求的,而是需要针对手机访问用户提供单独的手机网站。
thinkphp 完全可以实现,而且相当简单。和tpm的智能模版切换引擎一样,只要对来路进行判断处理即可。
一、将 ismobile() 加入到{项目/common/common.php}
function ismobile() {
// 如果有http_x_wap_profile则一定是移动设备
if (isset ($_server['http_x_wap_profile']))
return true;
//此条摘自tpm智能切换模板引擎,适合tpm开发
if(isset ($_server['http_client']) &&'phoneclient'==$_server['http_client'])
return true;
//如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset ($_server['http_via']))
//找不到为flase,否则为true
return stristr($_server['http_via'], 'wap') ? true : false;
//判断手机发送的客户端标志,兼容性有待提高
if (isset ($_server['http_user_agent'])) {
$clientkeywords = array(
'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
);
//从http_user_agent中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_server['http_user_agent']))) {
return true;
}
}
//协议法,因为有可能不准确,放到最后判断
if (isset ($_server['http_accept'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_server['http_accept'], 'vnd.wap.wml') !== false) && (strpos($_server['http_accept'], 'text/html') === false || (strpos($_server['http_accept'], 'vnd.wap.wml') < strpos($_server['http_accept'], 'text/html')))) {
return true;
}
}
return false;
}
二、在{项目/lib/}创建一个 commonaction.php,如果你的项目已公共控制器,则无需创建,直接加在里面即可。
class commonaction extends action{
public function _initialize(){
//移动设备浏览,则切换模板
if (ismobile()) {
//设置默认默认主题为 mobile
c('default_theme','mobile');
}
//............你的更多代码.......
}
}
通过以上2种方式均可实现移动端访问,一种是原生,一种是伪原生,小伙伴们根据自己的项目需求来选择吧。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!