| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | <?phpnamespace 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);                // 尝试解析为JSON,如果解析成功说明是错误信息,如果失败说明是图片buffer        $jsonResult = json_decode($result, true);        if (json_last_error() === JSON_ERROR_NONE) {            // JSON解析成功,返回错误信息数组            return $jsonResult;        } else {            // JSON解析失败,说明是图片二进制数据,直接返回            return $result;        }    }    /**     * 获取手机号     * @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'] : '网络错误'];    }   } 
 |