Controller.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Foundation\Bus\DispatchesJobs;
  4. use Illuminate\Routing\Controller as BaseController;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Validator;
  9. use Illuminate\Http\JsonResponse;
  10. use Illuminate\Pagination\LengthAwarePaginator;
  11. class Controller extends BaseController
  12. {
  13. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  14. /**
  15. * 获取用户所属的项目ID
  16. * @return array|null
  17. */
  18. public function projectIds()
  19. {
  20. $user = Auth::user();
  21. if (!empty($user->is_super))
  22. return null;
  23. $role = $user->role ?? null;
  24. if (!$role) return [];
  25. return $role->projects->pluck('id')->toArray();
  26. }
  27. /**
  28. * ajax返回
  29. * @param $rows
  30. * @param $total
  31. * @return JsonResponse
  32. */
  33. public function ajaxListSuccess($rows, $total)
  34. {
  35. return response()->json([
  36. 'rows' => $rows,
  37. 'total' => $total,
  38. ]);
  39. }
  40. /**
  41. * 成功返回
  42. * @param $message
  43. * @return JsonResponse
  44. */
  45. public function success($message)
  46. {
  47. return response()->json(['message' => $message]);
  48. }
  49. /**
  50. * 错误返回
  51. * @param $message
  52. * @return JsonResponse
  53. */
  54. public function error($message)
  55. {
  56. return response()->json(['message' => $message], 400);
  57. }
  58. /**
  59. * 表单验证
  60. * @param $data
  61. * @param $rules
  62. * @param $message
  63. * @param $model object
  64. * @param $id
  65. * @return JsonResponse
  66. */
  67. public function verification($data, $rules, $message, $model, $id)
  68. {
  69. $validator = Validator::make($data, $rules, $message);
  70. if ($validator->fails()) {
  71. $errorList = $validator->errors()->all();
  72. return $this->error($errorList[0]);
  73. }
  74. if (empty($id)) {
  75. $result = $model->insert($data);
  76. } else {
  77. $data['updated_at'] = date('Y-m-d H:i:s', time());
  78. $result = $model->where('id', $id)->update($data);
  79. }
  80. if (empty($result)) {
  81. return $this->error('服务器发生错误');
  82. }
  83. return $this->success('操作成功');
  84. }
  85. /**
  86. * 获取列表
  87. * @param $model object
  88. * @param $pageSize
  89. * @return JsonResponse
  90. */
  91. public function getList($model, $pageSize)
  92. {
  93. $result = $model->whereNull('deleted_at')->paginate($pageSize);
  94. return $this->ajaxListSuccess($result->items(), $result->total());
  95. }
  96. /**
  97. * 删除
  98. * @param $model object
  99. * @param $id
  100. * @return JsonResponse
  101. */
  102. public function deleteInfo($model, $id)
  103. {
  104. $result = $model->where('id', $id)->update(['deleted_at' => date('Y-m-d H:i:s', time())]);
  105. if (!empty($result)) {
  106. return $this->success('删除成功');
  107. } else {
  108. return $this->error('删除失败');
  109. }
  110. }
  111. /**
  112. * 自定义分页方法
  113. * @param $collection object
  114. * @param $perPage
  115. * @param string $pageName
  116. * @param null $fragment
  117. * @return LengthAwarePaginator
  118. */
  119. public function paginateCollection($collection, $perPage, $pageName = 'page', $fragment = null)
  120. {
  121. $currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage($pageName);
  122. $currentPageItems = $collection->slice(($currentPage - 1) * $perPage, $perPage)->values();
  123. parse_str(request()->getQueryString(), $query);
  124. unset($query[$pageName]);
  125. $paginator = new \Illuminate\Pagination\LengthAwarePaginator(
  126. $currentPageItems,
  127. $collection->count(),
  128. $perPage,
  129. $currentPage,
  130. [
  131. 'pageName' => $pageName,
  132. 'path' => \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPath(),
  133. 'query' => $query,
  134. 'fragment' => $fragment
  135. ]
  136. );
  137. return $paginator;
  138. }
  139. /**
  140. * 二维数组排序
  141. * @param $array
  142. * @param $keys
  143. * @param string $sort
  144. * @return array
  145. */
  146. public function arraySort($array, $keys, $sort = 'asc')
  147. {
  148. $newArr = $valArr = array();
  149. foreach ($array as $key => $value) {
  150. $valArr[$key] = $value[$keys];
  151. }
  152. ($sort == 'asc') ? asort($valArr) : arsort($valArr);
  153. reset($valArr);
  154. foreach ($valArr as $key => $value) {
  155. $newArr[$key] = $array[$key];
  156. }
  157. return $newArr;
  158. }
  159. /**
  160. * 二维数组分组
  161. * @param $arr
  162. * @param $key
  163. * @return array
  164. */
  165. public function arrayGroupBy($arr, $key)
  166. {
  167. $grouped = array();
  168. foreach ($arr as $value) {
  169. $grouped[$value[$key]][] = $value;
  170. }
  171. if (func_num_args() > 2) {
  172. $args = func_get_args();
  173. foreach ($grouped as $key => $value) {
  174. $parameter = array_merge($value, array_slice($args, 2, func_num_args()));
  175. $grouped[$key] = call_user_func_array('array_group_by', $parameter);
  176. }
  177. }
  178. return $grouped;
  179. }
  180. /**
  181. * 模版站api成功返回
  182. * @param $list
  183. * @param $siteInfo
  184. * @param $setting
  185. * @param $advertise
  186. * @return JsonResponse
  187. */
  188. public function returnApiSuccess($list, $siteInfo = [], $setting = [], $advertise = [])
  189. {
  190. return response()->json([
  191. 'status' => 200,
  192. 'message' => 'success',
  193. 'data' => $list,
  194. 'siteInfo' => $siteInfo,
  195. 'setting' => $setting,
  196. 'advertise' => $advertise,
  197. ]);
  198. }
  199. /**
  200. * 模版站api失败返回
  201. * @param array $data
  202. * @return JsonResponse
  203. */
  204. public function returnApiError($data = [])
  205. {
  206. return response()->json([
  207. 'status' => 500,
  208. 'message' => 'error',
  209. 'data' => $data,
  210. ]);
  211. }
  212. }