| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- <?php
- namespace common\helpers;
- use common\enums\GlobalFuncEnum;
- use common\enums\StatusEnum;
- use common\models\common\AddonsInvoker;
- use common\models\mall\MallAddons;
- use Yii;
- use yii\web\NotFoundHttpException;
- use yii\helpers\Json;
- use common\enums\RedisKeyEnum;
- use common\models\common\AddonsConfig;
- use common\enums\AppEnum;
- use League\Flysystem\Filesystem;
- use League\Flysystem\Adapter\Local;
- /**
- * Class AddonHelper
- * @package common\helpers
- * @author qimall
- */
- class AddonHelper
- {
- /**
- * 服务
- *
- * @var array
- */
- protected static $_service = [];
- /**
- * 配置
- *
- * @var array
- */
- protected static $_config = [];
- /**
- * 获取插件配置
- *
- * @param $name
- * @return string
- */
- public static function getAddonConfig($name)
- {
- return static::getAddonRoot($name) . "AddonConfig";
- }
- /**
- * 获取插件微信消息配置
- *
- * @param $name
- * @return string
- */
- public static function getAddonMessage($name)
- {
- return static::getAddonRoot($name) . "AddonMessage";
- }
- /**
- * 获取插件的命名空间
- *
- * @param $name
- * @return string
- */
- public static function getAddonRoot($name)
- {
- return "addons" . "\\" . $name . "\\";
- }
- /**
- * 获取插件的根目录目录
- *
- * @param $name
- * @return string
- */
- public static function getAddonRootPath($name)
- {
- return Yii::getAlias('@addons') . "/{$name}/";
- }
- /**
- * 验证插件目录是否存在
- *
- * @param $name
- * @return bool
- */
- public static function has($name)
- {
- if (!is_dir(static::getAddonRootPath($name))) {
- return false;
- }
- return true;
- }
- /**
- * @param $name
- * @return string
- * @throws \League\Flysystem\FileExistsException
- * @throws \League\Flysystem\FileNotFoundException
- */
- public static function getAddonIcon($name)
- {
- $adapter = new Local(Yii::getAlias('@root'));
- $filesystem = new Filesystem($adapter);
- $localIconPath = static::getAddonRoot($name) . 'icon.jpg';
- if ($filesystem->has($localIconPath)) {
- $md5 = md5(Json::encode($filesystem->getMetadata($localIconPath)));
- $newPath = '/assets/tmp/' . $md5 . '.jpg';
- $newLocalIconPath = 'web/backend' . $newPath;
- if (!$filesystem->has($newLocalIconPath)) {
- $filesystem->copy($localIconPath, $newLocalIconPath);
- }
- // 后台独立域名
- if ($backendUrl = Yii::getAlias('@backendUrl')) {
- return $backendUrl . $newPath;
- }
- return '/backend' . $newPath;
- }
- return Yii::getAlias('@web') . '/resources/img/icon.jpg';
- }
- /**
- * 获取生成asset的资源文件目录
- *
- * @param string $assets
- * @return string
- */
- public static function filePath($assets = '')
- {
- if (!$assets) {
- $assets = [];
- $assets[] = 'addons';
- $assets[] = Yii::$app->params['addon']['name'];
- $assets[] = Yii::$app->params['realAppId'];
- $assets[] = 'assets';
- $assets[] = 'AppAsset';
- $assets = implode('\\', $assets);
- }
- if (!isset(Yii::$app->view->assetBundles[$assets])) {
- /* @var $assets \yii\web\AssetBundle */
- $assets::register(Yii::$app->view);
- }
- return Yii::$app->view->assetBundles[$assets]->baseUrl . '/';
- }
- /**
- * 获取资源文件
- *
- * @return string
- */
- public static function file($path, $assets = '')
- {
- return self::filePath($assets) . $path;
- }
- /**
- * @param $path
- * @param array $options
- * @param string $assets
- * @return string
- */
- public static function jsFile($path, $options = [], $assets = '')
- {
- return Html::jsFile(self::filePath($assets) . $path, $options);
- }
- /**
- * @param $path
- * @param array $options
- * @param string $assets
- * @return string
- */
- public static function cssFile($path, $options = [], $assets = '')
- {
- return Html::cssFile(self::filePath($assets) . $path, $options);
- }
- /**
- * 获取后台配置信息
- *
- * @return array|mixed
- */
- public static function getBackendConfig($noCache = false, $name = '')
- {
- return static::findConfig($noCache, '', $name, AppEnum::BACKEND);
- }
- /**
- * 获取商户配置信息
- *
- * @return array|mixed
- */
- public static function getMerchantConfig($noCache = false, $name = '', $merchant_id = '')
- {
- !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
- !$merchant_id && $merchant_id == 1;
- return static::findConfig($noCache, $merchant_id, $name, AppEnum::MERCHANT);
- }
- /**
- * 获取商户配置信息
- *
- * @return array|mixed
- */
- public static function getConfig($noCache = false, $name = '', $merchant_id = '')
- {
- empty($merchant_id) && $merchant_id = Yii::$app->services->merchant->getId();
- $app_id = empty($merchant_id) ? AppEnum::BACKEND : AppEnum::MERCHANT;
- return static::findConfig($noCache, $merchant_id, $name, $app_id);
- }
- /**
- * 写入配置信息
- *
- * @param array $config
- * @param string $app_id
- * @return array|mixed
- */
- public static function setConfig(array $config, $merchant_id = '', $app_id = '')
- {
- !$app_id && $app_id = Yii::$app->id;
- !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
- $name = Yii::$app->params['addon']['name'];
- if (empty($configModel = Yii::$app->services->addonsConfig->findByName($name, $app_id, $merchant_id))) {
- $configModel = new AddonsConfig();
- $configModel->addons_name = $name;
- $configModel->app_id = $app_id;
- $configModel->data = [];
- }
- $configModel->data = array_merge($configModel->data, $config);
- $configModel->save();
- return self::getConfig(true, $name, $merchant_id);
- }
- /**
- * 获取配置信息
- *
- * @return array|mixed
- */
- public static function findConfig($noCache, $merchant_id, $name, $app_id)
- {
- !$name && $name = Yii::$app->params['addon']['name'];
- $updated_at = Yii::$app->params['addon']['updated_at'] ?? '';
- $cacheKey = [
- 'addonsConfig',
- $app_id,
- $name,
- $updated_at,
- $merchant_id
- ];
- $cacheKey = implode(':', $cacheKey);
- // 判断是否已经读取
- if (isset(self::$_config[$cacheKey]) && $noCache == false) {
- return self::$_config[$cacheKey];
- }
- if ($noCache == true || !($configModel = Yii::$app->cache->get($cacheKey))) {
- if (empty($configModel = Yii::$app->services->addonsConfig->findByName($name, $app_id, $merchant_id))) {
- return [];
- }
- Yii::$app->cache->set($cacheKey, $configModel, 7200);
- }
- self::$_config[$cacheKey] = $configModel->data;
- return self::$_config[$cacheKey];
- }
- /**
- * 调用其他插件的服务
- *
- * @param $name
- * @return object
- * @throws NotFoundHttpException
- * @throws \yii\base\InvalidConfigException
- * @throws \yii\di\NotInstantiableException
- */
- public static function service($name)
- {
- if (!$name) {
- throw new NotFoundHttpException("插件名称不能为空");
- }
- if (!($addon = Yii::$app->services->addons->findByNameWithBinding($name))) {
- throw new NotFoundHttpException($name . "插件不存在,请先安装");
- }
- // 初始化服务
- if (empty($addon->service)) {
- throw new NotFoundHttpException($name . "不支持服务调用");
- }
- // 动态注入服务
- $service_name = lcfirst($addon->name) . 'Service';
- if (isset(self::$_service[$service_name])) {
- return self::$_service[$service_name];
- }
- Yii::$app->set($service_name, [
- 'class' => $addon->service,
- ]);
- self::$_service[$service_name] = Yii::$app->get($service_name);
- return self::$_service[$service_name];
- }
- /**
- * 初始化模块信息
- *
- * @param string $name 模块名称
- * @param string $route 路由
- * @return bool
- * @throws NotFoundHttpException
- */
- public static function initAddon($name)
- {
- if (!$name) {
- throw new NotFoundHttpException("插件不能为空");
- }
- if (!($addon = Yii::$app->services->addons->findByNameWithBinding($name, Yii::$app->id == AppEnum::BACKEND))) {
- throw new NotFoundHttpException("插件不存在");
- }
- // 当前模块实例
- Yii::$app->params['addon'] = $addon;
- // 菜单
- Yii::$app->params['menu']['menu'] = !empty($addon['menu']) ? ArrayHelper::toArray($addon['menu']) : [];
- // 导航
- Yii::$app->params['menu']['cover'] = !empty($addon['bindingCover']) ? ArrayHelper::toArray($addon['bindingCover']) : [];
- Yii::$app->params['addonName'] = StringHelper::toUnderScore(Yii::$app->params['addon']['name']);
- Yii::$app->params['inAddon'] = true;
- return true;
- }
- /**
- * 全局功能调用
- * @Author: hua
- * @Date: 2022/3/23
- * @Time: 18:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $invoke_type
- * @param int $mall_id
- */
- // public static function globalFuncInvoke($invoke_type, $mall_id = 0, $params = [])
- // {
- // //获取商城已安装插件列表
- // $addons_list = MallAddons::lists(['where' => [
- // ['mall_id' => $mall_id],
- // ['status' => StatusEnum::ENABLED],
- // ], 'select' => 'addons_name']);
- // $install_addons = array_column($addons_list, 'addons_name');
- // switch ($invoke_type) {
- // //提现条件全局调用
- // case GlobalFuncEnum::WITHDRAW_COND:
- // $return = [];
- // $invoke_list = AddonsInvoker::lists(['where' => [['invoker_id' => GlobalFuncEnum::WITHDRAW_COND, 'addons_name' => $install_addons]]]);
- // foreach ($invoke_list as $invoke_info) {
- // $method = $invoke_info['method'];
- // if (class_exists($invoke_info['class']) == false) continue;
- // $obj = new $invoke_info['class']();
- // if (method_exists($obj, $method) == false) continue;
- // $result = $obj->$method();
- // $return = array_merge($return, $result);
- // }
- // return $return;
- // break;
- // //分享海报
- // case GlobalFuncEnum::SHARE_VENGEANCE:
- // $return = [];
- // $invoke_list = AddonsInvoker::lists(['where' => [['invoker_id' => GlobalFuncEnum::SHARE_VENGEANCE, 'addons_name' => $install_addons]]]);
- // foreach ($invoke_list as $invoke_info) {
- // $method = $invoke_info['method'];
- // if (class_exists($invoke_info['class']) == false) continue;
- // $obj = new $invoke_info['class']();
- // if (method_exists($obj, $method) == false) continue;
- // $return = $obj->$method($mall_id,$params);
- // }
- // return $return;
- // break;
- //
- // }
- // }
- public static function globalFuncInvoke($invoke_type, $mall_id = 0, $params = [])
- {
- //获取商城已安装插件列表
- $install_addons = MallAddons::getValidAddons($mall_id);
- $param = Yii::$app->params;
- $params['mall_id'] = $mall_id;
- $return = [];
- if(isset($param['invoker'][$invoke_type])){
- if(class_exists($param['invoker'][$invoke_type]['class'])){
- $class = $param['invoker'][$invoke_type]['class'];
- $obj = new $class;
- if(method_exists($obj,'handle'))$return = $obj->handle($invoke_type,$install_addons,$params);
- }
- }
- return $return;
- }
- }
|