海南旅游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.

71 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. if (empty($user)) {
  28. $this->user_id = 0;
  29. $this->error('登录信息已过期,请重新登录')->send();
  30. exit();
  31. }
  32. }
  33. }
  34. //检查代理商
  35. $agent = Agent::query()->firstWhere(['appid' => $appid, 'status' => 1]);
  36. if (!$agent) {
  37. $this->error('商户不存在或已被禁用')->send();
  38. exit();
  39. }
  40. $this->agent_id = $agent->id;
  41. }
  42. protected function success($data = [], $msg = 'success', $code = 0, $status = 200)
  43. {
  44. return response()->json([
  45. 'code' => $code,
  46. 'msg' => $msg,
  47. 'data' => $data,
  48. 'status' => $status,
  49. 'debug_time' => microtime(true) - LARAVEL_START
  50. ]);
  51. }
  52. protected function error($msg = 'error', $code = -1, $status = 500)
  53. {
  54. return response()->json([
  55. 'code' => $code,
  56. 'msg' => $msg,
  57. 'data' => [],
  58. 'status' => $status,
  59. 'debug_time' => microtime(true) - LARAVEL_START
  60. ]);
  61. }
  62. }