您当前的位置: 首页 > 知识百科 > 微信小程序获取用户id的两种方案

微信小程序获取用户id的两种方案

时间:2023-07-01 14:05 阅读数:788 人阅读 分类:知识百科

  在微信开发项目中,获取用户id是经常会遇到的事情,只有微信小程序获取用户id了之后,才能进行相应的开发和设置,那么微信小程序获取用户id的方法是什么呢?

  有两种获取用户id的方案。

  1、不包含敏感信息openId 的json对象(包含:nickname、avatarUrl等基本信息)

  2、包含敏感信息openId的基本信息。

  第一种获取方案

  1、首先调用wx.login()小程序接口 让用户授权验证,也就是我们肉眼观察到的,你是否对xxxxx授权这种信息。

  2、用户成功授权后,调用wx.getUserInfo() 接口获取用户信息。

  完整代码如下

  wx.login({

  success:function(){

  wx.getUserInfo({

  success:function(res){

  var simpleUser = res.userInfo;

  console.log(simpleUser.nickName);

  }

  });

  }

  });

  第二种比较复杂了,需要与后台进行交互才能获得userInfo,但是这种方案获得的数据是完整的(包含openId)。

  1、调用wx.login()接口 授权 在success 成功函数的参数中包含code。

  2、调用wx.getUserInfo()接口success 函数中包含encryptedData、iv

  3、将上述小程序参数传给后台解析,生成userInfo

  代码如下

  js

  var request = require("../../utils/request.js");

  wx.login({

  success:function(res_login){

  if(res_login.code)

  {

  wx.getUserInfo({

  withCredentials:true,

  success:function(res_user){

  var requestUrl = "/getUserApi/xxx.php";

  var jsonData = {

  code:res_login.code,

  encryptedData:res_user.encryptedData,

  iv:res_user.iv

  };

  request.sPostRequest(requestUrl,jsonData,function(res){

  console.log(res.openId);

  });

  }

  })

  }

  }

  })

  后台解析

  /**

  * 获取粉丝信息

  * 其中的参数就是前端传递过来的

  */

  public function wxUserInfo($code,$encryptedData,$iv)

  {

  $apiUrl = "api.weixin.qq/sns/jscode2session?appid={$this->wxConfig['appid']}&secret={$this->wxConfig['appsecret']}&js_code={$code}&grant_type=authorization_code";

  $apiData = json_decode(curlHttp($apiUrl,true),true);

  if(!isset($apiData['session_key']))

  {

  echoJson(array(

  "code" => 102,

  "msg" => "curl error"

  ),true);

  }

  $userInfo = getUserInfo($this->wxConfig['appid'],$apiData['session_key'],$encryptedData,$iv);

  if(!$userInfo)

  {

  echoJson(array(

  "code" => 105,

  "msg" => "userInfo not"

  ));

  }

  //$userInfo = json_decode($userInfo,true);

  //载入用户服务

  //$userService = load_service("User");

  //$userService->checkUser($this->projectId,$userInfo);

  echo $userInfo; //微信响应的就是一个json数据

  }

  getUserInfo function 其中wxBizDataCrypt.php 就是微信官方提供的素材包

  curlHttp 函数是一个小程序自定义函数 该函数的源码查看我的这篇文章

  curlHttp

  //获取粉丝信息

  function getUserInfo($appid,$sessionKey,$encryptedData,$iv){

  require_once ROOTPATH . "/extends/wxUser/wxBizDataCrypt.php";

  $data = array();

  $pc = new WXBizDataCrypt($appid, $sessionKey);

  $errCode = $pc->decryptData($encryptedData, $iv, $data );

  if ($errCode == 0) {

  return $data;

  } else {

  return false;

  }

  }

  自己写的小工具 request.js

  var app = getApp();

  //远程请求

  var __sRequest = {

  // 请求

  s_request : function(obj){

  wx.request(obj);

  },

  //文件上传

  upload_request : function(dataSource){

  wx.uploadFile(dataSource);

  }

  };

  module.exports = {

  //执行异步请求get

  sRequest:function(obj){

  var jsonUrl = {};

  jsonUrl.url = obj.url;

  if(obj.header)jsonUrl.header=obj.header;

  if(obj.type)

  jsonUrl.method = obj.type;

  else

  jsonUrl.method="GET";

  if(obj.data)jsonUrl.data = obj.data;

  obj.dataType?(jsonUrl.dataType=obj.dataType):(jsonUrl.dataType="json");

  jsonUrl.success = obj.success;

  jsonUrl.data.projectId = app.globalData.projectId;

  __sRequest.s_request(jsonUrl);

  },

  //get 请求

  sGetRequest:function(req_url,req_obj,res_func)

  {

  var jsonUrl = {

  url:app.globalData.host + req_url,

  header:{"Content-Type":"application/json"},

  dataType:"json",

  method:"get",

  success:function(res)

  {

  typeof res_func == "function" && res_func(res.data);

  }

  }

  if(req_obj)

  {

  jsonUrl.data = req_obj;

  }

  jsonUrl.data.projectId = app.globalData.projectId;

  __Request.s_request(jsonUrl);

  },

  //post 请求

  sPostRequest:function(req_url,req_obj,res_func)

  {

  var jsonUrl = {

  url:app.globalData.host + req_url,

  header:{"Content-Type":"application/x--form-urlencoded"},

  dataType:"json",

  method:"post",

  success:function(res)

  {

  typeof res_func == "function" && res_func(res.data);

  }

  }

  if(req_obj)

  {

  jsonUrl.data = req_obj;

  }

  jsonUrl.data.projectId = app.globalData.projectId;

  __sRequest.s_request(jsonUrl);

  },

  //文件上传

  sUpload:function(uid,fileDataSource,res_func)

  {

  dataSource = {

  url:app.globalData.host + req_url,

  header:{

  "Content-Type":"multipart/form-data"

  },

  dataType:"json",

  formData : {

  "uid" : uid

  },

  filePath : fileDataSource,

  name : "fileObj",

  success:function(res){

  typeof res_func == "function" && res_func(res);

  }

  }

  __sRequest.upload_request(dataSource);

  }

  };

  看完以上的资料,微信小程序获取用户id的方法大家都知道了吗?这两种方法都是小编亲测有效的,希望能对大家有所帮助。如果还想获取更多相关资料可以关注微信小程序商店。

  

  如何获取微信小程序店铺id?

  微信小程序获取元素id方法是什么?

  微信公众号关联小程序id怎么添加?