| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace addons\WechatVideoShop\common\logic;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use addons\WechatVideoShop\common\models\WechatVideoShop;
- use addons\WechatVideoShop\common\service\SharerService;
- use addons\WechatVideoShop\common\service\ShopService;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\Mall;
- class SyncSharerLogic
- {
- private static $lock_key = 'wechat:video:shop:sync:sharer';
- private static $lock_sec = 300;
- public static function sharer()
- {
- $is_sync = \Yii::$app->redis->get(self::$lock_key);
- if (!$is_sync) {
- \Yii::error(__METHOD__ . ' 执行同步 ');
- try {
- // 先加锁
- \Yii::$app->redis->setex(self::$lock_key, self::$lock_sec, 1);
- // 取出所有的商城
- $mids = Mall::getEnableMallIds();
- foreach ($mids as $mid) {
- $config = \Yii::$app->services->setting->addons->get($mid, WechatVideoShopEnums::ADDONS_NAME, 'basic');
- if (!empty($config['is_enable'])) { // 开启状态
- // 定时处理授权
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,
- ['mall_id' => $mid], ShopService::class, 'auth');
- // 取出所有店铺
- $shop_list = WechatVideoShop::lists([
- 'where' => [
- ['mall_id' => $mid],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'id asc'
- ]);
- if (!empty($shop_list)) {
- // 遍历所有店铺
- foreach ($shop_list as $shop) {
- // 获取分销员
- $sharerService = new SharerService($shop['id'], $mid);
- for ($i = 0; $i < 2; $i++) { // 分销员类型 0 普通分销员, 1 店铺分销员
- $page = 1;
- while (true) {
- $flag = $sharerService->sync([
- 'page' => $page,
- 'sharer_type' => $i,
- ]);
- ++$page;
- if (!$flag) break;
- }
- }
- }
- }
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- }
- // 解除锁
- \Yii::$app->redis->del(self::$lock_key);
- } else {
- \Yii::error(__METHOD__ . ' 上一次还在同步中... ');
- }
- }
- /**
- * 是否在同步中
- * @return mixed
- */
- public static function isSync()
- {
- return \Yii::$app->redis->get(self::$lock_key);
- }
- }
|