Trace.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 Carbon\Carbon;
  8. use GuzzleHttp\Client;
  9. use Illuminate\Console\Command;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Storage;
  12. use Org\Multilinguals\Apollo\Client\ApolloClient;
  13. use ZanySoft\Zip\Zip;
  14. class Trace extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'trace';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '自动打zip包,并进行上链操作';
  28. protected $save_dir = '';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->save_dir = '';
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. $this->line('-----------traceing---------');
  47. $metas = Meta::where('status', 0)->get();
  48. foreach ($metas as $meta) {
  49. $this->line('------开始:' . $meta['patch_num']);
  50. $this->save_dir = storage_path($meta['patch_num']);
  51. if (!is_dir($this->save_dir)) {
  52. mkdir($this->save_dir);
  53. }
  54. //1.生成txt
  55. $this->line('------开始生成txt:' . $meta['patch_num']);
  56. $myfile = fopen($this->save_dir . "/批次信息.txt", "w");
  57. $txt = "批次号:{$meta['patch_num']}\n";
  58. $txt .= "鸡种:{$meta['variety']}\n";
  59. $txt .= "年龄:{$meta['age']}\n";
  60. $txt .= "口粮:{$meta['food']}\n";
  61. $txt .= "水源:{$meta['water']}\n";
  62. fwrite($myfile, $txt);
  63. fclose($myfile);
  64. //2.生成质检信息
  65. $this->line('------开始生成质检信息:' . $meta['patch_num']);
  66. $myfile = fopen($this->save_dir . "/质检信息.html", "w");
  67. $txt = $meta['report'];
  68. fwrite($myfile, $txt);
  69. fclose($myfile);
  70. $this->line('------开始下载图片:' . $meta['patch_num']);
  71. $varietyImg = $this->downloadImg($this->save_dir, $meta['variety_img'], '鸡种');
  72. $foodImg = $this->downloadImg($this->save_dir, $meta['food_img'], '口粮');
  73. $waterImg = $this->downloadImg($this->save_dir, $meta['water_img'], '水源');
  74. $farms = \GuzzleHttp\json_decode($meta['farm'], true);
  75. foreach ($farms as $k => $v) {
  76. $farmImg = $this->downloadImg($this->save_dir, $v, '农场' . ($k + 1));
  77. }
  78. //生成zip包
  79. $this->line('------开始生成ZIP:' . $meta['patch_num']);
  80. $zipPath = storage_path() . '/' . $meta['patch_num'] . ".zip";
  81. $zip = Zip::create($zipPath);
  82. $zip->add($this->save_dir, true);
  83. $zip->close();
  84. $md5 = md5_file($zipPath);
  85. $this->line('------ZIP包MD5码:' . $md5);
  86. $zipUrl = '/trace/' . $meta['patch_num'] . ".zip";
  87. Storage::put($zipUrl, file_get_contents($zipPath));
  88. $data = [
  89. 'md5' => $md5,
  90. 'zip' => env('OSS_URL') . 'trace/' . $meta['patch_num'] . ".zip"
  91. ];
  92. $this->line('zip地址:' . env('OSS_URL') . 'trace/' . $meta['patch_num'] . ".zip");
  93. //$md5 = md5(time());
  94. $this->line('------开始上链:' . $md5);
  95. $i = 1;
  96. do {
  97. $row = $this->transcation($md5);
  98. if (!$row) {
  99. $this->line('------上链失败-继续重试:' . $i . var_export($row, true));
  100. $i++;
  101. sleep(10);
  102. }else{
  103. $this->line('------上链成功-结果:' . var_export($row, true));
  104. break;
  105. }
  106. } while ($i <= 3);
  107. if (isset($row['transaction_id']) && $row['transaction_id']) {
  108. $data['trans_id'] = $row['transaction_id'];
  109. $data['block_height'] = $row['block_num'];
  110. $data['block_time'] = date('Y-m-d H:i:s', strtotime($row['block_time']) + 8 * 3600)();
  111. $data['status'] = 1;
  112. } else {
  113. $data['status'] = 2;
  114. }
  115. Meta::where('patch_num', $meta['patch_num'])->update($data);
  116. }
  117. $this->line('-----------traceing-end--------');
  118. }
  119. //下载远程图片到服务器
  120. protected function downloadImg($path, $img, $name = '')
  121. {
  122. $client = new Client(['verify' => false]); //忽略SSL错误
  123. if ($name) {
  124. $path = $path . '/' . $name . '.jpg';
  125. } else {
  126. $path = $path . '/' . md5(time()) . '.jpg';
  127. }
  128. $response = $client->get($img, ['save_to' => $path]); //保存远程url到文件
  129. if ($response->getStatusCode() == 200) {
  130. return $path;
  131. }
  132. $this->line('下载' . $img . '失败');
  133. return false;
  134. }
  135. protected function transcation($md5)
  136. {
  137. try {
  138. $api = (new ChainFactory())->api(base_path());
  139. $walapi = (new WalletFactory())->api(base_path());
  140. $eos = (new EosRpc($api, $walapi));
  141. $accountA = "chxqsuyuanaa";
  142. $accountB = "chxqsuyuanbb";
  143. $coinCode = "EOS";
  144. $coin = "0.0001";//每次转多少
  145. $walletPassword = "PW5JbENyMrmYGc5LBMKGf2E6QaFoXSQZ6CDWMET7a6o6cXvy35qH1";
  146. $walletName = "caihongxingqiu";
  147. $eos->setWalletInfo($walletName, $walletPassword);
  148. $aliceBalance = json_decode($api->getCurrencyBalance('eosio.token', $accountA, $coinCode));
  149. $bobBalance = json_decode($api->getCurrencyBalance('eosio.token', $accountB, $coinCode));
  150. $balaceA = 0;
  151. $balaceB = 0;
  152. if (isset($aliceBalance[0])) {
  153. $balaceA = $aliceBalance[0];
  154. }
  155. if (isset($bobBalance[0])) {
  156. $balaceB = $bobBalance[0];
  157. }
  158. if (floatval($balaceA) < $coin || floatval($balaceB) < $coin) {
  159. $this->line("有个账号没钱了,快充钱");
  160. return false;
  161. }
  162. $this->line("准备发起交易");
  163. $rand = rand(0, 1);
  164. if ($rand == 0) {
  165. $trans = $eos->transfer($accountB, $accountA, $coin . " " . $coinCode, $md5, 'eosio.token', false);
  166. } elseif ($rand == 1) {
  167. $trans = $eos->transfer($accountA, $accountB, $coin . " " . $coinCode, $md5, 'eosio.token', false);
  168. }
  169. Log::debug('transinfo:' . \GuzzleHttp\json_encode($trans));
  170. return [
  171. 'transaction_id' => $trans['transaction_id'] ?? '',
  172. 'block_num' => $trans['processed']['block_num'] ?? 0,
  173. 'block_time' => $trans['processed']['block_time'] ?? ''
  174. ];
  175. } catch (\Exception $exception) {
  176. $this->line('上链失败:' . $exception->getMessage());
  177. return false;
  178. }
  179. }
  180. }