LakalaAccountClass.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace bizGhs\lakala\classes;
  3. use bizGhs\base\classes\BaseClass;
  4. use common\components\AsyncConsole;
  5. use common\components\lakala\LakalaOnboardingClient;
  6. use Yii;
  7. class LakalaAccountClass extends BaseClass
  8. {
  9. public static $baseFile = '\bizGhs\lakala\models\LakalaAccount';
  10. const AUTHORIZE_REFRESH_LOCK_PREFIX = 'lakala:authorize-refresh-';
  11. const AUTHORIZE_REFRESH_LOCK_SECONDS = 120;
  12. public static function dispatchAuthorizeStatesRefresh(array $accountIds)
  13. {
  14. $accountIds = array_values(array_unique(array_filter(array_map('intval', $accountIds), function ($id) {
  15. return $id > 0;
  16. })));
  17. if (empty($accountIds)) {
  18. return false;
  19. }
  20. $lockedIds = [];
  21. try {
  22. foreach ($accountIds as $accountId) {
  23. $key = self::AUTHORIZE_REFRESH_LOCK_PREFIX . $accountId;
  24. $locked = Yii::$app->redis->executeCommand('SET', [
  25. $key,
  26. (string)time(),
  27. 'EX',
  28. self::AUTHORIZE_REFRESH_LOCK_SECONDS,
  29. 'NX',
  30. ]);
  31. if ($locked === true || $locked === 'OK') {
  32. $lockedIds[] = $accountId;
  33. }
  34. }
  35. } catch (\Throwable $e) {
  36. Yii::warning('Unable to lock Lakala authorize refresh task: ' . $e->getMessage());
  37. self::releaseAuthorizeRefreshLocks($lockedIds);
  38. return false;
  39. }
  40. if (empty($lockedIds)) {
  41. return false;
  42. }
  43. $started = AsyncConsole::run([
  44. 'lakala-account/refresh-authorize-states',
  45. implode(',', $lockedIds),
  46. ], 'lakala-authorize-refresh.log');
  47. if (!$started) {
  48. self::releaseAuthorizeRefreshLocks($lockedIds);
  49. }
  50. return $started;
  51. }
  52. public static function refreshAuthorizeStates($accountId)
  53. {
  54. try {
  55. $account = self::getById((int)$accountId, true);
  56. if (empty($account)) {
  57. return true;
  58. }
  59. if ($account->wxAuthorizeState == 1 && $account->zfbAuthorizeState == 1) {
  60. return true;
  61. }
  62. $merchantNo = $account->merchantNo ?: '';
  63. $scanTermNo = $account->scanTermNo ?: '';
  64. $b2bTermNo = $account->b2bTermNo ?: '';
  65. if ($merchantNo === '' || $scanTermNo === '' || $b2bTermNo === '') {
  66. return true;
  67. }
  68. $client = new LakalaOnboardingClient();
  69. $account->wxAuthorizeState = self::authorizeState($client, $merchantNo, 'WXZF');
  70. $account->zfbAuthorizeState = self::authorizeState($client, $merchantNo, 'ZFBZF');
  71. $account->updateTime = date('Y-m-d H:i:s');
  72. if (!$account->save()) {
  73. throw new \RuntimeException(json_encode($account->getErrors(), JSON_UNESCAPED_UNICODE));
  74. }
  75. return true;
  76. } catch (\Throwable $e) {
  77. Yii::warning('Unable to refresh Lakala authorize state, accountId=' . (int)$accountId . ': ' . $e->getMessage());
  78. return false;
  79. }
  80. }
  81. private static function authorizeState($client, $merchantNo, $registerType)
  82. {
  83. $response = $client->query('/api/v3/tkbs/open_merchant_register_status_query', [
  84. 'merchant_no' => $merchantNo,
  85. 'register_type' => $registerType,
  86. ], 'both');
  87. Yii::info(json_encode($response));
  88. $data = $response['data'] ?? [];
  89. if (isset($data['authorize_state']) && ($data['authorize_state'] == 'AUTHORIZE_STATE_UNAUTHORIZED' || $data['authorize_state'] == 'UNAUTHORIZED')) {
  90. Yii::info('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is UNAUTHORIZED');
  91. // 未认证
  92. return 0;
  93. }
  94. if (isset($data['authorize_state']) && ($data['authorize_state'] == 'AUTHORIZE_STATE_AUTHORIZED' || $data['authorize_state'] == 'AUTHORIZED')) {
  95. Yii::info('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is AUTHORIZED');
  96. // 已认证
  97. return 1;
  98. }
  99. // 其他状态,默认未认证
  100. Yii::error('Lakala '.$merchantNo. ' '.$registerType. ' authorize state is UNKNOWN');
  101. return 0;
  102. }
  103. private static function releaseAuthorizeRefreshLocks(array $accountIds)
  104. {
  105. foreach ($accountIds as $accountId) {
  106. try {
  107. Yii::$app->redis->executeCommand('DEL', [self::AUTHORIZE_REFRESH_LOCK_PREFIX . $accountId]);
  108. } catch (\Throwable $e) {
  109. Yii::warning('Unable to release Lakala authorize refresh lock: ' . $e->getMessage());
  110. }
  111. }
  112. }
  113. }