海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Agent;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  7. use Illuminate\Foundation\Bus\DispatchesJobs;
  8. use Illuminate\Foundation\Validation\ValidatesRequests;
  9. use Illuminate\Routing\Controller as BaseController;
  10. class Controller extends BaseController
  11. {
  12. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  13. protected $user_id = 0;
  14. protected $agent_id = 0; //代理商ID
  15. public function __construct()
  16. {
  17. // Cache::put('wx0e8ebcd9ca9e4b97', 1);
  18. // Cache::put('c4ca4238a0b923820dcc509a6f75849b', 1);
  19. // TODO 登录部分待优化
  20. $auth = request()->header('Authentication');
  21. $appid = request()->header('appid');
  22. //检查用户
  23. if ($auth) {
  24. $this->user_id = Cache::get($auth);
  25. if ($this->user_id) {
  26. $user = User::query()->firstWhere(['id' => $this->user_id, 'status' => 1]);
  27. }
  28. if (empty($user)) {
  29. $this->error('用户不存在或已被禁用' . $this->user_id);
  30. exit();
  31. }
  32. }
  33. //检查代理商
  34. $agent = Agent::query()->firstWhere(['appid' => $appid, 'status' => 1]);
  35. if (!$agent) {
  36. $this->error('商户不存在或已被禁用');
  37. exit();
  38. }
  39. $this->agent_id = $agent->id;
  40. }
  41. protected function success($data = [], $msg = 'success', $code = 0, $status = 200)
  42. {
  43. return response()->json([
  44. 'code' => $code,
  45. 'msg' => $msg,
  46. 'data' => $data,
  47. 'status' => $status,
  48. 'debug_time' => microtime(true) - LARAVEL_START
  49. ])->send();
  50. }
  51. protected function error($msg = 'error', $code = -1, $status = 500)
  52. {
  53. return response()->json([
  54. 'code' => $code,
  55. 'msg' => $msg,
  56. 'data' => [],
  57. 'status' => $status,
  58. 'debug_time' => microtime(true) - LARAVEL_START
  59. ])->send();
  60. }
  61. }