1、微信小程序开发六微信小程序开发(六)第六章 APIAPI框架总览(绘图API专列章学习,暂略)1Wx.navigateTo7Wx.hideKeyBoard2Wx.navigateBack8Wx.stopPulldownFresh3Wx.directTo9Wx.setNavigationBarTitle4Wx.createAnimation10wx.showNavigationBarLoading5Wx.createContext11wx.hideNavigationBarLoading6Wx.drawCanvas第1节 网络API1. 发送请求:wx.request(object)+数据请求W
2、x.request( url:url, data:object, method:get|post, dataType:json, header:content-type:application/json, success:function(res)var data=res.data, fail:function(), complate:function()2. 上传下载:wx.upload(object)|wx.download(object)+下载一段音乐播放Wx.downloadFile( url:url, type:audio, success:function(res) wx.play
3、Voice(filePath:res.tmpFilePath)3. 嵌套字:+连接Wx.connectSocket(url:url,data:data,header:,method:get|post)+打开Wx.onSocketOpen(function(res)+出错Wx.onSocketError(function(res)+发数据Wx.sendSocketMessage(data:msg)+发消息(回调)Wx.onSocketMessage(function(res)+关闭Wx.closeSocket(url:url)+关闭(回调)Wx.onSocketClose(function(re
4、s)第2节 媒体API1. 图片:+选图Wx.chooseImage(count:1,sizetype:,sourceType:album,camera,success:function(res)+预览Wx.previewImage(current:,urls:)2. 录音:+录音Wx.startRecord(success:function(),fail:function()+停止Wx.stopRecord()3. 音频控制:+播放Wx.playVoice(filePath:xxxx,comlete:function()+暂停Wx.pauseVoice()+停止Wx.stopVoice()4
5、. 音乐控制:+获状态Wx.getBackgroundAudioPlayState(success:function(res)res.status|dataUrl|currentPosition|duration)+播放Wx.playBackgroundAudio(dataUrl:xx,title:xx,coverImgUrl:xx)+暂停Wx.pauseBackgroundAudio()+定位Wx.seekBackgroundAudio(position:60)+停止Wx.stopBackgroundAudion()5. 视频:+逻辑层Page( Data:src:xxx, getVideo
6、:function() var that=this; wx.chooseVideo( sourceType:, success:function(res) that.setData( src:res.tempfilePath)6. 音频组件控制:+创建上下文Var ctx=wx.createAudioContext(audioId)+播放Ctx.paly()+暂停Ctx.pause()+进到Ctx.seek(n)+从头Ctx.seek(0)+停止Ctx.stop()7. 视频组件控制:+创建上下文Var ctx=wx.createVedioContext(videoId)+发送弹幕Ctx.se
7、ndDanmu(text:xxxx,color:#xxxxxx)8. 文件:+保存文件Wx.saveFile(tempFilePath:xxx,success:function(res)+获已保存文件列表Wx.getSavedFileList(success:function(res)var list=res.fileList)+获已保存文件信息Wx.getSavedFileInfo(filePath:xxx,success:function(res)+移除已保存文件Wx.removeSavedFile(filePath:xxx,complete:function(res)+打开文件Wx.op
8、enDocument(filePath:xxx,success:function(res)第3节 数据API+设置数据Wx.setStorage(key:xxx,value:xxx)+获取数据Wx.getStorage(key:xxx,success:function(res)+清空数据Wx.clearStorage()+设;置数据(异步)Wx.setStorageSync(key:xxx,value:xxx)+获取数据(异步)Wx.getStorageSync(key:xxx,success:function(res)+清空数据(异步)Wx.clearStorageSync()第4节 位置A
9、PI1. 位置:+获取位置Wx.getLocation(type:xxx,success:function(res)+打开位置Wx.openLocation(latitude:xx,longItude:xx,scale:xx)+选择位置Wx.chooseLocation(success:function(res)2. 地图组件:+创建上下文Var ctx=wx.createMapContext(mapId)+中心打开Ctx.openCenterLocation(success:function(res)+移动位置Ctx.moveToLocation(success:function(res)第
10、5节 设备API1. 系统信息:+同步获取信息wx.getSystemInfo( success: function(res) console.log(res.model) console.log(res.pixelRatio) console.log(res.windowWidth) console.log(res.windowHeight) console.log(res.language) console.log(res.version) console.log(res.platform) )+异步获取信息wx.getSystemInfo(success:function(res)2.
11、网络类型:+网络类型wx.getNetworkType( success: function(res) var networkType = workType / 返回网络类型2g,3g,4g,wifi, none, unknown )3. 重力感应:+重力感应wx.onAccelerometerChange(function(res) console.log(res.x) console.log(res.y) console.log(res.z)4. 罗盘指南:+罗盘指南wx.onCompassChange(function (res) console.log(res.direction)5.
12、 拨打电话:+拨打电话wx.makePhoneCall( phoneNumber: )6. 扫码:+扫码wx.scanCode( success: function(res) console.log(res) )7. 剪贴板:+加入剪贴板Wx.setClipboadData(data:xx,success:function(res)+读取剪贴板Wx.getClipboardData(success:function(res)var data=res.data)8. 篮牙:+篮牙wx.openBluetoothAdapter( success: function (res) console.lo
13、g(res) )第6节 窗口API1. 互动操作:+自消窗口Wx.showToast(title:xx,icon:xx,duration:xx)+对话框Wx.showDialog(title:xx)+对话框(模态)Wx.showModal(title:xx,content:xx,success:function(res)+活动页签Wx.showActionSheet(itemList:,success:function(res)2. 导航设置:+设标题Wx.setNavigationBarTitle(title:xxx)+显动画Wx.showNavigationBarLoading()+隐动画
14、Wx.hideVavigationBarLoading()3. 导航操控:+转到Wx.vavigatorTo(url:xx,success:function(res)+重定向Wx.redirectTo(url:xx;success:funtion(res)+重载Wx.reLaunch(url:xx,success:function(res)+切页Wx.switchTab(url:xxx)+返回Wx.navigatorBack(delta:xx)4. 动画操作:+创建动画Var ani=wx.createAnimation( transformOrign:xx, duration:xx, tim
15、ingFunction:xx)+具体方法(略)第7节 绘图API详述于第七章.第8节 开放接口API1. 小程序登录API=登录微信:+登录微信Wx.login(success:function()+检查会话Wx.checkSession(success:function(),fail:function()=签名验证|加密解密为了确保开放接口返回用户数据的安全性,微信会对明文数据进行签名。开发者可以根据业务需要对数据包进行签名校验,确保数据的完整性。签名校验算法涉及用户的session_key,通过 wx.login 登录流程获取用户session_key,并自行维护与应用自身登录态的对应关系
16、。通过调用接口(如 wx.getUserInfo)获取数据时,接口会同时返回 rawData、signature,其中 signature = sha1( rawData + session_key )开发者将 signature、rawData 发送到开发者服务器进行校验。服务器利用用户对应的 session_key 使用相同的算法计算出签名 signature2 ,比对 signature 与 signature2 即可校验数据的完整性。2. 用户信息wx.getUserInfo( success: function(res) var userInfo = res.userInfo var
17、 nickName = userInfo.nickName var avatarUrl = userInfo.avatarUrl var gender = userInfo.gender /性别 0:未知、1:男、2:女 var province = userInfo.province var city = userInfo.city var country = userInfo.country )3. 微信支付wx.requestPayment( timeStamp: , nonceStr: , package: , signType: MD5, paySign: , success:fun
18、ction(res) , fail:function(res) )4. 模板信息不作讲解5. 客服操作API=接收消息和事件-进入客服:-客服消息转发server:response-消息类型:文本,图片,事件-格式类型:xml,json-公共字段:ToUserName,FromUserName,CreateTime,MsgType,-专有字段:文本:Content,MsgId图片:PicUrl,MsgId事件:Event,SessionFrom=发送客服消息http请求方式: POST.com/cgi-bin/message/custom/send?access_token=ACCESS_TO
19、KEN=临时素材接口 -新增临时素材Curl -F media=test.jpg .com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE -获取临时素材.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID=接入指导(消息服务器接入) Step1:埴写服务器配置: Step2:服务器验证(PHP)private function checkSignature() $signature = $_GETsignature; $timestamp = $_
20、GETtimestamp; $nonce = $_GETnonce; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr = $signature ) return true; else return false; Step3:实现服务功能6. 分享功能+设定分享Page( onShareAppMessage:function() r
21、eturn: title:xx,path:xx, success:function(res)/end return/end function)+显示分享Wx.showShareMenu()+隐藏分享Wx.hideShareMenu()7. 二维码通过后台接口可以获取小程序任意页面的二维码,扫描该二维码可以直接进入小程序对应的页面接口地址:.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN8. 收货地址wx.chooseAddress( success: function (res) console.log(res.user
22、Name) console.log(res.postalCode) console.log(res.provinceName) console.log(res.cityName) console.log(res.countyName) console.log(res.detailInfo) console.log(res.nationalCode) console.log(res.telNumber) )9. 卡券+加载卡券Wx.addCard(cardList:,success:function(res)+打开卡券Wx.openCard(cardList:,success:function(res)+card结构code:xx,openId:xx,timeStamp:xx,signature:xx10. 设置+打开设置Wx.openSetting(success:function(res)第9节 拓展接口API数据转换1 数据缓冲转为BASE64:Wx.arrayBufferToBase64(arrayBuffer)2 BASE64转为数据缓冲:Wx.base64ToArrayBuffer(base64)说明此章中没有列举绘图API,在第七章中专门学习.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1