0; }))); if (empty($accountIds)) { return false; } $lockedIds = []; try { foreach ($accountIds as $accountId) { $key = self::AUTHORIZE_REFRESH_LOCK_PREFIX . $accountId; $locked = Yii::$app->redis->executeCommand('SET', [ $key, (string)time(), 'EX', self::AUTHORIZE_REFRESH_LOCK_SECONDS, 'NX', ]); if ($locked === true || $locked === 'OK') { $lockedIds[] = $accountId; } } } catch (\Throwable $e) { Yii::warning('Unable to lock Lakala authorize refresh task: ' . $e->getMessage()); self::releaseAuthorizeRefreshLocks($lockedIds); return false; } if (empty($lockedIds)) { return false; } $started = AsyncConsole::run([ 'lakala-account/refresh-authorize-states', implode(',', $lockedIds), ], 'lakala-authorize-refresh.log'); if (!$started) { self::releaseAuthorizeRefreshLocks($lockedIds); } return $started; } public static function refreshAuthorizeStates($accountId) { try { $account = self::getById((int)$accountId, true); if (empty($account)) { return true; } if ($account->wxAuthorizeState == 1 && $account->zfbAuthorizeState == 1) { return true; } $merchantNo = $account->merchantNo ?: ''; $scanTermNo = $account->scanTermNo ?: ''; $b2bTermNo = $account->b2bTermNo ?: ''; if ($merchantNo === '' || $scanTermNo === '' || $b2bTermNo === '') { return true; } $client = new LakalaOnboardingClient(); $account->wxAuthorizeState = self::authorizeState($client, $merchantNo, 'WXZF'); $account->zfbAuthorizeState = self::authorizeState($client, $merchantNo, 'ZFBZF'); $account->updateTime = date('Y-m-d H:i:s'); if (!$account->save()) { throw new \RuntimeException(json_encode($account->getErrors(), JSON_UNESCAPED_UNICODE)); } return true; } catch (\Throwable $e) { Yii::warning('Unable to refresh Lakala authorize state, accountId=' . (int)$accountId . ': ' . $e->getMessage()); return false; } } private static function authorizeState($client, $merchantNo, $registerType) { $response = $client->query('/api/v3/tkbs/open_merchant_register_status_query', [ 'merchant_no' => $merchantNo, 'register_type' => $registerType, ], 'both'); Yii::info(json_encode($response)); $data = $response['data'] ?? []; if (isset($data['authorize_state']) && ($data['authorize_state'] == 'AUTHORIZE_STATE_UNAUTHORIZED' || $data['authorize_state'] == 'UNAUTHORIZED')) { Yii::info('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is UNAUTHORIZED'); // 未认证 return 0; } if (isset($data['authorize_state']) && ($data['authorize_state'] == 'AUTHORIZE_STATE_AUTHORIZED' || $data['authorize_state'] == 'AUTHORIZED')) { Yii::info('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is AUTHORIZED'); // 已认证 return 1; } // 其他状态,默认未认证 Yii::error('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is UNKNOWN'); return 0; } private static function releaseAuthorizeRefreshLocks(array $accountIds) { foreach ($accountIds as $accountId) { try { Yii::$app->redis->executeCommand('DEL', [self::AUTHORIZE_REFRESH_LOCK_PREFIX . $accountId]); } catch (\Throwable $e) { Yii::warning('Unable to release Lakala authorize refresh lock: ' . $e->getMessage()); } } } }