uniapp+.net?core實(shí)現(xiàn)微信小程序獲取手機(jī)號(hào)功能
獲取手機(jī)號(hào)
從基礎(chǔ)庫(kù) 2.21.2 開(kāi)始,對(duì)獲取手機(jī)號(hào)的接口進(jìn)行了安全升級(jí),以下是新版本接口使用指南。( 舊版本接口 目前可以繼續(xù)使用,但建議開(kāi)發(fā)者使用新版本接口,以增強(qiáng)小程序安全性)
因?yàn)樾枰脩糁鲃?dòng)觸發(fā)才能發(fā)起獲取手機(jī)號(hào)接口,所以該功能不由 API 來(lái)調(diào)用,需用 button 組件的點(diǎn)擊來(lái)觸發(fā)。另外,新版本接口 不再 需要提前調(diào)用 wx.login 進(jìn)行登錄。
注意:
- 目前該接口針對(duì)非個(gè)人開(kāi)發(fā)者,且完成了認(rèn)證的小程序開(kāi)放(不包含海外主體)。需謹(jǐn)慎使用,若用戶舉報(bào)較多或被發(fā)現(xiàn)在不必要場(chǎng)景下使用,微信有權(quán)永久回收該小程序的該接口權(quán)限;
- 在使用該接口時(shí),用戶可使用微信綁定手機(jī)號(hào)進(jìn)行授權(quán),也添加非微信綁定手機(jī)號(hào)進(jìn)行授權(quán)。若開(kāi)發(fā)者僅通過(guò)手機(jī)號(hào)作為業(yè)務(wù)關(guān)聯(lián)憑證,在重點(diǎn)場(chǎng)景可適當(dāng)增加短信驗(yàn)證邏輯。
使用方法
需要將 button 組件 open-type 的值設(shè)置為 getPhoneNumber ,當(dāng)用戶點(diǎn)擊并同意之后,可以通過(guò) bindgetphonenumber 事件回調(diào)獲取到動(dòng)態(tài)令牌 code ,然后把 code 傳到開(kāi)發(fā)者后臺(tái),并在開(kāi)發(fā)者后臺(tái)調(diào)用微信后臺(tái)提供的 phonenumber.getPhoneNumber 接口,消費(fèi) code 來(lái)?yè)Q取用戶手機(jī)號(hào)。每個(gè) code 有效期為5分鐘,且只能消費(fèi)一次。
注: getPhoneNumber 返回的 code 與 wx.login 返回的 code 作用是不一樣的,不能混用。
前端
template
使用getphonenumber獲取回調(diào)code
//小程序?qū)懛? <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button> //uni-app寫法 <button class="wx-login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信用戶一鍵登錄</button>
js
調(diào)用服務(wù)器的url,消費(fèi) code 來(lái)?yè)Q取用戶手機(jī)號(hào)
methods: {
getPhoneNumber: function(e) {
var that = this;
var userPhone = uni.getStorageSync('userPhone');
if(userPhone != '')
{
getApp().globalData.userPhone = userPhone;
uni.navigateTo({
url: 'personal'
});
return;
}
if (e.detail.errMsg == "getPhoneNumber:ok") {
//端口號(hào)是由后端服務(wù)器生成
wx.request({
url: '后端服務(wù)URL',
data: {
code: e.detail.code
},
method: "get",
success: function(res) {
uni.setStorageSync('userPhone', res.data);
getApp().globalData.userPhone = res.data;
uni.navigateTo({
url: 'personal'
});
},
fail: function(res) {
console.log(res.errMsg)
}
})
}
}
}后端
后端使用.net core配置api
appsetting配置
"Wx": {
"appid": "",
"secret": "",
"baseurl": "https://api.weixin.qq.com/",
"getToken": "cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
"getuserphonenumber": "wxa/business/getuserphonenumber?access_token={0}"
}Startup.cs
注冊(cè)HttpClient調(diào)用微信API
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient("WxClient", config =>
{
config.BaseAddress = new Uri(Configuration["Wx:baseurl"]);
config.DefaultRequestHeaders.Add("Accept", "application/json");
});
}public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
GlobalContext.HttpClientFactory = app.ApplicationServices.GetService<IHttpClientFactory>();
}GlobalContext.cs
獲取token方法與獲取手機(jī)號(hào)方法,通過(guò)HTTPClient調(diào)用獲取Token方法,用Token和Code調(diào)用 getuserphonenumber獲取用戶手機(jī)號(hào)
using System;
using System.Reflection;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Hosting;using Microsoft.AspNetCore.Http;
using System.Net.Http;
using Newtonsoft.Json;
namespace YiSha.Util
{
public class GlobalContext
{public static IHttpClientFactory HttpClientFactory { get; set; }
public static IConfiguration Configuration { get; set; }public static string Token { get; set; }
public static DateTime TimeOutDate { get; set; }
/// <summary>
/// 獲取Token
/// </summary>
/// <returns>Item1 Token;Item2 是否成功</returns>
public static Result GetToken()
{
//判斷Token是否存在 以及Token是否在有效期內(nèi)
if (string.IsNullOrEmpty(Token) || TimeOutDate > DateTime.Now)
{
//構(gòu)造請(qǐng)求鏈接
var requestBuild = Configuration["Wx:getToken"];
requestBuild = string.Format(requestBuild,
Configuration["Wx:appid"],
Configuration["Wx:secret"]
);
using (var wxClient = HttpClientFactory.CreateClient("WxClient"))
{
var httpResponse = wxClient.GetAsync(requestBuild).Result;
var dynamic = JsonConvert.DeserializeObject<dynamic>(
httpResponse.Content.ReadAsStringAsync().Result
);
if (dynamic.errmsg == null)//刷新Token
{
Token = dynamic.access_token;
var expires_in = Convert.ToDouble(dynamic.expires_in);
TimeOutDate = DateTime.Now.AddSeconds(expires_in);
return new Result(Token);
}
else
{
return new Result(errMsg:dynamic.errmsg);
}
}
}
else
{
return new Result(Token);
}
}
public static Result GetUserPhoneNumber(string code)
{
var token = GetToken();
if(!token.isSuccess)
{
return token;
}
//構(gòu)造請(qǐng)求鏈接
var requestBuild = Configuration["Wx:getuserphonenumber"];
requestBuild = string.Format(requestBuild, token.data);
//建立HttpClient
using (var wxClient = HttpClientFactory.CreateClient("WxClient"))
{
string content = $"{{\"code\":\"[code]\"}}";
byte[] data = Encoding.UTF8.GetBytes(content);
var bytearray = new ByteArrayContent(data);
var httpResponse = wxClient.PostAsync(requestBuild, bytearray).Result;
var dynamic = JsonConvert.DeserializeObject<dynamic>(
httpResponse.Content.ReadAsStringAsync().Result
);
if (dynamic.errmsg == "ok")
return new Result(dynamic.phone_info.phoneNumber.ToString());
else
return new Result(errMsg: dynamic.errmsg.ToString());
}
}
/// <summary>
/// 返回消息
/// </summary>
public class Result
{
public Result()
{
}
/// <summary>
/// 正確
/// </summary>
/// <param name="data"></param>
public Result(string data)
{
this.data = data;
this.isSuccess = true;
}
/// <summary>
/// 錯(cuò)誤
/// </summary>
/// <param name="errMsg"></param>
/// <param name="isSuccess"></param>
public Result(string errMsg,bool isSuccess = false)
{
this.errMsg = errMsg;
this.isSuccess = isSuccess;
}
public string data { get; set; }
public string errMsg { get; set; }
public bool isSuccess { get; set; }
}
}
}調(diào)用
[HttpGet]
public string GetPhone(string code)
{
var phone = GlobalContext.GetUserPhoneNumber(code);
if(!phone.isSuccess)
{
//錯(cuò)誤處理
}
return phone.data;
}獲取截圖


