首先先安装sdk
在配置文件composer.json里添加
运行 composer update 命令
"qcloud/weapp-sdk":"0.4.4",
接下来初始化sdk 在项目更目录index 配置各个参数(这里需要进行初始化配置)
use \QCloud_WeApp_SDK\Conf as Qconf;
function qcloud_setup($app_name = '') {// $sdkConfig = '/etc/qcloud/sdk.config';//该目录为sdk在服务器的配置文件// if ($app_name == 'secret_letter') {$sdkConfig = '/etc/qcloud/sdk_secret_letter.config';// }if (!file_exists($sdkConfig)) {echo "SDK 配置文件不存在";die;}$config = json_decode(file_get_contents($sdkConfig), TRUE);if (!is_array($config)) {echo "SDK 配置文件内容不合法";die;}/** --------------------------------------------------------------------* 设置 SDK 基本配置* --------------------------------------------------------------------*/Qconf::setup(array('ServerHost' => $config['serverHost'],'AuthServerUrl' => $config['authServerUrl'],'TunnelServerUrl' => $config['tunnelServerUrl'],'TunnelSignatureKey' => $config['tunnelSignatureKey'],));/*** 也可以调用独立方法进行设置** Qconf::setServerHost($config['serverHost']);* Qconf::setAuthServerUrl($config['authServerUrl']);* Qconf::setTunnelServerUrl($config['tunnelServerUrl']);* Qconf::setTunnelSignatureKey($config['tunnelSignatureKey']);*/// 设置网络请求超时时长(可选,默认 30 秒)Qconf::setNetworkTimeout($config['networkTimeout']);/** --------------------------------------------------------------------* 设置 SDK 日志输出配置(主要是方便调试)* --------------------------------------------------------------------*/// 开启日志输出功能Qconf::setEnableOutputLog(TRUE);// 指定 SDK 日志输出目录(注意尾斜杠不能省略)Qconf::setLogPath('/tmp/qc_logs/');// Qconf::setLogPath(__DIR__ . '/../../storage/logs');// 设置日志输出级别// 1 => ERROR, 2 => DEBUG, 3 => INFO, 4 => ALLQconf::setLogThresholdArray(array(2)); // output debug log only
}
qcloud_setup();
登录验证
use \QCloud_WeApp_SDK\Auth\LoginService as LoginService;(引入)
$login_result = LoginService::login();
if ( ! isset($login_result['data']['userInfo'])) {return;
}
$userInfo = $login_result['data']['userInfo'];
userInfo里面有用户的详细信息
$result = LoginService::check();
// check failed
if ($result['code'] !== 0) {exit;
}
判断是否登录 $result
返回的同样有用户的详细信息
例如
$openid = $result['data']['userInfo']['openId'];