1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?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 = $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'] : '网络错误'];
- }
-
- }
|