123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Foundation\Bus\DispatchesJobs;
- use Illuminate\Routing\Controller as BaseController;
- use Illuminate\Foundation\Validation\ValidatesRequests;
- use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Pagination\LengthAwarePaginator;
- class Controller extends BaseController
- {
- use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
- /**
- * 获取用户所属的项目ID
- * @return array|null
- */
- public function projectIds()
- {
- $user = Auth::user();
- if (!empty($user->is_super))
- return null;
- $role = $user->role ?? null;
- if (!$role) return [];
- return $role->projects->pluck('id')->toArray();
- }
- /**
- * ajax返回
- * @param $rows
- * @param $total
- * @return JsonResponse
- */
- public function ajaxListSuccess($rows, $total)
- {
- return response()->json([
- 'rows' => $rows,
- 'total' => $total,
- ]);
- }
- /**
- * 成功返回
- * @param $message
- * @return JsonResponse
- */
- public function success($message)
- {
- return response()->json(['message' => $message]);
- }
- /**
- * 错误返回
- * @param $message
- * @return JsonResponse
- */
- public function error($message)
- {
- return response()->json(['message' => $message], 400);
- }
- /**
- * 表单验证
- * @param $data
- * @param $rules
- * @param $message
- * @param $model object
- * @param $id
- * @return JsonResponse
- */
- public function verification($data, $rules, $message, $model, $id)
- {
- $validator = Validator::make($data, $rules, $message);
- if ($validator->fails()) {
- $errorList = $validator->errors()->all();
- return $this->error($errorList[0]);
- }
- if (empty($id)) {
- $result = $model->insert($data);
- } else {
- $data['updated_at'] = date('Y-m-d H:i:s', time());
- $result = $model->where('id', $id)->update($data);
- }
- if (empty($result)) {
- return $this->error('服务器发生错误');
- }
- return $this->success('操作成功');
- }
- /**
- * 获取列表
- * @param $model object
- * @param $pageSize
- * @return JsonResponse
- */
- public function getList($model, $pageSize)
- {
- $result = $model->whereNull('deleted_at')->paginate($pageSize);
- return $this->ajaxListSuccess($result->items(), $result->total());
- }
- /**
- * 删除
- * @param $model object
- * @param $id
- * @return JsonResponse
- */
- public function deleteInfo($model, $id)
- {
- $result = $model->where('id', $id)->update(['deleted_at' => date('Y-m-d H:i:s', time())]);
- if (!empty($result)) {
- return $this->success('删除成功');
- } else {
- return $this->error('删除失败');
- }
- }
- /**
- * 自定义分页方法
- * @param $collection object
- * @param $perPage
- * @param string $pageName
- * @param null $fragment
- * @return LengthAwarePaginator
- */
- public function paginateCollection($collection, $perPage, $pageName = 'page', $fragment = null)
- {
- $currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage($pageName);
- $currentPageItems = $collection->slice(($currentPage - 1) * $perPage, $perPage)->values();
- parse_str(request()->getQueryString(), $query);
- unset($query[$pageName]);
- $paginator = new \Illuminate\Pagination\LengthAwarePaginator(
- $currentPageItems,
- $collection->count(),
- $perPage,
- $currentPage,
- [
- 'pageName' => $pageName,
- 'path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath(),
- 'query' => $query,
- 'fragment' => $fragment
- ]
- );
- return $paginator;
- }
- /**
- * 二维数组排序
- * @param $array
- * @param $keys
- * @param string $sort
- * @return array
- */
- public function arraySort($array, $keys, $sort = 'asc')
- {
- $newArr = $valArr = array();
- foreach ($array as $key => $value) {
- $valArr[$key] = $value[$keys];
- }
- ($sort == 'asc') ? asort($valArr) : arsort($valArr);
- reset($valArr);
- foreach ($valArr as $key => $value) {
- $newArr[$key] = $array[$key];
- }
- return $newArr;
- }
- /**
- * 二维数组分组
- * @param $arr
- * @param $key
- * @return array
- */
- public function arrayGroupBy($arr, $key)
- {
- $grouped = array();
- foreach ($arr as $value) {
- $grouped[$value[$key]][] = $value;
- }
- if (func_num_args() > 2) {
- $args = func_get_args();
- foreach ($grouped as $key => $value) {
- $parameter = array_merge($value, array_slice($args, 2, func_num_args()));
- $grouped[$key] = call_user_func_array('array_group_by', $parameter);
- }
- }
- return $grouped;
- }
- /**
- * 模版站api成功返回
- * @param $list
- * @param $siteInfo
- * @param $setting
- * @param $advertise
- * @return JsonResponse
- */
- public function returnApiSuccess($list, $siteInfo = [], $setting = [], $advertise = [])
- {
- return response()->json([
- 'status' => 200,
- 'message' => 'success',
- 'data' => $list,
- 'siteInfo' => $siteInfo,
- 'setting' => $setting,
- 'advertise' => $advertise,
- ]);
- }
- /**
- * 模版站api失败返回
- * @param array $data
- * @return JsonResponse
- */
- public function returnApiError($data = [])
- {
- return response()->json([
- 'status' => 500,
- 'message' => 'error',
- 'data' => $data,
- ]);
- }
- }
|