PluginsController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 10/03/2017
  6. * Time: 16:42
  7. */
  8. namespace app\backend\controllers;
  9. use app\backend\modules\menu\Menu;
  10. use app\common\components\BaseController;
  11. use app\common\exceptions\ShopException;
  12. use app\common\helpers\Url;
  13. use app\common\services\plugin\DeliveryDriverSet;
  14. use Datatables;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Config;
  17. use app\common\services\PermissionService;
  18. use app\common\models\user\User;
  19. use Ixudra\Curl\Facades\Curl;
  20. class PluginsController extends BaseController
  21. {
  22. public $terminal = 'wechat|min|wap';
  23. private $request_domain = 'https://yun.yunzmall.com';
  24. public function showManage()
  25. {
  26. return view('public.admin.plugins');
  27. }
  28. public function config($name, Request $request)
  29. {
  30. $plugin = plugin($name);
  31. if ($plugin && $plugin->isEnabled() && $plugin->hasConfigView()) {
  32. return $plugin->getConfigView();
  33. } else {
  34. abort(404, trans('admin.plugins.operations.no-config-notice'));
  35. }
  36. }
  37. public function manage()
  38. {
  39. $name = request()->name;
  40. $action = request()->action;
  41. $plugins = app('app\common\services\PluginManager');
  42. $plugin = plugin($name);
  43. if (app()->environment() == 'production') {
  44. $this->proAuth($name, $action);
  45. }
  46. if ($plugin) {
  47. // pass the plugin title through the translator
  48. $plugin->title = trans($plugin->title);
  49. switch (request()->action) {
  50. case 'enable':
  51. $plugins->enable($name);
  52. \Artisan::call('config:cache');
  53. \Cache::flush();
  54. return $this->successJson('启用成功');
  55. case 'disable':
  56. $plugins->disable($name);
  57. \Artisan::call('config:cache');
  58. \Cache::flush();
  59. return $this->successJson('禁用成功');
  60. case 'delete':
  61. if (!PermissionService::isFounder()) {
  62. return $this->errorJson('您暂没有权限卸载插件');
  63. }
  64. $plugins->uninstall($name);
  65. \Artisan::call('config:cache');
  66. \Cache::flush();
  67. return $this->successJson('卸载成功');
  68. default:
  69. # code...
  70. break;
  71. }
  72. }
  73. }
  74. public function batchMange()
  75. {
  76. $plugins = app('app\common\services\PluginManager');
  77. $names = explode(',', request()->names);
  78. $action = request()->action;
  79. foreach ($names as $name) {
  80. if (app()->environment() == 'production') {
  81. $this->proAuth($name, $action);
  82. }
  83. $plugin = plugin($name);
  84. if ($plugin) {
  85. $plugin->title = trans($plugin->title);
  86. switch (request()->action) {
  87. case 'enable':
  88. $plugins->enable($name);
  89. break;
  90. case 'disable':
  91. $plugins->disable($name);
  92. break;
  93. case 'delete':
  94. $plugins->uninstall($name);
  95. break;
  96. default:
  97. die(json_encode(array(
  98. "result" => 0,
  99. "error" => "操作错误"
  100. )));
  101. break;
  102. }
  103. }
  104. }
  105. switch (request()->action) {
  106. case 'enable':
  107. return $this->successJson('启用成功');
  108. case 'disable':
  109. return $this->successJson('禁用成功');
  110. case 'delete':
  111. \Artisan::call('config:cache');
  112. \Cache::flush();
  113. return $this->successJson('卸载成功');
  114. }
  115. }
  116. public function getPluginData()
  117. {
  118. $url = $this->request_domain . '/plugin/plugin_install_auth';
  119. $domain = request()->getHttpHost();
  120. $installed = app('plugins')->getPlugins();
  121. $data = [];
  122. $plugins = [];
  123. $i = 0;
  124. $installed->each(function ($item, $key) use (&$data, &$i, &$plugins) {
  125. $plugins[] = $key;
  126. $data[$i] = $item->toArray();
  127. $data[$i]['status'] = $item->isEnabled() ? true : false;
  128. $data[$i]['new_version'] = 0;
  129. $data[$i]['permit_status'] = '未授权';
  130. $i++;
  131. });
  132. $content = Curl::to($url)
  133. ->withData(['domain' => $domain])
  134. ->asJsonResponse(true)
  135. ->get();
  136. //未授权插件数量
  137. $unPermitPlugin = 0;
  138. //TODO 数组合并
  139. foreach ($content['data'] as $k => $v) {
  140. foreach ($data as $key => &$value) {
  141. if ($k == $value['name']) {
  142. $value['new_version'] = $v['version'];
  143. $value['permit_status'] = $v['status'];
  144. }
  145. }
  146. }
  147. unset($value);
  148. foreach ($data as $key => $value) {
  149. if ('未授权' === $value['permit_status']) {
  150. $unPermitPlugin++;
  151. }
  152. }
  153. if (request()->ajax()) {
  154. return $this->searchPlugin($data, request()->search);
  155. }
  156. return view('admin.plugins', [
  157. 'data' => json_encode($data),
  158. 'unPermitPlugin' => $unPermitPlugin,
  159. 'countPlugin' => count($data),
  160. ]);
  161. }
  162. public function getPluginList()
  163. {
  164. $class = $this->getType();
  165. $data = [];
  166. $plugins = Menu::current()->getPluginMenus();//全部插件
  167. foreach ($plugins as $key => $plugin) {
  168. $name = explode('.',$plugin['url'])[1];
  169. if (!$plugin['type']) {
  170. continue;
  171. }
  172. $terminal = app('plugins')->getPlugin($name)->terminal;
  173. $data[$plugin['type']][$key] = $plugin;
  174. $data[$plugin['type']][$key]['terminal'] = explode('|',$terminal);
  175. $data[$plugin['type']][$key]['description'] = app('plugins')->getPlugin($name)->description?:$plugin['name'];
  176. $data[$plugin['type']][$key]['icon_url'] = file_exists(base_path('static/yunshop/plugins/list-icon/img/' . $plugin['list_icon'] . '.png')) ? static_url("yunshop/plugins/list-icon/img/{$plugin['list_icon']}.png") : static_url("yunshop/plugins/list-icon/img/default2.png");
  177. $data[$plugin['type']][$key]['url'] = $this->canAccess($key);
  178. }
  179. return view('admin.pluginslist', [
  180. 'plugins' => $plugins,
  181. 'data' => $data,
  182. 'class' => $class
  183. ]);
  184. }
  185. public static function canAccess($item)
  186. {
  187. $current_menu = Menu::current()->getPluginMenus()[$item];
  188. $url = $current_menu['url'];
  189. if (PermissionService::isFounder()) {
  190. return $url;
  191. }
  192. if (PermissionService::isOwner()) {
  193. return $url;
  194. }
  195. if (PermissionService::isManager()) {
  196. return $url;
  197. }
  198. if (PermissionService::checkNoPermission($item) === true) {
  199. return $url;
  200. }
  201. if (!isset($current_menu['child'])) {
  202. return $url;
  203. }
  204. $userPermission = User::userPermissionCache();
  205. //检测当前 key 下路由是否有权限访问
  206. foreach ($current_menu['child'] as $key => $value) {
  207. if ($value['url'] == $current_menu['url'] && in_array($key, $userPermission) && $value['menu'] == 1) {
  208. return $url;
  209. break;
  210. }
  211. continue;
  212. }
  213. //上面条件都不满足时,找第一个有权限访问的路由
  214. foreach ($current_menu['child'] as $key => $value) {
  215. if (in_array($key, $userPermission) && $value['menu'] == 1) {
  216. return $value['url'];
  217. break;
  218. }
  219. continue;
  220. }
  221. return 'index.index';
  222. }
  223. public function setTopShow()
  224. {
  225. $data = request()->input();
  226. // $data['action'] ?: app('plugins')->enTopShow($data['name'], 1);
  227. if ($data['action']) {
  228. app('plugins')->enTopShow($data['name'], 0);
  229. return $this->message('取消顶部栏成功', Url::web('plugins.getPluginList'));
  230. } else {
  231. $menu = config(config('app.menu_key', 'menu'));
  232. $counts = 0;
  233. //常用功能
  234. foreach ($menu as $key => $itme) {
  235. if (isset($itme['menu']) && $itme['menu'] == 1 && can($key) && ($itme['top_show'] == 1 || app('plugins')->isTopShow($key))) {
  236. ++$counts;
  237. if ($counts > 7) {
  238. return $this->message('顶部栏最大数量为八个');
  239. }
  240. }
  241. }
  242. app('plugins')->enTopShow($data['name'], 1);
  243. return $this->message('添加顶部栏成功', Url::web('plugins.getPluginList'));
  244. }
  245. }
  246. public function proAuth($name, $action)
  247. {
  248. if ($action == 'enable') {
  249. $key = \Setting::get('shop.key')['key'];
  250. $secret = \Setting::get('shop.key')['secret'];
  251. $url = config('auto-update.proAuthUrl') . "/chkname/{$name}";
  252. $res = \Curl::to($url)
  253. ->withHeader(
  254. "Authorization: Basic " . base64_encode("{$key}:{$secret}")
  255. )
  256. ->asJsonResponse(true)
  257. ->get();
  258. // dd($res);
  259. // \Log::debug('-------update res-----', $res);
  260. if (0 == $res['status']) {
  261. throw new ShopException('应用未授权');
  262. }
  263. }
  264. }
  265. public function getType()
  266. {
  267. return [
  268. 'dividend' => [
  269. 'name' => '入口类',
  270. 'color' => '#F15353',
  271. ],
  272. 'industry' => [
  273. 'name' => '行业类',
  274. 'color' => '#eb6f50',
  275. ],
  276. 'marketing' => [
  277. 'name' => '营销类',
  278. 'color' => '#f0b652',
  279. ],
  280. 'business_management' => [
  281. 'name' => '企业管理类',
  282. 'color' => '#f05295',
  283. ],
  284. 'tool' => [
  285. 'name' => '工具类',
  286. 'color' => '#f59753',
  287. ],
  288. 'recharge' => [
  289. 'name' => '生活充值',
  290. 'color' => '#50d9a7',
  291. ],
  292. 'api' => [
  293. 'name' => '接口类',
  294. 'color' => '#53d5f0',
  295. ],
  296. 'store' => [
  297. 'name' => '门店应用类',
  298. 'color' => '#98aafa',
  299. ],
  300. 'blockchain' => [
  301. 'name' => '区块链类',
  302. 'color' => '#469de2',
  303. ],
  304. ];
  305. }
  306. public function searchPlugin($data, $search)
  307. {
  308. foreach ($data as $key => $value) {
  309. if ($search['title'] && !strexists($value['title'], $search['title'])) {
  310. unset($data[$key]);
  311. }
  312. if ($search['permit_status'] && !strexists($value['permit_status'], $search['permit_status'])) {
  313. unset($data[$key]);
  314. }
  315. if ($search['update_status'] == '可升级' && $value['version'] == $value['new_version']) {
  316. unset($data[$key]);
  317. }
  318. if ($search['update_status'] == '不可升级' && $value['version'] < $value['new_version']) {
  319. unset($data[$key]);
  320. }
  321. if ($search['status'] === 'enable' && $value['status'] == false) {
  322. unset($data[$key]);
  323. }
  324. if ($search['status'] === 'disable' && $value['status'] == true) {
  325. unset($data[$key]);
  326. }
  327. };
  328. return $this->successJson('请求成功', array_values($data));
  329. }
  330. /**
  331. * 中转方法,安装应用菜单判断应用市场是否开启
  332. */
  333. public function jump()
  334. {
  335. if(app('plugins')->isEnabled('plugins-market')){
  336. return view('Yunshop\PluginsMarket::new_market')->render();
  337. }else{
  338. return $this->message('请先开启插件市场插件',yzWebFullUrl('plugins.get-plugin-data'));
  339. }
  340. }
  341. }