一、wxml页面
<!--pages/login/index.wxml-->
<view class="container">
<view class="loginBg-pic">
<image src="../../img/login-pic.png"></image>
</view>
<button class='loginBtn' open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
<image src="../../img/wx-icon.png"></image>
微信登录
</button>
</view>
wxss页面
/* pages/login/index.wxss */
page{
background: #f5f5f5;
}
.loginBg-pic{
width: 502rpx;
height: 476rpx;
margin: 134rpx auto 86rpx;
}
.loginBg-pic image{
width: 100%;
height: 100%;
}
.loginBtn{
width: 308rpx;
height: 88rpx;
background: -webkit-linear-gradient(left,#6feab0,#12ca94);
border-radius: 44rpx;
font-size: 36rpx;
line-height: 88rpx;
color: #fff;
font-family:PingFang SC;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.loginBtn::after{
border: 0;
}
.loginBtn image{
width: 49rpx;
height: 39rpx;
margin-right: 8rpx;
}
js页面
// pages/login/login.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let pages = getCurrentPages();//页面对象
let prevpage = pages[pages.length - 2];//上一个页面对象
console.log(prevpage.route)//上一个页面路由地址
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
//通过绑定手机号登录
getPhoneNumber: function (e) {
var ivObj = e.detail.iv
var telObj = e.detail.encryptedData
var codeObj = "";
var that = this;
//------执行Login
wx.login({
success: resu => {
console.log(resu)
wx.request({
url: 'https://xxxxx/user/userLogin', //微信登录接口地址
data: {
appid: "wxe7e1c32e685271a2",
secret: "f53f46d8ada0a7d4df4a54a5b2b23d85",
code: resu.code,
encryptedData: telObj,
iv: ivObj
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log('rrrrrrr',res)
wx.request({
url: 'https://xxxx/user/regist',//注册接口
data: {
phone: res.data.phoneNumber,
},
success: function(data) {
wx.request({
url: 'https://xxxx/user/open', //获取openid接口地址
data: {
code:resu.code,
phone:res.data.phoneNumber
},
header: {
'content-type': 'application/json' //默认值
},
success: function (resul) {
var open= JSON.parse(resul.data)
console.log('openid',resul)
wx.setStorageSync("openid", open.openid)
}
})
if(data.data.code==1001){//查看用户是否已注册,若无注册跳入注册页面
wx.navigateTo({//跳转注册页
url: '/pages/register/index'
})
}else{
wx.setStorageSync("phone", res.data.phoneNumber)
let pages = getCurrentPages();//页面对象
let prevpage = pages[pages.length - 2];//上一个页面对象
let routes=prevpage.route
console.log(prevpage.route)//上一个页面路由地址
if(routes=="pages/index/index"){
wx.switchTab({
url: '/pages/index/index'
})
}
}
}
});
}
})
//-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
wx.navigateTo({
url: '/pages/login/index',
})
}
}
});
//---------登录有效期检查
wx.checkSession({
success: function () {
//session_key 未过期,并且在本生命周期一直有效
},
fail: function () {
// session_key 已经失效,需要重新执行登录流程
wx.login() //重新登录
}
});
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
PHP后端
//微信登录
public function userLogin(){
include ("wxBizDataCrypt.php"); //引入 wxBizDataCrypt.php 文件
$request=request();
$appid =$request->param('appid');
$secret =$request->param('secret');
$js_code=$request->param('code');
// $session_key=$request->param('session_key');
$iv = ($request->param('iv'));
$encryptedData=($_GET['encryptedData']);
$grant_type='authorization_code';
$objSession=$this->http_curl("https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$js_code&grant_type=$grant_type");
$session_key = json_decode($objSession)->session_key;
$decodeData = new \WXBizDataCrypt($appid, $session_key);
$errCode = $decodeData->decryptData($encryptedData, $iv, $data);
if ($errCode == 0) {
print($data . "\n");
} else {
print($errCode . "\n");
}
}
public function http_curl($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$response=curl_exec($curl);
curl_close($curl);
return $response;
}
//用户注册
public function regist($phone = "",$openid="")
{
if ($phone){
//判断该用户是否已经注册
$userdata = Db::name('bf_member')->where('phone',$phone)->find();
if ($userdata){
// Db::table('bf_member')->where('phone',$phone)->update(['openid'=>"23"]);
$result= ['code'=>1000,'msg'=>'success','data'=>$userdata];
}else{
$result= ['code'=>1001,'msg'=>'User not registered','data'=>''];
}
return $result;
}
}
//获取openid(code只可使用一次)
public function open(){
$request=request();
$code=$request->param('code');
$phone=$request->param('phone');
$appid = "xxxx";//小程序appid
//配置appscret
$secret = "xxxxx";//小程序密钥
$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";//api接口
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_URL, $api);
$res = curl_exec($curl);
curl_close($curl);
$data=json_decode($res,true);
Db::table('ember')->where('phone',$phone)->update(['openid'=>$data['openid']]);//修改用户表,openid入库
return $res;
}
wxBizDataCrypt.php文件下载链接:https://res.wx.qq.com/wxdoc/dist/assets/media/aes-sample.eae1f364.zip
微信登录授权链接:https://blog.csdn.net/it0_soft/article/details/84634910