文檔傳送門:
獲取手機(jī)號(hào): https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
到此這篇關(guān)于uniapp+.net core實(shí)現(xiàn)微信小程序獲取手機(jī)號(hào)的文章就介紹到這了,更多相關(guān)uniapp .net core小程序獲取手機(jī)號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp抖音小程序一鍵獲取用戶手機(jī)號(hào)的示例代碼
- UNIAPP實(shí)現(xiàn)微信小程序登錄授權(quán)和手機(jī)號(hào)授權(quán)功能(uniapp做微信小程序)
- 微信小程序獲取用戶手機(jī)號(hào)碼詳細(xì)教程(前端+后端)
- 微信小程序中獲取用戶手機(jī)號(hào)授權(quán)登錄詳細(xì)步驟
- uniapp微信小程序授權(quán)登錄并獲取手機(jī)號(hào)的方法
- PHP配合微信小程序?qū)崿F(xiàn)獲取手機(jī)號(hào)碼詳解
- 微信小程序登錄方法之授權(quán)登陸及獲取微信用戶手機(jī)號(hào)
- 微信小程序獲取用戶手機(jī)號(hào)碼的詳細(xì)步驟
- 微信小程序?qū)崿F(xiàn)手機(jī)號(hào)碼驗(yàn)證
- 抖音小程序一鍵獲取手機(jī)號(hào)的實(shí)現(xiàn)思路
相關(guān)文章
ASP.net與SQLite數(shù)據(jù)庫(kù)通過(guò)js和ashx交互(連接和操作)
這篇文章主要介紹了ASP.net與SQLite數(shù)據(jù)庫(kù)通過(guò)js和ashx交互(連接和操作),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件實(shí)例
這篇文章主要介紹了Asp.Net的FileUpload類實(shí)現(xiàn)上傳文件的方法,以實(shí)例形式講述了上傳文件類的具體實(shí)現(xiàn)方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11
Repeater的FooterTemplate中控件內(nèi)容設(shè)置方法
Repeater的FooterTemplate中控件內(nèi)容設(shè)置方法,需要的朋友可以參考下。2009-12-12
asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
asp.net 上傳圖片并作處理(生成縮略圖 、在圖片上增加文字水印、在圖片上生成圖片水?。┑膶?shí)例代碼,經(jīng)過(guò)測(cè)試!2013-06-06
.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化
這篇文章主要介紹了.Net Core實(shí)現(xiàn)選擇數(shù)據(jù)熱更新讓服務(wù)感知配置的變化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能
這篇文章主要介紹了asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能,大家參考使用吧2014-01-01
ASP.Net WebAPI與Ajax進(jìn)行跨域數(shù)據(jù)交互時(shí)Cookies數(shù)據(jù)的傳遞
本文主要介紹了ASP.Net WebAPI與Ajax進(jìn)行跨域數(shù)據(jù)交互時(shí)Cookies數(shù)據(jù)傳遞的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
IIS7偽靜態(tài)web.config配置的方法和規(guī)則
本文主要介紹IIS7上配置偽靜態(tài)的超簡(jiǎn)單的新方法,安裝URLRewrite插件,配置web.config即可。2016-04-04
asp.net core標(biāo)簽助手的高級(jí)用法TagHelper+Form
這篇文章主要為大家詳細(xì)介紹了asp.net core標(biāo)簽助手的高級(jí)用法TagHelper+Form,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

