ForceUserProperty.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Jobs;
  3. use App\Jobs\Job;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use DB;
  8. use Redis;
  9. use WechatOP;
  10. use Log;
  11. class ForceUserProperty extends Job implements ShouldQueue
  12. {
  13. use InteractsWithQueue, SerializesModels;
  14. private $data;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct($data)
  21. {
  22. $data['send_time']=date("Y-m-d H:i:s");
  23. $this->data = $data;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. try{
  33. $appid = $this->data['appid'];
  34. $openid = $this->data['openid'];
  35. $current_time = date('Y-m-d H:i:s');
  36. $user_data = DB::connection('zhuishuyun_user')->table('force_subscribe_user_properties')->where('appid',$appid)->where('openid',$openid)->first();
  37. if(!$user_data)
  38. {
  39. $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
  40. $component_refresh_token = Redis::Get($redis_key);
  41. $options = [
  42. 'app_id' => $appid,
  43. 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
  44. 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
  45. 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
  46. 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
  47. 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
  48. 'cache' => [
  49. 'driver' => 'redis', // redis, filesystem, laravel
  50. 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
  51. ],
  52. ];
  53. $app = WechatOP::app($options);
  54. $info = $app->user->get($openid);
  55. $nickname = $info->nickname;
  56. $sex = $info->sex;
  57. $data = [
  58. 'openid'=>$openid,
  59. 'appid'=>$appid,
  60. 'current_nickname'=>trim($nickname),
  61. 'sex'=>$sex,
  62. 'created_at'=>$current_time,
  63. 'updated_at'=>$current_time,
  64. ];
  65. DB::connection('zhuishuyun_user')->table('force_subscribe_user_properties')->insert($data);
  66. }
  67. }
  68. catch(\Exception $e){
  69. v('handle_ept: info:'.$e->getMessage());
  70. }
  71. }
  72. /**
  73. * The job failed to process.
  74. *
  75. * @param Exception $exception
  76. * @return void
  77. */
  78. public function failed(Exception $exception)
  79. {
  80. // Send user notification of failure, etc...
  81. v('sendNews_ept:'.$exception->getMessage().' data:'.json_encode($this->data));
  82. }
  83. }