Module.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller;
  3. use Throwable;
  4. use ba\Exception;
  5. use think\facade\Config;
  6. use app\admin\model\AdminLog;
  7. use app\admin\library\module\Server;
  8. use app\admin\library\module\Manage;
  9. use app\common\controller\Backend;
  10. class Module extends Backend
  11. {
  12. protected array $noNeedPermission = ['state', 'dependentInstallComplete'];
  13. public function initialize(): void
  14. {
  15. parent::initialize();
  16. }
  17. public function index(): void
  18. {
  19. $this->success('', [
  20. 'sysVersion' => Config::get('buildadmin.version'),
  21. 'installed' => Server::installedList(root_path() . 'modules' . DIRECTORY_SEPARATOR),
  22. ]);
  23. }
  24. public function state(): void
  25. {
  26. $uid = $this->request->get("uid/s", '');
  27. if (!$uid) {
  28. $this->error(__('Parameter error'));
  29. }
  30. $this->success('', [
  31. 'state' => Manage::instance($uid)->getInstallState()
  32. ]);
  33. }
  34. public function install(): void
  35. {
  36. AdminLog::setTitle(__('Install module'));
  37. $uid = $this->request->get("uid/s", '');
  38. $token = $this->request->get("token/s", '');
  39. $orderId = $this->request->get("orderId/d", 0);
  40. if (!$uid) {
  41. $this->error(__('Parameter error'));
  42. }
  43. $res = [];
  44. try {
  45. $res = Manage::instance($uid)->install($token, $orderId);
  46. } catch (Exception $e) {
  47. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  48. } catch (Throwable $e) {
  49. $this->error(__($e->getMessage()));
  50. }
  51. $this->success('', [
  52. 'data' => $res,
  53. ]);
  54. }
  55. public function dependentInstallComplete(): void
  56. {
  57. $uid = $this->request->get("uid/s", '');
  58. if (!$uid) {
  59. $this->error(__('Parameter error'));
  60. }
  61. try {
  62. Manage::instance($uid)->dependentInstallComplete('all');
  63. } catch (Exception $e) {
  64. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  65. } catch (Throwable $e) {
  66. $this->error(__($e->getMessage()));
  67. }
  68. $this->success();
  69. }
  70. public function changeState(): void
  71. {
  72. AdminLog::setTitle(__('Change module state'));
  73. $uid = $this->request->post("uid/s", '');
  74. $state = $this->request->post("state/b", false);
  75. if (!$uid) {
  76. $this->error(__('Parameter error'));
  77. }
  78. $info = [];
  79. try {
  80. $info = Manage::instance($uid)->changeState($state);
  81. } catch (Exception $e) {
  82. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  83. } catch (Throwable $e) {
  84. $this->error(__($e->getMessage()));
  85. }
  86. $this->success('', [
  87. 'info' => $info,
  88. ]);
  89. }
  90. public function uninstall(): void
  91. {
  92. AdminLog::setTitle(__('Unload module'));
  93. $uid = $this->request->get("uid/s", '');
  94. if (!$uid) {
  95. $this->error(__('Parameter error'));
  96. }
  97. try {
  98. Manage::instance($uid)->uninstall();
  99. } catch (Exception $e) {
  100. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  101. } catch (Throwable $e) {
  102. $this->error(__($e->getMessage()));
  103. }
  104. $this->success();
  105. }
  106. public function update(): void
  107. {
  108. AdminLog::setTitle(__('Update module'));
  109. $uid = $this->request->get("uid/s", '');
  110. $token = $this->request->get("token/s", '');
  111. $orderId = $this->request->get("orderId/d", 0);
  112. if (!$token || !$uid) {
  113. $this->error(__('Parameter error'));
  114. }
  115. try {
  116. Manage::instance($uid)->update($token, $orderId);
  117. } catch (Exception $e) {
  118. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  119. } catch (Throwable $e) {
  120. $this->error(__($e->getMessage()));
  121. }
  122. $this->success();
  123. }
  124. public function upload(): void
  125. {
  126. AdminLog::setTitle(__('Upload install module'));
  127. $file = $this->request->get("file/s", '');
  128. $token = $this->request->get("token/s", '');
  129. if (!$file) $this->error(__('Parameter error'));
  130. if (!$token) $this->error(__('Please login to the official website account first'));
  131. $info = [];
  132. try {
  133. $info = Manage::instance()->upload($token, $file);
  134. } catch (Exception $e) {
  135. $this->error(__($e->getMessage()), $e->getData(), $e->getCode());
  136. } catch (Throwable $e) {
  137. $this->error(__($e->getMessage()));
  138. }
  139. $this->success('', [
  140. 'info' => $info
  141. ]);
  142. }
  143. }