|
@@ -6,6 +6,7 @@ use App\Models\Meta;
|
|
|
use BlockMatrix\EosRpc\ChainFactory;
|
|
|
use BlockMatrix\EosRpc\EosRpc;
|
|
|
use BlockMatrix\EosRpc\WalletFactory;
|
|
|
+use Carbon\Carbon;
|
|
|
use GuzzleHttp\Client;
|
|
|
use Illuminate\Console\Command;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -51,87 +52,100 @@ class Trace extends Command
|
|
|
public function handle()
|
|
|
{
|
|
|
$this->line('-----------traceing---------');
|
|
|
- $metas = Meta::where('status',0)->get();
|
|
|
- foreach($metas as $meta){
|
|
|
+ $metas = Meta::where('status', 0)->get();
|
|
|
+ foreach ($metas as $meta) {
|
|
|
+ $this->line('------开始:' . $meta['patch_num']);
|
|
|
$this->save_dir = storage_path($meta['patch_num']);
|
|
|
- if(!is_dir($this->save_dir)){
|
|
|
+ if (!is_dir($this->save_dir)) {
|
|
|
mkdir($this->save_dir);
|
|
|
}
|
|
|
- $files = [];
|
|
|
//1.生成txt
|
|
|
- $myfile = fopen($this->save_dir."/批次信息.txt", "w");
|
|
|
- $txt = "批次号:{$meta['patch_num']}\n";
|
|
|
+ $this->line('------开始生成txt:' . $meta['patch_num']);
|
|
|
+ $myfile = fopen($this->save_dir . "/批次信息.txt", "w");
|
|
|
+ $txt = "批次号:{$meta['patch_num']}\n";
|
|
|
$txt .= "鸡种:{$meta['variety']}\n";
|
|
|
$txt .= "年龄:{$meta['age']}\n";
|
|
|
$txt .= "口粮:{$meta['food']}\n";
|
|
|
$txt .= "水源:{$meta['water']}\n";
|
|
|
fwrite($myfile, $txt);
|
|
|
fclose($myfile);
|
|
|
- array_push($files,$this->save_dir."/批次信息.txt");
|
|
|
//2.生成质检信息
|
|
|
- $myfile = fopen($this->save_dir."/质检信息.html", "w");
|
|
|
- $txt = $meta['report'];
|
|
|
+ $this->line('------开始生成质检信息:' . $meta['patch_num']);
|
|
|
+ $myfile = fopen($this->save_dir . "/质检信息.html", "w");
|
|
|
+ $txt = $meta['report'];
|
|
|
fwrite($myfile, $txt);
|
|
|
fclose($myfile);
|
|
|
- array_push($files,$this->save_dir."/质检信息.html");
|
|
|
- $varietyImg = $this->downloadImg($this->save_dir,$meta['variety_img'],'鸡种');
|
|
|
- array_push($files,$varietyImg);
|
|
|
- $foodImg = $this->downloadImg($this->save_dir,$meta['food_img'],'口粮');
|
|
|
- array_push($files,$foodImg);
|
|
|
- $waterImg = $this->downloadImg($this->save_dir,$meta['water_img'],'水源');
|
|
|
- array_push($files,$waterImg);
|
|
|
- $farms = \GuzzleHttp\json_decode($meta['farm'],true);
|
|
|
- foreach ($farms as $k=>$v){
|
|
|
- $farmImg = $this->downloadImg($this->save_dir,$v,'农场'.($k+1));
|
|
|
- array_push($files,$farmImg);
|
|
|
+ $this->line('------开始下载图片:' . $meta['patch_num']);
|
|
|
+ $varietyImg = $this->downloadImg($this->save_dir, $meta['variety_img'], '鸡种');
|
|
|
+ $foodImg = $this->downloadImg($this->save_dir, $meta['food_img'], '口粮');
|
|
|
+ $waterImg = $this->downloadImg($this->save_dir, $meta['water_img'], '水源');
|
|
|
+ $farms = \GuzzleHttp\json_decode($meta['farm'], true);
|
|
|
+ foreach ($farms as $k => $v) {
|
|
|
+ $farmImg = $this->downloadImg($this->save_dir, $v, '农场' . ($k + 1));
|
|
|
}
|
|
|
//生成zip包
|
|
|
- $zipPath = storage_path().'/'.$meta['patch_num'].".zip";
|
|
|
+ $this->line('------开始生成ZIP:' . $meta['patch_num']);
|
|
|
+ $zipPath = storage_path() . '/' . $meta['patch_num'] . ".zip";
|
|
|
$zip = Zip::create($zipPath);
|
|
|
- $zip->add($this->save_dir,true);
|
|
|
+ $zip->add($this->save_dir, true);
|
|
|
$zip->close();
|
|
|
$md5 = md5_file($zipPath);
|
|
|
- $this->line($zipPath.'ZIP包MD5码:'.$md5);
|
|
|
+ $this->line('------ZIP包MD5码:' . $md5 . ',地址:' . $zipPath);
|
|
|
|
|
|
- $zipUrl = Storage::put('/trace/'.date('Ym'), file_get_contents($zipPath));
|
|
|
- //Meta::where('patch_num',$meta['patch_num'])->update(['md5'=>$md5,'zip'=>$zipUrl]);
|
|
|
+ $zipUrl = Storage::put('/trace/' . date('Ym'), file_get_contents($zipPath));
|
|
|
+ $data = [
|
|
|
+ 'md5' => $md5,
|
|
|
+ 'zip' => $zipUrl
|
|
|
+ ];
|
|
|
+ $this->line('------开始上链:' . $md5);
|
|
|
$row = $this->transcation($md5);
|
|
|
- var_dump($row);
|
|
|
+ $this->line('------上链结果:' . var_export($row, true));
|
|
|
+ if (isset($row['transaction_id']) && $row['transaction_id']) {
|
|
|
+ $data['transaction_id'] = $row['transaction_id'];
|
|
|
+ $data['block_height'] = $row['block_num'];
|
|
|
+ $data['block_time'] = Carbon::parse($row['block_time'])->toDateTimeString();
|
|
|
+ $data['status'] = 1;
|
|
|
+ } else {
|
|
|
+ $data['status'] = 2;
|
|
|
+ }
|
|
|
+ Meta::where('patch_num', $meta['patch_num'])->update($data);
|
|
|
}
|
|
|
+ $this->line('-----------traceing-end--------');
|
|
|
}
|
|
|
+
|
|
|
//下载远程图片到服务器
|
|
|
- protected function downloadImg($path,$img,$name=''){
|
|
|
+ protected function downloadImg($path, $img, $name = '')
|
|
|
+ {
|
|
|
$client = new Client(['verify' => false]); //忽略SSL错误
|
|
|
- if($name){
|
|
|
- $path = $path .'/'. $name . '.jpg';
|
|
|
- }else{
|
|
|
- $path = $path .'/'. md5(time()) . '.jpg';
|
|
|
+ if ($name) {
|
|
|
+ $path = $path . '/' . $name . '.jpg';
|
|
|
+ } else {
|
|
|
+ $path = $path . '/' . md5(time()) . '.jpg';
|
|
|
}
|
|
|
|
|
|
$response = $client->get($img, ['save_to' => $path]); //保存远程url到文件
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
return $path;
|
|
|
}
|
|
|
- Log::debug('下载'.$img.'失败');
|
|
|
+ Log::debug('下载' . $img . '失败');
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- protected function transcation($md5){
|
|
|
+ protected function transcation($md5)
|
|
|
+ {
|
|
|
$api = (new ChainFactory())->api(base_path());
|
|
|
$walapi = (new WalletFactory())->api(base_path());
|
|
|
$eos = (new EosRpc($api, $walapi));
|
|
|
$walletPassword = "PW5KFL5mQSWauhwnYTMFU3v9ALhkTCuCkfvVJgzJTpRKZysygTJZe";
|
|
|
$eos->setWalletInfo("default", $walletPassword);
|
|
|
- $trans = $eos->transfer("alice", "bob", "1.0000 SYS", $md5);
|
|
|
- $aliceBalance = $api->getCurrencyBalance('eosio.token', 'alice', 'SYS');
|
|
|
- $bobBalance = $api->getCurrencyBalance('eosio.token', 'bob', 'SYS');
|
|
|
- $row = $api->getBlockHeaderState($trans);
|
|
|
+ $trans = $eos->transfer("alice", "bob", "1.0000 SYS", $md5, 'eosio.token', false);
|
|
|
+// $aliceBalance = $api->getCurrencyBalance('eosio.token', 'alice', 'SYS');
|
|
|
+// $bobBalance = $api->getCurrencyBalance('eosio.token', 'bob', 'SYS');
|
|
|
|
|
|
return response()->json([
|
|
|
- 'trans' => $trans,
|
|
|
- 'alice' => $aliceBalance,
|
|
|
- 'bob' => $bobBalance,
|
|
|
- 'row' => $row,
|
|
|
+ 'transaction_id' => $trans['trans']['transaction_id'] ?? '',
|
|
|
+ 'block_num' => $trans['trans']['processed']['block_num'] ?? 0,
|
|
|
+ 'block_time' => $trans['trans']['processed']['block_time'] ?? ''
|
|
|
]);
|
|
|
}
|
|
|
}
|