12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\common\library\easywechatPlus;
- use fast\Http;
- use think\Log;
- use think\Config;
- use app\common\exception\BusinessException;
- use app\common\facade\HttpClient;
- /**
- * 微信服务类 - 继承EasywechatPlus,支持facade配置
- */
- class WechatService extends EasywechatPlus
- {
- /**
- * 生成小程序码
- * @param array $param
- * @return string
- */
- public function getWxCodeUnlimited($param)
- {
- $access_token = $this->getAccessToken();
- $url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit';
- $data = array_merge([
- 'width' => 280
- ], $param);
- $result = HttpClient::request('post', $url, [
- 'body' => json_encode($data, JSON_UNESCAPED_UNICODE),
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $result = $result->getBody()->getContents();
- // Log::write('getWxCodeUnlimited:'.$result);
- return json_decode($result,true);
- }
- /**
- * 获取手机号
- * @param string $code
- * @return array
- */
- public function getWechatMobile($code)
- {
- $access_token = $this->getAccessToken();
- $url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
- $res = HttpClient::request('post', $url, [
- 'body' => json_encode(['code' => $code]),
- 'query' => ["access_token" => $access_token['access_token']],
- 'headers' => ['Content-Type' => 'application/json']
- ]);
- $res = $res->getBody()->getContents();
- $res = (array)json_decode($res, true);
-
- if (!isset($res['phone_info']) && Config::get('app_debug')) {
- Log::write($res, 'get_phone');
- }
- return $res['phone_info'] ?? [];
- }
- /**
- * 获取Session信息
- * @param string $code
- * @return array
- */
- public function getWechatSession($code)
- {
- // $access_token = $this->getAccessToken();
- // $url = "https://api.weixin.qq.com/sns/jscode2session";
- // $result = HttpClient::request('get', $url, [
- // 'query' => [
- // "access_token" => $access_token['access_token'],
- // 'appid' => $this->app->app_id,
- // 'secret' => $this->app->app_secret,
- // 'js_code' => $code,
- // 'grant_type' => 'authorization_code'
- // ],
- // ]);
- $result = $this->app->auth->session($code);
- Log::write('getWechatSession:'.$result);
- //$result = $result->getBody()->getContents();
- $result = json_decode($result, true);
- if ($result['errcode'] == 0) {
- return $result;
- }
-
- if (Config::get('app_debug')) {
- Log::write($result, 'get_session');
- }
- return ['errmsg' => Config::get('app_debug') ? $result['errmsg'] : '网络错误'];
- }
-
- }
|