REAPI/app/queue/command/kuaishou/KsAccessTokenUpdate.php
2024-09-29 15:43:18 +08:00

126 lines
3.9 KiB
PHP

<?php
namespace app\queue\command\kuaishou;
use app\merchant\service\MerchantService;
use dever\Log;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
use think\Collection;
/**
* 快手商家更新令牌
* Class KamiOrderStatusSync
* @package app\data\command
*/
class KsAccessTokenUpdate extends Command
{
protected function configure()
{
$this->setName('xQueue:KsAccessTokenUpdate')->setDescription('[ 快手商家 ] 更新令牌');
}
/**
* @param Input $input
* @param Output $output
* @throws \think\admin\Exception
*/
protected function execute(Input $input, Output $output)
{
ini_set('memory_limit', '1024M');
$MerchantService = MerchantService::instance();
$time = time() + 3600;
$whereRaw = ' access_token_time <=' . $time;
list($count, $total) = [0, $MerchantService->db()->whereRaw('status = 1 and merchant_type = 5 and' . $whereRaw)->count()];
$error_count = 0;
// $taobao = new \app\openapi\controller\Taobao($this->app);
$MerchantService->db()->whereRaw('status = 1 and merchant_type = 5 and' . $whereRaw)->chunk(100, function (Collection $data) use (&$count, $total,$MerchantService,&$error_count) {
// var_dump($orderlist);die;
foreach ($data->toArray() as $vo) {
$count++;
if (empty($vo['refresh_token'])) {
$this->setQueueProgress("快手商家 {$vo['name']} 无refresh_token", $count / $total * 100);
$error_count++;
}
if (empty($vo['agentId'])) {
$this->setQueueProgress("快手商家 {$vo['name']} 无agentId", $count / $total * 100);
$error_count++;
}
if (empty($vo['agentkey'])) {
$this->setQueueProgress("快手商家 {$vo['name']} 无agentkey", $count / $total * 100);
$error_count++;
}
$up = $this->getKuaishouToken($vo['id'], $vo['refresh_token'], $vo['agentId'], $vo['agentkey']);
if ($up == 'error') {
$this->setQueueProgress("快手商家 {$vo['name']} 更新失败", $count / $total * 100);
$error_count++;
} else {
$this->setQueueProgress("快手商家 {$vo['name']} 更新成功", $count / $total * 100);
}
}
});
$this->setQueueSuccess("总计 {$total} 个, 处理成功 {$count} 个快手商家,其中 {$error_count} 个商家异常!");
}
public function getKuaishouToken($mid,$refresh_token,$agentId,$agentkey)
{
$tokenurl = 'https://openapi.kwaixiaodian.com/oauth2/refresh_token';
$token_data = array(
'grant_type' => 'refresh_token',
'refresh_token' => $refresh_token,
'app_id' => $agentId,
'app_secret' => $agentkey
);
try {
$resulttoken = http_post($tokenurl, $token_data);
$tokenresult = json_decode($resulttoken, true);
if ($tokenresult['result'] != 1) {
#判断未获取数据
Log::write('errorTip', 'getKSToken_error', $resulttoken);
return 'error';
}
} catch (\Exception $e) {
Log::write('errorTip', 'getKSToken_error', $e->getMessage());
return 'error';
}
#更新refresh_token
$updaterefresh_token = array(
'refresh_token' => $tokenresult['refresh_token'],
'access_token' => $tokenresult['access_token'],
'access_token_time' => time() + $tokenresult['expires_in']
);
(new \think\facade\Db)->name('merchant_list')->where('id','=',$mid)->update($updaterefresh_token);
Log::write('gateway', 'token', $tokenresult);
return $tokenresult['access_token'];
}
}