UpgradeService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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\upgrade;
  12. use app\dict\addon\AddonDict;
  13. use app\model\addon\Addon;
  14. use app\service\admin\install\InstallSystemService;
  15. use app\service\core\addon\CoreAddonCloudService;
  16. use app\service\core\addon\CoreAddonInstallService;
  17. use app\service\core\addon\CoreAddonService;
  18. use app\service\core\addon\CoreDependService;
  19. use app\service\core\addon\WapTrait;
  20. use app\service\core\menu\CoreMenuService;
  21. use app\service\core\niucloud\CoreModuleService;
  22. use app\service\core\schedule\CoreScheduleInstallService;
  23. use core\base\BaseAdminService;
  24. use core\exception\CommonException;
  25. use core\util\niucloud\BaseNiucloudClient;
  26. use think\facade\Cache;
  27. use think\facade\Db;
  28. /**
  29. * 框架及插件升级
  30. * @package app\service\core\upgrade
  31. */
  32. class UpgradeService extends BaseAdminService
  33. {
  34. use WapTrait;
  35. use ExecuteSqlTrait;
  36. protected $upgrade_dir;
  37. protected $root_path;
  38. protected $cache_key = 'upgrade';
  39. protected $upgrade_task = null;
  40. protected $addon = '';
  41. private $steps = [
  42. 'requestUpgrade' => ['step' => 'requestUpgrade', 'title' => '请求升级'],
  43. 'downloadFile' => ['step' => 'downloadFile', 'title' => '下载更新文件'],
  44. 'backupCode' => ['step' => 'backupCode', 'title' => '备份源码'],
  45. 'backupSql' => ['step' => 'backupSql', 'title' => '备份数据库'],
  46. 'coverCode' => ['step' => 'coverCode', 'title' => '合并更新文件'],
  47. 'handleUniapp' => ['step' => 'handleUniapp', 'title' => '处理uniapp'],
  48. 'refreshMenu' => ['step' => 'refreshMenu', 'title' => '刷新菜单'],
  49. 'installSchedule' => ['step' => 'installSchedule', 'title' => '安装计划任务'],
  50. 'upgradeComplete' => ['step' => 'upgradeComplete', 'title' => '升级完成']
  51. ];
  52. public function __construct()
  53. {
  54. parent::__construct();
  55. $this->root_path = dirname(root_path()) . DIRECTORY_SEPARATOR;
  56. $this->upgrade_dir = $this->root_path . 'upgrade' . DIRECTORY_SEPARATOR;
  57. $this->upgrade_task = Cache::get($this->cache_key);
  58. }
  59. /**
  60. * 升级前环境检测
  61. * @param string $addon
  62. * @return void
  63. */
  64. public function upgradePreCheck(string $addon = '') {
  65. $niucloud_dir = $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR;
  66. $admin_dir = $this->root_path . 'admin' . DIRECTORY_SEPARATOR;
  67. $web_dir = $this->root_path . 'web' . DIRECTORY_SEPARATOR;
  68. $wap_dir = $this->root_path . 'uni-app' . DIRECTORY_SEPARATOR;
  69. try {
  70. if (!is_dir($admin_dir)) throw new CommonException('ADMIN_DIR_NOT_EXIST');
  71. if (!is_dir($web_dir)) throw new CommonException('WEB_DIR_NOT_EXIST');
  72. if (!is_dir($wap_dir)) throw new CommonException('UNIAPP_DIR_NOT_EXIST');
  73. } catch (\Exception $e) {
  74. throw new CommonException($e->getMessage());
  75. }
  76. $data = [
  77. // 目录检测
  78. 'dir' => [
  79. // 要求可读权限
  80. 'is_readable' => [],
  81. // 要求可写权限
  82. 'is_write' => []
  83. ]
  84. ];
  85. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $niucloud_dir), 'status' => is_readable($niucloud_dir)];
  86. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $admin_dir), 'status' => is_readable($admin_dir)];
  87. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $web_dir), 'status' => is_readable($web_dir)];
  88. $data['dir']['is_readable'][] = ['dir' => str_replace(project_path(), '', $wap_dir), 'status' => is_readable($wap_dir)];
  89. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $niucloud_dir), 'status' => is_write($niucloud_dir) ];
  90. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $admin_dir), 'status' => is_write($admin_dir) ];
  91. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $web_dir), 'status' => is_write($web_dir) ];
  92. $data['dir']['is_write'][] = ['dir' => str_replace(project_path(), '', $wap_dir), 'status' => is_write($wap_dir) ];
  93. $check_res = array_merge(
  94. array_column($data['dir']['is_readable'], 'status'),
  95. array_column($data['dir']['is_write'], 'status')
  96. );
  97. // 是否通过校验
  98. $data['is_pass'] = !in_array(false, $check_res);
  99. return $data;
  100. }
  101. /**
  102. * 升级
  103. * @param $addon
  104. * @return array
  105. */
  106. public function upgrade(string $addon = '') {
  107. if ($this->upgrade_task) throw new CommonException('UPGRADE_TASK_EXIST');
  108. $upgrade = [
  109. 'product_key' => BaseNiucloudClient::PRODUCT,
  110. 'framework_version' => config('version.version')
  111. ];
  112. if (!$addon) {
  113. $upgrade['app_key'] = AddonDict::FRAMEWORK_KEY;
  114. $upgrade['version'] = config('version.version');
  115. } else {
  116. $upgrade['app_key'] = $addon;
  117. $upgrade['version'] = (new Addon())->where([ ['key', '=', $addon] ])->value('version');
  118. }
  119. $response = (new CoreAddonCloudService())->upgradeAddon($upgrade);
  120. if (isset($response['code']) && $response['code'] == 0) throw new CommonException($response['msg']);
  121. try {
  122. $key = uniqid();
  123. $upgrade_dir = $this->upgrade_dir . $key . DIRECTORY_SEPARATOR;
  124. if (!is_dir($upgrade_dir)) {
  125. dir_mkdir($upgrade_dir);
  126. }
  127. $upgrade_tsak = [
  128. 'key' => $key,
  129. 'upgrade' => $upgrade,
  130. 'step' => 'requestUpgrade',
  131. 'executed' => ['requestUpgrade'],
  132. 'log' => [ $this->steps['requestUpgrade']['title'] ],
  133. 'params' => ['token' => $response['token'] ],
  134. 'upgrade_content' => $this->getUpgradeContent($addon)
  135. ];
  136. Cache::set($this->cache_key, $upgrade_tsak);
  137. return $upgrade_tsak;
  138. } catch (\Exception $e) {
  139. throw new CommonException($e->getMessage());
  140. }
  141. }
  142. /**
  143. * 执行升级
  144. * @return true
  145. */
  146. public function execute() {
  147. if (!$this->upgrade_task) return true;
  148. $steps = array_keys($this->steps);
  149. $index = array_search($this->upgrade_task['step'], $steps);
  150. $step = $steps[ $index + 1 ] ?? '';
  151. $params = $this->upgrade_task['params'] ?? [];
  152. if ($step) {
  153. try {
  154. $res = $this->$step(...$params);
  155. if (is_array($res)) {
  156. $this->upgrade_task['params'] = $res;
  157. } else {
  158. $this->upgrade_task['step'] = $step;
  159. $this->upgrade_task['params'] = [];
  160. $this->upgrade_task['executed'][] = $step;
  161. $this->upgrade_task['log'][] = $this->steps[$step]['title'];
  162. }
  163. Cache::set($this->cache_key, $this->upgrade_task);
  164. } catch (\Exception $e) {
  165. $this->upgrade_task['step'] = $step;
  166. $this->upgrade_task['error'] = $e->getMessage();
  167. $this->upgradeErrorHandle();
  168. }
  169. return true;
  170. } else {
  171. return true;
  172. }
  173. }
  174. /**
  175. * 下载升级文件
  176. * @param string $token
  177. * @param string $dir
  178. * @param int $index
  179. * @param $step
  180. * @return true|null
  181. */
  182. public function downloadFile(string $token, string $dir = '', int $index = -1, $step = 0, $length = 0) {
  183. if (!$dir) {
  184. $dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR;
  185. dir_mkdir($dir);
  186. }
  187. $res = (new CoreAddonCloudService())->downloadUpgradeFile($token, $dir, $index, $step, $length);
  188. return $res;
  189. }
  190. /**
  191. * 备份源码
  192. * @return true
  193. */
  194. public function backupCode() {
  195. (new BackupService())->backupCode();
  196. return true;
  197. }
  198. /**
  199. * 备份数据库
  200. * @return true
  201. */
  202. public function backupSql() {
  203. (new BackupService())->backupSql();
  204. return true;
  205. }
  206. /**
  207. * 覆盖更新升级的代码
  208. * @return void
  209. */
  210. public function coverCode($index = 0) {
  211. $this->upgrade_task['is_cover'] = 1;
  212. $addon = $this->upgrade_task['upgrade']['app_key'];
  213. $version_list = array_reverse($this->upgrade_task['upgrade_content']['version_list']);
  214. $code_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  215. $version_item = $version_list[$index];
  216. $version_no = $version_item['version_no'];
  217. $to_dir = $addon == AddonDict::FRAMEWORK_KEY ? $this->root_path : $this->root_path . 'niucloud' . DIRECTORY_SEPARATOR . 'addon' . DIRECTORY_SEPARATOR . $addon;
  218. // 获取文件变更记录
  219. if (file_exists($code_dir . $version_no . '.txt')) {
  220. $change = array_filter(explode("\n", file_get_contents($code_dir . $version_no . '.txt')));
  221. foreach ($change as &$item) {
  222. list($operation, $md5, $file) = $item = explode(' ', $item);
  223. if ($operation == '-') {
  224. @unlink($to_dir . $file);
  225. }
  226. }
  227. // 合并依赖
  228. $this->installDepend($code_dir . $version_no, array_column($change, 2));
  229. }
  230. // 覆盖文件
  231. if (is_dir($code_dir . $version_no)) {
  232. dir_copy($code_dir . $version_no, $to_dir);
  233. if ($addon != AddonDict::FRAMEWORK_KEY) {
  234. (new CoreAddonInstallService($addon))->installDir();
  235. }
  236. }
  237. $upgrade_file_dir = 'v' . str_replace('.', '', $version_no);
  238. if ($addon == AddonDict::FRAMEWORK_KEY) {
  239. $class_path = "\\app\\upgrade\\{$upgrade_file_dir}\\Upgrade";
  240. $sql_file = root_path() . 'app' . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . $upgrade_file_dir . DIRECTORY_SEPARATOR . 'upgrade.sql';
  241. } else {
  242. $class_path = "\\addon\\{$addon}\\app\\upgrade\\{$upgrade_file_dir}\\Upgrade";
  243. $sql_file = root_path() . 'addon' . DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR . $upgrade_file_dir . DIRECTORY_SEPARATOR . 'upgrade.sql';
  244. }
  245. // 执行升级sql
  246. if (file_exists($sql_file)) {
  247. $this->executeSql($sql_file);
  248. }
  249. // 执行升级方法
  250. if (class_exists($class_path)) {
  251. (new $class_path())->handle();
  252. }
  253. $index ++;
  254. if ($index < count($version_list)) {
  255. return compact('index');
  256. } else {
  257. return true;
  258. }
  259. }
  260. /**
  261. * 合并依赖
  262. * @param string $version_no
  263. * @return void
  264. */
  265. public function installDepend(string $dir, array $change_files) {
  266. $addon = $this->upgrade_task['upgrade']['app_key'];
  267. $depend_service = new CoreDependService();
  268. if ($addon == AddonDict::FRAMEWORK_KEY) {
  269. $composer = '/niucloud/composer.json';
  270. $admin_package = '/admin/package.json';
  271. $web_package = '/web/package.json';
  272. $uniapp_package = '/uni-app/package.json';
  273. } else {
  274. $composer = "/niucloud/addon/{$addon}/package/composer.json";
  275. $admin_package = "/niucloud/addon/{$addon}/package/admin-package.json";
  276. $web_package = "/niucloud/addon/{$addon}/package/web-package.json";
  277. $uniapp_package = "/niucloud/addon/{$addon}/package/uni-app-package.json";
  278. }
  279. if (in_array($composer, $change_files)) {
  280. $original = $depend_service->getComposerContent();
  281. $new = $depend_service->jsonFileToArray($dir . $composer);
  282. foreach ($new as $name => $value) {
  283. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  284. }
  285. $depend_service->writeArrayToJsonFile($original, $dir . $composer);
  286. }
  287. if (in_array($admin_package, $change_files)) {
  288. $original = $depend_service->getNpmContent('admin');
  289. $new = $depend_service->jsonFileToArray($dir . $admin_package);
  290. foreach ($new as $name => $value) {
  291. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  292. }
  293. $depend_service->writeArrayToJsonFile($original, $dir . $admin_package);
  294. }
  295. if (in_array($web_package, $change_files)) {
  296. $original = $depend_service->getNpmContent('web');
  297. $new = $depend_service->jsonFileToArray($dir . $web_package);
  298. foreach ($new as $name => $value) {
  299. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  300. }
  301. $depend_service->writeArrayToJsonFile($original, $dir . $web_package);
  302. }
  303. if (in_array($uniapp_package, $change_files)) {
  304. $original = $depend_service->getNpmContent('uni-app');
  305. $new = $depend_service->jsonFileToArray($dir . $uniapp_package);
  306. foreach ($new as $name => $value) {
  307. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  308. }
  309. $depend_service->writeArrayToJsonFile($original, $dir . $uniapp_package);
  310. }
  311. }
  312. /**
  313. * 处理手机端
  314. * @param string $verson_no
  315. * @return true
  316. */
  317. public function handleUniapp() {
  318. $code_dir = $this->upgrade_dir .$this->upgrade_task['key'] . DIRECTORY_SEPARATOR . 'download' . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR;
  319. dir_copy($code_dir . 'uni-app', $this->root_path . 'uni-app');
  320. $addon_list = (new CoreAddonService())->getInstallAddonList();
  321. //todo 插件升级
  322. $depend_service = new CoreDependService();
  323. if (!empty($addon_list)) {
  324. foreach ($addon_list as $addon => $item) {
  325. $this->addon = $addon;
  326. // 编译 diy-group 自定义组件代码文件
  327. $this->compileDiyComponentsCode($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $addon);
  328. // 编译 fixed-group 固定模板组件代码文件
  329. $this->compileFixedComponentsCode($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $addon);
  330. // 编译 pages.json 页面路由代码文件
  331. $this->installPageCode($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR);
  332. // 编译 加载插件标题语言包
  333. $this->compileLocale($this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $addon);
  334. // 合并插件依赖
  335. $addon_uniapp_package = str_replace('/', DIRECTORY_SEPARATOR, project_path() . "niucloud/addon/{$addon}/package/uni-app-package.json");
  336. if (file_exists($addon_uniapp_package)) {
  337. $original = $depend_service->getNpmContent('uni-app');
  338. $new = $depend_service->jsonFileToArray($addon_uniapp_package);
  339. foreach ($new as $name => $value) {
  340. $original[$name] = isset($original[$name]) && is_array($original[$name]) ? array_merge($original[$name], $new[$name]) : $new[$name];
  341. }
  342. $uniapp_package = $this->root_path . 'uni-app' . DIRECTORY_SEPARATOR . 'package.json';
  343. $depend_service->writeArrayToJsonFile($original, $uniapp_package);
  344. }
  345. }
  346. }
  347. return true;
  348. }
  349. /**
  350. * 执行升级sql
  351. * @param string $sql_file
  352. * @return true
  353. */
  354. private function executeSql(string $sql_file) {
  355. $sql_content = file_get_contents($sql_file);
  356. if (!empty($sql_content)) {
  357. $prefix = config('database.connections.mysql.prefix');
  358. $sql_data = array_filter($this->getSqlQuery($sql_content));
  359. if (!empty($sql_data)) {
  360. foreach ($sql_data as $sql) {
  361. $sql = $prefix ? $this->handleSqlPrefix($sql, $prefix) : $sql;
  362. Db::query($sql);
  363. }
  364. }
  365. }
  366. return true;
  367. }
  368. /**
  369. * 刷新菜单
  370. * @return void
  371. */
  372. public function refreshMenu() {
  373. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  374. (new InstallSystemService())->installMenu();
  375. } else {
  376. (new CoreMenuService())->refreshAddonMenu($this->upgrade_task['upgrade']['app_key']);
  377. }
  378. return true;
  379. }
  380. /**
  381. * 安装计划任务
  382. * @return true
  383. */
  384. public function installSchedule() {
  385. if ($this->upgrade_task['upgrade']['app_key'] == AddonDict::FRAMEWORK_KEY) {
  386. (new CoreScheduleInstallService())->installSystemSchedule();
  387. } else {
  388. (new CoreScheduleInstallService())->installAddonSchedule($this->upgrade_task['upgrade']['app_key']);
  389. }
  390. return true;
  391. }
  392. /**
  393. * 更新完成
  394. * @return void
  395. */
  396. public function upgradeComplete() {
  397. $addon = $this->upgrade_task['upgrade']['app_key'];
  398. if ($addon != AddonDict::FRAMEWORK_KEY) {
  399. $core_addon_service = new CoreAddonService();
  400. $install_data = $core_addon_service->getAddonConfig($addon);
  401. $install_data['icon'] = 'addon/' . $addon . '/icon.png';
  402. $core_addon_service->set($install_data);
  403. }
  404. $this->clearUpgradeTask(5);
  405. return true;
  406. }
  407. /**
  408. * 升级出错之后的处理
  409. * @return true|void
  410. */
  411. public function upgradeErrorHandle() {
  412. try {
  413. if (isset($this->upgrade_task['is_cover'])) {
  414. $restore_service = (new RestoreService());
  415. $restore_service->restoreCode();
  416. $restore_service->restoreSql();
  417. }
  418. $this->clearUpgradeTask(5);
  419. return true;
  420. } catch (\Exception $e) {
  421. $this->clearUpgradeTask(5);
  422. return true;
  423. }
  424. }
  425. /**
  426. * 获取升级内容
  427. * @param string $addon
  428. * @return array|\core\util\niucloud\Response|object|\Psr\Http\Message\ResponseInterface
  429. * @throws \GuzzleHttp\Exception\GuzzleException
  430. */
  431. public function getUpgradeContent(string $addon = '') {
  432. $upgrade = [
  433. 'product_key' => BaseNiucloudClient::PRODUCT
  434. ];
  435. if (!$addon) {
  436. $upgrade['app_key'] = AddonDict::FRAMEWORK_KEY;
  437. $upgrade['version'] = config('version.version');
  438. } else {
  439. $upgrade['app_key'] = $addon;
  440. $upgrade['version'] = (new Addon())->where([ ['key', '=', $addon] ])->value('version');
  441. }
  442. return (new CoreModuleService())->getUpgradeContent($upgrade)['data'] ?? [];
  443. }
  444. /**
  445. * 获取正在进行的升级任务
  446. * @return mixed|null
  447. */
  448. public function getUpgradeTask() {
  449. return $this->upgrade_task;
  450. }
  451. /**
  452. * 清除升级任务
  453. * @return true
  454. */
  455. public function clearUpgradeTask(int $delayed = 0) {
  456. if ($delayed) {
  457. Cache::set($this->cache_key, $this->upgrade_task, $delayed);
  458. } else {
  459. Cache::set($this->cache_key, null);
  460. }
  461. return true;
  462. }
  463. /**
  464. * 获取插件定义的package目录
  465. * @param string $addon
  466. * @return string
  467. */
  468. public function geAddonPackagePath(string $addon)
  469. {
  470. return root_path() . 'addon' .DIRECTORY_SEPARATOR . $addon . DIRECTORY_SEPARATOR . 'package' . DIRECTORY_SEPARATOR;
  471. }
  472. }