Script.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicWeChat;
  17. use WeChat\Contracts\Tools;
  18. use WeChat\Exceptions\InvalidResponseException;
  19. /**
  20. * 微信前端支持
  21. * Class Script
  22. * @package WeChat
  23. */
  24. class Script extends BasicWeChat
  25. {
  26. /**
  27. * 删除JSAPI授权TICKET
  28. * @param string $type TICKET类型(wx_card|jsapi)
  29. * @param string $appid 强制指定有效APPID
  30. * @return void
  31. */
  32. public function delTicket($type = 'jsapi', $appid = null)
  33. {
  34. is_null($appid) && $appid = $this->config->get('appid');
  35. $cache_name = "{$appid}_ticket_{$type}";
  36. Tools::delCache($cache_name);
  37. }
  38. /**
  39. * 获取JSAPI_TICKET接口
  40. * @param string $type TICKET类型(wx_card|jsapi)
  41. * @param string $appid 强制指定有效APPID
  42. * @return string
  43. * @throws \WeChat\Exceptions\InvalidResponseException
  44. * @throws \WeChat\Exceptions\LocalCacheException
  45. */
  46. public function getTicket($type = 'jsapi', $appid = null)
  47. {
  48. is_null($appid) && $appid = $this->config->get('appid');
  49. $cache_name = "{$appid}_ticket_{$type}";
  50. $ticket = Tools::getCache($cache_name);
  51. if (empty($ticket)) {
  52. $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type={$type}";
  53. $this->registerApi($url, __FUNCTION__, func_get_args());
  54. $result = $this->httpGetForJson($url);
  55. if (empty($result['ticket'])) {
  56. throw new InvalidResponseException('Invalid Resoponse Ticket.', '0');
  57. }
  58. $ticket = $result['ticket'];
  59. Tools::setCache($cache_name, $ticket, 7000);
  60. }
  61. return $ticket;
  62. }
  63. /**
  64. * 获取JsApi使用签名
  65. * @param string $url 网页的URL
  66. * @param string $appid 用于多个appid时使用(可空)
  67. * @param string $ticket 强制指定ticket
  68. * @param array $jsApiList 需初始化的 jsApiList
  69. * @return array
  70. * @throws \WeChat\Exceptions\InvalidResponseException
  71. * @throws \WeChat\Exceptions\LocalCacheException
  72. */
  73. public function getJsSign($url, $appid = null, $ticket = null, $jsApiList = null)
  74. {
  75. list($url,) = explode('#', $url);
  76. is_null($ticket) && $ticket = $this->getTicket('jsapi');
  77. is_null($appid) && $appid = $this->config->get('appid');
  78. is_null($jsApiList) && $jsApiList = [
  79. 'updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone',
  80. 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice',
  81. 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType', 'openLocation', 'getLocation',
  82. 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem',
  83. 'closeWindow', 'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard',
  84. ];
  85. $data = ["url" => $url, "timestamp" => '' . time(), "jsapi_ticket" => $ticket, "noncestr" => Tools::createNoncestr(16)];
  86. return [
  87. 'debug' => false,
  88. "appId" => $appid,
  89. "nonceStr" => $data['noncestr'],
  90. "timestamp" => $data['timestamp'],
  91. "signature" => $this->getSignature($data, 'sha1'),
  92. 'jsApiList' => $jsApiList,
  93. ];
  94. }
  95. /**
  96. * 数据生成签名
  97. * @param array $data 签名数组
  98. * @param string $method 签名方法
  99. * @param array $params 签名参数
  100. * @return bool|string 签名值
  101. */
  102. protected function getSignature($data, $method = "sha1", $params = [])
  103. {
  104. ksort($data);
  105. if (!function_exists($method)) return false;
  106. foreach ($data as $k => $v) $params[] = "{$k}={$v}";
  107. return $method(join('&', $params));
  108. }
  109. }