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

74 lines
2.0 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. $this->agent_id = Cache::get($appid);
  35. if (!$this->agent_id) {
  36. $this->error('商户不存在或已被禁用');
  37. exit();
  38. }
  39. $agent = Agent::query()->firstWhere(['id' => $this->agent_id, 'status' => 1]);
  40. if (!$agent) {
  41. $this->error('商户不存在或已被禁用');
  42. exit();
  43. }
  44. }
  45. protected function success($data = [], $msg = 'success', $code = 0, $status = 200)
  46. {
  47. return response()->json([
  48. 'code' => $code,
  49. 'msg' => $msg,
  50. 'data' => $data,
  51. 'status' => $status,
  52. 'time' => microtime(true) - LARAVEL_START
  53. ])->send();
  54. }
  55. protected function error($msg = 'error', $code = -1, $status = 500)
  56. {
  57. return response()->json([
  58. 'code' => $code,
  59. 'msg' => $msg,
  60. 'data' => [],
  61. 'status' => $status,
  62. 'time' => microtime(true) - LARAVEL_START
  63. ])->send();
  64. }
  65. }