
getOpenid:function(){
var that = this;
return new Promise(function (resolve, reject){
wx.login({
success: function (res) {
if (res.code) {
//发起网络请求
wx.request({
url: config.api+'/api/v1.user/getUserInfo',
data: {
code:res.code //微信小程序生成的code
},
success: function (res) {
console.log("登录成功");
}
});
}
}
});
}).then(function(){
});
},PHP代码:
$url_get = 'https://api.weixin.qq.com/sns/jscode2session?
grant_type=authorization_code&appid=' . config("appid") . '&secret=' . config("appsecret") . '&js_code=' . $code;
$result = json_decode(curlGet($url_get),true);
$open_id = $result['openid'];
function curlGet($url)
{
$ch = curl_init();
$header = [
"Content-Type: application/json;charset=UTF-8",
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}