uniapp微信小程序登錄的前端已經發過一篇博客了(地址:https://blog.csdn.net/qq_35616850/article/details/89711597),搞明白API之後會發現很簡單
很多朋友諮詢我後端的寫法,由於太忙,沒有回答,今天通過博客的方式統一回復大家。
首先我們要了解在開發項目的時候你需要哪些參數,微信小程序登錄過程中我們需要用戶唯一標識,和一些用戶信息,主要用到wx.login()和wx.getUserInfo()兩個接口。
獲取唯一標識這一步需要後端配合,也就是在你點擊登錄的時候把需要後端需要的數據傳過去就可以了。具體看代碼:
<button @click="login"></button>
login(){
var self=this;
uni.showLoading({
mask:true,
title: '正在登錄···',
complete:()=>{}
});
uni.login({
provider: 'weixin',
success: function (loginRes) {
let js_code=loginRes.code;
uni.getUserInfo({
provider: 'weixin',
success: function (infoRes) {
let username=infoRes.userInfo.nickName;
let gender=infoRes.userInfo.gender;
let formdata={code:js_code,username:username,sex:gender};
self.$go.post("/login",formdata).then(res=>{
if(res.code==200){
}
})
},
fail:function(res){}
})
},
fail:function(res){}
})
}
後臺php接口示例,僅供參考,一定要理清邏輯
public function login(){
$appid = 'wx321212121212121';
$secret = '211kdshsakdakh121k2j1h2k';
$code = input('code');
$username = input('username');
$sex = input('sex');
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$secret."&js_code=".$code."&grant_type=authorization_code";
$res = $this->https_request($url);
$res = json_decode($res,true);
if(isset($res['errcode'])){
}else{
$user = db('user')->where(array('unionid'=>$res['unionid']))->find();
if(empty($user)){
}else{
}
}
這就是uniapp微信小程序完整的登陸流程了,仔細看哦!有問題可以評論,或進羣:682783851
覺得不錯的請點個贊,謝謝!也可以搜索小程序【購靚號】進去體驗哦!