Index.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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' => 10,
  89. 'form_params' => [
  90. 'authorization_code' => $this->authCode,
  91. 'version_code' => app_version_code,
  92. 'domain' => request()->domain()
  93. ]
  94. ]);
  95. $info = [];
  96. $info['version'] = app_version;
  97. $info['version_code'] = app_version_code;
  98. $info['php_version'] = phpversion();
  99. if ($result->getStatusCode() == 200) {
  100. $jsonStr = $result->getBody()->getContents();
  101. $json = json_decode($jsonStr, true);
  102. $info['remote'] = $json;
  103. return $this->success($info);
  104. } else {
  105. return $this->error('授权服务器连接失败', $info);
  106. }
  107. }
  108. function cardList(): \think\response\Json
  109. {
  110. $this->getAdmin();
  111. $this->initAuth();
  112. $result = \Axios::http()->post($this->authService . '/card', [
  113. 'timeout' => 15,
  114. 'form_params' => [
  115. 'authorization_code' => $this->authCode
  116. ]
  117. ]);
  118. try {
  119. $json = $result->getBody()->getContents();
  120. $json = json_decode($json, true);
  121. if ($json['code'] === 1) {
  122. return $this->success('ok', $json['data']);
  123. }
  124. } catch (\Exception $e) {
  125. }
  126. return $this->error('远程卡片获取失败');
  127. }
  128. //获取本地应用
  129. function localCard(): \think\response\Json
  130. {
  131. $this->getAdmin();
  132. $apps = CardModel::select();
  133. return $this->success('ok', $apps);
  134. }
  135. function stopCard(): \think\response\Json
  136. {
  137. $this->getAdmin();
  138. is_demo_mode(true);
  139. $name_en = $this->request->post('name_en', '');
  140. CardModel::where('name_en', $name_en)->update(['status' => 0]);
  141. Cache::delete('cardList');
  142. return $this->success('设置成功');
  143. }
  144. function startCard(): \think\response\Json
  145. {
  146. $this->getAdmin();
  147. $name_en = $this->request->post('name_en', '');
  148. CardModel::where('name_en', $name_en)->update(['status' => 1]);
  149. Cache::delete('cardList');
  150. return $this->success('设置成功');
  151. }
  152. function installCard(): \think\response\Json
  153. {
  154. $this->getAdmin();
  155. $this->initAuth();
  156. $name_en = $this->request->post("name_en", '');
  157. $version = 0;
  158. $type = $this->request->post('type', 'install');
  159. if (mb_strlen($name_en) > 0) {
  160. $card = CardModel::where('name_en', $name_en)->find();
  161. if ($card) {
  162. if ($type == 'install') {
  163. return $this->error('您已安装当前卡片组件');
  164. }
  165. if ($type == 'update') {
  166. $version = $card['version'];
  167. }
  168. }
  169. $result = \Axios::http()->post($this->authService . '/installCard', [
  170. 'timeout' => 15,
  171. 'form_params' => [
  172. 'authorization_code' => $this->authCode,
  173. 'name_en' => $name_en,
  174. 'version' => $version
  175. ]
  176. ]);
  177. try {
  178. $json = $result->getBody()->getContents();
  179. $json = json_decode($json, true, JSON_UNESCAPED_UNICODE);
  180. if ($json['code'] == 0) {
  181. return $this->error($json['msg']);
  182. }
  183. return $this->installCardTask($json['data']);
  184. } catch (\Exception $e) {
  185. }
  186. }
  187. return $this->error("没有需要安装的卡片插件!");
  188. }
  189. function uninstallCard(): \think\response\Json
  190. {
  191. $this->getAdmin();
  192. is_demo_mode(true);
  193. $name_en = $this->request->post("name_en");
  194. if ($name_en) {
  195. $this->deleteDirectory(root_path() . 'plugins/' . $name_en);
  196. CardModel::where('name_en', $name_en)->delete();
  197. Cache::delete('cardList');
  198. }
  199. return $this->success('卸载完毕!');
  200. }
  201. private function deleteDirectory($dir)
  202. {
  203. if (!is_dir($dir)) {
  204. return;
  205. }
  206. $files = scandir($dir);
  207. foreach ($files as $file) {
  208. if ($file != '.' && $file != '..') {
  209. if (is_dir("$dir/$file")) {
  210. $this->deleteDirectory("$dir/$file");
  211. } else {
  212. unlink("$dir/$file");
  213. }
  214. }
  215. }
  216. rmdir($dir);
  217. }
  218. private function readCardInfo($name_en)
  219. {
  220. $file = root_path() . 'plugins/' . $name_en . '/info.json';
  221. $info = file_get_contents($file);
  222. try {
  223. return json_decode($info, true);
  224. } catch (\Exception $e) {
  225. }
  226. return false;
  227. }
  228. private function installCardTask($info): \think\response\Json
  229. {
  230. if ($info['download']) {
  231. $task = new \PluginsInstall($info);
  232. $state = $task->run();
  233. if ($state === true) {
  234. $config = $this->readCardInfo($info['name_en']);
  235. $data = [
  236. 'name' => $config['name'],
  237. 'name_en' => $config['name_en'],
  238. 'version' => $config['version'],
  239. 'tips' => $config['tips'],
  240. 'src' => $config['src'],
  241. 'url' => $config['url'],
  242. 'window' => $config['window'],
  243. ];
  244. if (isset($config['setting'])) {
  245. $data['setting'] = $config['setting'];
  246. }
  247. $find = CardModel::where('name_en', $info['name_en'])->find();
  248. if ($find) {
  249. $find->force()->save($data);
  250. } else {
  251. CardModel::create($data);
  252. }
  253. Cache::delete('cardList');
  254. return $this->success("安装成功");
  255. }
  256. return $this->error($state);
  257. }
  258. return $this->error('新版本没有提供下载地址!');
  259. }
  260. }