当前位置:网站首页>Wechat applet +php realizes authorized login
Wechat applet +php realizes authorized login
2022-07-04 05:30:00 【Yuan rises and falls】
1. Write wxml page
<!-- Foreground page , Decide whether to display login or information through judgment -->
<view wx:if="{
{isHide}}">
<view wx:if="{
{canIUse}}" >
<view class='header'>
<image src='/image/hhh.jpg'></image>
</view>
<view class='content'>
<view> Request permission to </view>
<text> Get your public information ( nickname , Head etc. )</text>
</view>
<button class='bottom' type='primary' open-type="getUserInfo" bindtap="login">
Authorized login
</button>
</view>
<view wx:else> Please upgrade wechat version </view>
</view>
<view wx:else>
<view> My homepage content </view>
</view>2. Write wxss page
.header {
margin: 90rpx 0 90rpx 50rpx;
border-bottom: 1px solid #ccc;
text-align: center;
width: 650rpx;
height: 300rpx;
line-height: 450rpx;
}
.header image {
width: 200rpx;
height: 200rpx;
}
.content {
margin-left: 50rpx;
margin-bottom: 90rpx;
}
.content text {
display: block;
color: #9d9d9d;
margin-top: 40rpx;
}
.bottom {
border-radius: 80rpx;
margin: 70rpx 50rpx;
font-size: 35rpx;
}
3. Write js page
// index.js
// Get application instance
const app = getApp()
Page({
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo'),
// This is what the front desk judges
isHide: true
},
// Click event
login(){
var _this=this;
wx.getUserProfile({
desc: 'desc',
success: (res)=>{
// Click allow to get wechat nickname and wechat avatar
var nickName=res.userInfo.nickName;
var log=res.userInfo.avatarUrl;
wx.login({
success:function(e){
// Get your... After the request is successful code value
var code=e.code;
wx.request({
// Request backstage
url: 'http://www.day12.com/home/Login/loginInfo',
// Pass on code、nickName、log
data: {code:code,nickName:nickName,log:log},
header:{
'content-type':'application/x-www-form-urlencoded'
},
success: function(arr){
// Judge whether the background request is successful
if(arr.data.code==200){
// Will return ID Values are stored in the cache
wx.setStorageSync('token', arr.data.data.token)
// Bullet box prompt
wx.showToast({
title: arr.data.msg,
icon: 'success'
})
// modify isHide value , To facilitate the judgment and display of the front desk
_this.setData({
isHide:false
})
}
}
})
}
})
},
fail:(res)=>{
// Click reject to pop up a prompt
wx.showToast({
title: ' Authorization login failed ',
icon: 'error'
})
}
})
}
})4. Write PHP Background code
public function loginInfo(){
// Receive the value from the foreground
$code=input('code');
$nickName=input('nickName');
$log=input('log');
// Determine whether it is null
if (empty($code)||empty($nickName)||empty($log)){
return json(['code'=>1,'data'=>'','mag'=>' parameter is incorrect ']);
}
// stay config The encapsulation of , The packaging style is as follows ( This code will appear after scanning with your own wechat ) website :https://mp.weixin.qq.com/
// return [
// 'appID'=>'',
// 'AppSecret'=>''
// ];
$appID=config('appID');
$appSecret=config('AppSecret');
// Request address
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appID."&secret=".$appSecret."&js_code=".$code."&grant_type=authorization_code";
// getUrl Is in common The encapsulation of , The packaging style is as follows
// function getUrl($url){
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output = curl_exec($ch);
// curl_close($ch);
// $output = json_decode($output,true);
// return $output;
// }
$res=getUrl($url);
// Define a new array after the call succeeds , Most important session_key and openid Two values
$arr=[
'nickName'=>$nickName,
'log'=>$log,
'session_key'=>$res['session_key'],
'openid'=>$res['openid'],
];
// according to openid Query a piece of data
$item=User::where('openid',$res['openid'])->find();
// If there is, modify session_key This field , If not, add a new piece of data
if ($item){
$data=User::where('openid',$res['openid'])->update(['session_key'=>$res['session_key']]);
$token=JWTS::createJwt($item->id);
// Return primary key ID TOKEN
return json(['code' => 200, 'data'=>[
'uid' => $item->id,
'token'=>$token,
], 'msg' => ' Login successful ',]);
}else{
// add to
$data =User::addUser($arr);
$token=JWTS::createJwt($data->id);
// Return primary key ID TOKEN
return json(['code' => 200, 'data'=>[
'uid' => $data->id,
'token'=>$token,
], 'msg' => ' Login successful ',]);
}
}Wechat applet +PHP Realize authorized login - Dragon-v - Blog Garden (cnblogs.com)
边栏推荐
- With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?
- [matlab] communication signal modulation general function - low pass filter
- 总线的基本概念
- JS string splicing
- C语言简易学生管理系统(含源码)
- LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
- LabVIEW错误对话框的出现
- 云原生架构实战案例及优化解决方案
- ping端口神器psping
- How to use postman to realize simple interface Association [add, delete, modify and query]
猜你喜欢

2022 a special equipment related management (elevator) examination questions simulation examination platform operation

RSA加密应用常见缺陷的原理与实践

模拟小根堆

Ping port artifact psping

Just do it with your hands 7 - * project construction details 2 - hook configuration

(4) Canal multi instance use

Void convolution, deformable convolution, deformable ROI pooling

Analysis of classical pointer and array written test questions in C language

ETCD数据库源码分析——初始化总览

Trie number dictionary tree
随机推荐
National vocational college skills competition (secondary vocational group) network security competition questions - Analysis
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Unity is connected to the weather system
Evolution of system architecture: differences and connections between SOA and microservice architecture
tutle时钟改进版
Void convolution, deformable convolution, deformable ROI pooling
Simulated small root pile
Write a complete answer applet (including single choice questions, judgment questions and multiple topics) (III) single choice questions, judgment questions, and the first question display
Integer type of C language
云原生架构实战案例及优化解决方案
LM小型可编程控制器软件(基于CoDeSys)笔记二十一:错误3703
远程桌面客户端 RDP
[high concurrency, high performance and high availability of massive data MySQL practice-7] - memory data drop disk
VB.net 简单的处理图片,黑白(类库——7)
2022 question bank and answers for safety management personnel of hazardous chemical business units
Letter meaning and parameter abbreviation of optical module Daquan
Automated testing selenium foundation -- webdriverapi
【雕爷学编程】Arduino动手做(105)---压电陶瓷振动模块
VB. Net calls ffmpeg to simply process video (class Library-6)
2022年T电梯修理操作证考试题库及模拟考试