微信小程序简单教程3
二十三 关闭应用调试
打开 application/config.php 配置文件
'app_debug' => Env::get('app.debug', false),
二十四 编辑微信接口
/**
* 中药一览
* @ApiTitle (中药一览)
* @ApiMethod (POST)
*/
public function medic_list()
{
$name = $this->request->post("name");
//$request = Config::get('upload');//static固定方法
//$medic = new Medic();//非固定方法需先实例化对象
//$request = $medic->index('name',$name);
$data = Db::table('medic')->where('name', $name)->find();//单条指定数据
$this->success('success', $data);
}
二十五 测试接口
php think api --force=true
https://访问域名/api.html
https://admin.huchangyi.com/api/api_wechat/medic_list
二十六 小程序中调用接口wx.request
self = this;
wx.request({
url: 'https://admin.huchangyi.com/api/api_wechat/medic_list',
method: 'POST',
data: {
'name': name,
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
//console.log(res.data),
json = res.data.data,
json = JSON.stringify(json),
json = JSON.parse(json),
console.log(json);
if(json!=null){
self.setData({
inputValue: e.detail.value,
name:json['name'],
class:json['class'],
alias:json['alias'],
form:json['form'],
habitat:json['habitat'],
mining:json['mining'],
chemical:json['chemical'],
taste:json['taste'],
effect:json['effect'],
apply:json['apply'],
mix:json['mix'],
pinyin:json['pinyin'],
phr:json['phr'],
});
}
else{
self.setData({
inputValue: e.detail.value,
name:'无匹配中药名称,请重新输入',
class:'',
alias:'',
form:'',
habitat:'',
mining:'',
chemical:'',
taste:'',
effect:'',
apply:'',
mix:'',
pinyin:'',
phr:'',
});
}
},
fail (errMsg) {
console.log(errMsg.data)
}
});
评论已关闭