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

4 years ago
4 years ago
4 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. return $this->error('用户不存在或已被禁用' . $this->user_id);
  30. }
  31. }
  32. //检查代理商
  33. $this->agent_id = Cache::get($appid);
  34. if (!$this->agent_id) {
  35. return $this->error('商户不存在或已被禁用');
  36. }
  37. $agent = Agent::query()->firstWhere(['id' => $this->agent_id, 'status' => 1]);
  38. if (!$agent) {
  39. return $this->error('商户不存在或已被禁用');
  40. }
  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. 'time' => microtime(true) - LARAVEL_START
  50. ])->send();
  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. 'time' => microtime(true) - LARAVEL_START
  60. ])->send();
  61. }
  62. }