WechatHelper.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\helpers;
  3. use Yii;
  4. /**
  5. * Class WechatHelper
  6. * @package common\helpers
  7. * @author jianyan74 <751393839@qq.com>
  8. */
  9. class WechatHelper
  10. {
  11. /**
  12. * 验证token是否一致
  13. *
  14. * @param string $signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
  15. * @param integer $timestamp 时间戳
  16. * @param integer $nonce 随机数
  17. * @return bool
  18. */
  19. public static function verifyToken($signature, $timestamp, $nonce)
  20. {
  21. $config = Yii::$app->services->config->configAll(true);
  22. $token = $config['wechat_mp_token'] ?? '';
  23. $tmpArr = [$token, $timestamp, $nonce];
  24. sort($tmpArr, SORT_STRING);
  25. $tmpStr = implode($tmpArr);
  26. $tmpStr = sha1($tmpStr);
  27. return $tmpStr == $signature ? true : false;
  28. }
  29. /**
  30. * 验证token是否一致
  31. *
  32. * @param string $signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
  33. * @param integer $timestamp 时间戳
  34. * @param integer $nonce 随机数
  35. * @return bool
  36. */
  37. public static function verifyTokenMiniProgram($signature, $timestamp, $nonce)
  38. {
  39. $config = Yii::$app->services->config->configAll(true);
  40. $token = $config['wechat_mini_token'] ?? '';
  41. $tmpArr = [$token, $timestamp, $nonce];
  42. sort($tmpArr, SORT_STRING);
  43. $tmpStr = implode($tmpArr);
  44. $tmpStr = sha1($tmpStr);
  45. return $tmpStr == $signature ? true : false;
  46. }
  47. /**
  48. * 生成微信分享代码
  49. *
  50. * @param array $attr
  51. * [
  52. * 'title' => 标题,
  53. * 'desc' => 内容,
  54. * 'url' => 跳转网址,
  55. * 'img' => 图片
  56. * ]
  57. * @param bool $importJs 默认导入js,false则不导入
  58. * @return string
  59. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  60. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  61. * @throws \Psr\SimpleCache\InvalidArgumentException
  62. */
  63. public static function share(array $attr, $importJs = true)
  64. {
  65. if (!Yii::$app->wechat->getIsWechat()) {
  66. return '';
  67. }
  68. // 截止到Mar 29, 2019, 17:00,android版本的分享新接口还是失效,需要将老接口加入进去申明列表,则功能正常
  69. $jsapiConfig = Yii::$app->wechat->app->jssdk->buildConfig([
  70. 'updateAppMessageShareData',
  71. 'updateTimelineShareData',
  72. 'onMenuShareTimeline',
  73. 'onMenuShareAppMessage'
  74. ], false);
  75. // 是否引入js
  76. $importJs = $importJs
  77. ? '<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js" type="text/javascript" charset="utf-8"></script>'
  78. : '';
  79. $str
  80. = <<< EOF
  81. $importJs
  82. <script type="text/javascript" charset="utf-8">
  83. wx.config($jsapiConfig);
  84. wx.ready(function(){
  85. //“分享给朋友”及“分享到QQ”
  86. wx.updateAppMessageShareData({
  87. title: '{$attr['title']}', // 分享标题
  88. desc: '{$attr['desc']}', // 分享描述
  89. link: '{$attr['url']}', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  90. imgUrl: '{$attr['img']}', // 分享图标
  91. success: function () {
  92. // 设置成功
  93. }
  94. });
  95. //“分享到朋友圈”及“分享到QQ空间”
  96. wx.updateTimelineShareData({
  97. title: '{$attr['title']}', // 分享标题
  98. link: '{$attr['url']}', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  99. imgUrl: '{$attr['img']}', // 分享图标
  100. success: function () {
  101. // 设置成功
  102. }
  103. })
  104. });
  105. </script>
  106. EOF;
  107. return $str;
  108. }
  109. /**
  110. * 告诉微信已经成功了
  111. *
  112. * @return bool|string
  113. */
  114. public static function success()
  115. {
  116. return ArrayHelper::toXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
  117. }
  118. /**
  119. * 告诉微信失败了
  120. *
  121. * @return bool|string
  122. */
  123. public static function fail()
  124. {
  125. return ArrayHelper::toXml(['return_code' => 'FAIL', 'return_msg' => 'OK']);
  126. }
  127. }
粤ICP备19079148号