public function getAccessToken()
{
//小程序appid
$appId = 'wxf6bce3a3c80e8602';
//秘钥
$appSecret = 'cf58ad3c4ab89020b47d22cb9cedf6f8';
$r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?
grant_type=client_credential&appid=$appId&secret=$appSecret"); //返回的是字符串,需要用json_decode转换成数组
$data = json_decode($r, true);
return $data['access_token'];
}
//接收下单数据接口
//$door上门时间,$name联系人姓名,$tel联系人电话,$address地址,$detailed详细地址,$type废品类型,$welfare公益赠送
public function purchase(){
$data=input('post.');
$rows=model('user')->where('city','=',$data['address'])->find();
//判断用户提交订单的城市有没有开通服务
if($rows==null){
return json(['code'=>402,'msg'=>'此地区暂未开通服务']);
}
$token = $this->getAccessToken();
//接收消息的用户openid
$openid = $rows['openid'];
//定义模板消息
$datas=[
'thing4'=>[
'value'=>input('type')
],
'name7'=>[
'value'=>input('name')
],
'phone_number9'=>[
'value'=>input('tel')
],
'thing10'=>[
'value'=>input('detailed')
],
'date2'=>[
'value'=>input('door')
],
];
$postData = [
'access_token'=>$token,
'touser'=>$openid,
//模板id
'template_id'=>'z0lL9PBe3s3DLMxN4vgCFCbFMyYwcjuQx92hQdlH5Xo',
'data'=>$datas,
];
$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=$token";
// halt($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData,320));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen(json_encode($postData,320))
)
);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data['create_time']=time();
$row=model('Purchase')->insert($data);
return json(['code'=>200]);
}
08-07
3683
12-19