This article mainly introduces relevant information to you about the detailed examples of WeChat applet authorization to obtain user details openid. I hope this article can help you. Friends in need can refer to it. I hope it can help you.
The applet gets the user’s avatar, nickname, openid, etc.
The first way is to use wx.getUserInfo Directly obtain the WeChat avatar, nickname
wx.getUserInfo({ success: function (res) { that.setData({ nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, }) }, })
The second type
When we use the wx.login API of the mini program to log in, we use it directly wx.getUserInfo cannot obtain more information, such as the openid of the WeChat user.
Official tip, you need to send the obtained code to request WeChat's back-end API, and perform user decryption and other operations before you can obtain it.
According to the document, you only need to make a get request Just go to the following address:
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这个不用改动。
js file
var openId = (wx.getStorageSync('openId')) if (openId) { wx.getUserInfo({ success: function (res) { that.setData({ nickName: res.userInfo.nickName, avatarUrl: res.userInfo.avatarUrl, }) }, fail: function () { // fail console.log("获取失败!") }, complete: function () { // complete console.log("获取用户信息完成!") } }) } else { 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', data: { code: res.code, encryptedData: res_user.encryptedData, iv: res_user.iv }, method: 'GET', header: { 'content-type': 'application/json' }, success: function (res) { // this.globalData.userInfo = JSON.parse(res.data); 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', data: { code: res_login.code, encryptedData: res_user.encryptedData, iv: res_user.iv }, method: 'GET', header: { '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 }
The background is php and the framework is laravel5.4 version
Official document:
https://mp.weixin.qq.com/debug/wxadoc/dev/api/signature.html
WeChat official provides sample codes in multiple programming languages (click to download). The interface names are consistent for each language type. The calling method can refer to the example.
After downloading, introduce it into the php file:
<?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) { // require_once ROOTPATH . "./PHP/wxBizDataCrypt.php"; $code = $request->get('code'); $encryptedData = $request->get('encryptedData'); $iv = $request->get('iv'); $appid = "***" ; $secret = "***"; $URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code"; $apiData=file_get_contents($URL); // var_dump($code,'wwwwwwww',$apiData['errscode']); // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, $URL); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_HEADER, 0); // $output = curl_exec($ch); // curl_close($ch) if(!isset($apiData['errcode'])){ $sessionKey = json_decode($apiData)->session_key; $userifo = new \WXBizDataCrypt($appid, $sessionKey); $errCode = $userifo->decryptData($encryptedData, $iv, $data ); if ($errCode == 0) { return ($data . "\n"); } else { return false; } } }
The login flow chart of the official document. The entire login process is basically as shown below:
Related recommendations:
Thinkphp5 WeChat applet how to obtain user information interface
Instance method of WeChat applet to obtain user information
How to obtain user information through WeChat applet
The above is the detailed content of Example of WeChat applet authorization to obtain user details. For more information, please follow other related articles on the PHP Chinese website!

默认情况下,Windows11不会在文件资源管理器中显示文件夹大小,但是,您可以在资源管理器设置中进行某些更改以使其可见。在本指南中,我们将讨论一些显示文件夹大小的最简单方法,以便您可以有效地管理PC上的磁盘空间。如何在Windows11上查看文件夹的大小?1.使用文件夹的“属性”窗口按+打开Windows资源管理器窗口。WindowsE转到要检查其大小的文件夹,右键单击它,然后从上下文菜单中选择“属性”。在文件夹属性窗口中,转到“常规”选项卡并找到“大小”部分以了解文件夹占用的空间。2.启用“

随着移动互联网技术和智能手机的普及,微信成为了人们生活中不可或缺的一个应用。而微信小程序则让人们可以在不需要下载安装应用的情况下,直接使用小程序来解决一些简单的需求。本文将介绍如何使用Python来开发微信小程序。一、准备工作在使用Python开发微信小程序之前,需要安装相关的Python库。这里推荐使用wxpy和itchat这两个库。wxpy是一个微信机器

小程序能用react,其使用方法:1、基于“react-reconciler”实现一个渲染器,生成一个DSL;2、创建一个小程序组件,去解析和渲染DSL;3、安装npm,并执行开发者工具中的构建npm;4、在自己的页面中引入包,再利用api即可完成开发。

实现思路x01服务端的建立首先,在服务端,使用socket进行消息的接受,每接受一个socket的请求,就开启一个新的线程来管理消息的分发与接受,同时,又存在一个handler来管理所有的线程,从而实现对聊天室的各种功能的处理x02客户端的建立客户端的建立就要比服务端简单多了,客户端的作用只是对消息的发送以及接受,以及按照特定的规则去输入特定的字符从而实现不同的功能的使用,因此,在客户端这里,只需要去使用两个线程,一个是专门用于接受消息,一个是专门用于发送消息的至于为什么不用一个呢,那是因为,只

微信小程序是一种轻量级的应用程序,可以在微信平台上运行,不需要下载安装,方便快捷。Java语言作为一种广泛应用于企业级应用开发的语言,也可以用于微信小程序的开发。在Java语言中,可以使用SpringBoot框架和第三方工具包来开发微信小程序。下面是一个简单的微信小程序开发过程。创建微信小程序首先,需要在微信公众平台上注册一个小程序。注册成功后,可以获取到

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了如何在小程序中用公众号模板消息,下面一起来看一下,希望对大家有帮助。

PHP与小程序的地理位置定位与地图显示地理位置定位与地图显示在现代科技中已经成为了必备的功能之一。随着移动设备的普及,人们对于定位和地图显示的需求也越来越高。在开发过程中,PHP和小程序是常见的两种技术选择。本文将为大家介绍PHP与小程序中的地理位置定位与地图显示的实现方法,并附上相应的代码示例。一、PHP中的地理位置定位在PHP中,我们可以使用第三方地理位

随着小程序的广泛应用,越来越多的开发者需要将其与后台服务器进行数据交互,其中最常见的业务场景之一就是上传文件。本文将介绍在小程序中实现文件上传的PHP后台实现方法。一、小程序中的文件上传在小程序中实现文件上传,主要依赖于小程序APIwx.uploadFile()。该API接受一个options对象作为参数,其中包含了要上传的文件路径、需要传递的其他数据以及


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
