小程序java获取openid怎么获取用户的openid

1340人阅读
微信小程序(17)
小程序获取用户的头像昵称openid之类
第一种使用wx.getUserInfo直接获取微信头像,昵称
wx.getUserInfo({
success: function (res) {
that.setData({
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl,
我们在使用小程序wx.login API进行登录的时候,直接使用wx.getUserInfo是不能获取更多的信息的,如微信用户的openid。
官方提示,需要发送获取到的code进行请求到微信的后端API,进行用户解密之类的操作才可以获取,
根据文档,只需要进行一个get请求到如下地址即可:
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
appid和secret在微信小程序后台可以看到,js_code为使用wx.login登录时获取到的code参数数据,grant_type这个不用改动。
var openId = (wx.getStorageSync('openId'))
if (openId) {
wx.getUserInfo({
success: function (res) {
that.setData({
nickName: res.userInfo.nickName,
avatarUrl: res.userInfo.avatarUrl,
fail: function () {
console.log("获取失败!")
complete: function () {
console.log("获取用户信息完成!")
wx.login({
success: function (res) {
console.log(res.code)
if (res.code) {
wx.getUserInfo({
withCredentials: true,
success: function (res_user) {
wx.request({
url: 'https://....com/wx/login',
code: res.code,
encryptedData: res_user.encryptedData,
iv: res_user.iv
method: 'GET',
'content-type': 'application/json'
success: function (res) {
that.setData({
nickName: res.data.nickName,
avatarUrl: res.data.avatarUrl,
wx.setStorageSync('openId', res.data.openId);
}, fail: function () {
wx.showModal({
title: '警告通知',
content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
success: function (res) {
if (res.confirm) {
wx.openSetting({
success: (res) =& {
if (res.authSetting["scope.userInfo"]) {
wx.login({
success: function (res_login) {
if (res_login.code) {
wx.getUserInfo({
withCredentials: true,
success: function (res_user) {
wx.request({
url: 'https://....com/wx/login',
code: res_login.code,
encryptedData: res_user.encryptedData,
iv: res_user.iv
method: 'GET',
'content-type': 'application/json'
success: function (res) {
that.setData({
nickName: res.data.nickName,
avatarUrl: res.data.avatarUrl,
wx.setStorageSync('openId', res.data.openId);
}, fail: function (res) {
}, complete: function (res) {
globalData: {
userInfo: null
后台是php 框架是laravel5.4版本
官方文档:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html
微信官方提供了多种编程语言的示例代码(点击下载)。每种语言类型的接口名字均一致。调用方式可以参照示例。
下载之后在php文件中引入:
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Wechatuser;
include_once
app_path('/Http/Controllers/Admin/PHP/wxBizDataCrypt.php');
public function getWxLogin(Request $request)
$request-&get('code');
$encryptedData
$request-&get('encryptedData');
$request-&get('iv');
$URL = "https://api./sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
$apiData=file_get_contents($URL);
if(!isset($apiData['errcode'])){
$sessionKey = json_decode($apiData)-&session_
$userifo = new \WXBizDataCrypt($appid, $sessionKey);
$errCode = $userifo-&decryptData($encryptedData, $iv, $data );
if ($errCode == 0) {
return ($data . "\n");
return false;
官方文档的登录流程图,整个登录流程基本如下图所示:
他说他是个天才,这个世界配不上他,我说他是个傻逼。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:49312次
积分:1126
积分:1126
排名:千里之外
原创:63篇
(11)(13)(1)(15)(6)(7)(10)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'你的位置: >
> 小程序丨微信小程序如何根据openid获取用户信息?
微信如何根据openid获取用户信息?
PS:根据服务器返回的N多的openid,前台根据openid获取相对应的用户信息
网友回复:
你好,小程序目前不支持在服务器使用openId获取用户信息。
爱盈利()移动互联网最具影响力的盈利指导网站。定位于服务移动互联网创业者,移动盈利指导。我们的目标是让盈利目标清晰可见!降低门槛,让缺乏经验、资金有限的个人和团队获得经验和机会,提高热情,激发产品。
转载请注明: &
与本文相关的文章获取微信小程序登录账号的openid方法 - 简书
获取微信小程序登录账号的openid方法
我们在使用小程序wx.login API进行登录的时候,是不能获取更多的信息的,如openid。官方提示,需要发送获取到的code进行请求到微信的后端API,才能正确获取更多信息,这也许是为了安全起见吧。但是我们很多时候,都要求判断登录的用户是不是唯一的,这也就需要使用微信账号唯一的openid来验证了。
不多说,根据文档,得出,只需要进行一个get请求到如下地址即可:https://api./sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
appid和secret在微信小程序后台可以看到,js_code为使用wx.login登录时获取到的code参数数据,grant_type这个不用改动。
所以,我们直接来实现吧!
这里,我使用Parse.Cloud脚本来实现,具体的其他代码,自行参考:
const APPID = "自行修改";
const SECRET = "自行修改";
Parse.Cloud.define('getOpenId', (req, res) =& {
const { code } = req.
if (!code || (typeof code !== 'string') || code.length !== 32) {
return res.error({
message: 'code格式不正确',
result: false
Parse.Cloud.httpRequest({
url: `https://api./sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${code}&grant_type=authorization_code`,
success: (_) =& {
const { data } = _;
if (data.errcode) {
return res.success({
result: false,
error: data,
message: '获取失败'
res.success({
result: _.data,
message: '获取成功'
error: (_) =& {
res.success({
result: false,
message: '获取错误'
然后前端(也就是微信小程序里边),直接把code参数传入即可:
Parse.Cloud.run('getOpenId', {
code: "003Vq34S115jF91vhi4S1Cn14S1Vq333"
}, (ret) =& {
非正常代码研究中心微信小程序 获取openid客户端_Android开发_动态网站制作指南
微信小程序 获取openid客户端
来源:人气:238
& & var that = this
& & wx.login({
& & & success: function (res) {
& & & & var appId = ' & & ';//微信公众号平台申请的appid
& & & & var appSecret = ' & ';//微信公众号平台申请的app secret
& & & & var js_code = res.//调用登录接口获得的用户的登录凭证code
& & & & wx.request({
& & & & & url: 'https://api.weixin..com/sns/jscode2?appid=' + appId + '&secret=' + appSecret + '&js_code=' + js_code + '&grant_type=authorization_code',
& & & & & data: {},
& & & & & method: 'GET',
& & & & & success: function (res) {
& & & & & & var openid = res.data.openid //返回的用户唯一标识符openid
& & & & & & console.log(openid)
& & & & & & console.log(&试试吧上面就是获得的openid&)
& & & & & }
& & & & })
& & //试验自己的服务器获取openId
& & //调用登录接口
& & wx.login({
& & & success: function (res) {
& & & & //console.log(res);
& & & & that.globalData.loginCode = res.code
& & & & wx.getUserInfo({
& & & & & success: function (res) {
& & & & & & that.globalData.userInfo = res.userInfo
& & & & & & that.globalData.iv = res.iv
& & & & & & that.globalData.encryptedData = res.encryptedData
& & & & & & typeof cb == &function& && cb(that.globalData.userInfo)
& & & & & & that.req(
& & & & & & & 'https://lifar网址.x?Action=ActionLogin',
& & & & & & & {
& & & & & & & & encryptedData: that.globalData.encryptedData,
& & & & & & & & iv: that.globalData.iv,
& & & & & & & & code: that.globalData.loginCode
& & & & & & & },
& & & & & & & 'GET',
& & & & & & & function (res) {
& & & & & & & & console.log(res)
& & & & & & & & if (res.data.success) {
& & & & & & & & & console.log('试验自己的服务器获取openId:')
& & & & & & & & & console.log(res.data)
& & & & & & & & & var sessionId = res.data.
& & & & & & & & & wx.setStorageSync('sessionId', sessionId)
& & & & & & & & & console.log(sessionId)
& & & & & & & & }
& & & & & & & },
& & & & & & & function (res) {
& & & & & & & & console.log(res)
& & & & & & & }
& & & & & & );
& & & & & }
& & & & })
& & //试验自己的服务器获取openId结束
& req: function (url, data, method, success, fail) {
& & var mydata = data || {};
& & //mydata['appId'] = app.globalData.appId;
& & wx.request({
& & & url: url,
& & & data: mydata,
& & & method: method,
& & & success: success,
& & & fail: fail,
& & & complete: function () {
& & & & // complete
优质网站模板

我要回帖

更多关于 微信小程序获取openid 的文章

 

随机推荐