Trace.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Meta;
  4. use BlockMatrix\EosRpc\ChainFactory;
  5. use BlockMatrix\EosRpc\EosRpc;
  6. use BlockMatrix\EosRpc\WalletFactory;
  7. use GuzzleHttp\Client;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Storage;
  11. use Org\Multilinguals\Apollo\Client\ApolloClient;
  12. use ZanySoft\Zip\Zip;
  13. class Trace extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'trace';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '自动打zip包,并进行上链操作';
  27. protected $save_dir = '';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. $this->save_dir = '';
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return mixed
  42. */
  43. public function handle()
  44. {
  45. $this->line('-----------traceing---------');
  46. $metas = Meta::where('status',0)->get();
  47. foreach($metas as $meta){
  48. $this->save_dir = storage_path($meta['patch_num']);
  49. if(!is_dir($this->save_dir)){
  50. mkdir($this->save_dir);
  51. }
  52. $files = [];
  53. //1.生成txt
  54. $myfile = fopen($this->save_dir."/批次信息.txt", "w");
  55. $txt = "批次号:{$meta['patch_num']}\n";
  56. $txt .= "鸡种:{$meta['variety']}\n";
  57. $txt .= "年龄:{$meta['age']}\n";
  58. $txt .= "口粮:{$meta['food']}\n";
  59. $txt .= "水源:{$meta['water']}\n";
  60. fwrite($myfile, $txt);
  61. fclose($myfile);
  62. array_push($files,$this->save_dir."/批次信息.txt");
  63. //2.生成质检信息
  64. $myfile = fopen($this->save_dir."/质检信息.html", "w");
  65. $txt = $meta['report'];
  66. fwrite($myfile, $txt);
  67. fclose($myfile);
  68. array_push($files,$this->save_dir."/质检信息.html");
  69. $varietyImg = $this->downloadImg($this->save_dir,$meta['variety_img'],'鸡种');
  70. array_push($files,$varietyImg);
  71. $foodImg = $this->downloadImg($this->save_dir,$meta['food_img'],'口粮');
  72. array_push($files,$foodImg);
  73. $waterImg = $this->downloadImg($this->save_dir,$meta['water_img'],'水源');
  74. array_push($files,$waterImg);
  75. $farms = \GuzzleHttp\json_decode($meta['farm'],true);
  76. foreach ($farms as $k=>$v){
  77. $farmImg = $this->downloadImg($this->save_dir,$v,'农场'.($k+1));
  78. array_push($files,$farmImg);
  79. }
  80. //生成zip包
  81. $zipPath = storage_path().'/'.$meta['patch_num'].".zip";
  82. $zip = Zip::create($zipPath);
  83. $zip->add($this->save_dir,true);
  84. $zip->close();
  85. $md5 = md5_file($zipPath);
  86. $this->line($zipPath.'ZIP包MD5码:'.$md5);
  87. $zipUrl = Storage::put('/trace/'.date('Ym'), file_get_contents($zipPath));
  88. //Meta::where('patch_num',$meta['patch_num'])->update(['md5'=>$md5,'zip'=>$zipUrl]);
  89. $row = $this->transcation($md5);
  90. var_dump($row);
  91. }
  92. }
  93. //下载远程图片到服务器
  94. protected function downloadImg($path,$img,$name=''){
  95. $client = new Client(['verify' => false]); //忽略SSL错误
  96. if($name){
  97. $path = $path .'/'. $name . '.jpg';
  98. }else{
  99. $path = $path .'/'. md5(time()) . '.jpg';
  100. }
  101. $response = $client->get($img, ['save_to' => $path]); //保存远程url到文件
  102. if ($response->getStatusCode() == 200) {
  103. return $path;
  104. }
  105. Log::debug('下载'.$img.'失败');
  106. return false;
  107. }
  108. protected function transcation($md5){
  109. $api = (new ChainFactory())->api(base_path());
  110. $walapi = (new WalletFactory())->api(base_path());
  111. $eos = (new EosRpc($api, $walapi));
  112. $walletPassword = "PW5KFL5mQSWauhwnYTMFU3v9ALhkTCuCkfvVJgzJTpRKZysygTJZe";
  113. $eos->setWalletInfo("default", $walletPassword);
  114. $trans = $eos->transfer("alice", "bob", "1.0000 SYS", $md5);
  115. $aliceBalance = $api->getCurrencyBalance('eosio.token', 'alice', 'SYS');
  116. $bobBalance = $api->getCurrencyBalance('eosio.token', 'bob', 'SYS');
  117. $row = $api->getBlockHeaderState($trans);
  118. return response()->json([
  119. 'trans' => $trans,
  120. 'alice' => $aliceBalance,
  121. 'bob' => $bobBalance,
  122. 'row' => $row,
  123. ]);
  124. }
  125. }