95 lines
3.2 KiB
PHP
95 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use App\Models\PayLog;
|
|
|
|
class UploadUrl implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
protected $imgSrcArr;
|
|
protected $content;
|
|
public $tries = 3;
|
|
public $timeout = 500;
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($content,$imgSrcArr)
|
|
{
|
|
$this->content = $content;
|
|
$this->imgSrcArr = $imgSrcArr;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
\Log::info('start transfer pic');
|
|
$content = $this->content;
|
|
$path = Request()->path ? Request()->path : date('Y') . date('m') . "/" . date('d');
|
|
require_once base_path('vendor/aliyuncs/oss-sdk-php') . '/autoload.php';
|
|
//连接aliyun oss server
|
|
try {
|
|
$ossClient = new \OSS\OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
|
|
} catch (\OSS\Core\OssException $e) {
|
|
return $this->failure('oss_connect_failure', $e->getMessage());
|
|
}
|
|
foreach ($this->imgSrcArr as $key => $img_url) {
|
|
$rand = \CommonUtilsService::getTradeNO();
|
|
$object = $path . "/" . $rand.".jpg";
|
|
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
|
|
$return_content = $this->http_get_data($img_url);
|
|
// $filename = "upload\.$rand.jpg";
|
|
// $filename = storage_path('qrcode/'.time().$rand.'.png');
|
|
$filename = storage_path("qrcode/".time().$rand.'.png');
|
|
$fp= @fopen($filename,"a"); //将文件绑定到流
|
|
fwrite($fp,$return_content); //写入文件
|
|
$ossClient->uploadFile(config('alioss.buckets.picture'), $object, $filename);
|
|
fclose($fp);
|
|
unlink($filename);
|
|
$data = str_replace($img_url,$file_url,$content); //将富文本链接替换成阿里云链接
|
|
$content = $data;
|
|
}
|
|
$content = str_replace('.jpg&','.jpg?',$content);
|
|
$payLog = new PayLog();
|
|
$payLog->type = 'copy_wechat';
|
|
$payLog->remark = $content;
|
|
$payLog->trade_no = substr($content,50,30);
|
|
$payLog->save();
|
|
\Log::info('end transfer pic');
|
|
}
|
|
|
|
public function http_get_data($url) {
|
|
$ch = curl_init ();
|
|
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
|
|
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
|
|
curl_setopt ( $ch, CURLOPT_URL, $url );
|
|
ob_start ();
|
|
curl_exec ( $ch );
|
|
$return_content = ob_get_contents ();
|
|
ob_end_clean ();
|
|
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE );
|
|
return $return_content;
|
|
}
|
|
|
|
//接口返回失败
|
|
public function failure($msg, $data=[], $jsonp=false){
|
|
$result = [
|
|
'code'=> 1,
|
|
'message'=> $msg,
|
|
'data'=> $data,
|
|
];
|
|
return Response()->json($result);
|
|
}
|
|
}
|