Base.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\mobile\controller;
  3. use app\core\exception\AppException;
  4. use app\core\service\User;
  5. use think\Config;
  6. use think\Log;
  7. class Base
  8. {
  9. public $user;
  10. public $data = [];
  11. public $request;
  12. public $app;
  13. public function __construct()
  14. {
  15. $this->request = request();
  16. $this->data = json_decode($this->request->getInput(), true);
  17. $this->check();
  18. $this->app = $this->data['app'];
  19. $this->request->bind('data', $this->data);
  20. $this->request->bind('app', $this->app);
  21. if(in_array($this->request->path(), Config::get('tourist_path')))
  22. {
  23. //游客直接跳过
  24. }
  25. else {
  26. $user = User::getUserByToken($this->data['token'], $this->data['osType']);
  27. if(!$user) {
  28. throw new AppException(-1000, 'token失效,需要重新登录');
  29. }
  30. $this->request->bind('user', $user);
  31. }
  32. $this->__initialize();
  33. }
  34. public function check() {
  35. Log::info($this->data);
  36. if(!isset($this->data['apiV'])) {
  37. throw new AppException(-101, 'api版本号不能为空');
  38. }
  39. if(!isset($this->data['osV'])) {
  40. throw new AppException(-102, 'osV操作系统版本号不能为空');
  41. }
  42. if(!isset($this->data['osType'])) {
  43. throw new AppException(-103, 'osType操作系统类型不能为空');
  44. }
  45. if(!isset($this->data['deviceID'])) {
  46. throw new AppException(-104, 'deviceID宿主app的唯一标识不能为空');
  47. }
  48. if(!isset($this->data['token'])) {
  49. throw new AppException(-105, 'token不能为空');
  50. }
  51. if(!isset($this->data['sign'])) {
  52. throw new AppException(-106, 'sign不能为空');
  53. }
  54. if(!isset($this->data['time'])) {
  55. throw new AppException(-107, 'time不能为空');
  56. }
  57. if(!isset($this->data['ip'])) {
  58. throw new AppException(-108, 'ip不能为空');
  59. }
  60. if(!isset($this->data['app'])) {
  61. throw new AppException(-109, 'app应用数据不能为空');
  62. }
  63. if(!isset($this->data['appV'])) {
  64. throw new AppException(-110, 'app版本号不能为空');
  65. }
  66. if(!isset($this->data['channel'])) {
  67. throw new AppException(-111, 'channel不能为空');
  68. }
  69. $key = '';
  70. if($this->data['osType'] == 1 || $this->data['osType'] == 2) {
  71. $key = Config::get('system.mobile_key');
  72. }
  73. else if($this->data['osType'] == 3) {
  74. $key = Config::get('system.pc_key');
  75. }
  76. $signPre = '';
  77. if($this->data['app'] === '') {
  78. $signPre = $key.$this->data['token'].$this->data['time'].$this->data['appV'].$this->data['apiV'].$this->data['osV'].$this->data['osType'].$this->data['deviceID'].$this->data['ip'].$this->data['channel'];
  79. }
  80. else if($this->data['app'] === []) {
  81. $signPre = $key.$this->data['token'].$this->data['time'].$this->data['appV'].$this->data['apiV'].$this->data['osV'].$this->data['osType'].$this->data['deviceID'].$this->data['ip'].$this->data['channel'].'{}';
  82. }
  83. else {
  84. $signPre = $key.$this->data['token'].$this->data['time'].$this->data['appV'].$this->data['apiV'].$this->data['osV'].$this->data['osType'].$this->data['deviceID'].$this->data['ip'].$this->data['channel'].json_encode($this->data['app'], JSON_UNESCAPED_UNICODE);
  85. }
  86. Log::info($signPre);
  87. $sign = md5($signPre);
  88. Log::info($sign);
  89. if($sign != $this->data['sign']) {
  90. throw new AppException(-108, 'sign签名不正确');
  91. }
  92. }
  93. public function __call($name, $arguments)
  94. {
  95. // TODO: Implement __call() method.
  96. }
  97. }