作者 lyh

gx

... ... @@ -126,15 +126,13 @@ zFePUMXy1bFghAfzNKlrc5XgH4ixeeMh3cDtU97K
* @time :2023/5/8 9:47
* https://app.ayrshare.com/api/media/upload
*/
public function post_media_upload($data){
public function post_media_upload($data,$api_key){
$param = [
'file'=>$data['file'],//base64编码
'fileName'=>$data['file_name'],//生成图片名称
'description'=>$data['description'],//描述
];
$this->headers['Authorization'] = $this->headers['Authorization'].$data['profile_key'];
$this->headers['Authorization'] = $this->headers['Authorization'].$api_key;
$url = $this->path.'/api/media/upload';
return $this->http_click('posts',$url,$param,$this->headers);
return $this->http_click('post',$url,$param,$this->headers);
}
/**
... ...
... ... @@ -5,8 +5,12 @@ namespace App\Http\Controllers\Bside\AyrShare;
use App\Enums\Common\Code;
use App\Helper\AyrShare as AyrShareHelper;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Controllers\Bside\FileController;
use App\Http\Controllers\file\ImageController;
use App\Http\Logic\Bside\AyrShare\AyrReleaseLogic;
use App\Http\Logic\Bside\AyrShare\AyrShareLogic;
use App\Models\File\Image;
use App\Models\File\Image as ImageModel;
/**
* @name:社交发布
... ... @@ -14,12 +18,23 @@ use App\Http\Logic\Bside\AyrShare\AyrShareLogic;
class AyrReleaseController extends BaseController
{
/**
* @name :(获取发送数据详情)info
* @author :lyh
* @method :post
* @time :2023/5/10 14:57
*/
public function info(AyrReleaseLogic $ayrReleaseLogic){
$info = $ayrReleaseLogic->release_info();
$this->response('success',Code::SUCCESS,$info);
}
/**
* @name :(获取当前用户已绑定的社交链接)info
* @author :lyh
* @method :post
* @time :2023/5/9 16:00
*/
public function info(AyrShareLogic $ayrShareLogic){
public function share_info(AyrShareLogic $ayrShareLogic){
$info = $ayrShareLogic->ayr_share_info();
$this->response('success',Code::SUCCESS,$info);
}
... ... @@ -29,7 +44,7 @@ class AyrReleaseController extends BaseController
* @method :post
* @time :2023/5/9 9:36
*/
public function send_post(AyrReleaseLogic $ayrReleaseLogic,AyrShareLogic $ayrShareLogic){
public function send_post(AyrReleaseLogic $ayrReleaseLogic,AyrShareLogic $ayrShareLogic,AyrShareHelper $ayrShare){
// DB::beginTransaction();
// try {
//获取发送账号详情
... ... @@ -39,10 +54,8 @@ class AyrReleaseController extends BaseController
'files'=>$this->param['file'],
];
//参数处理
$image_data = $this->image_file_param($data);
$image_data = $ayrReleaseLogic->image_file_param($data);
$this->param['mediaUrls'] = array_merge($image_data['images_link'],$image_data['files_link']);
var_dump($this->param);
die();
//统一生成链接
$param = [
'post'=>$this->param['content'],
... ... @@ -50,8 +63,7 @@ class AyrReleaseController extends BaseController
'mediaUrls'=>$this->param['mediaUrls'],//参数处理
];
//发送请求注册社交用户
$ayrShareHelper = new AyrShareHelper();
$res = $ayrShareHelper->post_send_msg($param,$share_info['profile_key']);
$res = $ayrShare->post_send_msg($param,$share_info['profile_key']);
$this->response('success',Code::SUCCESS,$res);
//保存数据库
$ayrReleaseLogic->release_add();
... ... @@ -63,46 +75,50 @@ class AyrReleaseController extends BaseController
}
/**
* @name :(上传第三方参数参数)get_param
* @name :(图片上传到第三方平台)send_media
* @author :lyh
* @method :post
* @time :2023/5/10 10:27
* @time :2023/5/10 14:07
*/
public function image_file_param($data) {
if (empty($data) || !is_array($data)) {
return empty($data) ? is_array($data) ? [] : '' : $data;
public function send_media(AyrShareLogic $ayrShareLogic,AyrShareHelper $ayrShare){
$image_info = $ayrShareLogic->save_img_info($this->param['hash']);
if(empty($image_info['ayr_id'])){
//获取发送账号详情
$share_info = $ayrShareLogic->ayr_share_info();
//获取当前图片数据是否已上传到第三方
$arr = (new ImageController())->index($this->param['hash']);
//向第三方存储图片
$param = [
'file'=>($arr->original),//base64编码
];
$param_data = $ayrShare->post_media_upload($param,$share_info['profile_key']);
//更新图片库
$ayrShareLogic->save_img($param_data);
}
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->_extents($v);
} else {
if (is_null($v)) {
$data[$k] = '';
continue;
}
//获取操作人
switch ((string) $k) {
case 'image':
$data['image_link'] = url('/b/image/' . $v . '/2');
break;
case 'images':
$v = explode(',',$v);
foreach ($v as $k1=>$v1){
$data['images_link'][$k1] = url('/b/image/' . $v1 . '/2');
}
break;
case 'file':
$data['file_link'] = url('/b/file_hash/' . $v .'/mp4');
break;
case 'files':
$v = explode(',',$v);
foreach ($v as $k1=>$v1){
$data['files_link'][$k1] = url('/b/file_hash/' . $v1 . '/mp4');
}
break;
}
}
$this->response('success');
}
/**
* @name :(文件上传到第三方平台)send_media
* @author :lyh
* @method :post
* @time :2023/5/10 14:07
*/
public function send_media_file(AyrShareLogic $ayrShareLogic,AyrShareHelper $ayrShare){
$image_info = $ayrShareLogic->save_file_info($this->param['hash']);
if(empty($image_info['ayr_id'])){
//获取发送账号详情
$share_info = $ayrShareLogic->ayr_share_info();
//获取当前图片数据是否已上传到第三方
$arr = (new FileController())->index($this->param['hash']);
//向第三方存储图片
$param = [
'file'=>($arr->original),//base64编码
];
$param_data = $ayrShare->post_media_upload($param,$share_info['profile_key']);
//更新图片库
$ayrShareLogic->save_file($param_data);
}
return $data;
$this->response('success');
}
}
... ...
... ... @@ -33,7 +33,7 @@ class FileController
{
$this->request = request();
$this->config = config('filesystems.disks.upload');
$this->uploads = config('uploads.default_file');
$this->uploads = config('upload.default_file');
$this->path = $this->config['root'].$this->uploads['path'].'/';
}
... ... @@ -44,13 +44,15 @@ class FileController
* @method :post
* @time :2023/5/9 9:15
*/
public function index($hash = '',$type = 1){
public function index($hash = '', $type = 1)
{
// 检查是否有修改日期或ETag头部
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
header("HTTP/1.1 304 Not Modified");
exit;
}
$file_model = new File();
$info = $file_model->read(['hash'=>$hash]);
$info = $file_model->read(['hash' => $hash]);
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
... ... @@ -58,10 +60,54 @@ class FileController
if (!is_file($path)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
$size = $info['size'];
// 设置Content-Type头部
$header['Content-Type'] = 'video/' . $info['type'];
// 设置Accept-Ranges头部
$header['Accept-Ranges'] = 'bytes';
// 检查是否有范围请求
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE'];
$ranges = explode('-', substr($range, 6));
$start = intval($ranges[0]);
$end = $size - 1;
if (!empty($ranges[1])) {
$end = intval($ranges[1]);
}
$length = $end - $start + 1;
// 设置部分响应头部
$header['Content-Length'] = $length;
$header['Content-Range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
// 发送206 Partial Content状态码
header('HTTP/1.1 206 Partial Content');
header('Status: 206 Partial Content');
header('Accept-Ranges: bytes');
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $size);
// 读取部分内容并发送响应
$file = fopen($path, 'rb');
fseek($file, $start);
$buffer = 1024 * 8; // 设置缓冲区大小
while (!feof($file) && ($p = ftell($file)) <= $end) {
if ($p + $buffer > $end) {
// 最后一块缓冲区
$buffer = $end - $p + 1;
}
echo fread($file, $buffer);
flush(); // 将输出刷新到浏览器
}
fclose($file);
exit;
}
// 无范围请求,发送完整文件
$header['Content-Length'] = $size;
$content = file_get_contents($path);
$header['Content-Length'] = $info['size'];
$header['Content-Type'] = 'video/'.$info['type'];
return response($content, 200, $header);
// 发送完整响应
foreach ($header as $name => $value) {
header("$name: $value");
}
echo $content;
exit;
}
/**
* 图片上传
... ... @@ -73,6 +119,7 @@ class FileController
'file.required'=>'必须填写',
]);
$files = $this->request->file('file');
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
... ...
... ... @@ -4,6 +4,8 @@ namespace App\Http\Logic\Bside\AyrShare;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\AyrShare\AyrRelease;
use App\Models\File\File;
use App\Models\File\Image;
class AyrReleaseLogic extends BaseLogic
{
... ... @@ -17,7 +19,7 @@ class AyrReleaseLogic extends BaseLogic
}
/**
* @name :()release_info
* @name :(获取详情)release_info
* @author :lyh
* @method :post
* @time :2023/5/9 16:27
... ... @@ -42,20 +44,32 @@ class AyrReleaseLogic extends BaseLogic
}
return $this->success();
}
/**
* @name :(上传第三方平台)0ayr_release_add
* @name :(上传第三方图片参数处理)get_param
* @author :lyh
* @method :post
* @time :2023/5/9 10:02
* @time :2023/5/10 10:27
*/
public function ayr_release_add($param){
$param = [
'post'=>$this->param['content'],
'platforms'=>$this->param['platforms'],
'mediaUrls'=>$this->param['mediaUrls'],
];
return $this->success($param);
public function image_file_param($data) {
$arr = [];
foreach ($data as $k => $v){
if($k == 'images'){
$images = explode(',',$v);
$imageModel = new Image();
$list = $imageModel->list(['hash'=>['in',$images]],'id');
foreach ($list as $v1){
$arr[] = $v1['ayr_url'];
}
}else{
$files = explode(',',$v);
$filesModel = new File();
$list = $filesModel->list(['hash'=>['in',$files]],'id');
foreach ($list as $v2){
$arr[] = $v2['ayr_url'];
}
}
}
return $this->success($arr);
}
/**
... ...
... ... @@ -2,10 +2,10 @@
namespace App\Http\Logic\Bside\AyrShare;
use App\Helper\AyrShare as AyrShareHelper;
use App\Http\Controllers\file\ImageController;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\AyrShare\AyrShare;
use App\Models\File\File as FileModel;
use App\Models\File\Image as ImageModel;
class AyrShareLogic extends BaseLogic
{
... ... @@ -82,26 +82,76 @@ class AyrShareLogic extends BaseLogic
}
return $this->success();
}
/**
* @name :(更新图片库)save_info_info
* @author :lyh
* @method :post
* @time :2023/5/10 15:01
*/
public function save_img_info($hash){
$imageModel = new ImageModel();
$info = $imageModel->read(['hash'=>$hash]);
if($info === false){
$this->fail('error');
}
return $this->success();
}
/**
* @name :(向第三方平台发布帖子)api_public_msg
* @name :(更新图片库)save_info_info
* @author :lyh
* @method :post
* @time :2023/5/8 9:11
* @time :2023/5/10 15:01
*/
public function send_public_post($param){
//获取图片
$image_c = new ImageController();
$image_info = $image_c->index($param['hash']);
$param = [
'post'=>$param['text'],
'mediaUrls'=>$image_info,
'platforms'=>$this->param['platforms'],
public function save_file_info($hash){
$fileModel = new FileModel();
$info = $fileModel->read(['hash'=>$hash]);
if($info === false){
$this->fail('error');
}
return $this->success();
}
/**
* @name :(更新图片库)save_img
* @author :lyh
* @method :post
* @time :2023/5/10 15:01
*/
public function save_img($param_data){
//根据文件hash存入数据库
$ayr_data = [
'ayr_id'=>$param_data['id'],
'ayr_url'=>$param_data['url'],
'ayr_url_link'=>$param_data['url_1080']
];
$imageModel = new ImageModel();
$rs = $imageModel->edit($ayr_data,['hash'=>$this->param['hash']]);
if($rs === false){
$this->fail('更新失败');
}
return $this->success();
}
/**
* @name :(更新文件库)save_img
* @author :lyh
* @method :post
* @time :2023/5/10 15:01
*/
public function save_file($param_data){
//根据文件hash存入数据库
$ayr_data = [
'ayr_id'=>$param_data['id'],
'ayr_url'=>$param_data['url'],
'ayr_url_link'=>$param_data['url_1080']
];
//发布帖子
$ayrShareHelper = new AyrShareHelper();
$res = $ayrShareHelper->post_send_msg($param);
$this->success($res);
$imageModel = new FileModel();
$rs = $imageModel->edit($ayr_data,['hash'=>$this->param['hash']]);
if($rs === false){
$this->fail('更新失败');
}
return $this->success();
}
}
... ...
... ... @@ -24,7 +24,7 @@ return [
//默认视频
'default_file' =>[
'size' => [
'max' => 1024*1024*200, // 2M
'max' => 1024*1024*20, // 2M
],
'path' => '/file'
],
... ...
... ... @@ -218,8 +218,10 @@ Route::middleware(['bloginauth'])->group(function () {
Route::any('/bind', [\App\Http\Controllers\Bside\AyrShare\AyrShareController::class, 'bind_account'])->name('ayr_bind_account');
//社交绑定
Route::prefix('release')->group(function () {
Route::any('/', [\App\Http\Controllers\Bside\AyrShare\AyrReleaseController::class, 'info'])->name('ayr_release_info');
Route::any('/info', [\App\Http\Controllers\Bside\AyrShare\AyrReleaseController::class, 'info'])->name('ayr_release_info');
Route::any('/share_info', [\App\Http\Controllers\Bside\AyrShare\AyrReleaseController::class, 'share_info'])->name('ayr_release_share_info');
Route::any('/send', [\App\Http\Controllers\Bside\AyrShare\AyrReleaseController::class, 'send_post'])->name('ayr_release_send');
Route::any('/send_media', [\App\Http\Controllers\Bside\AyrShare\AyrReleaseController::class, 'send_media'])->name('ayr_release_send_media');
});
});
... ...