PluginsSetMealController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app\platform\modules\pluginsSetMeal\controllers;
  3. use app\backend\modules\menu\Menu;
  4. use app\common\models\Option;
  5. use app\platform\controllers\BaseController;
  6. use app\platform\modules\application\models\UniacidApp;
  7. use app\platform\modules\pluginsSetMeal\models\PluginsMealModel;
  8. use app\platform\modules\pluginsSetMeal\models\pluginsMealPlatform;
  9. use Illuminate\Support\Facades\Cache;
  10. use Ixudra\Curl\Facades\Curl;
  11. class PluginsSetMealController extends BaseController
  12. {
  13. public function pluginsList()
  14. {
  15. $type = request()->input('type');
  16. Menu::flush();
  17. $plugins = array_keys(app('plugins')->getPlugins()->toArray());
  18. $res = $this->getSourcePluginList();
  19. $data = [];
  20. $classification = $res['category_list'];
  21. if (empty($type)) {
  22. foreach ($res['list'] as $plugin) {
  23. if (in_array($plugin['name'],$plugins)) {
  24. $data[] = [
  25. 'title' => $plugin['name'], //配合前端传值,并不是写反了
  26. 'name' => $plugin['title'],
  27. 'type' => $plugin['category_id']
  28. ];
  29. }
  30. }
  31. } else {
  32. foreach ($res['list'] as $plugin) {
  33. if ($plugin['category_id'] == $type && in_array($plugin['name'],$plugins)) {
  34. $data[] = [
  35. 'title' => $plugin['name'],
  36. 'name' => $plugin['title'],
  37. 'type' => $plugin['category_id']
  38. ];
  39. }
  40. }
  41. }
  42. return $this->successJson('ok', [
  43. 'data' => $data,
  44. 'classification' => $classification
  45. ]);
  46. }
  47. public function addPluginsMeal()
  48. {
  49. $data = request()->all();
  50. $data['plugins'] = implode(',', $data['plugins']);
  51. $plugin = PluginsMealModel::where('name', $data['name'])->first();
  52. if ($plugin) {
  53. return $this->errorJson('该套餐名字已存在');
  54. }
  55. try {
  56. PluginsMealModel::create($data);
  57. return $this->successJson('套餐创建成功');
  58. } catch (\Exception $e) {
  59. return $this->errorJson('套餐创建失败,不能存在空值');
  60. }
  61. }
  62. public function empower()
  63. {
  64. if (request()->isMethod('GET')) {
  65. $platformList = (new UniacidApp())->select('id', 'name', 'uniacid')->get()->toArray();
  66. $pluginsMealList = (new PluginsMealModel())->getPluginsMealList();
  67. return $this->successJson('成功', ['platformList' => $platformList, 'pluginsMealList' => $pluginsMealList]);
  68. }
  69. if (request()->isMethod('POST')) {
  70. $uniacid = request()->input('uniacid');
  71. $plugins_meal_id = request()->input('plugins_meal_id');
  72. $power_type = request()->input('power_type') ?:'';
  73. if ($power_type == 'add' && PluginsMealPlatform::where('uniacid',$uniacid)->first())
  74. {
  75. return $this->errorJson('该平台已有使用套餐,重新授权即可无需新增');
  76. }
  77. if (!$uniacid or !$plugins_meal_id) {
  78. return $this->errorJson('参数错误');
  79. }
  80. $pluginsList = Option::where('uniacid', $uniacid)->pluck('option_name')->toArray();
  81. $pluginsMeal = (new PluginsMealModel())->getPluginsMealList($plugins_meal_id);
  82. if (!$pluginsMeal) {
  83. return $this->errorJson('套餐不存在');
  84. }
  85. Menu::flush();
  86. $plugins = array_keys(app('plugins')->getPlugins()->toArray());
  87. \Setting::$uniqueAccountId = $uniacid;
  88. $pluginManager = app('app\common\services\PluginManager');
  89. $afterPluginsCheck = ['store-alone-temp']; //todo 某些插件可能需要启动别的先驱插件才能启动,暂时它放在这处理
  90. $afterPluginsList = []; //后启动插件数组
  91. try {
  92. $pluginManager->disableMeal($uniacid);//关闭所有插件
  93. foreach ($pluginsList as $plugin){
  94. $pluginManager->dispatchEvent($plugin);
  95. }
  96. foreach ($pluginsMeal[0]['plugins'] as $plugin) {
  97. if (in_array($plugin, $plugins)) {
  98. if (in_array($plugin, $afterPluginsCheck)) {
  99. $afterPluginsList[] = $plugin;
  100. continue;
  101. }
  102. $pluginManager->enableMeal($plugin); //启用插件套餐
  103. }
  104. }
  105. if (!empty($afterPluginsList)) {
  106. \Artisan::call('config:cache'); //todo 必须刷新缓存,否则无法启动
  107. \Cache::flush();
  108. foreach ($afterPluginsList as $item) {
  109. $pluginManager->enableMeal($item); //启用插件套餐
  110. }
  111. }
  112. \Setting::$uniqueAccountId = '';
  113. if ($power_type == 'add')
  114. {
  115. (new pluginsMealPlatform())->create([
  116. 'uniacid' => $uniacid,
  117. 'plugins_meal_id' => $plugins_meal_id
  118. ]);
  119. } elseif ($power_type == "reset" ) {
  120. pluginsMealPlatform::where('uniacid', $uniacid)->update(['plugins_meal_id' => $plugins_meal_id]);
  121. }
  122. \Artisan::call('config:cache');
  123. \Cache::flush();
  124. return $this->successJson('套餐使用成功');
  125. } catch (\Exception $e) {
  126. \Artisan::call('config:cache');
  127. \Cache::flush();
  128. return $this->errorJson('套餐使用失败');
  129. }
  130. }
  131. }
  132. public function getPlugins()
  133. {
  134. if (\YunShop::app()->uid == 1) {
  135. $plugin = PluginsMealModel::orderBy('order_by', 'desc')->paginate();;
  136. } else {
  137. $plugin = PluginsMealModel::where('state', 1)->orderBy('order_by', 'desc')->paginate();;
  138. }
  139. return $this->successJson('ok', $plugin);
  140. }
  141. public function record()
  142. {
  143. $data = pluginsMealPlatform::with([
  144. 'hasOnePluginsMeal' => function ($query) {
  145. $query->select('id', 'name', 'plugins');
  146. },
  147. 'hasOneUniacidApp' => function ($query) {
  148. $query->select('id', 'uniacid', 'name');
  149. }
  150. ])->orderBy('created_at', 'desc')->paginate()->toArray();
  151. foreach ($data['data'] as &$item) {
  152. $item['has_one_plugins_meal']['count_plugins'] = $item['has_one_plugins_meal']['plugins'] ? count(explode(',', $item['has_one_plugins_meal']['plugins'])) : 0;
  153. }
  154. return $this->successJson('ok', $data);
  155. }
  156. public function editMeal()
  157. {
  158. if (request()->isMethod("POST")) {
  159. $form = request()->input('form');
  160. $pluginsMealModel = PluginsMealModel::find($form['id']);
  161. if (!$pluginsMealModel) {
  162. return $this->errorJson('该套餐不存在');
  163. }
  164. $form['plugins'] = implode(',', $form['plugins']);
  165. $pluginsMealModel->fill($form);
  166. if ($pluginsMealModel->save()) {
  167. return $this->successJson('套餐修改成功');
  168. } else {
  169. return $this->errorJson('套餐修改失败');
  170. }
  171. }
  172. $plugins_meal_id = request()->input('plugins_meal_id');
  173. $data = PluginsMealModel::where('id', $plugins_meal_id)->first();
  174. if (!$data) {
  175. return $this->errorJson('该套餐不存在');
  176. }
  177. $data = $data->toArray();
  178. $data['plugins'] = explode(',', $data['plugins']);
  179. return $this->successJson('ok', $data);
  180. }
  181. public function delMeal()
  182. {
  183. $plugins_meal_id = request()->input('plugins_meal_id');
  184. $pluginsMealModel = PluginsMealModel::find($plugins_meal_id);
  185. if ($pluginsMealModel->delete()) {
  186. return $this->successJson('套餐删除成功');
  187. } else {
  188. return $this->errorJson('套餐删除失败');
  189. }
  190. }
  191. public function changeSate()
  192. {
  193. $data = request()->input();
  194. if ($data['state'] == 1 || $data['state'] == 0) {
  195. $plugin = PluginsMealModel::where('id', $data['id'])->update(['state' => $data['state']]);
  196. if ($plugin) {
  197. return $this->successJson('修改套餐显示状态成功');
  198. }
  199. return $this->errorJson('修改套餐显示失败,套餐可能已被删除');
  200. }
  201. return $this->errorJson('参数错误');
  202. }
  203. /**
  204. * 获取所有源插件
  205. * @return mixed|null
  206. */
  207. private function getSourcePluginList()
  208. {
  209. //增加缓存
  210. if (Cache::has('plugins-market-list')) {
  211. //return Cache::get('plugins-market-list');
  212. }
  213. if (empty(option('market_source'))) {
  214. //A source maintained by me
  215. option(['market_source' => config('app.PLUGIN_MARKET_SOURCE') ?: 'https://yun.yunzmall.com/plugin.json']);
  216. }
  217. //TODO 加上不同的域名
  218. $domain = request()->getHttpHost();
  219. $market_source_path = option('market_source') . '/domain/' . $domain;
  220. try {
  221. $json_content = Curl::to($market_source_path)
  222. ->get();
  223. } catch (\Exception $e) {
  224. return null;
  225. }
  226. $json_content = json_decode($json_content, true);
  227. Cache::put('plugins-market-list', $json_content, 60 * 5);
  228. return $json_content;
  229. }
  230. }