123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Console\Commands;
- use GuzzleHttp\Client;
- use Illuminate\Console\Command;
- use Redis;
- use WechatOP;
- use DB;
- class test2 extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'test:test3';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- $this->appId='wx1037462ad78bf1f2';
- $this->appSecret='d6bda51bfdb4cf6eb504c622fd72c210';
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->fix();
- }
- private function https_request($url, $data = null, $timeout = 5000)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (! empty($data)) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- if ($timeout) {
- curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- if ($output === false) {
- \Log::error('Curl error: ' . curl_error($curl).". url:".$url);
- }
- curl_close($curl);
- return $output;
- }
- public function getToken($is_force = true )
- {
- $appId = $this->appId;
- $appSecret = $this->appSecret;
- $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appId . '&secret=' . $appSecret;
- $rs1 = $this->https_request($url);
- \Log::info(($this->appId).'get_token-'.$rs1);
- if ($rs1)
- {
- $rs = json_decode($rs1, true);
- if (is_array($rs) && $rs['access_token'])
- {
- $access_token = $rs['access_token'];
- $expires_in = intval($rs['expires_in'], 10);
- if ($expires_in <= 0)
- {
- $expires_in = 5;
- } else if ($expires_in >= 7200)
- {
- $expires_in = 7000;
- }
- $data = array();
- $data['expire_time'] = time() + $expires_in;
- $data['access_token'] = $access_token;
- Redis::setex(($this->appId).'access_token', $expires_in, serialize($data));
- return $access_token;
- } else
- {
- $content = "get access token error:" . $rs1;
- \Log::info(($this->appId).$content);
- }
- } else
- {
- $content = "get access token error:" . $rs1;
- \Log::info(($this->appId).$content);
- }
- return false;
- }
- //修复crm用户昵称问题
- public function fix()
- {
-
- $uids = collect(DB::select("select distinct uid from yueduyun.user_bind_hk_welfare
- 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;});
- $sub_users = DB::table('yueduyun.force_subscribe_users')->whereIn('uid',$uids)->where('is_subscribed',1)->get();
- $time = date("Y-m-d H:i:s");
- foreach($sub_users as $sub_user)
- {
- $appid = $sub_user->appid;
- $openid = $sub_user->openid;
- $redis_key = '[wechat_op.common.component_refresh_token.'.$appid.']';
- $component_refresh_token = Redis::Get($redis_key);
- $options = [
- 'app_id' => $appid,
- 'secret' => env('WECHAT_OP_SECRET'), // 仅适用于 单独配置公众号
- 'token' => env('WECHAT_OP_TOKEN'), // 仅适用于 单独配置公众号
- 'aes_key' => env('WECHAT_OP_AES_KEY'), // 仅适用于 单独配置公众号
- 'auth_type' => 'COMPONENT', // COMPONENT 开放平台授权公众号,MANUAL 单独配置公众号
- 'component_refresh_token' => $component_refresh_token, // 授权回调时获取的 authorizer_refresh_token,仅适用于 开放品台授权公众号
- 'oauth' => [
- 'scopes' => ['snsapi_base'], // 公众号授权用户方式 snsapi_base, snsapi_userinfo
- 'callback' => '/oauth_callback',
- ],
- 'cache' => [
- 'driver' => 'redis', // redis, filesystem, laravel
- 'dir' => storage_path('tmp') // 只有为filesystem时候这个目录才有效
- ],
- ];
- try{
- $app = WechatOP::app($options);
- $user_data = $app->user->get($openid);
- if(isset($user_data))
- {
- //DB::table('yueduyun.users')->where('id',$sub_user->uid)->update(['nickname'=>$user_data->nickname]);
- DB::connection('command_mysql')->table('zsy_crm.crm_users')->insert(['uid'=>$sub_user->uid,'nickname'=>$user_data->nickname,'created_at'=>$time,'updated_at'=>$time]);
- }
- dump($user_data);
- }catch (\Exception $e){
- dump($e->getMessage());
- };
- }
- }
- }
|