Module.php 4.0 KB

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