AddonHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. namespace common\helpers;
  3. use common\enums\GlobalFuncEnum;
  4. use common\enums\StatusEnum;
  5. use common\models\common\AddonsInvoker;
  6. use common\models\mall\MallAddons;
  7. use Yii;
  8. use yii\web\NotFoundHttpException;
  9. use yii\helpers\Json;
  10. use common\enums\RedisKeyEnum;
  11. use common\models\common\AddonsConfig;
  12. use common\enums\AppEnum;
  13. use League\Flysystem\Filesystem;
  14. use League\Flysystem\Adapter\Local;
  15. /**
  16. * Class AddonHelper
  17. * @package common\helpers
  18. * @author qimall
  19. */
  20. class AddonHelper
  21. {
  22. /**
  23. * 服务
  24. *
  25. * @var array
  26. */
  27. protected static $_service = [];
  28. /**
  29. * 配置
  30. *
  31. * @var array
  32. */
  33. protected static $_config = [];
  34. /**
  35. * 获取插件配置
  36. *
  37. * @param $name
  38. * @return string
  39. */
  40. public static function getAddonConfig($name)
  41. {
  42. return static::getAddonRoot($name) . "AddonConfig";
  43. }
  44. /**
  45. * 获取插件微信消息配置
  46. *
  47. * @param $name
  48. * @return string
  49. */
  50. public static function getAddonMessage($name)
  51. {
  52. return static::getAddonRoot($name) . "AddonMessage";
  53. }
  54. /**
  55. * 获取插件的命名空间
  56. *
  57. * @param $name
  58. * @return string
  59. */
  60. public static function getAddonRoot($name)
  61. {
  62. return "addons" . "\\" . $name . "\\";
  63. }
  64. /**
  65. * 获取插件的根目录目录
  66. *
  67. * @param $name
  68. * @return string
  69. */
  70. public static function getAddonRootPath($name)
  71. {
  72. return Yii::getAlias('@addons') . "/{$name}/";
  73. }
  74. /**
  75. * 验证插件目录是否存在
  76. *
  77. * @param $name
  78. * @return bool
  79. */
  80. public static function has($name)
  81. {
  82. if (!is_dir(static::getAddonRootPath($name))) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * @param $name
  89. * @return string
  90. * @throws \League\Flysystem\FileExistsException
  91. * @throws \League\Flysystem\FileNotFoundException
  92. */
  93. public static function getAddonIcon($name)
  94. {
  95. $adapter = new Local(Yii::getAlias('@root'));
  96. $filesystem = new Filesystem($adapter);
  97. $localIconPath = static::getAddonRoot($name) . 'icon.jpg';
  98. if ($filesystem->has($localIconPath)) {
  99. $md5 = md5(Json::encode($filesystem->getMetadata($localIconPath)));
  100. $newPath = '/assets/tmp/' . $md5 . '.jpg';
  101. $newLocalIconPath = 'web/backend' . $newPath;
  102. if (!$filesystem->has($newLocalIconPath)) {
  103. $filesystem->copy($localIconPath, $newLocalIconPath);
  104. }
  105. // 后台独立域名
  106. if ($backendUrl = Yii::getAlias('@backendUrl')) {
  107. return $backendUrl . $newPath;
  108. }
  109. return '/backend' . $newPath;
  110. }
  111. return Yii::getAlias('@web') . '/resources/img/icon.jpg';
  112. }
  113. /**
  114. * 获取生成asset的资源文件目录
  115. *
  116. * @param string $assets
  117. * @return string
  118. */
  119. public static function filePath($assets = '')
  120. {
  121. if (!$assets) {
  122. $assets = [];
  123. $assets[] = 'addons';
  124. $assets[] = Yii::$app->params['addon']['name'];
  125. $assets[] = Yii::$app->params['realAppId'];
  126. $assets[] = 'assets';
  127. $assets[] = 'AppAsset';
  128. $assets = implode('\\', $assets);
  129. }
  130. if (!isset(Yii::$app->view->assetBundles[$assets])) {
  131. /* @var $assets \yii\web\AssetBundle */
  132. $assets::register(Yii::$app->view);
  133. }
  134. return Yii::$app->view->assetBundles[$assets]->baseUrl . '/';
  135. }
  136. /**
  137. * 获取资源文件
  138. *
  139. * @return string
  140. */
  141. public static function file($path, $assets = '')
  142. {
  143. return self::filePath($assets) . $path;
  144. }
  145. /**
  146. * @param $path
  147. * @param array $options
  148. * @param string $assets
  149. * @return string
  150. */
  151. public static function jsFile($path, $options = [], $assets = '')
  152. {
  153. return Html::jsFile(self::filePath($assets) . $path, $options);
  154. }
  155. /**
  156. * @param $path
  157. * @param array $options
  158. * @param string $assets
  159. * @return string
  160. */
  161. public static function cssFile($path, $options = [], $assets = '')
  162. {
  163. return Html::cssFile(self::filePath($assets) . $path, $options);
  164. }
  165. /**
  166. * 获取后台配置信息
  167. *
  168. * @return array|mixed
  169. */
  170. public static function getBackendConfig($noCache = false, $name = '')
  171. {
  172. return static::findConfig($noCache, '', $name, AppEnum::BACKEND);
  173. }
  174. /**
  175. * 获取商户配置信息
  176. *
  177. * @return array|mixed
  178. */
  179. public static function getMerchantConfig($noCache = false, $name = '', $merchant_id = '')
  180. {
  181. !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
  182. !$merchant_id && $merchant_id == 1;
  183. return static::findConfig($noCache, $merchant_id, $name, AppEnum::MERCHANT);
  184. }
  185. /**
  186. * 获取商户配置信息
  187. *
  188. * @return array|mixed
  189. */
  190. public static function getConfig($noCache = false, $name = '', $merchant_id = '')
  191. {
  192. empty($merchant_id) && $merchant_id = Yii::$app->services->merchant->getId();
  193. $app_id = empty($merchant_id) ? AppEnum::BACKEND : AppEnum::MERCHANT;
  194. return static::findConfig($noCache, $merchant_id, $name, $app_id);
  195. }
  196. /**
  197. * 写入配置信息
  198. *
  199. * @param array $config
  200. * @param string $app_id
  201. * @return array|mixed
  202. */
  203. public static function setConfig(array $config, $merchant_id = '', $app_id = '')
  204. {
  205. !$app_id && $app_id = Yii::$app->id;
  206. !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
  207. $name = Yii::$app->params['addon']['name'];
  208. if (empty($configModel = Yii::$app->services->addonsConfig->findByName($name, $app_id, $merchant_id))) {
  209. $configModel = new AddonsConfig();
  210. $configModel->addons_name = $name;
  211. $configModel->app_id = $app_id;
  212. $configModel->data = [];
  213. }
  214. $configModel->data = array_merge($configModel->data, $config);
  215. $configModel->save();
  216. return self::getConfig(true, $name, $merchant_id);
  217. }
  218. /**
  219. * 获取配置信息
  220. *
  221. * @return array|mixed
  222. */
  223. public static function findConfig($noCache, $merchant_id, $name, $app_id)
  224. {
  225. !$name && $name = Yii::$app->params['addon']['name'];
  226. $updated_at = Yii::$app->params['addon']['updated_at'] ?? '';
  227. $cacheKey = [
  228. 'addonsConfig',
  229. $app_id,
  230. $name,
  231. $updated_at,
  232. $merchant_id
  233. ];
  234. $cacheKey = implode(':', $cacheKey);
  235. // 判断是否已经读取
  236. if (isset(self::$_config[$cacheKey]) && $noCache == false) {
  237. return self::$_config[$cacheKey];
  238. }
  239. if ($noCache == true || !($configModel = Yii::$app->cache->get($cacheKey))) {
  240. if (empty($configModel = Yii::$app->services->addonsConfig->findByName($name, $app_id, $merchant_id))) {
  241. return [];
  242. }
  243. Yii::$app->cache->set($cacheKey, $configModel, 7200);
  244. }
  245. self::$_config[$cacheKey] = $configModel->data;
  246. return self::$_config[$cacheKey];
  247. }
  248. /**
  249. * 调用其他插件的服务
  250. *
  251. * @param $name
  252. * @return object
  253. * @throws NotFoundHttpException
  254. * @throws \yii\base\InvalidConfigException
  255. * @throws \yii\di\NotInstantiableException
  256. */
  257. public static function service($name)
  258. {
  259. if (!$name) {
  260. throw new NotFoundHttpException("插件名称不能为空");
  261. }
  262. if (!($addon = Yii::$app->services->addons->findByNameWithBinding($name))) {
  263. throw new NotFoundHttpException($name . "插件不存在,请先安装");
  264. }
  265. // 初始化服务
  266. if (empty($addon->service)) {
  267. throw new NotFoundHttpException($name . "不支持服务调用");
  268. }
  269. // 动态注入服务
  270. $service_name = lcfirst($addon->name) . 'Service';
  271. if (isset(self::$_service[$service_name])) {
  272. return self::$_service[$service_name];
  273. }
  274. Yii::$app->set($service_name, [
  275. 'class' => $addon->service,
  276. ]);
  277. self::$_service[$service_name] = Yii::$app->get($service_name);
  278. return self::$_service[$service_name];
  279. }
  280. /**
  281. * 初始化模块信息
  282. *
  283. * @param string $name 模块名称
  284. * @param string $route 路由
  285. * @return bool
  286. * @throws NotFoundHttpException
  287. */
  288. public static function initAddon($name)
  289. {
  290. if (!$name) {
  291. throw new NotFoundHttpException("插件不能为空");
  292. }
  293. if (!($addon = Yii::$app->services->addons->findByNameWithBinding($name, Yii::$app->id == AppEnum::BACKEND))) {
  294. throw new NotFoundHttpException("插件不存在");
  295. }
  296. // 当前模块实例
  297. Yii::$app->params['addon'] = $addon;
  298. // 菜单
  299. Yii::$app->params['menu']['menu'] = !empty($addon['menu']) ? ArrayHelper::toArray($addon['menu']) : [];
  300. // 导航
  301. Yii::$app->params['menu']['cover'] = !empty($addon['bindingCover']) ? ArrayHelper::toArray($addon['bindingCover']) : [];
  302. Yii::$app->params['addonName'] = StringHelper::toUnderScore(Yii::$app->params['addon']['name']);
  303. Yii::$app->params['inAddon'] = true;
  304. return true;
  305. }
  306. /**
  307. * 全局功能调用
  308. * @Author: hua
  309. * @Date: 2022/3/23
  310. * @Time: 18:47
  311. * @Copyright: copyright (c) 2021 广东七件事集团
  312. * @param $invoke_type
  313. * @param int $mall_id
  314. */
  315. // public static function globalFuncInvoke($invoke_type, $mall_id = 0, $params = [])
  316. // {
  317. // //获取商城已安装插件列表
  318. // $addons_list = MallAddons::lists(['where' => [
  319. // ['mall_id' => $mall_id],
  320. // ['status' => StatusEnum::ENABLED],
  321. // ], 'select' => 'addons_name']);
  322. // $install_addons = array_column($addons_list, 'addons_name');
  323. // switch ($invoke_type) {
  324. // //提现条件全局调用
  325. // case GlobalFuncEnum::WITHDRAW_COND:
  326. // $return = [];
  327. // $invoke_list = AddonsInvoker::lists(['where' => [['invoker_id' => GlobalFuncEnum::WITHDRAW_COND, 'addons_name' => $install_addons]]]);
  328. // foreach ($invoke_list as $invoke_info) {
  329. // $method = $invoke_info['method'];
  330. // if (class_exists($invoke_info['class']) == false) continue;
  331. // $obj = new $invoke_info['class']();
  332. // if (method_exists($obj, $method) == false) continue;
  333. // $result = $obj->$method();
  334. // $return = array_merge($return, $result);
  335. // }
  336. // return $return;
  337. // break;
  338. // //分享海报
  339. // case GlobalFuncEnum::SHARE_VENGEANCE:
  340. // $return = [];
  341. // $invoke_list = AddonsInvoker::lists(['where' => [['invoker_id' => GlobalFuncEnum::SHARE_VENGEANCE, 'addons_name' => $install_addons]]]);
  342. // foreach ($invoke_list as $invoke_info) {
  343. // $method = $invoke_info['method'];
  344. // if (class_exists($invoke_info['class']) == false) continue;
  345. // $obj = new $invoke_info['class']();
  346. // if (method_exists($obj, $method) == false) continue;
  347. // $return = $obj->$method($mall_id,$params);
  348. // }
  349. // return $return;
  350. // break;
  351. //
  352. // }
  353. // }
  354. public static function globalFuncInvoke($invoke_type, $mall_id = 0, $params = [])
  355. {
  356. //获取商城已安装插件列表
  357. $install_addons = MallAddons::getValidAddons($mall_id);
  358. $param = Yii::$app->params;
  359. $params['mall_id'] = $mall_id;
  360. $return = [];
  361. if(isset($param['invoker'][$invoke_type])){
  362. if(class_exists($param['invoker'][$invoke_type]['class'])){
  363. $class = $param['invoker'][$invoke_type]['class'];
  364. $obj = new $class;
  365. if(method_exists($obj,'handle'))$return = $obj->handle($invoke_type,$install_addons,$params);
  366. }
  367. }
  368. return $return;
  369. }
  370. }