Addons.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\controller;
  12. use app\admin\service\addons\AddonsService;
  13. use app\common\controller\Backend;
  14. use app\common\library\QRcode;
  15. use app\common\model\Config;
  16. use think\annotation\route\Group;
  17. use think\annotation\route\Route;
  18. use app\common\model\Addons as AddonsModel;
  19. use think\facade\Db;
  20. use think\facade\Cache;
  21. function parseMenu(array $menu)
  22. {
  23. $ids=[];
  24. foreach ($menu as $value){
  25. $ids[]=$value['id'];
  26. if(isset($value['childlist'])){
  27. $ids=array_merge($ids,parseMenu($value['childlist']));
  28. }
  29. }
  30. return $ids;
  31. }
  32. #[Group("addons")]
  33. class Addons extends Backend
  34. {
  35. protected $noNeedRight = ['*'];
  36. /* @var AddonsService $service*/
  37. private $service;
  38. protected function _initialize()
  39. {
  40. parent::_initialize();
  41. if(!$this->auth->isSuperAdmin()){
  42. $this->error(__('超级管理员才能访问'));
  43. }
  44. $this->service=AddonsService::newInstance();
  45. }
  46. #[Route('GET,JSON','index')]
  47. public function index()
  48. {
  49. if($this->request->isAjax()){
  50. $type=$this->filter('type');
  51. $plain=$this->filter('plain');
  52. $page=$this->request->post('page/d',1);
  53. $limit=$this->request->post('limit/d',10);
  54. $keywords=$this->request->post('searchValue/s');
  55. try {
  56. if($plain=='local'){
  57. $where=[];
  58. if($type){
  59. $where[]=['type','=',$type];
  60. }
  61. if($plain=='free'){
  62. $where[]=['price','=',0];
  63. }
  64. if($plain=='not-free'){
  65. $where[]=['price','>',0];
  66. }
  67. if($keywords){
  68. $where[]=['name|key','like','%'.$keywords.'%'];
  69. }
  70. $fields='id,key,secret_key,pack,type,name,description,author,document,price,version,open,install';
  71. $result=AddonsModel::list($page,$limit,$fields,$where);
  72. foreach ($result['rows'] as $key=>$value){
  73. $result['rows'][$key]['download']=0;
  74. $result['rows'][$key]['packed']=0;
  75. $result['rows'][$key]['local']=1;
  76. //判断目录是否存在
  77. if(is_dir($this->service->getAddonsPath($value['type'],$value['pack']))){
  78. $result['rows'][$key]['download']=1;
  79. }
  80. if(file_exists($this->service->getAddonsPack($value['type'],$value['pack'],$value['version']))){
  81. $result['rows'][$key]['packed']=1;
  82. }
  83. //判断是否是作者
  84. if(AddonsModel::checkKey($value)){
  85. $result['rows'][$key]['is_author']=1;
  86. }
  87. unset($result['rows'][$key]['secret_key']);
  88. }
  89. }else{
  90. //远程获取的扩展
  91. $result=$this->service->getAddons($page,$type,$plain,$limit,$keywords);
  92. $where=[];
  93. if($type){
  94. $where[]=['type','=',$type];
  95. }
  96. $local=AddonsModel::list(1,1000,'id,type,pack,author,version,key,secret_key,open,install',$where);
  97. //对比远程扩展和本地扩展
  98. foreach ($result['rows'] as $key=>$value){
  99. $result['rows'][$key]['open']=0;
  100. $result['rows'][$key]['install']=0;
  101. $result['rows'][$key]['download']=0;
  102. $result['rows'][$key]['packed']=0;
  103. $result['rows'][$key]['local']=0;
  104. foreach ($local['rows'] as $v){
  105. if($value['key']==$v['key']){
  106. $result['rows'][$key]['id']=$v['id'];
  107. $result['rows'][$key]['local']=1;
  108. $result['rows'][$key]['open']=$v['open'];
  109. $result['rows'][$key]['install']=$v['install'];
  110. //判断目录是否存在
  111. if(is_dir($this->service->getAddonsPath($value['type'],$v['pack']))){
  112. $result['rows'][$key]['download']=1;
  113. }
  114. if(file_exists($this->service->getAddonsPack($value['type'],$v['pack'],$value['version']))){
  115. $result['rows'][$key]['packed']=1;
  116. }
  117. //判断是否是作者
  118. if(AddonsModel::checkKey($v)){
  119. $result['rows'][$key]['is_author']=1;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return json($result);
  126. }catch (\Exception $e){
  127. $this->error($e->getMessage());
  128. }
  129. }
  130. $this->assign('type',AddonsModel::TYPE);
  131. $this->assign('plugins_host',config('yunqi.plugins_host'));
  132. return $this->fetch();
  133. }
  134. #[Route('POST','multi')]
  135. public function multi()
  136. {
  137. $ids = $this->request->param('ids');
  138. $field = $this->request->param('field');
  139. $value = $this->request->param('value');
  140. if($field=='open'){
  141. $addon=AddonsModel::find($ids);
  142. if(!AddonsModel::checkKey($addon)){
  143. //禁止修改、删除,否则后果自负
  144. $this->error('不是你的扩展,无法操作');
  145. }
  146. $packfile=$this->service->getAddonsPack($addon['type'],$addon['pack'],$addon['version']);
  147. if($value && !is_file($packfile)){
  148. $this->error('扩展未打包,请先打包');
  149. }
  150. $addon->open=$value;
  151. $addon->save();
  152. }
  153. $this->success();
  154. }
  155. #[Route('GET,POST','create')]
  156. public function create()
  157. {
  158. if($this->request->isPost()){
  159. $param=$this->request->post('row/a');
  160. try{
  161. $param['version']=(string)$param['version'];
  162. if(intval($param['version'])<1000){
  163. $param['version']=implode('.',str_split($param['version']));
  164. }else{
  165. $param['version']=substr($param['version'],0,2).'.'.substr($param['version'],2,1).'.'.substr($param['version'],3,1);
  166. }
  167. $this->service->create($param);
  168. Cache::set('download-addons','');
  169. }catch (\Exception $e){
  170. $this->error($e->getMessage());
  171. }
  172. $this->success('创建成功');
  173. }
  174. $id=$this->request->get('id');
  175. if($id){
  176. $rows=AddonsModel::find($id);
  177. if(!AddonsModel::checkKey($rows)){
  178. $this->error('不是你的扩展,无法操作');
  179. }
  180. $rows->version=intval(str_replace('.','',$rows['version']));
  181. $info=$this->service->getAddonsInstallInfo($rows);
  182. //将数组转换成换行隔开的字符串
  183. $rows->files=implode("\n",$info['files']);
  184. $rows->unpack=implode("\n",$info['unpack']);
  185. $rows->require=implode("\n",array_map(function($item){return "\\".$item;},$info['require']));
  186. $rows->addons=implode("\n",array_keys($info['addons']));
  187. $rows->tables=$info['tables'];
  188. $rows->config=array_map(function($item){return $item['id'];},$info['config']);
  189. $rows->menu=parseMenu($info['menu']);
  190. $this->assign('rows',$rows);
  191. }else{
  192. $this->assign('rows',['menu'=>[]]);
  193. }
  194. $config=Config::where('can_delete',1)->column('id,name,title','id');
  195. $dbname=config('database.connections.mysql.database');
  196. $tableList =Db::query("SELECT `TABLE_NAME` AS `name` FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` = '{$dbname}';");
  197. $table=array_map(function ($item){return $item['name'];},$tableList);
  198. $this->assign('type',AddonsModel::TYPE);
  199. $this->assign('table',$table);
  200. $this->assign('sonfig',$config);
  201. return $this->fetch();
  202. }
  203. #[Route('POST','pack')]
  204. public function pack()
  205. {
  206. $key=$this->request->post('key');
  207. try{
  208. $this->service->package($key);
  209. }catch (\Exception $e){
  210. $this->error($e->getMessage());
  211. }
  212. $this->success('打包成功');
  213. }
  214. #[Route('GET','checkTransactionId')]
  215. public function checkTransactionId()
  216. {
  217. $pack=$this->request->get('pack');
  218. $transaction_id=$this->request->get('transaction_id');
  219. try{
  220. $result=$this->service->checkTransactionId($pack,$transaction_id);
  221. }catch (\Exception $e){
  222. $this->error($e->getMessage());
  223. }
  224. $this->success('',$result);
  225. }
  226. #[Route('POST','install')]
  227. public function install()
  228. {
  229. $key=$this->request->post('key');
  230. try{
  231. $this->service->install($key);
  232. Cache::set('download-addons','');
  233. Cache::delete('admin_rule_list');
  234. Cache::delete('admin_menu_list');
  235. }catch (\Exception $e){
  236. $this->error($e->getMessage());
  237. }
  238. $this->success('安装成功');
  239. }
  240. #[Route('GET','payCode')]
  241. public function payCode()
  242. {
  243. $key=$this->request->get('key');
  244. $out_trade_no=$this->request->get('out_trade_no');
  245. try{
  246. $code_url=$this->service->payCode($key,$out_trade_no);
  247. }catch (\Exception $e){
  248. $this->error($e->getMessage());
  249. }
  250. $errorCorrectionLevel = 'L'; //容错级别
  251. $matrixPointSize = 10;//生成图片大小
  252. QRcode::png($code_url,false, $errorCorrectionLevel, $matrixPointSize, 2);
  253. exit;
  254. }
  255. #[Route('GET,POST','uninstall')]
  256. public function uninstall()
  257. {
  258. if($this->request->isGet()){
  259. $key=$this->request->get('key');
  260. $addon=AddonsModel::where('key',$key)->find();
  261. $result=$this->service->getAddonsInstallInfo($addon);
  262. $this->assign('addon',$addon);
  263. $this->assign('menu',$result['menu']);
  264. $this->assign('conf',$result['config']);
  265. $this->assign('tables',$result['tables']);
  266. return $this->fetch();
  267. }
  268. if($this->request->isPost()){
  269. $key=$this->request->post('key');
  270. $actions=$this->request->post('actions',[]);
  271. try{
  272. $this->service->uninstall($key,$actions);
  273. Cache::set('download-addons','');
  274. Cache::delete('admin_rule_list');
  275. Cache::delete('admin_menu_list');
  276. }catch (\Exception $e){
  277. $this->error($e->getMessage());
  278. }
  279. $this->success('卸载成功');
  280. }
  281. }
  282. #[Route('POST','download')]
  283. public function download()
  284. {
  285. $postdata=$this->request->post();
  286. try{
  287. $this->service->download($postdata);
  288. Cache::set('download-addons','');
  289. }catch (\Exception $e){
  290. $this->error($e->getMessage());
  291. }
  292. $this->success('下载成功');
  293. }
  294. #[Route('POST','del')]
  295. public function del()
  296. {
  297. $key=$this->request->post('key');
  298. try{
  299. $this->service->delAddons($key);
  300. Cache::set('download-addons','');
  301. }catch (\Exception $e){
  302. $this->error($e->getMessage());
  303. }
  304. $this->success('删除成功');
  305. }
  306. #[Route('GET','checkPayStatus')]
  307. public function checkPayStatus()
  308. {
  309. $out_trade_no=$this->request->get('out_trade_no');
  310. $key=$this->request->get('key');
  311. try{
  312. $r=$this->service->checkPayStatus($key,$out_trade_no);
  313. }catch (\Exception $e){
  314. $this->error($e->getMessage());
  315. }
  316. $this->success('',$r);
  317. }
  318. }