Base.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\mobile_v_2_1\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. $user = null;
  22. if(in_array($this->request->path(), Config::get('tourist_path')))
  23. {
  24. //如果还要实现数据同步,则注释掉该端段代码,然后把loginPassword中的同步代码再加回来
  25. $user = User::getUserByToken($this->data['token'], $this->data['osType']);
  26. }
  27. else {
  28. $user = User::getUserByToken($this->data['token'], $this->data['osType']);
  29. if(!$user) {
  30. throw new AppException(-1000, 'token失效,需要重新登录');
  31. }
  32. }
  33. $this->request->bind('user', $user);
  34. $this->__initialize();
  35. }
  36. public function check() {
  37. Log::info($this->data);
  38. if(!isset($this->data['apiV'])) {
  39. throw new AppException(-101, 'api版本号不能为空');
  40. }
  41. if(!isset($this->data['osV'])) {
  42. throw new AppException(-102, 'osV操作系统版本号不能为空');
  43. }
  44. if(!isset($this->data['osType'])) {
  45. throw new AppException(-103, 'osType操作系统类型不能为空');
  46. }
  47. if(!isset($this->data['deviceID'])) {
  48. throw new AppException(-104, 'deviceID宿主app的唯一标识不能为空');
  49. }
  50. if(!isset($this->data['token'])) {
  51. throw new AppException(-105, 'token不能为空');
  52. }
  53. if(!isset($this->data['sign'])) {
  54. throw new AppException(-106, 'sign不能为空');
  55. }
  56. if(!isset($this->data['time'])) {
  57. throw new AppException(-107, 'time不能为空');
  58. }
  59. if(!isset($this->data['ip'])) {
  60. throw new AppException(-108, 'ip不能为空');
  61. }
  62. if(!isset($this->data['app'])) {
  63. throw new AppException(-109, 'app应用数据不能为空');
  64. }
  65. if(!isset($this->data['appV'])) {
  66. throw new AppException(-110, 'app版本号不能为空');
  67. }
  68. if(!isset($this->data['channel'])) {
  69. throw new AppException(-111, 'channel不能为空');
  70. }
  71. $key = '';
  72. if($this->data['osType'] == 1 || $this->data['osType'] == 2) {
  73. $key = Config::get('system.mobile_key');
  74. }
  75. else if($this->data['osType'] == 3) {
  76. $key = Config::get('system.pc_key');
  77. }
  78. $signPre = '';
  79. if($this->data['app'] === '') {
  80. $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'];
  81. }
  82. else if($this->data['app'] === []) {
  83. $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'].'{}';
  84. }
  85. else {
  86. $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);
  87. }
  88. Log::info($signPre);
  89. $sign = md5($signPre);
  90. Log::info($sign);
  91. if($sign != $this->data['sign']) {
  92. throw new AppException(-108, 'sign签名不正确');
  93. }
  94. }
  95. public function __call($name, $arguments)
  96. {
  97. // TODO: Implement __call() method.
  98. }
  99. }