1、小程序获取用户信息,直接调用app的getUserInfo方法。
2、获取用户OpenId。
小程序里面通过login得到一个code:
wx.login({
success: function (res) {
if(res.code) {
wx.request({
url: 'https://xxx/testApp.php',
data: {
op: 'getId',
code: res.code
},
success: function(res) {
console.log(res);
var data = JSON.parse(res.data);
Qx.dialog('OpenId', data.openid);
that.setData({ openId: data.openid });
}
})
}
}
})
服务端需要通过code去微信服务器换取openId(testApp.php):
<?php
function post($url, $data){
$opts = array('http' =>
array(
'method' => 'POST',
'header' => array("User-Agent: liny", "Content-type: application/json; charset=utf-8") ,
'content' => $data
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
$param = $_GET;
$op = $param['op'];
if($op = 'getId') {
$code = $param['code'];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=&secret=&js_code=".$code."&grant_type=authorization_code";
$res = post($url, "");
echo json_encode($res);
}