EmpowerServies.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\zskk\servies;
  4. use app\zskk\model\Institution;
  5. use think\facade\Cache;
  6. class EmpowerServies
  7. {
  8. public $institutionModel = null;
  9. public function __construct(Institution $institutionModel)
  10. {
  11. // parent::__construct();
  12. $this->institutionModel = $institutionModel;
  13. }
  14. public function getRecogToken($params): array
  15. {
  16. $ID_CARDNUM = $params['ID_CARDNUM'];
  17. $ORGCODE = $params['ORGCODE'];
  18. $ORGNAME = $params['ORGNAME'];
  19. $DOCTORNAME = $params['DOCTORNAME'];
  20. $DOCTORCODE = $params['DOCTORCODE'];
  21. $aesKey = $this->generateRandomLetters();
  22. // todo
  23. // $aesKey = 'testToken1';
  24. $expire = 86400;
  25. $arr = [
  26. 'ID_CARDNUM'=>$ID_CARDNUM,
  27. 'ORGCODE'=>$ORGCODE,
  28. 'ORGNAME'=>$ORGNAME,
  29. 'DOCTORNAME'=>$DOCTORNAME,
  30. 'DOCTORCODE'=>$DOCTORCODE,
  31. 'HR_ITEM_CODE'=>$params['HR_ITEM_CODE'] ?? '',
  32. 'DEVICE_IP'=>$params['DEVICE_IP'] ?? '',
  33. 'DEVICE_MAC'=>$params['DEVICE_MAC'] ?? ''
  34. ];
  35. Cache::set($aesKey,$arr,$expire);
  36. return ['TOKEN'=>$aesKey];
  37. }
  38. public function getInsToken($params): array
  39. {
  40. $orgCode = $params['clientid']; //机构编码
  41. $authID = $params['clientsecret']; //授权码
  42. $institution = $this->institutionModel->where('port_empower',$authID)->where('institution_code',$orgCode)->find();
  43. if(empty($institution))
  44. {
  45. return ['msg'=>'机构编码或授权码错误','data'=>[]];
  46. }
  47. $key = md5($orgCode.rand(0,99));
  48. $expire = 3600;
  49. $aesKey = $this->generateRandomLetters();
  50. // todo
  51. // $aesKey = 'zLxapoeqWYpoeqWY';
  52. // $institution['id'] = '001';
  53. $token = base64_encode(md5($orgCode.$institution['id'].$aesKey));
  54. $arr = ['token'=>$token,'key'=>$aesKey,'expire'=>$expire,'appId'=>$key,'orgCode'=>$orgCode];
  55. // if(empty(Cache::get($orgCode)))
  56. // {
  57. Cache::set($orgCode,[[$orgCode=>$token]],$expire);
  58. // }else{
  59. // Cache::push($orgCode,[$orgCode=>$token]);
  60. // }
  61. Cache::set($token,$arr,$expire);
  62. return ['msg'=>'success','data'=>$arr];
  63. }
  64. public function generateRandomLetters($length = 32): string
  65. {
  66. $letters = 'abcdefghijklmnopqrstuvwxyz1234567890'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  67. $lettersLength = strlen($letters);
  68. $randomLetters = '';
  69. for ($i = 0; $i < $length; $i++) {
  70. $randomLetters .= $letters[random_int(0, $lettersLength - 1)];
  71. }
  72. return $randomLetters;
  73. }
  74. }