


Brief introduction: Implement the authorized login function of the mini program
Nov 07, 2022 pm 05:32 PM本篇文章給大家?guī)?lái)了關(guān)于微信小程序的相關(guān)知識(shí),其中主要介紹了怎么實(shí)現(xiàn)小程序授權(quán)登錄功能的相關(guān)內(nèi)容,下面一起來(lái)看一下,希望對(duì)大家有幫助。
【相關(guān)學(xué)習(xí)推薦:小程序?qū)W習(xí)教程】
在我們平時(shí)工作、學(xué)習(xí)、生活中,微信小程序已成為我們密不可分的一部分,我們仔細(xì)留意下,每當(dāng)我們使用一個(gè)新的小程序時(shí),總會(huì)遇到如下頁(yè)面:
這便是微信小程序授權(quán)登錄功能了,授權(quán)登錄后,我們就可以正常使用小程序,而小程序也會(huì)獲取到我們的用戶(hù)權(quán)益,手機(jī)號(hào)等個(gè)人信息
授權(quán)登錄功能剖析
微信小程序的授權(quán)登錄具體步驟如下所示
具體實(shí)現(xiàn)主要有以下三個(gè)步驟:
調(diào)用wx.login() 微信api獲取臨時(shí)登錄憑證code,并回傳到開(kāi)發(fā)者服務(wù)器
調(diào)用auth.code2Session?微信api接口,獲取用戶(hù)唯一標(biāo)識(shí)OpenID、 用戶(hù)在微信開(kāi)放平臺(tái)帳號(hào)下的唯一標(biāo)識(shí)UnionID和會(huì)話密鑰session_key
通過(guò)步驟2獲取的參數(shù)進(jìn)行解密操作,獲取用戶(hù)手機(jī)號(hào),頭像等特性,并把需要的數(shù)據(jù)保存到緩存中
步驟實(shí)現(xiàn)代碼如下:
一、獲取臨時(shí)登錄憑證code
由于微信官方修改了getUserInfo接口,現(xiàn)在無(wú)法實(shí)現(xiàn)一進(jìn)入微信小程序就會(huì)自動(dòng)彈出授權(quán)窗口,所以我們只能通過(guò)button按鈕讓用戶(hù)手動(dòng)觸發(fā)
我們先寫(xiě)一個(gè)簡(jiǎn)單的彈框,用isShow變量控制,isShow取決于步驟3中的緩存信息,當(dāng)所有步驟都走通,會(huì)正確緩存用戶(hù)信息,此時(shí)彈框隱藏,否則彈框都為顯示狀態(tài)
??<view> ????????<view> ??????????<text>需要先授權(quán)獲取個(gè)人信息</text> ??????????<button>微信賬號(hào)快速授權(quán)</button> ????????</view> ??</view>
點(diǎn)擊按鈕時(shí),調(diào)用getUserInfo方法,isShow設(shè)置為false,同時(shí)使用wx.login獲取到登錄憑證code
getUserInfo:e=>{??????this.setData({????????isShow:false ??????}) ??????wx.login({????????success:?function?(res)?{???????????????let?code?=?res.code?//?登錄憑證code ?????????} ??????}) ?????? ??}
二、根據(jù)登錄憑證code,獲取用戶(hù)登錄信息
拿到登錄憑證code后,調(diào)用auth.code2Session?微信api接口(此處為服務(wù)端操作,后端大佬搞定,我們直接調(diào)用他給的接口就好)
?????????wx.request({????????????????url:?獲取用戶(hù)信息的auth.code2Session微信api接口,????????????????method:?'POST',????????????????data:{??????????????????code:code//登錄憑證code ????????????????},????????????????header:?{??????????????????'content-type':?'application/json;charset=UTF-8' ????????????????},???????????????? ????????????????success:?function?(res)?{??????????????????var?userphone=?res.data.data??????????????????//解密手機(jī)號(hào) ??????????????????var?msg?=?e.detail.errMsg;??????????????????var?sessionKey?=?userphone.session_key;//會(huì)話密鑰 ??????????????????var?encryptedData=e.detail.encryptedData;?//簽名 ??????????????????var?unionid?=?userphone.unionid//唯一標(biāo)識(shí) ??????????????????var?iv=?e.detail.iv;??????????????????//授權(quán)成功 ??????????????????if?(msg?==?'getPhoneNumber:ok')?{ ????????????????????wx.checkSession({??????????????????????success:function(){????????????????????????//進(jìn)行請(qǐng)求服務(wù)端解密手機(jī)號(hào) ????????????????????????this.deciyption(sessionKey,encryptedData,iv,unionid); ??????????????????????} ????????????????????}) ??????????????????} ????????????????} ??????????????}) ????????} ??????})
此時(shí)大多數(shù)用戶(hù)信息我們已經(jīng)獲取了,但用戶(hù)手機(jī)號(hào),用戶(hù)頭像等信息還處于加密狀態(tài),我們需要去解密獲取這些參數(shù)
三、根據(jù)用戶(hù)信息,解密獲取用戶(hù)手機(jī)號(hào)
deciyption(sessionKey,encryptedData,iv,unionid){????var?that?=?this; ????wx.request({??????url:?解密接口,??????method:?'POST',??????data:?{????????sessionKey:?sessionKey,????????encryptedData:encryptedData,????????iv:?iv ??????},??????header:?{????????'content-type':?'application/json;charset=UTF-8' ??????},??????success:?function(res)?{ ????????let?data?=?res.data????????if?(data.resultCode?==?'success')?{ ????????????wx.setStorageSync('userTel',?data.data.phoneNumber);//存儲(chǔ)解密后的用戶(hù)手機(jī)號(hào) ????????}else{ ????????????wx.showToast({????????????????title:?'獲取信息失敗請(qǐng)重新授權(quán)',????????????????icon:?'none' ????????????}) ????????????that.setData({????????????????isShow:true ????????????}) ????????}???? ??????}, ??????fail:function(res)?{ ????????wx.showToast({????????????title:?'獲取失敗請(qǐng)重新授權(quán)',????????????icon:?'none' ????????}) ????????that.setData({??????????isShow:true ????????}) ??????} ????}) ??},
此時(shí)授權(quán)登錄功能已完成
【相關(guān)學(xué)習(xí)推薦:小程序?qū)W習(xí)教程】
The above is the detailed content of Brief introduction: Implement the authorized login function of the mini program. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to

Implementing the sliding delete function in WeChat mini programs requires specific code examples. With the popularity of WeChat mini programs, developers often encounter problems in implementing some common functions during the development process. Among them, the sliding delete function is a common and commonly used functional requirement. This article will introduce in detail how to implement the sliding delete function in the WeChat applet and give specific code examples. 1. Requirements analysis In the WeChat mini program, the implementation of the sliding deletion function involves the following points: List display: To display a list that can be slid and deleted, each list item needs to include
