微信小程序開發(fā)應(yīng)用之靜默登錄

2023-02-16    分類: 微信小程序

1.微信小程序開發(fā)靜默登入,通過獲取用戶的open_id來實(shí)現(xiàn)靜默登入,獲取open_id,不需要用戶同意,獲取code再通過code獲取微信的open_id。
wx.login({
success(res){
_this.setData({
code:res.code,
});
console.log(res.code);
}
});
獲取code后用code去獲取用戶的open_id
/**獲取open_id*/
publicfunctionweixin_login($code){
$ret['code']=200;
$app_id='xxxx';
$app_secret='xxxx';
$code=$code;//微信前端登陸獲取的CODE
//server調(diào)用微信提供的jsoncode2session接口獲取openid,session_key
$url="https://api.weixin.qq.com/sns/jscode2session";
$params=array(
'appid'=>$app_id,
'secret'=>$app_secret,
'js_code'=>$code,
'grant_type'=>'authorization_code'
);
$res=makeRequest($url,$params);//curl
if($res['code']!==200||!isset($res['result'])||!isset($res['result'])){
$ret['code']=500;
$ret['msg']='調(diào)用jsoncode2session失敗';
}
$reqData=json_decode($res['result'],true);
if(!isset($reqData['session_key'])){
$ret['code']=500;
$ret['msg']='獲得session_key失敗';
$ret['error']=$reqData;
}
//$ret['session_key']=$reqData['session_key'];
$ret=$reqData['openid'];
return$ret;
}
2.獲取用戶頭像、昵稱 信息
getUserProfile(){
constapp=this
console.log("wx.canIUse('getUserProfile')",wx.canIUse('getUserProfile'));
wx.canIUse('getUserProfile')&&wx.getUserProfile({
lang:'zh_CN',
desc:'獲取用戶相關(guān)信息',
success({
userInfo
}){
console.log('用戶同意了授權(quán)')
console.log('userInfo:',userInfo)
App.getUserInfo(userInfo,()=>{
//跳轉(zhuǎn)回原頁面
app.onNavigateBack(1)
});
},
fail(){
console.log('用戶拒絕了授權(quán)')
}
})
},
/**
*授權(quán)登錄
*/
getUserInfo(userInfo,callback){
letApp=this;
wx.showLoading({
title:"正在登錄",
mask:true
});
//執(zhí)行微信登錄
App._post_form('Member/userInfo',{
user_info:JSON.stringify(userInfo),
},result=>{
//執(zhí)行回調(diào)函數(shù)
callback&&callback();
},false,()=>{
wx.hideLoading();
});
},
3.獲取用戶手機(jī)號,新版的必須要用戶點(diǎn)擊button才能觸發(fā)獲取手機(jī)
通過code 在去獲取手機(jī)
<buttonclass="btnAuthorize"open-type="getPhoneNumber"bindgetphonenumber="getPhoneNumber">去授權(quán)</button>
getPhoneNumber(e){
let_this=this
//app.wx_login(e.detail.code);
app._post_form('Member/get_phone',{
phone_code:e.detail.code,
},result=>{
},false,()=>{
wx.hideLoading();
});
},

獲取手機(jī)號碼

先獲取access_token
/**獲取AccessToken*/
functionaccess_token(){
$app_id='xxxx';
$app_secret=xxxx;
$url="https://api.weixin.qq.com/cgi-bin/token";
$params=array(
'appid'=>$app_id,
'secret'=>$app_secret,
'grant_type'=>'client_credential'
);
$res=makeRequest($url,$params);
$reqData=json_decode($res['result'],true);
return$reqData['access_token'];
}
通過access_token和code 獲取手機(jī)
publicfunctionweixin_phone($code){
//$ret['code']=200;
$key=$this->access_token();
$url="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=".$key;
$params=array(
'code'=>$code,
);
$res=makeRequest($url,$params,'json');//curl params數(shù)據(jù)必須是json
$reqData=json_decode($res['result'],true);
return$reqData['phone_info']['phoneNumber'];
}

消息訂閱

消息通知

消息訂閱,這里以狀態(tài)審核為例子
紅圈就是發(fā)送數(shù)據(jù)的鍵名
publicfunctionsubmessage(){
/**@var*
*微信小程序消息訂閱
*/
$access_token=$this->access_token();
$url_2="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
$template_id="xxx";//模板id
$data['touser']=$openid;//open_id
$data['template_id']=$template_id;
$data['page']="pages/login/index";//用戶點(diǎn)擊消息跳轉(zhuǎn)頁面
$data['data']=array(
'thing7'=>array('value'=>$realname),
'phrase2'=>array('value'=>'審核通過'),//字?jǐn)?shù)不能超過5個
'time3'=>array('value'=>date('Y-m-dH:i',time())),
'thing4'=>array('value'=>'身份驗(yàn)證審核'),
);
//$data['data']=json_encode($data['data']);
//跳轉(zhuǎn)小程序類型:developer為開發(fā)版;trial為體驗(yàn)版;formal為正式版;默認(rèn)為正式版
$data['miniprogram_state']='trial';
//print_r($data);
$result=$this->posturl($url_2,$data);
if($result['errmsg']=='ok'){
return1;
}else{
return$result['errmsg'];
}

審核結(jié)果

本文題目:微信小程序開發(fā)應(yīng)用之靜默登錄
文章源于:http://www.muchs.cn/news15/237365.html

網(wǎng)站建設(shè)、網(wǎng)絡(luò)推廣公司-創(chuàng)新互聯(lián),是專注品牌與效果的網(wǎng)站制作,網(wǎng)絡(luò)營銷seo公司;服務(wù)項(xiàng)目有小程序開發(fā)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)