WeChatService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\service;
  20. use app\common\exception\WechatException;
  21. use app\common\logic\BaseLogic;
  22. use EasyWeChat\{
  23. Factory,
  24. Kernel\Http\StreamResponse,
  25. Kernel\Exceptions\Exception,
  26. };
  27. /**
  28. * 微信功能类
  29. * Class WeChatService
  30. * @package app\common\service
  31. */
  32. class WeChatService extends BaseLogic
  33. {
  34. /**
  35. * @notes 公众号-根据code获取微信信息
  36. * @param array $params
  37. * @return array
  38. * @throws Exception
  39. * @throws \GuzzleHttp\Exception\GuzzleException
  40. * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException
  41. * @author cjhao
  42. * @date 2021/8/16 14:55
  43. */
  44. public static function getOaResByCode(array $params)
  45. {
  46. $config = WeChatConfigService::getOaConfig();
  47. $app = Factory::officialAccount($config);
  48. $response = $app->oauth
  49. ->scopes(['snsapi_userinfo'])
  50. ->userFromCode($params['code'])
  51. ->getRaw();
  52. if (!isset($response['openid']) || empty($response['openid'])) {
  53. throw new Exception('获取openID失败');
  54. }
  55. return $response;
  56. }
  57. /**
  58. * @notes 小程序-根据code获取微信信息
  59. * @param $post
  60. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  61. * @throws Exception
  62. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  63. * @author cjhao
  64. * @date 2021/8/16 14:57
  65. */
  66. public static function getMnpResByCode($post)
  67. {
  68. $config = WeChatConfigService::getMnpConfig();
  69. $app = Factory::miniProgram($config);
  70. $response = $app->auth->session($post['code']);
  71. if (!isset($response['openid']) || empty($response['openid'])) {
  72. throw new Exception('获取openID失败');
  73. }
  74. return $response;
  75. }
  76. /**
  77. * @notes 公众号跳转url
  78. * @param $url
  79. * @return string
  80. * @author cjhao
  81. * @date 2021/8/16 15:00
  82. */
  83. public static function getCodeUrl($url)
  84. {
  85. $config = WeChatConfigService::getOaConfig();
  86. $app = Factory::officialAccount($config);
  87. $response = $app
  88. ->oauth
  89. ->scopes(['snsapi_userinfo'])
  90. ->redirect($url);
  91. return $response;
  92. }
  93. /**
  94. * @notes 生成小程序码,使用wxacode.getUnlimited接口
  95. * @param array $param $param 参数配置 page:页面路径;scene:页面参数;saveDir:保存路径;fileName:文件名
  96. * @param string $type 返回类型:resource时返回资源类型,file保存并返回文件,base64返回base64
  97. * @return mixed|string
  98. * @author cjhao
  99. * @date 2021/8/16 14:43
  100. */
  101. public static function makeMpQrCode(array $param, string $type = 'resource')
  102. {
  103. try {
  104. $page = $param['page'] ?? '';
  105. $scene = $param['scene'] ?? 'null';
  106. $saveDir = $param['save_dir'] ?? 'uploads/qr_code/user_share/';
  107. $fileName = $param['file_name'] ?? time() . '.png';
  108. $config = WeChatConfigService::getMnpConfig();
  109. $app = Factory::miniProgram($config);
  110. $response = $app->app_code->getUnlimit($scene, [
  111. 'page' => $page,
  112. // 'env_version' => 'trial' //要打开的小程序版本。正式版为 release,体验版为 trial,开发版为 develop
  113. ]);
  114. if (is_array($response) && isset($response['errcode'])) {
  115. //开启错误提示,小程序未发布和页面不存在,返回提示
  116. if (41030 === $response['errcode']) {
  117. throw new Exception('所传page页面不存在,或者小程序没有发布');
  118. }
  119. throw new Exception($response['errmsg']);
  120. }
  121. $contents = $response->getBody()->getContents();
  122. switch ($type){
  123. case 'file':
  124. if ($response instanceof StreamResponse) {
  125. $fileName = $response->saveAs($saveDir, $fileName);
  126. $contents = $saveDir . $fileName;
  127. }
  128. break;
  129. case 'base64':
  130. $mpBase64 = chunk_split(base64_encode($contents));
  131. $contents = 'data:image/png;base64,' . $mpBase64;
  132. }
  133. self::$returnData = $contents;
  134. return true;
  135. } catch (Exception $e) {
  136. self::$returnData = $e->getMessage();
  137. return false;
  138. }
  139. }
  140. /**
  141. * @notes 获取直播间
  142. * @param int $start
  143. * @param int $limit
  144. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  145. * @throws \GuzzleHttp\Exception\GuzzleException
  146. * @author cjhao
  147. * @date 2021/11/27 10:00
  148. */
  149. public static function getLiveRoom(int $start = 0,int $limit = 25)
  150. {
  151. try {
  152. $config = WeChatConfigService::getMnpConfig();
  153. $app = Factory::miniProgram($config);
  154. $result = $app->broadcast->getRooms($start, $limit);
  155. if( 0 !=$result['errcode']){
  156. throw new WechatException($result['errmsg'],$result['errcode']);
  157. }
  158. return $result;
  159. } catch (Exception |\think\Exception $e) {
  160. return $e->getMessage();
  161. }
  162. }
  163. }