test2.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Console\Commands;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Console\Command;
  5. use Redis;
  6. use WechatOP;
  7. use DB;
  8. class test2 extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'test:test3';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $this->appId='wx1037462ad78bf1f2';
  31. $this->appSecret='d6bda51bfdb4cf6eb504c622fd72c210';
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $this->fix();
  41. }
  42. private function https_request($url, $data = null, $timeout = 5000)
  43. {
  44. $curl = curl_init();
  45. curl_setopt($curl, CURLOPT_URL, $url);
  46. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  48. if (! empty($data)) {
  49. curl_setopt($curl, CURLOPT_POST, 1);
  50. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  51. }
  52. if ($timeout) {
  53. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  54. }
  55. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  56. $output = curl_exec($curl);
  57. if ($output === false) {
  58. \Log::error('Curl error: ' . curl_error($curl).". url:".$url);
  59. }
  60. curl_close($curl);
  61. return $output;
  62. }
  63. public function getToken($is_force = true )
  64. {
  65. $appId = $this->appId;
  66. $appSecret = $this->appSecret;
  67. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret;
  68. $rs1 = $this->https_request($url);
  69. \Log::info(($this->appId).'get_token-'.$rs1);
  70. if ($rs1)
  71. {
  72. $rs = json_decode($rs1, true);
  73. if (is_array($rs) && $rs['access_token'])
  74. {
  75. $access_token = $rs['access_token'];
  76. $expires_in = intval($rs['expires_in'], 10);
  77. if ($expires_in <= 0)
  78. {
  79. $expires_in = 5;
  80. } else if ($expires_in >= 7200)
  81. {
  82. $expires_in = 7000;
  83. }
  84. $data = array();
  85. $data['expire_time'] = time() + $expires_in;
  86. $data['access_token'] = $access_token;
  87. Redis::setex(($this->appId).'access_token', $expires_in, serialize($data));
  88. return $access_token;
  89. } else
  90. {
  91. $content = "get access token error:" . $rs1;
  92. \Log::info(($this->appId).$content);
  93. }
  94. } else
  95. {
  96. $content = "get access token error:" . $rs1;
  97. \Log::info(($this->appId).$content);
  98. }
  99. return false;
  100. }
  101. //修复crm用户昵称问题
  102. public function fix()
  103. {
  104. $uids = collect(DB::select("select distinct uid from yueduyun.user_bind_hk_welfare
  105. where not exists(select 1 from zsy_crm.crm_users where uid = yueduyun.user_bind_hk_welfare.uid)"))->pluck('uid');//->map(function ($user){ return $user->id;});
  106. $sub_users = DB::table('yueduyun.force_subscribe_users')->whereIn('uid',$uids)->where('is_subscribed',1)->get();
  107. $time = date("Y-m-d H:i:s");
  108. foreach($sub_users as $sub_user)
  109. {
  110. $appid = $sub_user->appid;
  111. $openid = $sub_user->openid;
  112. $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
  113. $component_refresh_token = Redis::Get($redis_key);
  114. $options = [
  115. 'app_id' => $appid,
  116. 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
  117. 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
  118. 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
  119. 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
  120. 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
  121. 'oauth' => [
  122. 'scopes' => ['snsapi_base'], // 公众号授权用户方式 snsapi_base, snsapi_userinfo
  123. 'callback' => '/oauth_callback',
  124. ],
  125. 'cache' => [
  126. 'driver' => 'redis', // redis, filesystem, laravel
  127. 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
  128. ],
  129. ];
  130. try{
  131. $app = WechatOP::app($options);
  132. $user_data = $app->user->get($openid);
  133. if(isset($user_data))
  134. {
  135. //DB::table('yueduyun.users')->where('id',$sub_user->uid)->update(['nickname'=>$user_data->nickname]);
  136. DB::connection('command_mysql')->table('zsy_crm.crm_users')->insert(['uid'=>$sub_user->uid,'nickname'=>$user_data->nickname,'created_at'=>$time,'updated_at'=>$time]);
  137. }
  138. dump($user_data);
  139. }catch (\Exception $e){
  140. dump($e->getMessage());
  141. };
  142. }
  143. }
  144. }