Index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace app\controller\admin;
  3. use app\BaseController;
  4. use app\model\CardModel;
  5. use app\model\LinkStoreModel;
  6. use app\model\SettingModel;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  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->systemSetting('authCode', '', true);
  26. if (strlen($authCode) == 0) {
  27. $authCode = env('authCode', '');
  28. }
  29. $this->authCode = $authCode;
  30. $this->authService = $this->systemSetting('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. return $this->error($e->getMessage());
  57. }
  58. }
  59. if ($upGrade === null) {
  60. $upGrade = new \Upgrade2();
  61. }
  62. if (!empty($json['info']['update_zip'])) {
  63. $upGrade->update_download_url = $json['info']['update_zip'];
  64. }
  65. if (!empty($json['info']['update_sql'])) {
  66. $upGrade->update_sql_url = $json['info']['update_sql'];
  67. }
  68. try {
  69. $upGrade->run(); //启动任务
  70. if (file_exists($upgradePhp)) {
  71. unlink($upgradePhp);
  72. }
  73. return $this->success('更新完毕');
  74. } catch (\Exception $e) {
  75. return $this->error($e->getMessage());
  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. $info = [];
  88. $info['version'] = app_version;
  89. $info['version_code'] = app_version_code;
  90. $info['php_version'] = phpversion();
  91. try {
  92. $result = \Axios::http()->post($this->authService . '/checkAuth', [
  93. 'timeout' => 10,
  94. 'form_params' => [
  95. 'authorization_code' => $this->authCode,
  96. 'version_code' => app_version_code,
  97. 'domain' => request()->domain()
  98. ]
  99. ]);
  100. if ($result->getStatusCode() == 200) {
  101. $jsonStr = $result->getBody()->getContents();
  102. $json = json_decode($jsonStr, true);
  103. $info['remote'] = $json;
  104. if (!isset($json['auth'])) {
  105. $f = SettingModel::where('keys', 'authCode')->find();
  106. if ($f) {
  107. $f->value = '';
  108. $f->save();
  109. }
  110. Cache::delete('webConfig');
  111. }
  112. return $this->success($info);
  113. }
  114. } catch (\Exception $e) {
  115. }
  116. $info['remote'] = [
  117. "auth" => (bool)$this->authCode
  118. ];
  119. return $this->success('授权服务器连接失败', $info);
  120. }
  121. function cardList(): \think\response\Json
  122. {
  123. $this->getAdmin();
  124. $this->initAuth();
  125. try {
  126. $result = \Axios::http()->post($this->authService . '/card', [
  127. 'timeout' => 15,
  128. 'form_params' => [
  129. 'authorization_code' => $this->authCode
  130. ]
  131. ]);
  132. $json = $result->getBody()->getContents();
  133. $json = json_decode($json, true);
  134. if ($json['code'] === 1) {
  135. return $this->success('ok', $json['data']);
  136. }
  137. } catch (\Exception $e) {
  138. }
  139. return $this->error('远程卡片获取失败');
  140. }
  141. //获取本地应用
  142. function localCard(): \think\response\Json
  143. {
  144. $this->getAdmin();
  145. $apps = CardModel::select();
  146. return $this->success('ok', $apps);
  147. }
  148. function stopCard(): \think\response\Json
  149. {
  150. $this->getAdmin();
  151. is_demo_mode(true);
  152. $name_en = $this->request->post('name_en', '');
  153. CardModel::where('name_en', $name_en)->update(['status' => 0]);
  154. Cache::delete('cardList');
  155. return $this->success('设置成功');
  156. }
  157. function startCard(): \think\response\Json
  158. {
  159. $this->getAdmin();
  160. $name_en = $this->request->post('name_en', '');
  161. CardModel::where('name_en', $name_en)->update(['status' => 1]);
  162. Cache::delete('cardList');
  163. return $this->success('设置成功');
  164. }
  165. function installCard(): \think\response\Json
  166. {
  167. $this->getAdmin();
  168. $this->initAuth();
  169. $name_en = $this->request->post("name_en", '');
  170. $version = 0;
  171. $type = $this->request->post('type', 'install');
  172. if (mb_strlen($name_en) > 0) {
  173. $card = CardModel::where('name_en', $name_en)->find();
  174. if ($card) {
  175. if ($type == 'install') {
  176. return $this->error('您已安装当前卡片组件');
  177. }
  178. if ($type == 'update') {
  179. $version = $card['version'];
  180. }
  181. }
  182. $result = \Axios::http()->post($this->authService . '/installCard', [
  183. 'timeout' => 15,
  184. 'form_params' => [
  185. 'authorization_code' => $this->authCode,
  186. 'name_en' => $name_en,
  187. 'version' => $version,
  188. 'version_code' => app_version_code,
  189. ]
  190. ]);
  191. try {
  192. $json = $result->getBody()->getContents();
  193. $json = json_decode($json, true, JSON_UNESCAPED_UNICODE);
  194. if ($json['code'] == 0) {
  195. return $this->error($json['msg']);
  196. }
  197. return $this->installCardTask($json['data']);
  198. } catch (\Exception $e) {
  199. return $this->error($e->getMessage());
  200. }
  201. }
  202. return $this->error("没有需要安装的卡片插件!");
  203. }
  204. function uninstallCard(): \think\response\Json
  205. {
  206. $this->getAdmin();
  207. is_demo_mode(true);
  208. $name_en = $this->request->post("name_en");
  209. if ($name_en) {
  210. $this->deleteDirectory(root_path() . 'plugins/' . $name_en);
  211. CardModel::where('name_en', $name_en)->delete();
  212. Cache::delete('cardList');
  213. }
  214. return $this->success('卸载完毕!');
  215. }
  216. private function deleteDirectory($dir)
  217. {
  218. if (!is_dir($dir)) {
  219. return;
  220. }
  221. $files = scandir($dir);
  222. foreach ($files as $file) {
  223. if ($file != '.' && $file != '..') {
  224. if (is_dir("$dir/$file")) {
  225. $this->deleteDirectory("$dir/$file");
  226. } else {
  227. unlink("$dir/$file");
  228. }
  229. }
  230. }
  231. rmdir($dir);
  232. }
  233. private function readCardInfo($name_en)
  234. {
  235. $file = root_path() . 'plugins/' . $name_en . '/info.json';
  236. $info = file_get_contents($file);
  237. try {
  238. return json_decode($info, true);
  239. } catch (\Exception $e) {
  240. }
  241. return false;
  242. }
  243. private function installCardTask($info): \think\response\Json
  244. {
  245. if ($info['download']) {
  246. $task = new \PluginsInstall($info);
  247. $state = $task->run();
  248. if ($state === true) {
  249. $config = $this->readCardInfo($info['name_en']);
  250. $data = [
  251. 'name' => $config['name'],
  252. 'name_en' => $config['name_en'],
  253. 'version' => $config['version'],
  254. 'tips' => $config['tips'],
  255. 'src' => $config['src'],
  256. 'url' => $config['url'],
  257. 'window' => $config['window'],
  258. ];
  259. if (isset($config['setting'])) {
  260. $data['setting'] = $config['setting'];
  261. }
  262. $find = CardModel::where('name_en', $info['name_en'])->find();
  263. if ($find) {
  264. $find->force()->save($data);
  265. } else {
  266. CardModel::create($data);
  267. }
  268. Cache::delete('cardList');
  269. return $this->success("安装成功");
  270. }
  271. return $this->error($state);
  272. }
  273. abort(0, "新版本没有提供下载地址!");
  274. }
  275. //打包扩展
  276. function build(): \think\response\Json
  277. {
  278. $this->getAdmin();
  279. is_demo_mode(true);
  280. if (!extension_loaded('zip')) {
  281. return $this->error("系统未安装或开启zip扩展,请安装后重试!");
  282. }
  283. if (!$this->auth) {
  284. return $this->error("请获取授权后进行操作");
  285. }
  286. $ExtInfo = $this->request->post("extInfo", []);
  287. $build = new \BrowserExtBuild($ExtInfo);
  288. try {
  289. $status = $build->runBuild();
  290. if ($status) {
  291. return $this->success('打包完毕', ['url' => '/browserExt.zip']);
  292. }
  293. } catch (\Exception $e) {
  294. return $this->error($e->getMessage());
  295. }
  296. return $this->success('打包失败');
  297. }
  298. function folders(): \think\response\Json
  299. {
  300. $this->getAdmin();
  301. $this->initAuth();
  302. $result = \Axios::http()->post($this->authService . '/client/folders', [
  303. 'timeout' => 15,
  304. 'form_params' => [
  305. 'authorization_code' => $this->authCode
  306. ]
  307. ]);
  308. $json = $result->getBody()->getContents();
  309. $json = json_decode($json, true);
  310. if ($json['code'] === 1) {
  311. return $this->success('ok', $json['data']);
  312. }
  313. return $this->success('获取失败');
  314. }
  315. function links(): \think\response\Json
  316. {
  317. $this->getAdmin();
  318. $this->initAuth();
  319. $folders = $this->request->get("folders");
  320. $page = $this->request->get("page", 1);
  321. $limit = $this->request->get("limit", 18);
  322. $result = \Axios::http()->post($this->authService . '/client/links', [
  323. 'timeout' => 15,
  324. 'form_params' => [
  325. 'folders' => $folders,
  326. 'limit' => $limit,
  327. 'page' => $page,
  328. 'authorization_code' => $this->authCode
  329. ]
  330. ]);
  331. $json = $result->getBody()->getContents();
  332. $json = json_decode($json, true);
  333. if ($json['code'] === 1) {
  334. $arrName = [];
  335. $arrUrl = [];
  336. foreach ($json['data']['data'] as $key => $value) {
  337. $arrName[] = $value['name'];
  338. $arrUrl[] = $value['url'];
  339. }
  340. $res = LinkStoreModel::whereOr([["name", 'in', $arrName], ['url', 'in', $arrUrl]])->select();
  341. return json(['code' => 1, 'msg' => 'ok', 'data' => $json['data'], 'local' => $res]);
  342. }
  343. return $this->success('获取失败');
  344. }
  345. }