微信小程序二
小程序API
https://www.w3xue.com/mobile/wxminiapp/hpm41q8p.html
基礎(chǔ):
API,全稱Application Programming Interface,即應(yīng)用程序編程接口。
-
API 是一些預(yù)先定義函數(shù),目的是用來(lái)提供應(yīng)用程序與開(kāi)發(fā)人員基于某軟件或者某硬件得以訪問(wèn)一組例程的能力,并且無(wú)需訪問(wèn)源碼或無(wú)需理解內(nèi)部工作機(jī)制細(xì)節(jié)。
-
API 就是操作系統(tǒng)給應(yīng)用程序的調(diào)用接口,應(yīng)用程序通過(guò)調(diào)用操作系統(tǒng)的 API而使操作系統(tǒng)去執(zhí)行應(yīng)用程序的命令(動(dòng)作)。在 Windows 中,系統(tǒng)API是以函數(shù)調(diào)用的方式提供的。
說(shuō)明:
-
wx.on 開(kāi)頭的 API 是監(jiān)聽(tīng)某個(gè)事件發(fā)生的API接口
如:接受一個(gè)CALLBACK函數(shù)作為參數(shù),當(dāng)該事件觸發(fā)時(shí),會(huì)調(diào)用CALLBACK函數(shù)。
-
如未特殊約定,其他API接口都接受一個(gè)OBJECT作為參數(shù)。
-
OBJECT中可以指定success,fail,complete來(lái)接收接口調(diào)用結(jié)果。
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
success | Function | 否 | 接口調(diào)用成功的回調(diào)函數(shù) |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |
外部 api 的引用示例:
js文件:
// pages/demo02/demo02.js
const app = getApp()
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
code:'',
testList:[],
dreamCode:'',
dreamResult:[]
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad(options) {
self=this;
},
test1:function() {
//訪問(wèn)外部api需請(qǐng)求:
wx.request({
url: 'http://api.tianapi.com/starinfo/index',
method:'GET',
//請(qǐng)求參數(shù):
data:{
key:'9b46f4069f578a5808847c85ce6b93eb',
name:'劉德華'
},
//接口調(diào)用成功的回調(diào)函數(shù):
success:function(res){
console.log(res.data);
self.setData({
testList:res.data.code,
testList:res.data.newslist
})
}
})
},
test2:function() {
wx.request({
url: 'http://api.tianapi.com/dream/index',
method:'GET',
data:{
key:'9b46f4069f578a5808847c85ce6b93eb',
num:5,
word:'西瓜'
},
success: function(res) {
console.log(res.data)
//頁(yè)面獲得數(shù)據(jù)
/*self.setData({
dreamCode:res.data.code,
dreamResult: res.data.newslist
})*/
}
})
}
})
wxml文件:
<!--pages/demo02/demo02.wxml-->
<text>pages/demo02/demo02.wxml</text>
<button open-type="share" bindtap="test2">周公解夢(mèng)</button>-
<view wx:for="{{dreamResult}}" >
<view>夢(mèng)到{{dreamResult[index].title}}------{{dreamResult[index].result}}</view>
</view>
<button type="primary" bindtap="test1">明星百科大全</button>
<view wx:for="{{testList}}" >
<view>名字:{{testList[index].nationality}}</view>
</view>
訂閱消息實(shí)戰(zhàn)
1. 搭配環(huán)境,建目錄
注意:創(chuàng)建項(xiàng)目時(shí)先不要使用云開(kāi)發(fā),避免不必要的目錄
-
在app.js文件中初始云環(huán)境
onLaunch() { wx.cloud.init({ env:"cloud1-0gdcaicsce323e0e" }) },
-
在project.config.json文件中,創(chuàng)建云函數(shù)
"cloudfunctionRoot": "cloud1/",
在目錄處新建文件夾命名(出現(xiàn)小云朵成功)
右鍵選自己的一個(gè)環(huán)境,如果只有一個(gè)環(huán)境的話,就是當(dāng)前環(huán)境。
-
創(chuàng)建云函數(shù),右鍵cloud1(自己的那個(gè)云函數(shù)目錄),選擇—>新建Node.js云函數(shù),創(chuàng)建云函數(shù)(先獲得openid),此文件中的inde.js的 cloud.init() 中初始化,可以用常量cloud.DYNAMIC_CURRENT_ENV
cloud.init({ env:cloud.DYNAMIC_CURRENT_NV //默認(rèn)取當(dāng)前云環(huán)境ID })
2. 獲取openid
在pages目錄的-----index.wxml中
<button bindtap="getOpenid">獲取用戶openID</button>
在pages目錄的-----index.js的代碼中
Page({
//獲取用戶openID
getOpenid(){
wx.cloud.callFunction({
name:"getOpenid"
}).then(res=>{
console.log("獲取Openid成功",res)
}).catch(res=>{
console.log("獲取Openid失敗",res)
})
}
})
保存后需要再次上傳并部署,內(nèi)容修改就得部署一次
3.獲取用戶授權(quán)
每次訂閱消息的時(shí)候都需要獲取授權(quán)
一次性訂閱消息、長(zhǎng)期訂閱消息,詳見(jiàn)接口 wx.requestSubscribeMessage
設(shè)備訂閱消息,詳見(jiàn)接口 wx.requestSubscribeDeviceMessage
- 定義按鈕:
<button bindtap="shouquan" >2.獲取用戶授權(quán)</button>
-
授權(quán),綁定模板ID
//pages目錄的-----index.js的Page({})中 shouquan(){ wx.requestSubscribeMessage({ tmplIds: [ '3PlhCeepxyLBh7SziJ_BCc3Eyg5GOy313bJYATK3EHA' ], //此處添加申請(qǐng)的模板ID,多個(gè)用英文逗號(hào)隔開(kāi) success(res){ console.log('授權(quán)成功-----',res) }, fail(res){ console.log('授權(quán)失敗-----',res) } }) }
【當(dāng)有多個(gè)模板的時(shí)候可以通過(guò)真機(jī)調(diào)試,手機(jī)進(jìn)行模板的選擇—>
真機(jī)調(diào)試–>手機(jī)掃碼(模擬器與手機(jī)同步)—>點(diǎn)擊獲取授權(quán),選擇模板】
4. 調(diào)用接口下發(fā)訂閱消息
新建云函數(shù) (getMessages) 初始化步驟同上---->略
- getMessages下的index.js文件中
//getMessages下的index.js文件中
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
try{
//發(fā)送訂閱消息給用戶
const result = await cloud.openapi.subscribeMessage.send({
touser:event.openid, //獲取當(dāng)前環(huán)境的openid
page:"pages/index/index", //要跳轉(zhuǎn)到哪個(gè)頁(yè)面
data:{ //推送的內(nèi)容
thing1:{
value:"小程序編程"
},
name2:{
value:'Aurora'
},
date3:{
value:'2022/09/15 14:00'
},
thing4:{
value:'大教室01'
}
},
//模板id
templateId:'3PlhCeepxyLBh7SziJ_BCc3Eyg5GOy313bJYATK3EHA' //與前面模板ID相同
})
console.log(result)
return result.errcode
}catch(err){
console.log(err);
return err
}
}
申請(qǐng)的模板,點(diǎn)擊詳情中的部分與代碼中的名稱相對(duì)應(yīng)
-
在pages目錄的-----index.js和index.wxml中
按鈕:3.發(fā)送訂閱消息給單個(gè)用戶
//發(fā)送消息給單個(gè)用戶 sendOne(){ wx.cloud.callFunction({ name:"sendMessages" }).then(res=>{ console.log("發(fā)送一條信息成功-----",res) }).catch(res=>{ console.log("發(fā)送一條信息失敗~~~",res) }) }
【此時(shí)運(yùn)行整個(gè)過(guò)程,控制臺(tái)全部成功,但是手機(jī)并不能獲取到信息】
解決:
openid沒(méi)有上傳
在pages目錄的index.js中添加傳入openid
sendOne(){
wx.cloud.callFunction({
name:"sendMessages",
data:{
//此處openid與touser:event.openid的相同,將第一步獲取的openid復(fù)制
openid:"oMKZh4zYY0j7X-DYLAQa2ngdlpfg"
}
}).then(res=>{
console.log("發(fā)送一條信息成功-----",res)
}).catch(res=>{
console.log("發(fā)送一條信息失敗-----",res)
})
}
5.其他問(wèn)題:
-
模板ID中的參數(shù)名莫錯(cuò),模板ID莫復(fù)制錯(cuò)
-
手機(jī)未收到消息去控制臺(tái)找錯(cuò)來(lái)源:
![[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來(lái)直接上傳文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-483306.html
errMsg會(huì)出現(xiàn)40003(openid有誤)等等的錯(cuò)誤,可以去官方文檔的社區(qū)中,將錯(cuò)誤信息或者錯(cuò)誤代碼復(fù)制,看他人如何解決或者官方文檔提示的錯(cuò)誤文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-483306.html
- 授權(quán)一次只能發(fā)送一次消息
到了這里,關(guān)于微信小程序----API、獲取openid、消息訂閱的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!