Index.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\controller\admin;
  3. use app\BaseController;
  4. use app\model\CardModel;
  5. use app\model\SettingModel;
  6. use think\facade\Cache;
  7. use think\facade\Db;
  8. //use Upgrade;
  9. class Index extends BaseController
  10. {
  11. public $authService = "https://auth.mtab.cc";
  12. public $authCode = '';
  13. function setSubscription(): \think\response\Json
  14. {
  15. $this->getAdmin();
  16. $code = $this->request->post("code", "");
  17. if (trim($code)) {
  18. Db::table('setting')->replace()->insert(['keys' => 'authCode', 'value' => $code]);
  19. SettingModel::refreshSetting();
  20. }
  21. return $this->success("ok");
  22. }
  23. private function initAuth()
  24. {
  25. $authCode = $this->Setting('authCode', '', true);
  26. if (strlen($authCode) == 0) {
  27. $authCode = env('authCode', '');
  28. }
  29. $this->authCode = $authCode;
  30. $this->authService = $this->Setting('authServer', 'https://auth.mtab.cc', true);
  31. }
  32. function updateApp($n = 0): \think\response\Json
  33. {
  34. $this->getAdmin();
  35. $this->initAuth();
  36. $result = \Axios::http()->post($this->authService . '/getUpGrade', [
  37. 'timeout' => 10,
  38. 'form_params' => [
  39. 'authorization_code' => $this->authCode,
  40. 'version_code' => app_version_code,
  41. ]
  42. ]);
  43. if ($result->getStatusCode() == 200) {
  44. $json = json_decode($result->getBody()->getContents(), true);
  45. if ($json['code'] === 1) {
  46. $upgradePhp = runtime_path() . 'update.php';
  47. $f = "";
  48. $upGrade = null;
  49. if (!empty($json['info']['update_php'])) {
  50. try {//用远程脚本更新,一般用不到,除非上一个版本发生一些问题需要额外脚本处理
  51. $f = file_get_contents($json['info']['update_php']);
  52. file_put_contents(runtime_path() . 'update.php', $f);
  53. require_once $upgradePhp;
  54. $upGrade = new \Upgrade();
  55. } catch (\Exception $e) {
  56. }
  57. }
  58. if ($upGrade === null) {
  59. $upGrade = new \Upgrade2();
  60. }
  61. if (!empty($json['info']['update_zip'])) {
  62. $upGrade->update_download_url = $json['info']['update_zip'];
  63. }
  64. if (!empty($json['info']['update_sql'])) {
  65. $upGrade->update_sql_url = $json['info']['update_sql'];
  66. }
  67. $status = $upGrade->run();//启动任务
  68. try {
  69. unlink($upgradePhp);
  70. } catch (\Exception $e) {
  71. }
  72. if ($status === true) {
  73. return $this->success('更新完毕');
  74. } else {
  75. return $this->error($status);
  76. }
  77. } else {
  78. return $this->error($json['msg']);
  79. }
  80. }
  81. return $this->error("没有更新的版本");
  82. }
  83. function authorization(): \think\response\Json
  84. {
  85. $this->getAdmin();
  86. $this->initAuth();
  87. $result = \Axios::http()->post($this->authService . '/checkAuth', [
  88. 'timeout' => 5,
  89. 'form_params' => [
  90. 'authorization_code' => $this->authCode,
  91. 'version_code' => app_version_code,
  92. ]
  93. ]);
  94. $info = [];
  95. $info['version'] = app_version;
  96. $info['version_code'] = app_version_code;
  97. $info['php_version'] = phpversion();
  98. if ($result->getStatusCode() == 200) {
  99. $jsonStr = $result->getBody()->getContents();
  100. $json = json_decode($jsonStr, true);
  101. $info['remote'] = $json;
  102. return $this->success($info);
  103. } else {
  104. return $this->error('授权服务器连接失败', $info);
  105. }
  106. }
  107. function cardList(): \think\response\Json
  108. {
  109. $this->getAdmin();
  110. $this->initAuth();
  111. $result = \Axios::http()->post($this->authService . '/card', [
  112. 'timeout' => 15,
  113. 'form_params' => [
  114. 'authorization_code' => $this->authCode
  115. ]
  116. ]);
  117. try {
  118. $json = $result->getBody()->getContents();
  119. $json = json_decode($json, true);
  120. if ($json['code'] === 1) {
  121. return $this->success('ok', $json['data']);
  122. }
  123. } catch (\Exception $e) {
  124. }
  125. return $this->error('远程卡片获取失败');
  126. }
  127. //获取本地应用
  128. function localCard(): \think\response\Json
  129. {
  130. $this->getAdmin();
  131. $apps = CardModel::select();
  132. return $this->success('ok', $apps);
  133. }
  134. function stopCard(): \think\response\Json
  135. {
  136. $this->getAdmin();
  137. $name_en = $this->request->post('name_en', '');
  138. CardModel::where('name_en', $name_en)->update(['status' => 0]);
  139. Cache::delete('cardList');
  140. return $this->success('设置成功');
  141. }
  142. function startCard(): \think\response\Json
  143. {
  144. $this->getAdmin();
  145. $name_en = $this->request->post('name_en', '');
  146. CardModel::where('name_en', $name_en)->update(['status' => 1]);
  147. Cache::delete('cardList');
  148. return $this->success('设置成功');
  149. }
  150. function installCard(): \think\response\Json
  151. {
  152. $this->getAdmin();
  153. $this->initAuth();
  154. $name_en = $this->request->post("name_en", '');
  155. $version = 0;
  156. $type = $this->request->post('type', 'install');
  157. if (mb_strlen($name_en) > 0) {
  158. $card = CardModel::where('name_en', $name_en)->find();
  159. if ($card) {
  160. if ($type == 'install') {
  161. return $this->error('您已安装当前卡片组件');
  162. }
  163. if ($type == 'update') {
  164. $version = $card['version'];
  165. }
  166. }
  167. $result = \Axios::http()->post($this->authService . '/installCard', [
  168. 'timeout' => 15,
  169. 'form_params' => [
  170. 'authorization_code' => $this->authCode,
  171. 'name_en' => $name_en,
  172. 'version' => $version
  173. ]
  174. ]);
  175. try {
  176. $json = $result->getBody()->getContents();
  177. $json = json_decode($json, true, JSON_UNESCAPED_UNICODE);
  178. if ($json['code'] == 0) {
  179. return $this->error($json['msg']);
  180. }
  181. return $this->installCardTask($json['data']);
  182. } catch (\Exception $e) {
  183. }
  184. }
  185. return $this->error("没有需要安装的卡片插件!");
  186. }
  187. function uninstallCard(): \think\response\Json
  188. {
  189. $this->getAdmin();
  190. $name_en = $this->request->post("name_en");
  191. if ($name_en) {
  192. $this->deleteDirectory(root_path() . 'plugins/' . $name_en);
  193. CardModel::where('name_en', $name_en)->delete();
  194. Cache::delete('cardList');
  195. }
  196. return $this->success('卸载完毕!');
  197. }
  198. private function deleteDirectory($dir)
  199. {
  200. if (!is_dir($dir)) {
  201. return;
  202. }
  203. $files = scandir($dir);
  204. foreach ($files as $file) {
  205. if ($file != '.' && $file != '..') {
  206. if (is_dir("$dir/$file")) {
  207. $this->deleteDirectory("$dir/$file");
  208. } else {
  209. unlink("$dir/$file");
  210. }
  211. }
  212. }
  213. rmdir($dir);
  214. }
  215. private function readCardInfo($name_en)
  216. {
  217. $file = root_path() . 'plugins/' . $name_en . '/info.json';
  218. $info = file_get_contents($file);
  219. try {
  220. return json_decode($info, true);
  221. } catch (\Exception $e) {
  222. }
  223. return false;
  224. }
  225. private function installCardTask($info): \think\response\Json
  226. {
  227. if ($info['download']) {
  228. $task = new \PluginsInstall($info);
  229. $state = $task->run();
  230. if ($state === true) {
  231. $config = $this->readCardInfo($info['name_en']);
  232. $data = [
  233. 'name' => $config['name'],
  234. 'name_en' => $config['name_en'],
  235. 'version' => $config['version'],
  236. 'tips' => $config['tips'],
  237. 'src' => $config['src'],
  238. 'url' => $config['url'],
  239. 'window' => $config['window'],
  240. ];
  241. if (isset($config['setting'])) {
  242. $data['setting'] = $config['setting'];
  243. }
  244. $find = CardModel::where('name_en', $info['name_en'])->find();
  245. if ($find) {
  246. $find->force()->save($data);
  247. } else {
  248. CardModel::create($data);
  249. }
  250. Cache::delete('cardList');
  251. return $this->success("安装成功");
  252. }
  253. return $this->error($state);
  254. }
  255. return $this->error('新版本没有提供下载地址!');
  256. }
  257. }