false]; if (!$this->getIsEnable()) { return $defaultRes; } $unclaimedGift = UpgradeGiftGoods::getUnclaimedGift($this->user_id); if (!$unclaimedGift) { return $defaultRes; } $data = $this->getUnclaimedGiftGoodsIdByLevel(array_column($unclaimedGift, 'level')); if ($data) { $defaultRes['is_have'] = true; return array_merge($defaultRes, $data); } return $defaultRes; } /** * 根据等级返回当前要领取的商品(因为要拿最新配置的商品,所以不能数据库存储) * * @param array $levels * * @return array */ public function getUnclaimedGiftGoodsIdByLevel(array $levels): array { if (!$this->getIsEnable()) { return []; } $detail = $this->getConfig(); if ( empty($detail) || empty($detail['gift_type']) || $detail['gift_type'] !== GiftEnum::GIFT_TYPE_USER || empty($detail['setting']) ) { return []; } $detail['setting'] = array_filter($detail['setting'], function ($item) { if ( !isset($item['mall_id'], $item['type'], $item['level']) || $item['mall_id'] != $this->mall_id || $item['type'] !== GiftEnum::SETTING_GOODS ) { return false; } return true; }); if (empty($detail['setting'])) { return []; } $levelGoodsId = array_column($detail['setting'], 'id', 'level'); foreach ($levels as $level) { if (isset($levelGoodsId[$level])) { return [ 'level' => $level, 'goods_id' => $levelGoodsId[$level] ]; } } return []; } /** * @return array */ public function getConfig(): array { if (!isset($this->config)) { $detail = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_UPGRADEGIFT, GiftEnum::GROUP_NAME . "." . GiftEnum::GIFT_TYPE_USER); $this->config = empty($detail) ? [] : Json::decode($detail); } return $this->config; } /** * 用户升级奖励是否开启 * * @return bool */ public function getIsEnable(): bool { return $this->isInstall() && !empty($this->getConfig()['status']); } }