1.微信小程序開發(fā)靜默登入,通過獲取用戶的open_id來實現(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 */
public function weixin_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() {
const app = 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) {
let App = 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. 獲取用戶手機號,新版的必須要用戶點擊button才能觸發(fā)獲取手機
通過code 在去獲取手機
<button class="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();
});
},
先獲取access_token
/**獲取AccessToken */
function access_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 獲取手機
public function weixin_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'];
}
紅圈就是發(fā)送數(shù)據(jù)的鍵名
public function submessage(){
/** @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" ; //用戶點擊消息跳轉(zhuǎn)頁面
$data['data'] = array(
'thing7'=>array('value'=>$realname),
'phrase2'=>array('value'=>'審核通過'),//字數(shù)不能超過5個
'time3'=>array('value'=>date('Y-m-d H:i', time())),
'thing4'=>array('value'=>'身份驗證審核'),
);
//$data['data'] = json_encode($data['data']);
//跳轉(zhuǎn)小程序類型:developer為開發(fā)版;trial為體驗版;formal為正式版;默認為正式版
$data['miniprogram_state'] = 'trial';
//print_r($data);
$result = $this->posturl($url_2,$data);
if($result['errmsg']=='ok'){
return 1;
}else{
return $result['errmsg'] ;
}
}