WechatService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\common\library\easywechatPlus;
  3. use fast\Http;
  4. use think\Log;
  5. use think\Config;
  6. use app\common\exception\BusinessException;
  7. use app\common\facade\HttpClient;
  8. /**
  9. * 微信服务类 - 继承EasywechatPlus,支持facade配置
  10. */
  11. class WechatService extends EasywechatPlus
  12. {
  13. /**
  14. * 生成小程序码
  15. * @param array $param
  16. * @return string
  17. */
  18. public function getWxCodeUnlimited($param)
  19. {
  20. $access_token = $this->getAccessToken();
  21. $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit';
  22. $data = array_merge([
  23. 'width' => 280
  24. ], $param);
  25. $result = HttpClient::request('post', $url, [
  26. 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
  27. 'query' => ["access_token" => $access_token['access_token']],
  28. 'headers' => ['Content-Type' => 'application/json']
  29. ]);
  30. $result = $result->getBody()->getContents();
  31. // Log::write('getWxCodeUnlimited:'.$result);
  32. // 尝试解析为JSON,如果解析成功说明是错误信息,如果失败说明是图片buffer
  33. $jsonResult = json_decode($result, true);
  34. if (json_last_error() === JSON_ERROR_NONE) {
  35. // JSON解析成功,返回错误信息数组
  36. return $jsonResult;
  37. } else {
  38. // JSON解析失败,说明是图片二进制数据,直接返回
  39. return $result;
  40. }
  41. }
  42. /**
  43. * 获取手机号
  44. * @param string $code
  45. * @return array
  46. */
  47. public function getWechatMobile($code)
  48. {
  49. $access_token = $this->getAccessToken();
  50. $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
  51. $res = HttpClient::request('post', $url, [
  52. 'body' => json_encode(['code' => $code]),
  53. 'query' => ["access_token" => $access_token['access_token']],
  54. 'headers' => ['Content-Type' => 'application/json']
  55. ]);
  56. $res = $res->getBody()->getContents();
  57. $res = (array)json_decode($res, true);
  58. if (!isset($res['phone_info']) && Config::get('app_debug')) {
  59. Log::write($res, 'get_phone');
  60. }
  61. return $res['phone_info'] ?? [];
  62. }
  63. /**
  64. * 获取Session信息
  65. * @param string $code
  66. * @return array
  67. */
  68. public function getWechatSession($code)
  69. {
  70. // $access_token = $this->getAccessToken();
  71. // $url = "https://api.weixin.qq.com/sns/jscode2session";
  72. // $result = HttpClient::request('get', $url, [
  73. // 'query' => [
  74. // "access_token" => $access_token['access_token'],
  75. // 'appid' => $this->app->app_id,
  76. // 'secret' => $this->app->app_secret,
  77. // 'js_code' => $code,
  78. // 'grant_type' => 'authorization_code'
  79. // ],
  80. // ]);
  81. $result = $this->app->auth->session($code);
  82. Log::write('getWechatSession:'.$result);
  83. //$result = $result->getBody()->getContents();
  84. $result = json_decode($result, true);
  85. if ($result['errcode'] == 0) {
  86. return $result;
  87. }
  88. if (Config::get('app_debug')) {
  89. Log::write($result, 'get_session');
  90. }
  91. return ['errmsg' => Config::get('app_debug') ? $result['errmsg'] : '网络错误'];
  92. }
  93. }