一.獲取手機號(舊版本,只能在測試號調(diào)用)
1.獲取手機號首先要先登錄拿到code,用code去獲取session_key
2.獲取?code需要知道自己的AppID(小程序ID)和AppSecret(小程序密鑰)
3.解密后得到手機號
?登錄微信公眾平臺拿到自己的AppID(小程序ID)和AppSecret(小程序密鑰)
?微信公眾平臺
?補充獲取?code:
uni.login({
success: (loginRes) => {
console.log(loginRes.code,"code");
}
});
補充獲取openId:
wx.request({
url: `https://api.weixin.qq.com/sns/jscode2session?appid=你的appid&secret=你的secret&js_code=${code}&grant_type=authorization_code`,
method: 'POST',
data: {
code: code
},
success: res => {
console.log(res.data.openid, "獲取openId");
}
})
獲取session_key:
uni.login({
success: (loginRes) => {
let code = loginRes.code
wx.request({
url: `https://api.weixin.qq.com/sns/jscode2session?appid=你的appid&secret=你的secret&js_code=${code}&grant_type=authorization_code`,
method: 'POST',
data: {
code: code
},
header: {
'content-type': 'application/json;charset=UTF-8'
},
success: (res) => {
this.sessionkey = res.data.session_key
}
})
}
});
解密:引入官方解密開放數(shù)據(jù)
服務端獲取開放數(shù)據(jù) | 微信開放文檔
?下載之后我們拿到WXBizDataCrypt.js文件把它放在項目中
?import WXBizDataCrypt from "@/js/WXBizDataCrypt.js"
<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">喚起授權(quán)手機號</button>
getPhoneNumber(e) {
let pc = new WXBizDataCrypt('你的AppID', this.sessionkey);
let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
console.log(data, "解密之后的數(shù)據(jù)包含手機號")
},
獲取手機號(新版本)
獲取access_token:
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret`,
success: (res) => {
console.log(res.data.access_token);
}
})
<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">喚起授權(quán)手機號</button>
?注:getPhoneNumber
?返回的?code
?與?wx.login
?返回的?code
?作用是不一樣的,不能混用
getPhoneNumber |
code | 動態(tài)令牌,可通過動態(tài)令牌換取用戶手機號 |
getPhoneNumber(e) {
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret`,
success: (res) => {
this.access_token = res.data.access_token
wx.request({
url:'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' +this.access_token,
method: 'POST',
data: {
code: e.detail.code
},
success: (res) => {
console.log(res, "獲取到的手機號");
}
})
}
})
},
二.獲取用戶地理位置
uni.chooseLocation({
success: function(res) {
console.log(res, "獲取當前的地理位置");
}
});
三.根據(jù)位置獲取經(jīng)緯度跳轉(zhuǎn)高德
高德地圖key獲取方法:登錄高德開放平臺-控制臺-應用管理-我的應用(創(chuàng)建新應用并添加key)
高德開放平臺 | 高德地圖API
?
uni.request({
url: 'https://restapi.amap.com/v3/geocode/geo?parameters',
method: 'GET',
data: {
key: '你的高德地圖key',
address: "杭州市西湖區(qū)學院路007號"
},
success: (res) => {
var latlon = res.data.geocodes[0].location.split(',')
this.latitude = latlon[0]
this.longitude = latlon[1]
this.openMap()
}
})
?跳轉(zhuǎn)到高德地圖相應地址
openMap() {
uni.openLocation({
latitude: Number(this.longitude),
longitude: Number(this.latitude),
success: (res) => {
console.log(res, 'success');
}
});
},
?四.小程序訂閱消息提醒
微信公眾平臺
登錄微信公眾平臺->訂閱消息->選取一個模版
?
?文章來源地址http://www.zghlxwxcb.cn/news/detail-435202.html
wx.requestSubscribeMessage({
tmplIds: [
'模板ID',
],
success: res => {
if (res.errMsg === 'requestSubscribeMessage:ok') {
wx.request({
url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret',
success: res => {
var access_token = res.data.access_token
wx.request({
url: `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token}`,
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: {
"touser": this.openid,
"template_id": "模板ID",
"page": "index",
"miniprogram_state": "developer",
"lang": "zh_CN",
"data": {
"time9": {
"value": this.dataform.appointmentTime
},
"thing14": {
"value": this.dataform.projects
},
"thing8": {
"value": this.dataform.mobile
}
},
},
success(res) {},
})
},
})
}
},
})
文章來源:http://www.zghlxwxcb.cn/news/detail-435202.html
?
到了這里,關(guān)于uni-app獲取手機號-獲取用戶地理位置-根據(jù)位置獲取經(jīng)緯度跳轉(zhuǎn)高德的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!