Controller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  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 App\Services\Base\ErrorCode;
  8. use Request, Response, Auth, Log;
  9. class Controller extends BaseController
  10. {
  11. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  12. // protected $_data = null;
  13. protected $_user = null;
  14. public function __construct()
  15. {
  16. $this->middleware('auth:api', [
  17. 'except' => [
  18. 'upload', 'getCode', 'reset', 'login', 'get', 'register', 'alipayNotify', 'wechatpayNotify', 'get', 'area', 'get_province', 'get_city', 'get_county', 'albumStyle', 'test', 'index', 'companyInfo', 'shop2', 'cardIndex', 'cardUserInfo', 'cardUserProgress', 'cardUserHonor', 'cardUserProject', 'CardUserTrend', 'projectDetail', 'trendDetail', 'albumSetting', 'albumXyxLogin', 'albumCat', 'albumchecklogin', 'albumGoods', 'albumGoodsDetail', 'albumSetPrice', 'albumXcxLogin', 'albumContentList', 'albumSearchGoods','albumContentDetail','albumFavoriteList','albumAddFavorite','albumFavoriteDel','getAttr','getOrder','getProgress','getReviewCount', 'furnitureNewsDetail','furnitureSetting','furnitureXcxLogin','furnitureGoodsList','serviceLogin','getFurnitureAds','getPhoneNumber','getQrcode','orderCount','searchList','printOrder','saveFormId','furnitureNewsList','getMoreComments','addToLike','albumSavePhone',
  19. 'albumGetWatchRecord','albumSetWatch','albumGetCartOfWatch','albumSaveFormId','albumAddAgent','albumGetBanner','albumGetDataGoods','newgoods_list','newgoods_index'
  20. ]
  21. ]);
  22. // \DB::connection()->enableQueryLog();
  23. // $queries = \DB::getQueryLog();
  24. // dd($queries);
  25. // $this->_user = Auth::user();
  26. // if ($this->_user !== null) {
  27. // $this->_user->last_ip = Request::ip();
  28. // }
  29. // $data = $this->rawPostData();
  30. // if (!$this->checkSignature($data, env('APP_SECRET'))) {
  31. // $this->rawError(ErrorCode::CLIENT_APP_CHECKSUM_ERROR);
  32. // }
  33. // unset($data['nonce_str'], $data['timestamp'], $data['sig']);
  34. // $this->_data = $data;
  35. }
  36. // public function saveLastIp() {
  37. // if ($this->_user !== null) {
  38. // $this->_user->save();
  39. // }
  40. // }
  41. public function rawPostData()
  42. {
  43. $request = Request::instance();
  44. $data = $request->getContent();
  45. return json_decode($data, true);
  46. }
  47. public function rawApi($data, $code = 0, $message = '')
  48. {
  49. $ret = $this->genApiData($data, $code, $message);
  50. return json_encode($ret);
  51. }
  52. public function api($data, $code = 0, $message = '')
  53. {
  54. $ret = $this->genApiData($data, $code, $message);
  55. $status = $code === 0 ? 200 : 400;
  56. return Response::json($ret, $status);
  57. }
  58. public function validatorError($arr, $code = 0, $message = '')
  59. {
  60. Log::info($arr);
  61. foreach ($arr as $val) {
  62. if ($val && $message == '') {
  63. $message = $val;
  64. }
  65. }
  66. $ret = $this->genApiData(null, $code, $message);
  67. $status = $code === 0 ? 200 : 400;
  68. return Response::json($ret, $status);
  69. }
  70. public function error($code, $message = '', $data = null)
  71. {
  72. return $this->api($data, $code, $message);
  73. }
  74. public function rawError($code, $message = '')
  75. {
  76. echo $this->rawApi(null, $code, $message);
  77. exit;
  78. }
  79. private function genApiData($data, $code = 0, $message = '')
  80. {
  81. if ($code !== 0 && ErrorCode::CLIENT_WRONG_PARAMS && empty($message)) {
  82. $message = ErrorCode::message($code);
  83. }
  84. $ret = [
  85. 'status' => $code == 0,
  86. 'status_code' => $code,
  87. 'message' => $message,
  88. 'data' => $data
  89. ];
  90. return $ret;
  91. }
  92. }