小程序用戶授權可以獲取用戶的信息


code2accessToken

登錄憑證校驗,通過 wx.login() 接口獲得臨時登錄憑證 code 后傳到開發者服務器調用此接口完成登錄流程。更多使用方法詳見 小程序登錄

請求地址

GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

wxml頁面

<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo></button>

js頁面

bindGetUserInfo:function(e){

console.log(e)

  wx.login({

success:res=>{

// ------ 獲取憑證 ------
var code = res.code;
console.log(code);
// console.log('獲取用戶登錄憑證:' + code);
if(code){
// ------ 發送憑證 ------
app.util.request({
  "url":"entry/wxapp/GetUid",
 data:{
   code:code
},
method:'POST',
header:{
  "content-type":'application/json'
},
success:function(res){
    console.log(res)
if (res.statusCode == 200) {
// console.log("獲取到的openid為:" + res.data)
// that.globalData.openid = res.data
wx.setStorageSync('openid', res.data)
} else {
console.log(res.errMsg)
}
}
})
}

}

})

}

wxapp.php

// 用戶授權

//獲取用戶信息
public function doPageTyMember() {
global $_GPC, $_W;
$uniacid = $_W['uniacid'];
$openid = $_REQUEST['openid'];
$item['u_name'] = $_GPC['u_name'];
$item['u_thumb'] = $_GPC['u_thumb'];
$item['uniacid'] = $uniacid;
if ($openid) {
$res = pdo_update('hyb_ylxc_userinfo', $item, array('openid' => $openid));
}
if (!$res['u_id']) {
$res = pdo_fetch('SELECT `u_id` FROM ' . tablename('hyb_ylxc_userinfo') . " where `openid`='{$openid}'");
}
$message = 'success';
$errno = 0;
return $this->result($errno, $message, $item);
}
public function doPageGetUid() {

global $_GPC, $_W;
$uniacid = $_W['uniacid'];
$result = pdo_fetch('SELECT * FROM ' . tablename('hyb_ylxc_key') . " where `uniacid`='{$uniacid}'");
$APPID = "wxbcf230862644aa93";
// $APPID = $result['appid'];
// $SECRET = $result['appsecret'];
$SECRET = "1ad4414c9da569ca333d216cc6d0f643";
$optionid = "oiw7_0FlOxmb4SmYHwT7K0rBdCnY";
$code = trim($_GPC['code']);
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$APPID}&secret={$SECRET}&js_code={$code}&grant_type=authorization_code";
$data['userinfo'] = json_decode($this->httpGet($url));
$openid = $data['userinfo']->openid;
$item['openid'] = $openid;
if ($openid) {
$res = pdo_fetch('SELECT `u_id` FROM ' . tablename('hyb_ylxc_userinfo') . " where `openid`='{$openid}'");
if (!$res['u_id']) {
$res = pdo_insert('hyb_ylxc_userinfo', $item);
}
}
$data['openid'] = $openid;
$message = 'success';
$errno = 0;
return $this->result($errno, $message, $data);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM