AppController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Jobs\InitAppPackJob;
  5. use App\Models\Mysql\AppModel;
  6. use App\Repositories\AgentRepository;
  7. use App\Repositories\AppRepository;
  8. use App\Repositories\ChannelRepository;
  9. use App\Repositories\Criterias\AppPagesCriteria;
  10. use App\Repositories\Criterias\OrderLimitCriteria;
  11. use Illuminate\Support\Facades\Validator;
  12. use Modules\ManageApi\Responses\AppPagesResponse;
  13. use Modules\ManageApi\Responses\CommonAllResponse;
  14. use Modules\ManageApi\Responses\CommonDetailResponse;
  15. use Modules\ManageApi\Responses\CommonSuccessUidResponse;
  16. use Modules\ManageApi\Responses\NotIncrementIdDetailResponse;
  17. class AppController extends BaseController
  18. {
  19. /**
  20. * 应用列表
  21. * @return \Illuminate\Http\JsonResponse|AppPagesResponse
  22. */
  23. public function lists(Validator $validatorFactory, AppRepository $appRepo)
  24. {
  25. $inputData = $this->getJsonData();
  26. // 针对layui table自动分页请求参数,优先GET值.
  27. $queryPage = request()->has('page') ? request()->input('page', 1) : null;
  28. $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
  29. if (!is_null($queryPage)) {
  30. $inputData['page'] = $queryPage;
  31. }
  32. if (!is_null($queryPageSize)) {
  33. $inputData['page_size'] = $queryPageSize;
  34. }
  35. $rules = [
  36. 'page' => 'nullable|integer',
  37. 'page_size' => 'nullable|integer',
  38. 'name' => 'nullable',
  39. 'uid' => 'nullable',
  40. ];
  41. $validator = Validator::make($inputData, $rules);
  42. if ($validator->fails()) {
  43. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  44. return json_fail($validator->errors()->first());
  45. }
  46. $orders = [
  47. ['created_at', 'DESC'],
  48. ];
  49. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  50. if ($pageSize > 500) {
  51. $pageSize = CommonConstant::PAGE_SIZE;
  52. }
  53. $page = format_page($inputData['page'] ?? 0);
  54. $items = $appRepo->pushCriteria(new AppPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
  55. ['*'], $page);
  56. return new AppPagesResponse($items);
  57. }
  58. /**
  59. * 应用详情
  60. * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse
  61. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  62. */
  63. public function detail(Validator $validatorFactory, AppRepository $appRepo)
  64. {
  65. $inputData = $this->getJsonData();
  66. $rules = [
  67. 'uid' => 'required|string',
  68. ];
  69. $validator = Validator::make($inputData, $rules);
  70. if ($validator->fails()) {
  71. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  72. return json_fail($validator->errors()->first());
  73. }
  74. $app = $appRepo->findBy('uid', $inputData['uid']);
  75. return new NotIncrementIdDetailResponse($app);
  76. }
  77. /**
  78. * 添加应用
  79. * @return \Illuminate\Http\JsonResponse|mixed
  80. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  81. */
  82. public function create(Validator $validatorFactory, AppRepository $appRepo)
  83. {
  84. $inputData = $this->getJsonData();
  85. $rules = [
  86. 'name' => 'required|string|max:120',
  87. 'android_url' => 'nullable|string|max:700',
  88. 'ios_url' => 'nullable|string|max:700',
  89. 'android_version' => 'required|max:16',
  90. 'app_id' => 'required',
  91. 'ios_version' => 'required|max:700',
  92. 'android_enable' => 'required|integer|max:1',
  93. 'ios_enable' => 'required|integer|max:1',
  94. 'icon_url' => 'nullable|string|max:700',
  95. 'status' => 'nullable|integer',
  96. 'squad' => 'nullable|integer'
  97. ];
  98. $validator = Validator::make($inputData, $rules);
  99. if ($validator->fails()) {
  100. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  101. return json_fail($validator->errors()->first());
  102. }
  103. $setarr = [
  104. 'name' => $inputData['name'],
  105. 'android_url' => $inputData['android_url'],
  106. 'ios_url' => $inputData['ios_url'],
  107. 'android_version' => $inputData['android_version'],
  108. 'ios_version' => $inputData['ios_version'],
  109. 'android_enable' => $inputData['android_enable'],
  110. 'ios_enable' => $inputData['ios_enable'],
  111. 'icon_url' => $inputData['icon_url'],
  112. 'status' => $inputData['status'] ?? 1,
  113. 'squad' => $inputData['squad'] ?? 1000,
  114. 'app_id' => $inputData['app_id'],
  115. ];
  116. $result = $appRepo->create($setarr);
  117. if (!$result) {
  118. return json_fail('创建失败');
  119. } else {
  120. // 母包初始化打包 //目前简单判断
  121. if ($result->android_url){
  122. $initMomPack = [
  123. 'mom_app_name' => $result->name,
  124. 'mom_app_uid' => $result->uid,
  125. 'mom_app_id' => $result->app_id,
  126. 'android_or_ios' => 1,
  127. 'mom_app_download' => $result->android_url,
  128. 'mom_app_version' => $result->android_version,
  129. ];
  130. $this->dispatch((new InitAppPackJob($initMomPack))->onQueue('app_pack'));
  131. }
  132. $logSubject = '添加一个App应用, 名称:' . $result->name;
  133. $logSvalue = $result->uid;
  134. $logDbData = [
  135. 'after_items' => [
  136. $result->toArray(),
  137. ],
  138. 'before_items' => [
  139. ],
  140. ];
  141. $this->log($logSubject, $logSvalue, $logDbData);
  142. //创建渠道
  143. /**
  144. * @var AgentRepository $agentRepo
  145. */
  146. $agentRepo = app(AgentRepository::class);
  147. $agentData = $agentRepo->all(['uid']);
  148. /**
  149. * @var ChannelRepository $chanRepo
  150. */
  151. $chanRepo = app(ChannelRepository::class);
  152. $setData = [];
  153. foreach ($agentData->toArray() as $key => $item) {
  154. $setData['name'] = $result->uid . '_' . $key;
  155. $setData['agent_uid'] = $item['uid'];
  156. $setData['mom_app_uid'] = $result->uid;
  157. $chanRepo->create($setData);
  158. }
  159. return new CommonSuccessUidResponse($result->uid);
  160. }
  161. }
  162. /**
  163. * 编辑应用
  164. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  165. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  166. */
  167. public function modify(Validator $validatorFactory, AppRepository $appRepo)
  168. {
  169. $inputData = $this->getJsonData();
  170. $rules = [
  171. 'uid' => 'required|string',
  172. 'name' => 'required|string|max:120',
  173. 'android_url' => 'nullable|string|max:700',
  174. 'ios_url' => 'nullable|string|max:700',
  175. 'android_version' => 'required|max:16',
  176. 'ios_version' => 'required|max:700',
  177. 'android_enable' => 'required|integer|max:1',
  178. 'ios_enable' => 'required|integer|max:1',
  179. 'icon_url' => 'nullable|string|max:700',
  180. 'status' => 'nullable|integer',
  181. 'squad' => 'nullable|integer',
  182. 'app_id' => 'required',
  183. ];
  184. $validator = Validator::make($inputData, $rules);
  185. if ($validator->fails()) {
  186. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  187. return json_fail($validator->errors()->first());
  188. }
  189. $app = $appRepo->findBy('uid', $inputData['uid']);
  190. if (empty($app)) {
  191. return json_fail(trans('common.err_resource_not_found'));
  192. }
  193. $oldApp = clone $app;
  194. if (isset($inputData['name'])) {
  195. $app->name = trim($inputData['name']);
  196. }
  197. if (isset($inputData['android_url'])) {
  198. $app->android_url = trim($inputData['android_url']);
  199. }
  200. if (isset($inputData['ios_url'])) {
  201. $app->ios_url = trim($inputData['ios_url']);
  202. }
  203. if (isset($inputData['android_version'])) {
  204. $app->android_version = trim($inputData['android_version']);
  205. }
  206. if (isset($inputData['ios_version'])) {
  207. $app->ios_version = trim($inputData['ios_version']);
  208. }
  209. if (isset($inputData['android_enable'])) {
  210. $app->android_enable = trim($inputData['android_enable']);
  211. }
  212. if (isset($inputData['ios_enable'])) {
  213. $app->ios_enable = trim($inputData['ios_enable']);
  214. }
  215. if (isset($inputData['icon_url'])) {
  216. $app->icon_url = trim($inputData['icon_url']);
  217. }
  218. if (isset($inputData['status'])) {
  219. $app->status = trim($inputData['status']);
  220. }
  221. if (isset($inputData['app_id'])) {
  222. $app->app_id = trim($inputData['app_id']);
  223. }
  224. if (isset($inputData['squad'])) {
  225. $app->squad = trim($inputData['squad']);
  226. }
  227. $result = $app->save();
  228. if (!$result) {
  229. return json_fail('修改失败');
  230. } else {
  231. // 母包初始化打包 //目前简单判断
  232. if ($app->android_url){
  233. $initMomPack = [
  234. 'mom_app_name' => $app->name,
  235. 'mom_app_uid' => $app->uid,
  236. 'mom_app_id' => $app->app_id,
  237. 'android_or_ios' => 1,
  238. 'mom_app_download' => $app->android_url,
  239. 'mom_app_version' => $app->android_version,
  240. ];
  241. $this->dispatch((new InitAppPackJob($initMomPack))->onQueue('app_pack'));
  242. }
  243. $logSubject = '编辑一个App应用, 名称:' . $app->name;
  244. $logSvalue = $app->uid;
  245. $logDbData = [
  246. 'after_items' => [
  247. $app->toArray(),
  248. ],
  249. 'before_items' => [
  250. $oldApp->toArray(),
  251. ],
  252. ];
  253. $this->log($logSubject, $logSvalue, $logDbData);
  254. return new CommonSuccessUidResponse($app->uid);
  255. }
  256. }
  257. /**
  258. * 应用删除
  259. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  260. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  261. */
  262. public function delete(Validator $validatorFactory, AppRepository $appRepo)
  263. {
  264. $inputData = $this->getJsonData();
  265. $rules = [
  266. 'uid' => 'required',
  267. ];
  268. $validator = Validator::make($inputData, $rules);
  269. if ($validator->fails()) {
  270. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  271. return json_fail($validator->errors()->first());
  272. }
  273. $app = $appRepo->findBy('uid', $inputData['uid']);
  274. if (empty($app)) {
  275. return json_fail(trans('common.err_resource_not_found'));
  276. }
  277. $oldApp = clone $app;
  278. $result = $app->delete();
  279. if (!$result) {
  280. return json_fail('删除失败');
  281. } else {
  282. $logSubject = '删除一个应用,名称:' . $app->name;
  283. $logSvalue = $app->uid;
  284. $logDbData = [
  285. 'after_items' => [
  286. ],
  287. 'before_items' => [
  288. $oldApp->toArray(),
  289. ],
  290. ];
  291. $this->log($logSubject, $logSvalue, $logDbData);
  292. return new CommonSuccessUidResponse($app->uid);
  293. }
  294. }
  295. /**
  296. * 批量修改
  297. * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
  298. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  299. */
  300. public function batchDelete(Validator $validatorFactory, AppRepository $appRepo)
  301. {
  302. $inputData = $this->getJsonData();
  303. $rules = [
  304. 'uids' => 'required',
  305. ];
  306. $validator = Validator::make($inputData, $rules);
  307. if ($validator->fails()) {
  308. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  309. return json_fail($validator->errors()->first());
  310. }
  311. $appUidToArray = explode(',', $inputData['uids']);
  312. $appUIds = $appRepo->findArray('uid', $appUidToArray);
  313. if (empty($appUIds)) {
  314. return json_fail(trans('common.err_resource_not_found'));
  315. }
  316. $appUidToStr = implode(',', $appUIds);
  317. if (!$appRepo->delectUids('uid', $appUIds)) {
  318. return json_fail('删除失败');
  319. } else {
  320. $logSubject = '批量删除应用UID:' . $appUidToStr;
  321. $logSvalue = $appUidToStr;
  322. $logDbData = [
  323. 'after_items' => [
  324. ],
  325. 'before_items' => [
  326. $appUIds,
  327. ],
  328. ];
  329. $this->log($logSubject, $logSvalue, $logDbData);
  330. return new CommonSuccessUidResponse($appUidToStr);
  331. }
  332. }
  333. public function editStatus(Validator $validatorFactory, AppRepository $appRepo)
  334. {
  335. $inputData = $this->getJsonData();
  336. $rules = [
  337. 'uid' => 'required|string'
  338. ];
  339. $validator = Validator::make($inputData, $rules);
  340. if ($validator->fails()) {
  341. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  342. return json_fail($validator->errors()->first());
  343. }
  344. $app = $appRepo->findBy('uid', $inputData['uid']);
  345. if (empty($app)) {
  346. return json_fail(trans('common.err_resource_not_found'));
  347. }
  348. $oldApp = clone $app;
  349. $app->status = $app->status === 1 ? 2 : 1;
  350. $result = $app->save();
  351. if (!$result) {
  352. return json_fail('修改失败');
  353. } else {
  354. $logSubject = '编辑一个App应用, 名称:' . $app->name;
  355. $logSvalue = $app->uid;
  356. $logDbData = [
  357. 'after_items' => [
  358. $app->toArray(),
  359. ],
  360. 'before_items' => [
  361. $oldApp->toArray(),
  362. ],
  363. ];
  364. $this->log($logSubject, $logSvalue, $logDbData);
  365. return new CommonSuccessUidResponse($app->uid);
  366. }
  367. }
  368. public function editAndroidEnable(Validator $validatorFactory, AppRepository $appRepo)
  369. {
  370. $inputData = $this->getJsonData();
  371. $rules = [
  372. 'uid' => 'required|string'
  373. ];
  374. $validator = Validator::make($inputData, $rules);
  375. if ($validator->fails()) {
  376. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  377. return json_fail($validator->errors()->first());
  378. }
  379. $app = $appRepo->findBy('uid', $inputData['uid']);
  380. if (empty($app)) {
  381. return json_fail(trans('common.err_resource_not_found'));
  382. }
  383. $oldApp = clone $app;
  384. $app->android_enable = $app->android_enable === 1 ? 0 : 1;
  385. $result = $app->save();
  386. if (!$result) {
  387. return json_fail('修改失败');
  388. } else {
  389. $logSubject = '编辑一个App应用android_enable状态, 名称:' . $app->name;
  390. $logSvalue = $app->uid;
  391. $logDbData = [
  392. 'after_items' => [
  393. $app->toArray(),
  394. ],
  395. 'before_items' => [
  396. $oldApp->toArray(),
  397. ],
  398. ];
  399. $this->log($logSubject, $logSvalue, $logDbData);
  400. return new CommonSuccessUidResponse($app->uid);
  401. }
  402. }
  403. public function editIosEnable(Validator $validatorFactory, AppRepository $appRepo)
  404. {
  405. $inputData = $this->getJsonData();
  406. $rules = [
  407. 'uid' => 'required|string'
  408. ];
  409. $validator = Validator::make($inputData, $rules);
  410. if ($validator->fails()) {
  411. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  412. return json_fail($validator->errors()->first());
  413. }
  414. $app = $appRepo->findBy('uid', $inputData['uid']);
  415. if (empty($app)) {
  416. return json_fail(trans('common.err_resource_not_found'));
  417. }
  418. $oldApp = clone $app;
  419. $app->ios_enable = $app->ios_enable === 1 ? 0 : 1;
  420. $result = $app->save();
  421. if (!$result) {
  422. return json_fail('修改失败');
  423. } else {
  424. $logSubject = '编辑一个App应用ios_enable状态, 名称:' . $app->name;
  425. $logSvalue = $app->uid;
  426. $logDbData = [
  427. 'after_items' => [
  428. $app->toArray(),
  429. ],
  430. 'before_items' => [
  431. $oldApp->toArray(),
  432. ],
  433. ];
  434. $this->log($logSubject, $logSvalue, $logDbData);
  435. return new CommonSuccessUidResponse($app->uid);
  436. }
  437. }
  438. /**
  439. * 批量操作应用
  440. * @return \Illuminate\Http\JsonResponse|mixed
  441. */
  442. public function batch(Validator $validatorFactory, AppRepository $appRepo)
  443. {
  444. $inputData = $this->getJsonData();
  445. $operateName = $inputData['operate_name'] ?? '';
  446. switch ($operateName) {
  447. case 'delete':
  448. $itemPks = $inputData['item_pks'] ?? [];
  449. if (!empty($itemPks)) {
  450. $model = new AppModel();
  451. $resultItems = $model->whereIn('uid', $itemPks)->delete();
  452. if ($resultItems) {
  453. $strPks = implode(',', $itemPks);
  454. $logSubject = '批量删除多个app应用';
  455. $logSvalue = $strPks;
  456. $logDbData = [
  457. 'after_items' => [
  458. ],
  459. 'before_items' => [
  460. ],
  461. ];
  462. $this->log($logSubject, $logSvalue, $logDbData);
  463. }
  464. }
  465. $data = [
  466. 'result' => 'success',
  467. 'pks' => $itemPks,
  468. ];
  469. return json_success($data);
  470. break;
  471. default:
  472. break;
  473. }
  474. return json_fail('未知操作');
  475. }
  476. /**
  477. * 所用应用
  478. * @param AppRepository $appRepo
  479. * @return CommonAllResponse
  480. */
  481. public function all(AppRepository $appRepo)
  482. {
  483. $orders = [
  484. ['name', 'ASC'],
  485. ];
  486. $items = $appRepo->pushCriteria(new OrderLimitCriteria($orders))->all();
  487. return new CommonAllResponse($items);
  488. }
  489. }