小程序用户授权可以获取用户的信息

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);
}

转载于:https://www.cnblogs.com/isuansuan/p/9827399.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值