## zzzphp对接微信小程序,百度小程序等教程
**说在前面:你首先已经会做小程序了,并且熟悉zzzphp,需要对接微信小程序,阅读下面的内容才会有帮助的**
1.首先确定自己小程序的ajax方法,后面有代码示例可以参考。
2.想好自己需要调用那些内容,需要自己会一点php,后面有示例代码。
3.做好服务器配置,php配置,域名白名单等基础配置。
小程序的ajax方法,示例中的/utils/ajax.js
```
function _param(obj = {}) {
let _ = encodeURIComponent
return Object.keys(obj).map(k => `${_(k)}=${_(obj[k])}`).join('&')
}
function ajax({url, query, data, success, fail, complete}) {
url = 'https://xxxxxx.com/wxapi/?act='+ url + _param(query)
wx.request({
url: url,
data: data,
method: 'POST',
header: { "Content-Type": "application/x-www-form-urlencoded"},
success: success,
fail: fail,
complete: complete
})
}
module.exports = ajax
```
小程序使用方法,实例中的/pages/index/index/js
```
let ajax = require('../../utils/ajax')
```
首页调用网站中的推荐产品,支持下拉加载更多
```
ajax({
url: 'contentlist',
data: { 'type': 'product', 'ispic': '1', 'num': '10', 'page': ++this.data.page},
success: res => {
if (res.data.length < this.data.num) {
loadedEnd: true
}
var imgalist = this.data.imgalist;
for (var index in res.data) {
imgalist.push(app.globalData.weburl + res.data[index].c_pic)
}
this.setData({
list: this.data.list.concat(res.data),
imgalist: imgalist
})
},
complete: _ => {
wx.hideToast()
}
})
```
网站后台建立wxapi/index.php文件。
```
require '../inc/zzz_class.php';
$act=safe_word(getform('act','get'));
switch ($act) {
case'contentlist':contentlist();break;
}
function contentlist(){
$where=array('c_onoff'=>1);
$type=safe_key(getform('act','post'));if($type) arr_add($where,'c_type',$type);
$brand=safe_key(getform('brand','post'));if($brand) arr_add($where,'c_brand',toutf($brand)) ;
$sid=safe_key(getform('sid','post'));if($sid) arr_add($where,'c_sid',$sid);
$id=safe_key(getform('id','post'));if($id) arr_add($where,'cid',splits($id)) ;
$ispic=safe_key(getform('ispic','post'));if($ispic) arr_add($where,'ispic',$ispic);
$istop=safe_key(getform('istop','post'));if($istop) arr_add($where,'istop',$istop);
$order=safe_key(getform('order','post'));
$num=safe_key(getform('num','post','num',conf('pagesize')));
$page=safe_key(getform('page','post','num',1));
$col=safe_key(getform('col','post'));$col = empty($col) ? 'cid,c_title,c_pic,c_addtime,c_pagedesc,zprice' :$col ;
$asc=safe_key(getform('asc','post','','asc'));
$desc=$asc=='asc' ? 'desc' : 'asc';
$order=array('istop'=>$desc,'isgood'=>$desc,'c_order'=>$asc,'c_addtime'=>$desc,'cid'=>$desc);
$data=db_load('content',$where,$col,$num,$order,$page);
echo tojson(array('code'=>1,'msg'=>'成功','where'=>$where,'data'=>($data)));
}
```
## 示例附件
[zzzphp+微信小程序示例](http://115.29.55.18/zzzphp+wxapp.zip)