EmpowerServies.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. $token = base64_encode(md5($orgCode.$institution['id'].$aesKey));
  50. $arr = ['token'=>$token,'key'=>$aesKey,'expire'=>$expire,'appId'=>$key,'orgCode'=>$orgCode];
  51. // if(empty(Cache::get($orgCode)))
  52. // {
  53. Cache::set($orgCode,[[$orgCode=>$token]],$expire);
  54. // }else{
  55. // Cache::push($orgCode,[$orgCode=>$token]);
  56. // }
  57. Cache::set($token,$arr,$expire);
  58. return ['msg'=>'success','data'=>$arr];
  59. }
  60. public function generateRandomLetters($length = 32): string
  61. {
  62. $letters = 'abcdefghijklmnopqrstuvwxyz1234567890'; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  63. $lettersLength = strlen($letters);
  64. $randomLetters = '';
  65. for ($i = 0; $i < $length; $i++) {
  66. $randomLetters .= $letters[random_int(0, $lettersLength - 1)];
  67. }
  68. return $randomLetters;
  69. }
  70. }