JSSdk.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace WeWork;
  3. use WeWork\Crypt\PrpCrypt;
  4. use WeWork\Traits\CorpIdTrait;
  5. use WeWork\Traits\JsApiTicketTrait;
  6. use WeWork\Traits\TicketTrait;
  7. class JSSdk
  8. {
  9. use CorpIdTrait, JsApiTicketTrait, TicketTrait;
  10. /**
  11. * @param string $url
  12. * @return array
  13. * @throws \Psr\SimpleCache\InvalidArgumentException
  14. */
  15. public function getConfig(string $url): array
  16. {
  17. $appId = $this->corpId;
  18. $timestamp = $this->getTimestamp();
  19. $nonceStr = $this->getNonceStr();
  20. $signature = sha1("jsapi_ticket={$this->jsApiTicket->get()}&noncestr={$nonceStr}&timestamp={$timestamp}&url={$url}");
  21. return compact('appId', 'timestamp', 'nonceStr', 'signature');
  22. }
  23. /**
  24. * @return array
  25. * @throws \Psr\SimpleCache\InvalidArgumentException
  26. */
  27. public function getChooseInvoiceConfig(): array
  28. {
  29. $timestamp = $this->getTimestamp();
  30. $nonceStr = $this->getNonceStr();
  31. $array = ['INVOICE', $this->corpId, $timestamp, $nonceStr, $this->ticket->get()];
  32. sort($array, SORT_STRING);
  33. $str = implode($array);
  34. $cardSign = sha1($str);
  35. return compact('timestamp', 'nonceStr', 'cardSign');
  36. }
  37. /**
  38. * @return int
  39. */
  40. protected function getTimestamp(): int
  41. {
  42. return time();
  43. }
  44. /**
  45. * @return string
  46. */
  47. protected function getNonceStr(): string
  48. {
  49. return (new PrpCrypt(null))->getRandomStr();
  50. }
  51. }