![【移动应用开发技术】如何使微信小程序支持async await_第1页](http://file4.renrendoc.com/view/caea88dbaa994108923ff2c7000deb69/caea88dbaa994108923ff2c7000deb691.gif)
![【移动应用开发技术】如何使微信小程序支持async await_第2页](http://file4.renrendoc.com/view/caea88dbaa994108923ff2c7000deb69/caea88dbaa994108923ff2c7000deb692.gif)
![【移动应用开发技术】如何使微信小程序支持async await_第3页](http://file4.renrendoc.com/view/caea88dbaa994108923ff2c7000deb69/caea88dbaa994108923ff2c7000deb693.gif)
![【移动应用开发技术】如何使微信小程序支持async await_第4页](http://file4.renrendoc.com/view/caea88dbaa994108923ff2c7000deb69/caea88dbaa994108923ff2c7000deb694.gif)
![【移动应用开发技术】如何使微信小程序支持async await_第5页](http://file4.renrendoc.com/view/caea88dbaa994108923ff2c7000deb69/caea88dbaa994108923ff2c7000deb695.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】如何使微信小程序支持asyncawait
解决办法:import
regeneratorRuntime
from
'./lib/runtime'
App({
...
regeneratorRuntime,
onLaunch(){},
onShow()
{},
onHide()
{},
...
})const
METHOD
=
{
GET:
'GET',
POST:
'POST',
PUT:
'PUT',
DELETE:
'DELETE'
}
let
baseUrl
=
''
const
interceptors
=
[]
class
Request
{
/**
*
response
interceptor
*/
intercept(res,
resolve,
reject)
{
return
interceptors
.filter((f)
=>
typeof
f
===
'function')
.every((f)
=>
f(res,
resolve,
reject))
}
/**
*
request
*/
request(option)
{
const
header
=
{
'content-type':
'application/x-www-form-urlencoded'
}
const
{
url,
method,
data
=
{}
}
=
option
return
new
Promise((resolve,
reject)
=>
{
try
{
wx.request({
url:
baseUrl
+
url,
method:
method
||
METHOD.GET,
data,
header,
success:
(res)
=>
ercept(res,
resolve,
reject),
fail:
(err)
=>
{
if
(
err
&&
err.errMsg
&&
err.errMsg.indexOf('request:fail')
!==
-1
)
{
console.error('wx
request
error:
',
err)
}
else
{
wx.showToast({
title:
JSON.stringify(err),
icon:
'none',
duration:
10000
})
}
}
})
}
catch
(err)
{
wx.showToast({
title:
err.message,
icon:
'none'
})
}
})
}
get(url,
data)
{
return
this.request({
url,
method:
METHOD.GET,
data
})
}
post(url,
data)
{
return
this.request({
url,
method:
METHOD.POST,
data
})
}
put(url,
data)
{
return
this.request({
url,
method:
METHOD.PUT,
data
})
}
delete(url,
data)
{
return
this.request({
url,
method:
METHOD.DELETE,
data
})
}
all(tasks)
{
return
Promise.all(tasks)
}
}
/**
*
设置base
URL
*/
function
setBaseUrl(url)
{
baseUrl
=
url
}
/**
*
默认response拦截器
*/
function
addDefaultInterceptor()
{
interceptors.push((res,
resolve,
reject)
=>
{
const
status
=
res.statusCode
if
(status
!==
200)
{
return
reject(new
Error(`internet
error:
${status}`))
}
const
body
=
res.data
if
(body.errno
!==
0)
{
return
reject({
message:
body.error,
body
})
}
return
resolve(body.data)
})
}
const
request
=
new
Request()
export
{
setBaseUrl,
addDefaultInterceptor,
request
}import
{
request,
setBaseUrl,
addDefaultInterceptor
}
from
'./lib/request'
addDefaultInterceptor()
App({
...
async
onLaunch()
{
const
userInfo
=
await
request.get('/user/info')
console.log(userInfo)
}
...
})小程序原生api使用asyncawait/**
*
promise微信API方法
*/
function
wxPromise(api)
{
function
func(options,
...params)
{
return
new
Promise((resolve,
reject)
=>
{
api(
Object.assign({},
options,
{
success:
(res)
=>
{
resolve(res)
},
fail:
reject
}),
...params
)
})
}
return
func
}
export
default
{
//
界面交互
showToast:
wxPromise(wx.showToast),
showLoading:
wxPromise(wx.showLoading),
showModal:
wxPromise(wx.showModal),
showActionSheet:
wxPromise(wx.showActionSheet),
//
导航条
setNavigationBarTitle:
wxPromise(wx.setNavigationBarTitle),
setNavigationBarColor:
wxPromise(wx.setNavigationBarColor),
setTopBarText:
wxPromise(wx.setTopBarText),
//
导航
navigateTo:
wxPromise(wx.navigateTo),
redirectTo:
wxPromise(wx.redirectTo),
switchTab:
wxPromise(wx.switchTab),
reLaunch:
wxPromise(wx.reLaunch),
//
用户相关
login:
wxPromise(wx.login),
checkSession:
wxPromise(wx.checkSession),
authorize:
wxPromise(wx.authorize),
getUserInfo:
wxPromise(wx.getUserInfo),
//
支付
requestPayment:
wxPromise(wx.requestPayment),
//
图片
chooseImage:
wxPromise(wx.chooseImage),
previewImage:
wxPromise(wx.previewImage),
getImageInfo:
wxPromise(wx.getImageInfo),
saveImageToPhotosAlbum:
wxPromise(wx.saveImageToPhotosAlbum),
//
文件
uploadFile:
wxPromise(wx.uploadFile),
downloadFile:
wxPromise(wx.downloadFile),
//
录音
startRecord:
wxPromise(wx.startRecord),
//
音频播放
playVoice:
wxPromise(wx.playVoice),
//
音乐播放
getBackgroundAudioPlayerState:
wxPromise(wx.getBackgroundAudioPlayerState),
playBackgroundAudio:
wxPromise(wx.playBackgroundAudio),
seekBackgroundAudio:
wxPromise(wx.seekBackgroundAudio),
//
视频
chooseVideo:
wxPromise(wx.chooseVideo),
saveVideoToPhotosAlbum:
wxPromise(wx.saveVideoToPhotosAlbum),
//
文件
saveFile:
wxPromise(wx.saveFile),
getFileInfo:
wxPromise(wx.getFileInfo),
getSavedFileList:
wxPromise(wx.getSavedFileList),
getSavedFileInfo:
wxPromise(wx.getSavedFileInfo),
removeSavedFile:
wxPromise(wx.removeSavedFile),
openDocument:
wxPromise(wx.openDocument),
//
数据缓存
setStorage:
wxPromise(wx.setStorage),
getStorage:
wxPromise(wx.getStorage),
getStorageInfo:
wxPromise(wx.getStorageInfo),
removeStorage:
wxPromise(wx.removeStorage),
//
位置
getLocation:
wxPromise(wx.getLocation),
chooseLocation:
wxPromise(wx.chooseLocation),
openLocation:
wxPromise(wx.openLocation),
//
设备
getSystemInfo:
wxPromise(wx.getSystemInfo),
getNetworkType:
wxPromise(wx.getNetworkType),
startAccelerometer:
wxPromise(wx.startAccelerometer),
stopAccelerometer:
wxPromise(wx.stopAccelerometer),
startCompass:
wxPromise(wx.startCompass),
stopCompass:
wxPromise(wx.stopCompass),
//
打电话
makePhoneCall:
wxPromise(wx.makePhoneCall),
//
扫码
scanCode:
wxPromise(wx.scanCode),
//
剪切板
setClipboardData:
wxPromise(wx.setClipboardData),
getClipboardData:
wxPromise(wx.getClipboardData),
//
蓝牙
openBluetoothAdapter:
wxPromise(wx.openBluetoothAdapter),
closeBluetoothAdapter:
wxPromise(wx.closeBluetoothAdapter),
getBluetoothAdapterState:
wxPromise(wx.getBluetoothAdapterState),
startBluetoothDevicesDiscovery:
wxPromise(wx.startBluetoothDevicesDiscovery),
stopBluetoothDevicesDiscovery:
wxPromise(wx.stopBluetoothDevicesDiscovery),
getBluetoothDevices:
wxPromise(wx.getBluetoothDevices),
getConnectedBluetoothDevices:
wxPromise(wx.getConnectedBluetoothDevices),
createBLEConnection:
wxPromise(wx.createBLEConnection),
closeBLEConnection:
wxPromise(wx.closeBLEConnection),
getBLEDeviceServices:
wxPromise(wx.getBLEDeviceServices),
//
iBeacon
startBeaconDiscovery:
wxPromise(wx.startBeaconDiscovery),
stopBeaconDiscovery:
wxPromise(wx.stopBeaconDiscovery),
getBeacons:
wxPromise(wx.getBeacons),
//
屏幕亮度
setScreenBrightness:
wxPromise(wx.setScreenBrightness),
getScreenBrightness:
wxPromise(wx.getScreenBrightness),
setKeepScreenOn:
wxPromise(wx.setKeepScreenOn),
//
振动
vibrateLong:
wxPromise(wx.vibrateLong),
vibrateShort:
wxPromise(wx.vibrateShort),
//
联系人
addPhoneContact:
wxPromise(wx.addPhoneContact),
//
NFC
getHCEState:
wxPromise(wx.getHCEState),
startHCE:
wxPromise(wx.startHCE),
stopHCE:
wxPromise(wx.stopHCE),
sendHCEMessage:
wxPromise(wx.sendHCEMessage),
//
Wi-Fi
startWifi:
wxPromise(wx.startWifi),
stopWifi:
wxPromise(wx.stopWifi),
connectWifi:
wxPromise(wx.connectWifi),
getWifiList:
wxPromise(wx.getWifiList),
setWifiList:
wxPromise(wx.setWifiList),
getConnectedWifi:
wxPromise(wx.getConnectedWifi),
//
第三方平台
getExtConfig:
wxPromise(wx.getExtConfig),
//
转发
showShareMenu:
wxPromise(wx.showShareMenu),
hideShareMenu:
wxPromise(wx.hideShareMenu),
updateShareMenu:
wxPromise(wx.updateShareMenu),
getShareInfo:
wxPromise(wx.getShareInfo),
//
收货地址
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 卫生洁具市场细分领域开发策略与零售商市场布局规划考核试卷
- 医疗设备租赁国际市场开发考核试卷
- 2025-2030年国际美食节坚果展台行业深度调研及发展战略咨询报告
- 2025-2030年复古风格男士牛仔裤系列行业深度调研及发展战略咨询报告
- 2025-2030年文化用品艺术展览行业深度调研及发展战略咨询报告
- 2025-2030年文化用品跨界合展行业深度调研及发展战略咨询报告
- 2025-2030年户外攀岩塔行业跨境出海战略研究报告
- 2025年度农家乐资产转租合同范本(含租金支付条款)
- 2025-2030年可变换造型首饰行业跨境出海战略研究报告
- 2025-2030年户外溯溪凉鞋设计行业跨境出海战略研究报告
- 成都四川成都简阳市简城街道便民服务和智慧蓉城运行中心招聘综治巡防队员10人笔试历年参考题库附带答案详解
- 2025-2030全球废弃食用油 (UCO) 转化为可持续航空燃料 (SAF) 的催化剂行业调研及趋势分析报告
- 山东省临沂市兰山区2024-2025学年七年级上学期期末考试生物试卷(含答案)
- 湖北省武汉市2024-2025学年度高三元月调考英语试题(含答案无听力音频有听力原文)
- 商务星球版地理八年级下册全册教案
- 天津市河西区2024-2025学年四年级(上)期末语文试卷(含答案)
- 2025年空白离婚协议书
- 校长在行政会上总结讲话结合新课标精神给学校管理提出3点建议
- 北京市北京四中2025届高三第四次模拟考试英语试卷含解析
- 2024年快递行业无人机物流运输合同范本及法规遵循3篇
- 2025年护理质量与安全管理工作计划
评论
0/150
提交评论