love_php/app/Jobs/SynchronizationWeChat.php

116 lines
3.7 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use App\Models\PayLog;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Log;
use Exception;
class SynchronizationWeChat implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $pics;
protected $content;
protected $url;
/**
* Create a new job instance.
*
* @return void
*/
public $tries = 3;
public $timeout = 30;
public function __construct($pics,$content,$url)
{
$this->pics = $pics;
$this->content = $content;
$this->url = $url;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$PayLog = new PayLog();
$PayLog->type = 'syn-begin';
$PayLog->trade_no = 'test';
$PayLog->remark = $this->url;
$PayLog->save();
$content_noencode = $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->pics[1] as $key => $value) {
$offset = strpos($value,'wx_fmt=');
$type = substr($value,$offset+7);
$rand = \CommonUtilsService::getTradeNO();
$object = $path . "/" . $rand.'.'. $type;
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
$return_content = $this->http_get_data($value);
$filename = storage_path("qrcode/".time().$rand.'.png');
$fp= @fopen($filename,"a"); //将文件绑定到流
if(!is_resource($fp)){
return;
}
fwrite($fp,$return_content); //写入文件
$ossClient->uploadFile(config('alioss.buckets.picture'), $object, $filename);
fclose($fp);
unlink($filename);
$file_url = 'src='.$file_url;
$data = str_replace($this->pics[0][$key],$file_url,$content_noencode);
$content_noencode = $data;
}
$PayLog = new PayLog();
$PayLog->type = 'syn-wechat';
$PayLog->trade_no = $this->url;
$PayLog->remark = $content_noencode;
$PayLog->save();
}
//接口返回失败
public function failure($msg, $data=[], $jsonp=false){
$result = [
'code'=> 1,
'message'=> $msg,
'data'=> $data,
];
return Response()->json($result);
}
public function failed(Exception $exception)
{
$PayLog = new PayLog();
$rand = 123;
$filename = storage_path("wechat/".time().$rand.'.png');
$PayLog->type = $filename;
$PayLog->trade_no = $this->url.'-fail';
$PayLog->remark = $exception;
$PayLog->save();
}
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;
}
}