Script.php 4.6 KB

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