SyncSharerLogic.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace addons\WechatVideoShop\common\logic;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  4. use addons\WechatVideoShop\common\models\WechatVideoShop;
  5. use addons\WechatVideoShop\common\service\SharerService;
  6. use addons\WechatVideoShop\common\service\ShopService;
  7. use common\enums\RabbitMqEnum;
  8. use common\enums\StatusEnum;
  9. use common\helpers\FormatHelper;
  10. use common\models\mall\Mall;
  11. class SyncSharerLogic
  12. {
  13. private static $lock_key = 'wechat:video:shop:sync:sharer';
  14. private static $lock_sec = 300;
  15. public static function sharer()
  16. {
  17. $is_sync = \Yii::$app->redis->get(self::$lock_key);
  18. if (!$is_sync) {
  19. \Yii::error(__METHOD__ . ' 执行同步 ');
  20. try {
  21. // 先加锁
  22. \Yii::$app->redis->setex(self::$lock_key, self::$lock_sec, 1);
  23. // 取出所有的商城
  24. $mids = Mall::getEnableMallIds();
  25. foreach ($mids as $mid) {
  26. $config = \Yii::$app->services->setting->addons->get($mid, WechatVideoShopEnums::ADDONS_NAME, 'basic');
  27. if (!empty($config['is_enable'])) { // 开启状态
  28. // 定时处理授权
  29. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,
  30. ['mall_id' => $mid], ShopService::class, 'auth');
  31. // 取出所有店铺
  32. $shop_list = WechatVideoShop::lists([
  33. 'where' => [
  34. ['mall_id' => $mid],
  35. ['status' => StatusEnum::ENABLED]
  36. ],
  37. 'order' => 'id asc'
  38. ]);
  39. if (!empty($shop_list)) {
  40. // 遍历所有店铺
  41. foreach ($shop_list as $shop) {
  42. // 获取分销员
  43. $sharerService = new SharerService($shop['id'], $mid);
  44. for ($i = 0; $i < 2; $i++) { // 分销员类型 0 普通分销员, 1 店铺分销员
  45. $page = 1;
  46. while (true) {
  47. $flag = $sharerService->sync([
  48. 'page' => $page,
  49. 'sharer_type' => $i,
  50. ]);
  51. ++$page;
  52. if (!$flag) break;
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. } catch (\Exception $e) {
  60. \Yii::error(FormatHelper::exception($e, __METHOD__));
  61. }
  62. // 解除锁
  63. \Yii::$app->redis->del(self::$lock_key);
  64. } else {
  65. \Yii::error(__METHOD__ . ' 上一次还在同步中... ');
  66. }
  67. }
  68. /**
  69. * 是否在同步中
  70. * @return mixed
  71. */
  72. public static function isSync()
  73. {
  74. return \Yii::$app->redis->get(self::$lock_key);
  75. }
  76. }