<?php
namespace App\Http\Controllers;
引入所需的类 资源
class WechatController extends Controller
{
public $appId;
public $secret;
public $token;
public $encodingAESKey;
public function getAppID()
{
return $this->appId=config('wxconfig.appId');
}
public function getSecret()
{
return $this->secret=config('wxconfig.secret');
}
public function getToken()
{
return $this->token=config('wxconfig.TOKEN');
}
public function getEncodingAESKey()
{
return $this->encodingAESKey=config('wxconfig.encodeingAESKey');
}
/*
* 微信入口
*/
public function wechatserver()
{
$server = new Server($this->getAppID(), $this->getToken(), $this->getEncodingAESKey());
// 只监听指定类型事件
$server->on('event', 'subscribe', function ($event) {
$eventKey = $event['EventKey'];
$ticket = $event['Ticket'];
$openid = $event['FromUserName'];
});
// 已经关注公众号 扫码
$server->on('event', 'SCAN', function ($event) {
$meeting_id = $event['EventKey'];
$ticket = $event['Ticket'];
$openid = $event['FromUserName'];
});
// 只监听指定类型事件
$server->on('event', 'unsubscribe', function ($event) {
});
// 监听所有类型
$server->on('message', 'text', function ($message) {
return Message::make('text')->content(config('msgconfig.AUTO_PUSH_MSG'));
});
// // 监听所有类型
// $server->on('message', function ($message) {
// });
// 监听指定类型
$server->on('message', 'image', function ($message) {
// $message = file_get_contents('php://input');
// MailController::imageMessage($message);
return Message::make('text')->content(config('msgconfig.AUTO_PUSH_MSG'));
});
$result = $server->serve();
echo $result;
}
public function pushTempMessage($meeting_id, $openid)
{
//自己的逻辑
return $this->pushNoMeeting();
}
public function pushWecolme()
{
return Message::make('text')->content(config('msgconfig.PUSH_WELCOME_MSG'));
}
public function pushNoMeeting()
{
return Message::make('text')->content(config('msgconfig.PUSH_No_Meeting'));
}
public function addWechatUser($openid)
{
// Log::info('START -- FUNCTION----addWechatUser');
$userService = new Userwechat($this->getAppID(), $this->getSecret());
$userwedhat = $userService->get($openid);
if ($userwedhat) {
$headimgurl = $this->saveMedia($userwedhat['headimgurl']);
//自己的逻辑
//比如更改用户状态等信息
}
return 1;
}
public function saveMedia($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$package = curl_exec($ch);
$httpinfo = curl_getinfo($ch);
curl_close($ch);
$media = array_merge(array('mediaBody' => $package), $httpinfo);
preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
$fileExt = $extmatches[1];
$filename = time() . rand(100, 999) . ".{$fileExt}";
$dirname = "images/photo/";
if (!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
file_put_contents($dirname . $filename, $media['mediaBody']);
return $dirname . $filename;
}
}
微信使用整理2
最新推荐文章于 2021-06-21 10:49:23 发布