Index.php 11 KB

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