| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Jobs\InitAppPackJob;
- use App\Models\Mysql\AppModel;
- use App\Repositories\AgentRepository;
- use App\Repositories\AppRepository;
- use App\Repositories\ChannelRepository;
- use App\Repositories\Criterias\AppPagesCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use Illuminate\Support\Facades\Validator;
- use Modules\ManageApi\Responses\AppPagesResponse;
- use Modules\ManageApi\Responses\CommonAllResponse;
- use Modules\ManageApi\Responses\CommonDetailResponse;
- use Modules\ManageApi\Responses\CommonSuccessUidResponse;
- use Modules\ManageApi\Responses\NotIncrementIdDetailResponse;
- class AppController extends BaseController
- {
- /**
- * 应用列表
- * @return \Illuminate\Http\JsonResponse|AppPagesResponse
- */
- public function lists(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'name' => 'nullable',
- 'uid' => 'nullable',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orders = [
- ['created_at', 'DESC'],
- ];
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $items = $appRepo->pushCriteria(new AppPagesCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize,
- ['*'], $page);
- return new AppPagesResponse($items);
- }
- /**
- * 应用详情
- * @return \Illuminate\Http\JsonResponse|NotIncrementIdDetailResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function detail(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- return new NotIncrementIdDetailResponse($app);
- }
- /**
- * 添加应用
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function create(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'required|string|max:120',
- 'android_url' => 'nullable|string|max:700',
- 'ios_url' => 'nullable|string|max:700',
- 'android_version' => 'required|max:16',
- 'app_id' => 'required',
- 'ios_version' => 'required|max:700',
- 'android_enable' => 'required|integer|max:1',
- 'ios_enable' => 'required|integer|max:1',
- 'icon_url' => 'nullable|string|max:700',
- 'status' => 'nullable|integer',
- 'squad' => 'nullable|integer'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $setarr = [
- 'name' => $inputData['name'],
- 'android_url' => $inputData['android_url'],
- 'ios_url' => $inputData['ios_url'],
- 'android_version' => $inputData['android_version'],
- 'ios_version' => $inputData['ios_version'],
- 'android_enable' => $inputData['android_enable'],
- 'ios_enable' => $inputData['ios_enable'],
- 'icon_url' => $inputData['icon_url'],
- 'status' => $inputData['status'] ?? 1,
- 'squad' => $inputData['squad'] ?? 1000,
- 'app_id' => $inputData['app_id'],
- ];
- $result = $appRepo->create($setarr);
- if (!$result) {
- return json_fail('创建失败');
- } else {
- // 母包初始化打包 //目前简单判断
- if ($result->android_url){
- $initMomPack = [
- 'mom_app_name' => $result->name,
- 'mom_app_uid' => $result->uid,
- 'mom_app_id' => $result->app_id,
- 'android_or_ios' => 1,
- 'mom_app_download' => $result->android_url,
- 'mom_app_version' => $result->android_version,
- ];
- $this->dispatch((new InitAppPackJob($initMomPack))->onQueue('app_pack'));
- }
- $logSubject = '添加一个App应用, 名称:' . $result->name;
- $logSvalue = $result->uid;
- $logDbData = [
- 'after_items' => [
- $result->toArray(),
- ],
- 'before_items' => [
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- //创建渠道
- /**
- * @var AgentRepository $agentRepo
- */
- $agentRepo = app(AgentRepository::class);
- $agentData = $agentRepo->all(['uid']);
- /**
- * @var ChannelRepository $chanRepo
- */
- $chanRepo = app(ChannelRepository::class);
- $setData = [];
- foreach ($agentData->toArray() as $key => $item) {
- $setData['name'] = $result->uid . '_' . $key;
- $setData['agent_uid'] = $item['uid'];
- $setData['mom_app_uid'] = $result->uid;
- $chanRepo->create($setData);
- }
- return new CommonSuccessUidResponse($result->uid);
- }
- }
- /**
- * 编辑应用
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function modify(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string',
- 'name' => 'required|string|max:120',
- 'android_url' => 'nullable|string|max:700',
- 'ios_url' => 'nullable|string|max:700',
- 'android_version' => 'required|max:16',
- 'ios_version' => 'required|max:700',
- 'android_enable' => 'required|integer|max:1',
- 'ios_enable' => 'required|integer|max:1',
- 'icon_url' => 'nullable|string|max:700',
- 'status' => 'nullable|integer',
- 'squad' => 'nullable|integer',
- 'app_id' => 'required',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- if (isset($inputData['name'])) {
- $app->name = trim($inputData['name']);
- }
- if (isset($inputData['android_url'])) {
- $app->android_url = trim($inputData['android_url']);
- }
- if (isset($inputData['ios_url'])) {
- $app->ios_url = trim($inputData['ios_url']);
- }
- if (isset($inputData['android_version'])) {
- $app->android_version = trim($inputData['android_version']);
- }
- if (isset($inputData['ios_version'])) {
- $app->ios_version = trim($inputData['ios_version']);
- }
- if (isset($inputData['android_enable'])) {
- $app->android_enable = trim($inputData['android_enable']);
- }
- if (isset($inputData['ios_enable'])) {
- $app->ios_enable = trim($inputData['ios_enable']);
- }
- if (isset($inputData['icon_url'])) {
- $app->icon_url = trim($inputData['icon_url']);
- }
- if (isset($inputData['status'])) {
- $app->status = trim($inputData['status']);
- }
- if (isset($inputData['app_id'])) {
- $app->app_id = trim($inputData['app_id']);
- }
- if (isset($inputData['squad'])) {
- $app->squad = trim($inputData['squad']);
- }
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- // 母包初始化打包 //目前简单判断
- if ($app->android_url){
- $initMomPack = [
- 'mom_app_name' => $app->name,
- 'mom_app_uid' => $app->uid,
- 'mom_app_id' => $app->app_id,
- 'android_or_ios' => 1,
- 'mom_app_download' => $app->android_url,
- 'mom_app_version' => $app->android_version,
- ];
- $this->dispatch((new InitAppPackJob($initMomPack))->onQueue('app_pack'));
- }
- $logSubject = '编辑一个App应用, 名称:' . $app->name;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- /**
- * 应用删除
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function delete(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $result = $app->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个应用,名称:' . $app->name;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- /**
- * 批量修改
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function batchDelete(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uids' => 'required',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $appUidToArray = explode(',', $inputData['uids']);
- $appUIds = $appRepo->findArray('uid', $appUidToArray);
- if (empty($appUIds)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $appUidToStr = implode(',', $appUIds);
- if (!$appRepo->delectUids('uid', $appUIds)) {
- return json_fail('删除失败');
- } else {
- $logSubject = '批量删除应用UID:' . $appUidToStr;
- $logSvalue = $appUidToStr;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $appUIds,
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($appUidToStr);
- }
- }
- public function editStatus(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $app->status = $app->status === 1 ? 2 : 1;
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个App应用, 名称:' . $app->name;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- public function editAndroidEnable(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $app->android_enable = $app->android_enable === 1 ? 0 : 1;
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个App应用android_enable状态, 名称:' . $app->name;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- public function editIosEnable(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $app = $appRepo->findBy('uid', $inputData['uid']);
- if (empty($app)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldApp = clone $app;
- $app->ios_enable = $app->ios_enable === 1 ? 0 : 1;
- $result = $app->save();
- if (!$result) {
- return json_fail('修改失败');
- } else {
- $logSubject = '编辑一个App应用ios_enable状态, 名称:' . $app->name;
- $logSvalue = $app->uid;
- $logDbData = [
- 'after_items' => [
- $app->toArray(),
- ],
- 'before_items' => [
- $oldApp->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($app->uid);
- }
- }
- /**
- * 批量操作应用
- * @return \Illuminate\Http\JsonResponse|mixed
- */
- public function batch(Validator $validatorFactory, AppRepository $appRepo)
- {
- $inputData = $this->getJsonData();
- $operateName = $inputData['operate_name'] ?? '';
- switch ($operateName) {
- case 'delete':
- $itemPks = $inputData['item_pks'] ?? [];
- if (!empty($itemPks)) {
- $model = new AppModel();
- $resultItems = $model->whereIn('uid', $itemPks)->delete();
- if ($resultItems) {
- $strPks = implode(',', $itemPks);
- $logSubject = '批量删除多个app应用';
- $logSvalue = $strPks;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- }
- }
- $data = [
- 'result' => 'success',
- 'pks' => $itemPks,
- ];
- return json_success($data);
- break;
- default:
- break;
- }
- return json_fail('未知操作');
- }
- /**
- * 所用应用
- * @param AppRepository $appRepo
- * @return CommonAllResponse
- */
- public function all(AppRepository $appRepo)
- {
- $orders = [
- ['name', 'ASC'],
- ];
- $items = $appRepo->pushCriteria(new OrderLimitCriteria($orders))->all();
- return new CommonAllResponse($items);
- }
- }
|