AccessTokenCommand.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands\Wechat;
  3. use App\Models\Common\TokenModel;
  4. use Illuminate\Console\Command;
  5. class AccessTokenCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'wechat:access-token';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '公众号刷新access-token';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. //
  36. $medias = TokenModel::whereIn('type', ['微信小程序', '微信公众号', '微信服务号', '微信订阅号', '微信移动App'])->get();
  37. foreach ($medias as $media) {
  38. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$media->app_id}&secret={$media->app_secret}";
  39. $response = \Curl::to($url)->asJson()->get();
  40. if (isset($response->errcode) && $response->errcode != 0) {
  41. continue;
  42. }
  43. $media->access_token = $response->access_token;
  44. $media->save();
  45. \DB::connection('mysql_datalog')
  46. ->table('koudai.public_access_token')
  47. ->where('user_name', $media->user_name)
  48. ->update([
  49. 'access_token' => $response->access_token,
  50. 'updated_at' => time()
  51. ]);
  52. }
  53. $medias = TokenModel::where('type', 'QQ小程序')->get();
  54. foreach ($medias as $media) {
  55. $url = "https://api.q.qq.com/api/getToken?grant_type=client_credential&appid={$media->app_id}&secret={$media->app_secret}";
  56. $response = \Curl::to($url)->asJson()->get();
  57. if (isset($response->errcode) && $response->errcode != 0) {
  58. continue;
  59. }
  60. $media->access_token = $response->access_token;
  61. $media->save();
  62. \DB::connection('mysql_datalog')
  63. ->table('koudai.public_access_token')
  64. ->where('user_name', $media->user_name)
  65. ->update([
  66. 'access_token' => $response->access_token,
  67. 'updated_at' => time()
  68. ]);
  69. }
  70. }
  71. }