Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/Bean/OfficialAccount/MediaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use EasySwoole\HttpClient\Bean\Response;
use EasySwoole\Utility\Mime\MimeDetectorException;
use EasySwoole\Utility\MimeType;
use EasySwoole\WeChat\Exception\RequestError;
use EasySwoole\WeChat\Utility\NetWork;
use InvalidArgumentException;
use Swoole\Coroutine;

Expand Down Expand Up @@ -44,6 +46,22 @@ public function isJson(): bool
return false;
}


public function isVideo()
{
if (isset($this->httpResponse()->getHeaders()['content-type']) && $this->httpResponse()->getHeaders()['content-type'] === 'text/plain') {
$data = json_decode($this->getContent(), true);
if (isset($data['down_url'])) {
return $data['down_url'];
}
if (isset($data['video_url'])) {
return $data['video_url'];
}
}

return false;
}

public function isEmpty(): bool
{
return empty($this->getContent());
Expand All @@ -61,7 +79,7 @@ public function getContent(): string
* @return string
* @throws MimeDetectorException
*/
public function save(string $directory, string $filename = null)
public function save(string $directory, string $filename = null, int $timeout = 15)
{
$directory = rtrim($directory, '/');

Expand All @@ -73,18 +91,32 @@ public function save(string $directory, string $filename = null)
throw new InvalidArgumentException(sprintf("'%s' is not writable.", $directory));
}

if ($fileUrl = $this->isVideo()) {
NetWork::$TIMEOUT = $timeout;

$fileData = NetWork::get($fileUrl);

if (($fileData->getClient()->errCode)) {
throw new RequestError($fileData->getClient()->errMsg);
}

$this->httpResponse = $fileData;
}

$contents = $this->httpResponse()->getBody();

if (empty($filename)) {
if (preg_match('/filename="(?<filename>.*?)"/', $this->httpResponse()->getHeaders()['content-disposition'], $match)) {
if (isset($this->httpResponse()->getHeaders()['content-disposition']) && preg_match('/filename=["]{0,1}(?<filename>.*)/', $this->httpResponse()->getHeaders()['content-disposition'], $match)) {
$filename = $match['filename'];
} else {
$filename = md5($contents);
}
}

$filename = trim($filename, '"');

if (empty(pathinfo($filename, PATHINFO_EXTENSION))) {
$filename .= MimeType::getExtFromStream($contents);
$filename .= '.'.MimeType::getExtFromStream($contents);
}

Coroutine::writeFile($directory . '/' . $filename, $contents);
Expand Down
37 changes: 31 additions & 6 deletions src/Bean/OfficialAccount/RequestConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,40 @@ class RequestConst
*/
const MSG_TYPE_MPVIDEO = 'mpvideo';


/*
* 事件大小写不同,主要是微信自己本身事件大小写就不一样,不规范
*/
const EVENT_SUBSCRIBE = 'subscribe';
const EVENT_UNSUBSCRIBE = 'unsubscribe';
const EVENT_SCAN = 'SCAN';
const EVENT_LOCATION = 'LOCATION';
const EVENT_CLICK = 'CLICK';
const EVENT_VIEW = 'VIEW';
// 用户关注事件
const EVENT_SUBSCRIBE = 'subscribe';
// 用户取消关注事件
const EVENT_UNSUBSCRIBE = 'unsubscribe';
// 用户已关注事件
const EVENT_SCAN = 'SCAN';
// 上报地理位置事件
const EVENT_LOCATION = 'LOCATION';
// 点击自定义菜单拉取消息事件
const EVENT_CLICK = 'CLICK';
// 点击自定义菜单跳转事件
const EVENT_VIEW = 'VIEW';
// 发送模板消息通知事件
const EVENT_TEMPLATE_SEND_JOB_FINISH = 'TEMPLATESENDJOBFINISH';
// 群发结果事件
const EVENT_MASS_SEND_JOB_FINISH = 'MASSSENDJOBFINISH';
//扫码推事件的事件推送
const EVENT_SCANCODE_PUSH = 'scancode_push';
//扫码推事件且弹出“消息接收中”提示框的事件推送
const EVENT_SCANCODE_WAITMSG = 'scancode_waitmsg';
//弹出系统拍照发图的事件推送
const EVENT_PIC_SYSPHOTO = 'pic_sysphoto';
//弹出拍照或者相册发图的事件推送
const EVENT_PIC_PHOTO_OR_ALBUM = 'pic_photo_or_album';
//弹出微信相册发图器的事件推送
const EVENT_PIC_WEIXIN = 'pic_weixin';
//弹出地理位置选择器的事件推送
const EVENT_LOCATION_SELECT = 'location_select';
//点击菜单跳转小程序的事件推送
const EVENT_VIEW_MINIPROGRAM = 'view_miniprogram';


}
30 changes: 23 additions & 7 deletions src/Bean/OfficialAccount/TemplateMsg.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ class TemplateMsg extends SplBean
protected $url;
protected $miniprogram;
protected $scene;
protected $appid;
protected $pagepath;
protected $title;
protected $data = [];

/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}

/**
* @param string $title
*/
public function setTitle(string $title): void
{
$this->title = $title;
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -96,7 +112,7 @@ public function getMiniprogram()
/**
* @param mixed $miniprogram
*/
public function setMiniprogram($miniprogram): void
public function setMiniprogram(array $miniprogram): void
{
$this->miniprogram = $miniprogram;
}
Expand All @@ -106,31 +122,31 @@ public function setMiniprogram($miniprogram): void
*/
public function getAppid()
{
return $this->appid;
return $this->miniprogram['appid'];
}

/**
* @param mixed $appid
*/
public function setAppid($appid): void
{
$this->appid = $appid;
$this->miniprogram['appid'] = $appid;
}

/**
* @return mixed
*/
public function getPagepath()
{
return $this->pagepath;
return $this->miniprogram['pagepath'];
}

/**
* @param mixed $pagepath
*/
public function setPagepath($pagepath): void
{
$this->pagepath = $pagepath;
$this->miniprogram['pagepath'] = $pagepath;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/OfficialAccount/ApiUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ class ApiUrl
/*
* 获取临时素材
*/
const MEDIA_GET = 'https://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID';
const MEDIA_GET = 'https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID';

/*
* 获取高清语音素材
*/
const MEDIA_HD_GET = 'https://api.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=ACCESS_TOKEN&media_id=MEDIA_ID';

/*
* 上传临时素材
Expand Down
Loading