EmpowerServies.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. ];
  32. Cache::set($aesKey,$arr,$expire);
  33. return ['TOKEN'=>$aesKey];
  34. }
  35. public function getInsToken($params): array
  36. {
  37. $orgCode = $params['clientid']; //机构编码
  38. $authID = $params['clientsecret']; //授权码
  39. $institution = $this->institutionModel->where('port_empower',$authID)->where('institution_code',$orgCode)->find();
  40. if(empty($institution))
  41. {
  42. // return ['msg'=>'机构编码或授权码错误','data'=>[]];
  43. }
  44. $key = md5($orgCode.rand(0,99));
  45. $expire = 3600;
  46. $aesKey = $this->generateRandomLetters();
  47. // todo
  48. $aesKey = 'zLxapoeqWYpoeqWY';
  49. $institution['id'] = '001';
  50. $token = base64_encode(md5($orgCode.$institution['id'].$aesKey));
  51. $arr = ['token'=>$token,'key'=>$aesKey,'expire'=>$expire,'appId'=>$key,'orgCode'=>$orgCode];
  52. // if(empty(Cache::get($orgCode)))
  53. // {
  54. Cache::set($orgCode,[[$orgCode=>$token]],$expire);
  55. // }else{
  56. // Cache::push($orgCode,[$orgCode=>$token]);
  57. // }
  58. Cache::set($token,$arr,$expire);
  59. return ['msg'=>'success','data'=>$arr];
  60. }
  61. public function generateRandomLetters($length = 32): string
  62. {
  63. $letters = 'abcdefghijklmnopqrstuvwxyz1234567890'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  64. $lettersLength = strlen($letters);
  65. $randomLetters = '';
  66. for ($i = 0; $i < $length; $i++) {
  67. $randomLetters .= $letters[random_int(0, $lettersLength - 1)];
  68. }
  69. return $randomLetters;
  70. }
  71. }