AddonService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\addon;
  12. use app\dict\addon\AddonDict;
  13. use app\model\addon\Addon;
  14. use app\service\core\addon\CoreAddonCloudService;
  15. use app\service\core\addon\CoreAddonDownloadService;
  16. use app\service\core\addon\CoreAddonInstallService;
  17. use app\service\core\addon\CoreAddonService;
  18. use app\service\core\site\CoreSiteService;
  19. use core\base\BaseAdminService;
  20. use core\exception\CommonException;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. /**
  25. * 消息管理服务层
  26. */
  27. class AddonService extends BaseAdminService
  28. {
  29. public static $cache_tag_name = 'addon_cache';
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->model = new Addon();
  34. }
  35. public function getList(){
  36. return (new CoreAddonService())->getLocalAddonList();
  37. }
  38. /**
  39. * 获取当前站点消息
  40. * @return array
  41. */
  42. public function getLocalAddonList()
  43. {
  44. return (new CoreAddonService())->getLocalAddonList();
  45. }
  46. /**
  47. * 安装插件
  48. * @param string $addon
  49. */
  50. public function install(string $addon)
  51. {
  52. return ( new CoreAddonInstallService($addon) )->install();
  53. }
  54. /**
  55. * 云安装插件
  56. * @param string $addon
  57. */
  58. public function cloudInstall(string $addon)
  59. {
  60. return ( new CoreAddonInstallService($addon) )->install('cloud');
  61. }
  62. /**
  63. * 云安装日志
  64. * @param string $addon
  65. * @return null
  66. */
  67. public function cloudInstallLog(string $addon)
  68. {
  69. return ( new CoreAddonCloudService() )->getBuildLog($addon);
  70. }
  71. /**
  72. * 获取安装任务
  73. * @return mixed
  74. */
  75. public function getInstallTask()
  76. {
  77. return ( new CoreAddonInstallService('') )->getInstallTask();
  78. }
  79. /**
  80. * 安装插件检测安装环境
  81. * @param string $addon
  82. */
  83. public function installCheck(string $addon)
  84. {
  85. return ( new CoreAddonInstallService($addon) )->installCheck();
  86. }
  87. /**
  88. * 取消安装任务
  89. * @param string $addon
  90. */
  91. public function cancleInstall(string $addon)
  92. {
  93. return ( new CoreAddonInstallService($addon) )->cancleInstall();
  94. }
  95. /**
  96. * @param string $addon
  97. * @return void
  98. */
  99. public function uninstallCheck(string $addon) {
  100. return ( new CoreAddonInstallService($addon) )->uninstallCheck();
  101. }
  102. /**
  103. * 卸载插件
  104. * @param string $addon
  105. * @return true
  106. */
  107. public function uninstall(string $addon)
  108. {
  109. return CoreAddonInstallService::instance($addon)->uninstall();
  110. }
  111. /**
  112. * 获取插件列表
  113. * @param array $where
  114. * @return array
  115. */
  116. public function getPage(array $where = [])
  117. {
  118. return (new CoreAddonService())->getPage($where);
  119. }
  120. /**
  121. * 获取插件信息
  122. * @param int $id
  123. * @return array
  124. */
  125. public function getInfo(int $id)
  126. {
  127. return (new CoreAddonService())->getInfo($id);
  128. }
  129. /**
  130. * 设置插件状态
  131. * @param int $id
  132. * @param int $status
  133. */
  134. public function setStatus(int $id, int $status){
  135. return (new CoreAddonService())->setStatus($id, $status);
  136. }
  137. /**
  138. * 下载应用
  139. * @param string $app_key
  140. * @return true
  141. */
  142. public function download(string $app_key, string $version){
  143. if (empty($version)) throw new CommonException('ADDON_DOWNLOAD_VERSION_EMPTY');
  144. return (new CoreAddonDownloadService())->download($app_key, $version);
  145. }
  146. /**
  147. * 查询已安装应用
  148. * @return array
  149. */
  150. public function getInstallList(){
  151. return (new CoreAddonService())->getInstallAddonList();
  152. }
  153. /**
  154. * 获取站点拥有的应用列表
  155. * @param int $site_id
  156. * @return array|mixed|string|void
  157. * @throws DbException
  158. * @throws DataNotFoundException
  159. * @throws ModelNotFoundException
  160. */
  161. public function getAddonListBySiteId(int $site_id){
  162. $addon_keys = $this->getAddonKeysBySiteId($site_id);
  163. return $this->getAddonListByKeys($addon_keys);
  164. }
  165. /**
  166. * 应用key缓存
  167. * @param $keys
  168. * @return mixed|string
  169. * @throws DbException
  170. * @throws DataNotFoundException
  171. * @throws ModelNotFoundException
  172. */
  173. public function getAddonListByKeys($keys){
  174. sort($keys);
  175. $cache_name = 'addon_list'.implode('_', $keys);
  176. return cache_remember(
  177. $cache_name,
  178. function () use ($keys) {
  179. $where = [
  180. ['key', 'in', $keys],
  181. ['status', '=', AddonDict::ON]
  182. ];
  183. return $this->model->where($where)->field('title, icon, key, desc, status, cover')->select()->toArray();
  184. },
  185. self::$cache_tag_name
  186. );
  187. }
  188. /**
  189. * 获取站点支持的应用插件
  190. * @param int $site_id
  191. * @return array
  192. */
  193. public function getAddonKeysBySiteId(int $site_id){
  194. return (new CoreSiteService())->getAddonKeysBySiteId($site_id);
  195. }
  196. /**
  197. * 获取插件信息
  198. * @param int $id
  199. * @return array
  200. */
  201. public function getInfoByKey(string $key)
  202. {
  203. return $this->model->where([ [ 'key', '=', $key ] ])->field('title, icon, key, desc, status, cover')->findOrEmpty()->toArray();
  204. }
  205. }