Index.php 9.8 KB

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