公司小程序項(xiàng)目中快速登錄需要實(shí)現(xiàn)微信用戶授權(quán)手機(jī)登錄、注冊功能。結(jié)果遇到了
invalid code hint: [zHkDmt0sf-MBjga] rid: 64e3259f-1091b953-7e10f1da
目錄
服務(wù)端文檔
文檔描述
返回信息
服務(wù)端代碼
遇到問題
排查問題
1.服務(wù)端用錯了appid serect
2.小程序端用錯了appid serect
3.服務(wù)端用錯了access_token
4.是否存在code使用了多次
5.非指定code
總結(jié)
服務(wù)端文檔
文檔描述
該接口需配合手機(jī)號快速驗(yàn)證或手機(jī)號實(shí)時驗(yàn)證能力一起使用,當(dāng)用戶同意后,可以通過 bindgetphonenumber 或 bindrealtimegetphonenumber 事件回調(diào)獲取到動態(tài)令牌code,再調(diào)用該接口將code換取用戶手機(jī)號。
注意:每個code只能使用一次,code的有效期為5min。
返回信息
功能流程
服務(wù)端代碼
這里需要處理小程序發(fā)送過來code后,請求小程序手機(jī)號驗(yàn)證接口;
參數(shù)需要先獲取小程序的access_token,和小程序獲得的code。
class WeChat
{
/**
* 小程序appid
* @var string
*/
protected static $min_AppId = '你的小程序 appid';
/**
* 小程序app secret
* @var string
*/
protected static $min_AppSecret = '你的小程序secret';
private static $_self;
private function __construct()
{
// TODO: Implement __construct() method.
}
/**
* 外部直接調(diào)用靜態(tài)方法中的實(shí)例化
* @return WeChat
*/
public static function getInstance()
{
if (!self::$_self instanceof self) {
self::$_self = new self();
}
return self::$_self;
}
private function __clone()
{
// TODO: Implement __clone() method.
}
/**
* 獲取小程序接口調(diào)用憑證
* @Author: Yjl
* @Since: 2023/8/21 16:42
* @return mixed
*/
protected function getMiniAccessToken()
{
$APPID = self::$min_AppId;
$SECRET = self::$min_AppSecret;
$url = "https://api.weixin.qq.com/cgi-bin/token?appid={$APPID}&secret={$SECRET}&grant_type=client_credential";
$res = $this->linkCurl($url, 'GET');
$res = djson($res);
if (isset($res['errcode']) && $res['errcode']) {
return [‘status’ => 0,’msg’ => $res['errmsg'],’data’=> ‘’];
}
return [‘status’ => 1,’msg’ => ‘success’,’data’=> $res['access_token']];
}
/**
* 微信小程序-手機(jī)號快速驗(yàn)證
* @Author: Yjl
* @Since: 2023/8/21 16:01
* @param $code
* @return mixed
*/
public function getPhoneNumber($code)
{
$res = $this->getMiniAccessToken();
if ($res['status'] != 1) return $res;
$url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" . $res['data'];
$param = ['code' => $code];
$data = json_encode($param);
$header = array();
$header[] = 'content-type:application/json';
$res = $this->linkCurl($url, 'POST', $data, $header);
$res = djson($res);
if (isset($res['errcode']) && $res['errcode']) {
return [‘status’ => 0,’msg’ => $res['errmsg'],’data’=> ‘’];
}
return [‘status’ => 1,’msg’ => ‘success’,’data’=> $res];
}
/**
* 請求接口返回內(nèi)容
* @param $url :請求的URL地址
* @param $method :請求方式POST|GET
* @param $params :請求的參數(shù)
* @param $header : 請求頭
* @return bool|string
*/
protected function linkCurl($url, $method, $params = array(), $header = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (strpos("$" . $url, "https://") == 1) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if ($method == "POST") {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
} else if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($params));
}
$response = curl_exec($ch);
if ($response === false) {
return false;
}
curl_close($ch);
return $response;
}
}
遇到問題
無效的code
invalid code hint: [zHkDmt0sf-MBjga] rid: 64e3259f-1091b953-7e10f1da
返回截圖如下:
排查問題
1.服務(wù)端用錯了appid serect
經(jīng)過仔細(xì)排查代碼內(nèi)容,此項(xiàng)無問題。
2.小程序端用錯了appid serect
經(jīng)過前端同事對比,和原有小程序其他接口使用正常,此項(xiàng)可以排除。
3.服務(wù)端用錯了access_token
因?yàn)榉?wù)端也有公眾號操作,所以發(fā)現(xiàn)access_token用混了,改之后還是提示上方錯誤!
4.是否存在code使用了多次
通過前端打印請求日志和直接使用code請求微信接口,此項(xiàng)可排除。
5.非指定code
最后繼續(xù)研究文檔才發(fā)現(xiàn)此code非彼code;
怎么回事呢,項(xiàng)目中還有一個登錄獲取用戶信息的也需要小程序端生成code,然后服務(wù)端去獲取用戶信息實(shí)現(xiàn)登錄;跟手機(jī)號驗(yàn)證的code不是同一個code,前端給用混了。
文檔如下:
步驟1:需要將 button 組件 open-type 的值設(shè)置為 getPhoneNumber,當(dāng)用戶點(diǎn)擊并同意之后,通過 bindgetphonenumber 事件獲取回調(diào)信息;
步驟2:將 bindgetphonenumber 事件回調(diào)中的動態(tài)令牌code傳到開發(fā)者后臺,并在開發(fā)者后臺調(diào)用微信后臺提供的 phonenumber.getPhoneNumber 接口,消費(fèi)code來換取用戶手機(jī)號。每個code有效期為5分鐘,且只能消費(fèi)一次。
注:getPhoneNumber 返回的 code 與 wx.login 返回的 code 作用是不一樣的,不能混用。文章來源:http://www.zghlxwxcb.cn/news/detail-667096.html
總結(jié)
發(fā)現(xiàn)問題并解決了覺得很簡單,但當(dāng)時真的耽誤了一天;為什么開始沒發(fā)現(xiàn)這個問題,只能說是前后端聯(lián)調(diào)的鍋;在這里記錄一下吧,之前從網(wǎng)上找解決方法的時候看到很多原因,我這里也提供一種解決思路吧;最后還是要多看文檔,多做測試,多溝通。文章來源地址http://www.zghlxwxcb.cn/news/detail-667096.html
到了這里,關(guān)于微信小程序手機(jī)號驗(yàn)證開發(fā)遇到問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!