| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace bizGhs\lakala\classes;
- use bizGhs\base\classes\BaseClass;
- use common\components\AsyncConsole;
- use common\components\lakala\LakalaOnboardingClient;
- use Yii;
- class LakalaAccountClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\lakala\models\LakalaAccount';
- const AUTHORIZE_REFRESH_LOCK_PREFIX = 'lakala:authorize-refresh-';
- const AUTHORIZE_REFRESH_LOCK_SECONDS = 120;
- public static function dispatchAuthorizeStatesRefresh(array $accountIds)
- {
- $accountIds = array_values(array_unique(array_filter(array_map('intval', $accountIds), function ($id) {
- return $id > 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());
- }
- }
- }
- }
|