|
@@ -361,5 +361,71 @@ class Common extends Api
|
|
|
curl_close($curl);
|
|
|
return $res;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送微信模板消息
|
|
|
+ * @param $order_id
|
|
|
+ * @param $template
|
|
|
+ * @return false|string|\think\response\Json
|
|
|
+ */
|
|
|
+ public function sendWechatMessage($openid,$template_id,$data,$url)
|
|
|
+ {
|
|
|
+ $data = array(
|
|
|
+ "touser" => $openid, //openid
|
|
|
+ "template_id" => $template_id, //模板id
|
|
|
+ "url" => $url,
|
|
|
+ "data" => $data //模板数据
|
|
|
+ );
|
|
|
+ //下单用户发送
|
|
|
+ $access_token = $this->access_token();
|
|
|
+ if ($access_token == 400){
|
|
|
+ return ['code'=>0,'message'=>'获取access_token失败'];
|
|
|
+ }
|
|
|
+ $urls = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
|
|
|
+ $result = $this->http_post_json($urls,json_encode($data));//发送请求
|
|
|
+ if($result[0] == 200){
|
|
|
+ return ['code'=>1,'data'=>'推送成功'];
|
|
|
+ }
|
|
|
+ return ['code'=>0,'message'=>$result[1]['errmsg']];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function http_post_json($url, $jsonStr)
|
|
|
+ {
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
|
+ 'Content-Type: application/json; charset=utf-8',
|
|
|
+ 'Content-Length: ' . strlen($jsonStr)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ $response = curl_exec($ch);
|
|
|
+ $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
+ curl_close($ch);
|
|
|
+ return [$httpCode,$response];
|
|
|
+ // return json_encode(array($httpCode, $response),true);
|
|
|
+ //"[200,"{\"errcode\":0,\"errmsg\":\"ok\",\"msgid\":2696398438942572550}"]"
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取access_token
|
|
|
+ public function access_token(){
|
|
|
+ $appId = 'wx5e111b790c719cae';
|
|
|
+ $appSecret = 'a4e2a6d2a4fd06bd9e4ba279fd95e2d8';
|
|
|
+ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
|
|
|
+ $ch = curl_init();//初始化curl
|
|
|
+ curl_setopt($ch, CURLOPT_URL,$url); //要访问的地址
|
|
|
+ curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
|
+ $data = json_decode(curl_exec($ch),true);
|
|
|
+ if(curl_errno($ch)){
|
|
|
+ var_dump(curl_error($ch)); //打印错误信息
|
|
|
+ }
|
|
|
+ curl_close($ch);//关闭curl
|
|
|
+ return $data['access_token'];
|
|
|
+ }
|
|
|
|
|
|
}
|