AuthService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\index\service;
  3. use app\index\model\AuthCompanyModel;
  4. use app\index\model\AuthModel;
  5. use app\index\model\TokenModel;
  6. class AuthService extends BaseService
  7. {
  8. public function checkAuth($data)
  9. {
  10. $AuthModel = new AuthModel();
  11. $TokenModel = new TokenModel();
  12. $AuthCompanyModel = new AuthCompanyModel();
  13. $info = $AuthModel->getInfo(['account' => $data['account'], 'password' => $data['password']]);
  14. if (empty($info)) {
  15. return $this->return_code[20001];
  16. } else {
  17. if ($info['start_time'] > time() || $info['end_time'] < time()) {
  18. return $this->return_code[20002];
  19. } elseif ($info['status'] == 2) {
  20. return $this->return_code[20003];
  21. } else {
  22. $res = $this->return_code[200];
  23. $res['data'] = [];
  24. $res['data']['access_token'] = $this->_randVar(64,6);
  25. $res['data']['expires_in'] = 7200;
  26. $company_info = $AuthCompanyModel->getInfo(['company_code'=>$info['company_code']]);
  27. $insert['token'] = $res['data']['access_token'];
  28. $insert['start_time'] = time();
  29. $insert['end_time'] = time()+7200;
  30. $insert['company_code'] = $info['company_code'];
  31. $insert['status'] = 1;
  32. $insert['db_json'] = $company_info['db_json'];
  33. $insert['sms_json'] = $company_info['sms_json'];
  34. //$TokenModel->save(['status'=>2],['company_code'=>$info['company_code']]);
  35. $TokenModel->insert($insert);
  36. return $res;
  37. }
  38. }
  39. }
  40. private function _randVar($length = 0, $type = 0)
  41. {
  42. $range = array(
  43. 0 => '0123456789',
  44. 1 => 'abcdefghijklmnopqrstuvwxyz',
  45. 2 => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  46. 3 => '0123456789abcdefghijklmnopqrstuvwxyz',
  47. 4 => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
  48. 5 => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  49. 6 => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  50. 7 => '3456789abcdefghijkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY');
  51. if (false === array_key_exists($type, $range)) {
  52. $type = 6;
  53. }
  54. $character = '';
  55. $maxLength = strlen($range[$type]) - 1;
  56. for ($i = 0; $i < $length; ++$i) {
  57. $character .= $range[$type][mt_rand(0, $maxLength)];
  58. }
  59. return $character;
  60. }
  61. }