diff --git a/types/weapp-api/index.d.ts b/types/weapp-api/index.d.ts index f22ed51765..6c2b4d8ec0 100644 --- a/types/weapp-api/index.d.ts +++ b/types/weapp-api/index.d.ts @@ -1,9 +1,12 @@ // Type definitions for weapp // Project: https://mp.weixin.qq.com/debug/wxadoc/dev/index.html // Definitions by: vargeek +// pbestz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 declare namespace wx { + // import startPullDownRefresh = swan.startPullDownRefresh; type NoneParamCallback = () => void; type OneParamCallback = (data: any) => void; type ResponseCallback = (res: any) => void; @@ -37,25 +40,58 @@ declare namespace wx { [key: string]: any; } + interface referrerInfo { + /** 来源小程序、公众号或 App 的 appId */ + appId: string, + /** 来源小程序传过来的数据,scene=1037或1038时支持 */ + extraData: Object + } + + interface onLaunchOptions { + /** 启动小程序的路径 */ + path: string, + /** 启动小程序的场景值 */ + scene: number, + /** 启动小程序的query参数 */ + query: Object, + shareTicket: string, + /** 来源信息。从另一个小程序、公众号或App进入小程序时返回。范泽返回{} */ + referrerInfo: referrerInfo + } + + type onLaunchCallback = (options: onLaunchOptions) => void; + type onShowOptions = onLaunchOptions; + interface AppOptions { /** * 生命周期函数--监听小程序初始化 * 当小程序初始化完成时,会触发 onLaunch(全局只触发一次) */ - onLaunch?: NoneParamCallback; + onLaunch?: onLaunchCallback; /** * 生命周期函数--监听小程序显示 * 当小程序启动,或从后台进入前台显示,会触发 onShow */ - onShow?: NoneParamCallback; + onShow?: (options: onShowOptions) => void; /** * 生命周期函数--监听小程序隐藏 * 当小程序从前台进入后台,会触发 onHide */ onHide?: NoneParamCallback; + /** 小程序发生脚本错误或 API 调用报错时触发。也可以使用 wx.onError 绑定监听。*/ + onError?: ErrorCallback; + /** + * 小程序要打开的页面不存在时触发 + * 1. 开发者可以在回调中进行页面重定向,但必须在回调中同步处理,异步处理(例如 setTimeout 异步执行)无效。 + * 2. 若开发者没有调用 wx.onPageNotFound 绑定监听,也没有声明 App.onPageNotFound,当跳转页面不存在时,将推入微信客户端原生的页面不存在提示页面。 + * 3. 如果回调中又重定向到另一个不存在的页面,将推入微信客户端原生的页面不存在提示页面,并且不再第二次回调。 + */ + onPageNotFound?: NoneParamCallback; [key: string]: any } + // 网络 + // 发起请求 interface RequestHeader { [key: string]: string; } @@ -78,9 +114,79 @@ declare namespace wx { /** * wx.request发起的是https请求。一个微信小程序,同时只能有5个网络请求连接。 */ - function request(options: RequestOptions): void; + function request(options: RequestOptions): RequestTask; + /** + * 网络请求任务对象 + */ + interface RequestTask { + // 中断请求任务 + abort(): void; + // 监听 HTTP Response Header 事件。会比请求完成事件更早 + onHeadersReceived(callback: DataResponseCallback): void; + // 取消监听 HTTP Response Header 事件 + offHeadersReceived(callback: DataResponseCallback): void; + } + // 下载 + interface OnProgressCallbackOptions { + // 下载进度百分比 + progress: number; + // 已经下载的数据长度,单位 Bytes + totalBytesWritten: number; + // 预期需要下载的数据总长度,单位 Bytes + totalBytesExpectedToWrite: number; + } + /** + * 一个可以监听下载进度变化事件,以及取消下载任务的对象 + */ + interface DownloadTask { + // 中断下载任务 + abort(): void; + // 下载进度变化事件的回调函数 + onProgressUpdate(callback: (res: OnProgressCallbackOptions) => {}): void; + // 取消监听下载进度变化事件 + offProgressUpdate(callback: (res: OnProgressCallbackOptions) => {}): void; + // 监听 HTTP Response Header 事件。会比请求完成事件更早 + onHeadersReceived(callback: DataResponseCallback): void; + // 取消监听 HTTP Response Header 事件 + offHeadersReceived(callback: DataResponseCallback): void; + } + interface DownloadFileOptions { + /** 下载资源的 url */ + url: string; + /** 下载资源的类型,用于客户端识别处理,有效值:image/audio/video */ + type?: string; + /** HTTP 请求 Header */ + header?: RequestHeader; + /** 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: '文件的临时路径'} */ + success?: TempFileResponseCallback; + /** 接口调用失败的回调函数 */ + fail?: ResponseCallback; + /** 接口调用结束的回调函数(调用成功、失败都会执行) */ + complete?: ResponseCallback; + } + /** + * 下载文件资源到本地。客户端直接发起一个 HTTP GET 请求, + * 把下载到的资源根据 type 进行处理,并返回文件的本地临时路径。 + */ + function downloadFile(options: DownloadFileOptions): DownloadTask; + // 上传 + /** + * 一个可以监听上传进度变化事件,以及取消上传任务的对象 + */ + interface UploadTask { + // 中断上传任务 + abort(): void; + // 上传进度变化事件的回调函数 + onProgressUpdate(callback: (res: OnProgressCallbackOptions) => {}): void; + // 取消监听上传进度变化事件 + offProgressUpdate(callback: (res: OnProgressCallbackOptions) => {}): void; + // 监听 HTTP Response Header 事件。会比请求完成事件更早 + onHeadersReceived(callback: DataResponseCallback): void; + // 取消监听 HTTP Response Header 事件 + offHeadersReceived(callback: DataResponseCallback): void; + } interface UploadFileOptions { /** 开发者服务器 url */ url: string; @@ -106,30 +212,9 @@ declare namespace wx { * 客户端发起一个 HTTPS POST 请求, * 其中 Content-Type 为 multipart/form-data 。 */ - function uploadFile(options: UploadFileOptions): void; - - - interface DownloadFileOptions { - /** 下载资源的 url */ - url: string; - /** 下载资源的类型,用于客户端识别处理,有效值:image/audio/video */ - type?: string; - /** HTTP 请求 Header */ - header?: RequestHeader; - /** 下载成功后以 tempFilePath 的形式传给页面,res = {tempFilePath: '文件的临时路径'} */ - success?: TempFileResponseCallback; - /** 接口调用失败的回调函数 */ - fail?: ResponseCallback; - /** 接口调用结束的回调函数(调用成功、失败都会执行) */ - complete?: ResponseCallback; - } - /** - * 下载文件资源到本地。客户端直接发起一个 HTTP GET 请求, - * 把下载到的资源根据 type 进行处理,并返回文件的本地临时路径。 - */ - function downloadFile(options: DownloadFileOptions): void; - + function uploadFile(options: UploadFileOptions): UploadTask; + // WebSocket interface ConnectSocketOptions { /** 开发者服务器接口地址,必须是 HTTPS 协议,且域名必须是后台配置的合法域名 */ url: string; @@ -409,6 +494,29 @@ declare namespace wx { */ function chooseVideo(options: ChooseVideoOptions): void; + // 数据缓存 + interface StorageInfo { + // 当前 storage 中所有的 key + keys: Array; + // 当前占用的空间大小, 单位 KB + currentSize: number; + // 限制的空间大小,单位 KB + limitSize: number; + } + type StorageInfoCallback = (res: StorageInfoOptions) => void; + interface StorageInfoOptions extends CommonCallbackOptions{ + success?: StorageInfoCallback; + } + /** + * getStorageInfo的同步版本 + */ + function getStorageInfoSync(): StorageInfo; + /** + * 异步获取当前storage的相关信息 + * @param options + */ + function getStorageInfo(options: StorageInfoOptions): void; + interface SetStorageOptions { /** 本地缓存中的指定的 key */ key: string; @@ -467,6 +575,64 @@ declare namespace wx { */ function clearStorageSync(): void; + // 媒体 + // 地图 + interface LocationBaseOptions { + // 纬度,浮点数,范围为-90~90,负数表示南纬 + latitude: number; + // 经度,浮点数,范围为-180~180,负数表示西经 + longitude: number; + } + interface GetCenterLocationSuccCbOptions extends CommonCallbackOptions{ + success(res: LocationBaseOptions): void; + } + interface translateMarkerOptions extends CommonCallbackOptions{ + // 指定 marker + markerId: number; + // 指定 marker 移动到的目标点 + destination: LocationBaseOptions; + // 移动过程中是否自动旋转 marker + autoRotate: boolean; + // marker 的旋转角度 + rotate: number; + // 动画持续时长,平移与旋转分别计算 + duration?: number; + // 动画结束回调函数 + animationEnd?(): void; + } + interface zoomPointsOptions extends CommonCallbackOptions { + // 要显示在可视区域内的坐标点列表 + points: Array; + // 坐标点形成的矩形边缘到地图边缘的距离,单位像素。格式为[上,右,下,左],安卓上只能识别数组第一项,上下左右的padding一致。开发者工具暂不支持padding参数。 + padding?: Array; + } + interface GetReginSuccessCallbackOptions { + // 西南角经纬度 + southwest: number, + // 东北角经纬度 + northeast: number; + } + interface GetReginOptions extends CommonCallbackOptions{ + success?(callback: (res: GetReginSuccessCallbackOptions) => void): void; + } + interface GetScaleOptions extends CommonCallbackOptions{ + success?(callback: (res: {scale: number}) => void): void; + } + interface MapContext { + // 获取当前地图中心的经纬度。返回的是 gcj02 坐标系,可以用于 wx.openLocation() + getCenterLocation(options: GetCenterLocationSuccCbOptions): void; + // 将地图中心移动到当前定位点。需要配合map组件的show-location使用 + moveToLocation(): void; + // 平移marker,带动画 + translateMarker(options: translateMarkerOptions): void; + // 缩放视野展示所有经纬度 + includePoints(options: zoomPointsOptions): void; + // 获取当前地图的视野范围 + getRegion(options: GetReginOptions): void; + // 获取当前地图的缩放级别 + getScale(options: GetScaleOptions): void; + } + interface LocationData { /** 纬度,浮点数,范围为-90~90,负数表示南纬 */ latitude: number; @@ -561,6 +727,362 @@ declare namespace wx { */ function getSystemInfo(options: GetSystemInfoOptions): void; + interface UpdateManager { + /** + * 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。 + */ + applyUpdate(callback: DataResponseCallback): void; + /** + * 监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。 + */ + onCheckForUpdate(): void; + /** + * 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 + */ + onUpdateReady(callback: NoneParamCallback): void; + /** + * 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 + */ + onUpdateFailed(callback: NoneParamCallback): void; + } + /** + * 获取全局唯一的版本更新管理器,用于管理小程序更新。关于小程序的更新机制,可以查看运行机制文档。 + */ + function getUpdateManager(): UpdateManager; + + /** + * 获取小程序启动时的参数。与 App.onLaunch 的回调参数一致。 + */ + function getLaunchOptionsSync(): onLaunchCallback; + + // 应用级事件 + /** + * 取消监听小程序要打开的页面不存在事件 + */ + function offPageNotFound(): NoneParamCallback; + /** + * 监听小程序要打开的页面不存在事件。该事件与 App.onPageNotFound 的回调时机一致。 + */ + function onPageNotFound(): NoneParamCallback; + /** + * 取消监听小程序错误事件。 + */ + function offError(): NoneParamCallback; + /** + * 监听小程序错误事件。如脚本错误或 API 调用报错等。该事件与 App.onError 的回调时机与参数一致。 + */ + function onError(): ErrorCallback; + /** + * 取消监听小程序切前台事件 + */ + function offAppShow(): NoneParamCallback; + /** + * 监听小程序切前台事件。该事件与 App.onShow 的回调参数一致。 + * @param callback + */ + function onAppShow(callback: onShowOptions): void; + /** + * 取消监听小程序切后台事件 + */ + function offAppHide(): NoneParamCallback; + /** + * 监听小程序切后台事件。该事件与 App.onHide 的回调时机一致。 + */ + function onAppHide(): NoneParamCallback; + + // 调试 TODO + + // 路由 + interface routerOptions extends CommonCallbackOptions{ + // 需要跳转的应用内非 tabBar 的页面的路径, 路径后可以带参数。参数与路径之间使用 ? 分隔,参数键与参数值用 = 相连,不同参数用 & 分隔;如 'path?key=value&key2=value2' + url: string; + } + interface NavigateBackOptions extends CommonCallbackOptions { + // 返回的页面数,如果 delta 大于现有页面数,则返回到首页。 + delta: number; + } + /** + * 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。 + */ + function navigateBack(options: NavigateBackOptions): void; + /** + * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面 + * @param options + */ + function switchTab(options: routerOptions): void; + /** + * 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 wx.navigateBack 可以返回到原页面。 + * @param options + */ + function navigateTo(options: routerOptions): void; + /** + * 关闭所有页面,打开到应用内的某个页面 + */ + function reLaunch(options: routerOptions): void; + /** + * 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。 + */ + function redirectTo(options: routerOptions): void; + + // 界面 + // 交互 + // tapIndex为用户点击的按钮序号,从上到下的顺序,从0开始 + type ActionSheetSuccessCallback = (res: {tapIndex: number}) => void; + + interface ActionSheetOptions { + // 必填,按钮的文字数组,数组长度最大为 6 + itemList: Array; + // 按钮的文字颜色 + itemColor?: string; + success?: ActionSheetSuccessCallback; + fail?: ResponseCallback; + complete?: ResponseCallback; + } + + /** + * 公共回调函数 + */ + interface CommonCallbackOptions { + // 接口调用成功回调函数 + success?: ResponseCallback; + // 接口调用失败回调函数 + fail?: ResponseCallback; + // 接口调用结束的回调函数 + complete?: ResponseCallback; + } + + interface LoadingOptions extends CommonCallbackOptions{ + // must,提示的内容 + title: string; + // 默认false。是否显示透明蒙层,防止触摸穿透 + mask: boolean; + } + + type icon = 'success' | 'loading' | 'none'; + interface ToastOptions extends CommonCallbackOptions{ + // 提示的内容 + title: string; + // 图标,默认值'success' + icon?: icon; + // 自定义图标的本地路径,image 的优先级高于 icon + imgage?: string; + // 提示的延迟时间,默认值1500ms + duration?: number; + // 是否显示透明蒙层,防止触摸穿透,默认值false + mask: boolean; + } + + interface ModalOptions extends CommonCallbackOptions{ + // 提示的内容 + title: string; + // 提示的内容 + content: string; + // 是否显示取消按钮,默认值true + showCancel?: boolean; + // 取消按钮的文字,最多 4 个字符,默认值'取消' + cancelText?: string; + // 取消按钮的文字颜色,必须是 16 进制格式的颜色字符串,默认值'#000000' + cancelColor?: string; + // 确认按钮的文字,最多 4 个字符 + confirmText?: string; + // 确认按钮的文字颜色,必须是 16 进制格式的颜色字符串,默认值'#3cc51f' + confirmColor?: boolean; + } + /** + * 显示操作菜单 + */ + function showActionSheet(options: ActionSheetOptions): void; + /** + * 隐藏 loading 提示框 + * @param options + */ + function hideLoading(options?: CommonCallbackOptions): void; + /** + * 显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框 + * @param options + */ + function showLoading(options: LoadingOptions): void; + /** + * 隐藏消息提示框 + * @param options + */ + function hideToast(options?: CommonCallbackOptions): void; + /** + * 显示消息提示框 + * @param options + */ + function showToast(options: ToastOptions): void; + /** + * 显示模态对话框 + * @param options + */ + function showModal(options: ModalOptions): void; + + + interface NavigationBarColorAnimationOptions { + // 动画变化时间,单位 ms,默认0 + animation?: number; + // 动画变化方式.动画从头到尾的速度是相同的,动画以低速开始,动画以低速结束,动画以低速开始和结束 + timingFunc?: 'linear' | 'easeIn' | 'easeOut' | 'easeInOut' + } + interface NavigationBarColorOptions extends CommonCallbackOptions{ + // 前景颜色值,包括按钮、标题、状态栏的颜色,仅支持 #ffffff 和 #000000 + frontColor: string; + // 背景颜色值,有效值为十六进制颜色 + backgroundColor: string; + // 动画效果 + animation: NavigationBarColorAnimationOptions; + } + interface NavigationBarTitleOptions extends CommonCallbackOptions{ + // 动态设置当前页面的标题 + title: string; + } + // 导航栏 + function setNavigationBarColor(): void; + /** + * 在当前页面隐藏导航条加载动画 + */ + function hideNavigationBarLoading(options?: CommonCallbackOptions): void; + /** + * 在当前页面显示导航条加载动画 + */ + function showNavigationBarLoading(options: CommonCallbackOptions): void; + /** + * 动态设置当前页面的标题 + * @param options + */ + function setNavigationBarTitle(options: NavigationBarTitleOptions): void; + + // 背景 + function setBackgroundTextStyle(): void; + interface BackgroundColorOptions extends CommonCallbackOptions{ + // 窗口的背景色,必须为十六进制颜色值 + backgroundColor?: string; + // 顶部窗口的背景色,必须为十六进制颜色值,仅 iOS 支持 + backgroundColorTop?: string; + // 底部窗口的背景色,必须为十六进制颜色值,仅 iOS 支持 + backgroundColorBottom?: string; + } + function setBackgroundColor(): void; + // Tab Bar + interface TabBarItemOptions extends CommonCallbackOptions{ + // tabBar 的哪一项,从左边算起 + index: number; + // tab 上的按钮文字 + text?: string; + // 图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px,当 postion 为 top 时,此参数无效,不支持网络图片 + iconPath?: string; + // 选中时的图片路径,icon 大小限制为 40kb,建议尺寸为 81px * 81px ,当 postion 为 top 时,此参数无效 + selectedIconPath?: string; + } + /** + * 动态设置 tabBar 某一项的内容 + * @param options + */ + function setTabBarItem(options: TabBarItemOptions): void; + + interface TabBarStyleOptions extends CommonCallbackOptions{ + // tab 上的文字默认颜色,HexColor + color: string; + // tab 上的文字选中时的颜色,HexColor + selectedColor: string; + // tab 的背景色,HexColor + backgroundColor: string; + // tabBar上边框的颜色, 仅支持 black/white + borderStyle: string; + } + /** + * 动态设置tabBar的整体样式 + */ + function setTabBarStyle(options: TabBarItemOptions): void; + + interface TabBarAnimationOptions extends CommonCallbackOptions{ + // 是否需要动画效果 + animation: boolean; + } + /** + * 隐藏tabBar + */ + function hideTabBar(options: TabBarAnimationOptions): void; + /** + * 显示tabBar + */ + function showTabBar(options: TabBarAnimationOptions): void; + + interface TabBarRedDotOptions extends CommonCallbackOptions{ + // tabBar 的哪一项,从左边算起 + index: number; + } + /** + * 隐藏 tabBar 某一项的右上角的红点 + * @param options + */ + function hideTabBarRedDot(options: TabBarBadgeOptions): void; + /** + * 显示 tabBar 某一项的右上角的红点 + * @param options + */ + function showTabBarRedDot(options: TabBarRedDotOptions): void; + + interface TabBarBadgeOptions extends CommonCallbackOptions{ + // tabBar 的哪一项,从左边算起 + index: number; + // 显示的文本,超过 4 个字符则显示成 ... + text: string; + } + /** + * 移除 tabBar 某一项右上角的文本 + * @param options + */ + function removeTabBarBadge(options: TabBarRedDotOptions): void; + /** + * 为 tabBar 某一项的右上角添加文本 + * @param options + */ + function setTabBarBadge(options: TabBarBadgeOptions): void; + + interface FontDescOptions { + // 字体样式,可选值为 normal / italic / oblique + style?: string; + // 字体粗细,可选值为 normal / bold / 100 / 200../ 900 + weight?: string; + // 设置小型大写字母的字体显示文本,可选值为 normal / small-caps / inherit + variant?: string; + } + // 字体 + interface FontFaceOptions extends CommonCallbackOptions{ + // 定义的字体名称 + family: string; + // 字体资源的地址。建议格式为 TTF 和 WOFF,WOFF2 在低版本的iOS上会不兼容。 + source: string; + // 可选的字体描述符 + desc?: FontDescOptions; + } + function loadFontFace(options: FontFaceOptions): void; + + // 下拉刷新 + /** + * 停止当前页面下拉刷新。 + */ + function stopPullDownRefresh(options?: CommonCallbackOptions): void; + + /** + * 开始下拉刷新。调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。 + */ + function startPullDownRefresh(options?: CommonCallbackOptions): void; + + // 滚动 + interface PageScrollToOptions extends CommonCallbackOptions{ + // 滚动到页面的目标位置,单位 px + scrollTop: number; + // 滚动动画的时长,单位 ms。默认300 + duration: number; + } + /** + * 将页面滚动到 + */ + function pageScrollTo(): void; + interface AccelerometerData { /** X 轴 */ x: number; @@ -606,46 +1128,9 @@ declare namespace wx { */ function hideNavigationBarLoading(): void; - interface NavigateToOptions { - /** 需要跳转的应用内页面的路径 */ - url: string; - /** 成功获取系统信息的回调 */ - success?: ResponseCallback; - /** 接口调用失败的回调函数 */ - fail?: ResponseCallback; - /** 接口调用结束的回调函数(调用成功、失败都会执行) */ - complete?: ResponseCallback; - } - /** - * 保留当前页面,跳转到应用内的某个页面,使用wx.navigateBack可以返回到原页面。 - * - * 注意:为了不让用户在使用小程序时造成困扰, - * 我们规定页面路径只能是五层,请尽量避免多层级的交互方式。 - */ - function navigateTo(options: NavigateToOptions): void; - - interface RedirectToOptions { - /** 需要跳转的应用内页面的路径 */ - url: string; - /** 成功获取系统信息的回调 */ - success?: ResponseCallback; - /** 接口调用失败的回调函数 */ - fail?: ResponseCallback; - /** 接口调用结束的回调函数(调用成功、失败都会执行) */ - complete?: ResponseCallback; - } - /** - * 关闭当前页面,跳转到应用内的某个页面。 - */ - function redirectTo(options: RedirectToOptions): void; - - /** - * 关闭当前页面,回退前一页面。 - */ - function navigateBack(): void; - type TimingFunction = 'linear' | 'ease' | 'ease-in' | 'ease-in-out' | 'ease-out' | 'step-start' | 'step-end'; + // 动画 interface CreateAnimationOptions { /** 动画持续时间,单位ms,默认值 400 */ duration?: number; @@ -1012,6 +1497,8 @@ declare namespace wx { */ function hideKeyboard(): void; + // 开放接口 + // 登录 interface LoginResponse { /** 调用结果 */ errMsg: string; @@ -1030,6 +1517,10 @@ declare namespace wx { complete?: ResponseCallback; } + /** + * 检查登录态是否过期。 + */ + function checkSession(options: CommonCallbackOptions): void; /** * 调用接口获取登录凭证(code)进而换取用户登录态信息, * 包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key)。 @@ -1037,6 +1528,51 @@ declare namespace wx { */ function login(option: LoginOptions): void; + type envVersion = 'develop' | 'trial' | 'release'; + interface NavigateToMiniProgramOptions extends CommonCallbackOptions { + // 要打开的小程序 appId + appId: string; + // 打开的页面路径,如果为空则打开首页 + path?: string; + // 需要传递给目标小程序的数据,目标小程序可在 App.onLaunch,App.onShow 中获取到这份数据。 + extraData?: object; + // 要打开的小程序版本。仅在当前小程序为开发版或体验版时此参数有效。如果当前小程序是正式版,则打开的小程序必定是正式版。 + envVersion?: envVersion; + } + /** + * 打开另一个小程序 + */ + function navigateToMiniProgram(options: NavigateToMiniProgramOptions): void; + interface NavigateBackMiniProgramOptions extends CommonCallbackOptions { + // 需要返回给上一个小程序的数据,上一个小程序可在 App.onShow 中获取到这份数据。 + extraData: object; + } + /** + * 返回到上一个小程序。只有在当前小程序是被其他小程序打开时可以调用成功 + */ + function navigateBackMiniProgram(options: NavigateBackMiniProgramOptions): void; + + // 帐号信息 + interface AccountInfo { + // 小程序帐号信息 + miniProgram: { + // 小程序appId + appId: string; + }; + // 插件帐号信息(仅在插件中调用时包含这一项) + Plugin: { + // 插件appId + appId: string; + // 插件版本号 + vetsion: string + }; + } + /** + * 获取当前账号信息 + */ + function getAccountInfoSync(): AccountInfo; + + // 帐号信息 interface UserInfo { nickName: string; avatarUrl: string; @@ -1087,11 +1623,63 @@ declare namespace wx { /** 接口调用结束的回调函数(调用成功、失败都会执行) */ complete?: ResponseCallback; } + // 支付 /** * 发起微信支付。 */ function requestPayment(options: RequestPaymentOptions): void; + + // 授权 + /** + * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html + * 用户信息 wx.getUserInfo、 + * 地理位置 wx.getLocation,wx.chooseLocation、 + * 通讯地址 wx.chooseAddress、 + * 发票抬头 wx.chooseInvoiceTitle、 + * 获取发票 wx.chooseInvoice、 + * 微信运动步数 wx.getWeRunData、 + * 录音功能 wx.startRecord、 + * 保存到相册 wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum、 + * 摄像头 组件 + */ + type Scope = 'userInfo' | 'userLocation' | 'address' | 'invoiceTitle' | 'invoice' | 'werun' | 'record' | 'writePhotosAlbum' | 'camera'; + interface AuthorizeOptions extends CommonCallbackOptions { + // 需要获取权限的 scope,详见 scope 列表 + scope: Scope; + } + /** + * 提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据,但不会实际调用对应接口。如果用户之前已经同意授权,则不会出现弹窗,直接返回成功。 + */ + function authorize(options: AuthorizeOptions): void; + + // 设置 + interface SettingOptions extends CommonCallbackOptions{ + success?(res: AuthSetting): void; + } + /** + * 调起客户端小程序设置界面,返回用户设置的操作结果。设置界面只会出现小程序已经向用户请求过的权限。 + */ + function openSetting(options: SettingOptions): void; + /** + * 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。 + */ + function getSetting(options: SettingOptions): void; + /** + * 用户授权结果,参考 type Scope + */ + interface AuthSetting { + 'scope.userInfo': boolean; + 'scope.userLocation': boolean; + 'scope.address': boolean; + 'scope.invoiceTitle': boolean; + 'scope.invoice': boolean; + 'scope.werun': boolean; + 'scope.record': boolean; + 'scope.writePhotosAlbum': boolean; + 'scope.camera': boolean; + } } +// end of wx namespace interface Page { @@ -1133,3 +1721,43 @@ declare var App: AppConstructor; * 我们提供了全局的 getApp() 函数,可以获取到小程序实例。 */ declare function getApp(): App; + +// 定时器 +/** + * 设定一个定时器。在定时到期以后执行注册的回调函数 + * @param callback + * @param delay 延迟的时间,函数的调用会在该延迟之后发生,单位 ms。 + * @param rest param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 + */ +declare function setTimeout(callback: any, delay: number, rest?: any): number; + +/** + * 取消由 setTimeout 设置的定时器。 + * @param timeoutID 要取消的定时器的ID + */ +declare function clearTimeout(timeoutID: number): number; + +/** + * 设定一个定时器。按照指定的周期(以毫秒计)来执行注册的回调函数 + * @param callback + * @param delay 延迟的时间,函数的调用会在该延迟之后发生,单位 ms。 + * @param rest param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 + */ +declare function setInterval(callback: any, delay: number, rest: any): number; + +/** + * 取消由 setInterval 设置的定时器。 + * @param timeoutID 要取消的定时器的ID + */ +declare function clearInterval(timeoutID: number): number; + +export { + wx, + App, + Page, + getApp, + setTimeout, + clearTimeout, + setInterval, + clearInterval +} diff --git a/types/weapp-api/tsconfig.json b/types/weapp-api/tsconfig.json index ef43ffab75..9df5bc5879 100644 --- a/types/weapp-api/tsconfig.json +++ b/types/weapp-api/tsconfig.json @@ -21,4 +21,4 @@ "index.d.ts", "weapp-api-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/weapp-api/weapp-api-tests.ts b/types/weapp-api/weapp-api-tests.ts index 82a5b8d91d..73f86e31f5 100644 --- a/types/weapp-api/weapp-api-tests.ts +++ b/types/weapp-api/weapp-api-tests.ts @@ -1,12 +1,13 @@ +import {wx, App, Page, getApp, setTimeout, clearTimeout, setInterval, clearInterval} from './index' App({ onLaunch: function () { //调用API从本地缓存中获取数据 - var logs = wx.getStorageSync('logs') as Array || [] + let logs = wx.getStorageSync('logs') as Array || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }, getUserInfo: function (cb: Function) { - var that = this; + let that = this; if (this.globalData.userInfo) { typeof cb == "function" && cb(this.globalData.userInfo) } else { @@ -14,7 +15,7 @@ App({ wx.login({ success: function () { wx.getUserInfo({ - success: function (res) { + success: function (res: any) { that.globalData.userInfo = res.userInfo; typeof cb == "function" && cb(that.globalData.userInfo) } @@ -37,7 +38,6 @@ Page({ foo: 'bar' }, onLoad: function (options) { - console.log(options.title); } }) @@ -51,13 +51,13 @@ wx.request({ header: { 'Content-Type': 'application/json' }, - success: function (res) { + success: function (res: any) { console.log(res.data) } }) wx.chooseImage({ - success: function (res) { + success: function (res: any) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'http://example.com/upload', @@ -73,7 +73,7 @@ wx.chooseImage({ wx.downloadFile({ url: 'http://example.com/audio/123', type: 'audio', - success: function (res) { + success: function (res: any) { wx.playVoice({ filePath: res.tempFilePath }) @@ -95,17 +95,17 @@ wx.connectSocket({ wx.connectSocket({ url: 'test.php' }) -wx.onSocketOpen(function (res) { +wx.onSocketOpen(function (res: any) { console.log('WebSocket连接已打开!') }) wx.connectSocket({ url: 'test.php' }) -wx.onSocketOpen(function (res) { +wx.onSocketOpen(function (res: any) { console.log('WebSocket连接已打开!') }) -wx.onSocketError(function (res) { +wx.onSocketError(function (res: any) { console.log('WebSocket连接打开失败,请检查!') }) @@ -115,7 +115,7 @@ wx.connectSocket({ url: 'test.php' }) -wx.onSocketOpen(function (res) { +wx.onSocketOpen(function (res: any) { socketOpen = true for (var i = 0; i < socketMsgQueue.length; i++) { sendSocketMessage(socketMsgQueue[i]) @@ -137,7 +137,7 @@ wx.connectSocket({ url: 'test.php' }) -wx.onSocketMessage(function (res) { +wx.onSocketMessage(function (res: any) { console.log('收到服务器内容:' + res.data) }) @@ -152,7 +152,7 @@ wx.onSocketOpen(function () { wx.closeSocket() }) -wx.onSocketClose(function (res) { +wx.onSocketClose(function (res: any) { console.log('WebSocket 已关闭!') }) @@ -160,7 +160,7 @@ wx.chooseImage({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 - success: function (res) { + success: function (res: any) { // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片 var tempFilePaths = res.tempFilePaths } @@ -172,10 +172,10 @@ wx.previewImage({ }) wx.startRecord({ - success: function (res) { + success: function (res: any) { var tempFilePath = res.tempFilePath }, - fail: function (res) { + fail: function (res: any) { //录音失败 } }) @@ -185,7 +185,7 @@ setTimeout(function () { }, 10000) wx.startRecord({ - success: function (res) { + success: function (res: any) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath: tempFilePath, @@ -196,7 +196,7 @@ wx.startRecord({ }) wx.startRecord({ - success: function (res) { + success: function (res: any) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath: tempFilePath @@ -210,7 +210,7 @@ wx.startRecord({ }) wx.startRecord({ - success: function (res) { + success: function (res: any) { var tempFilePath = res.tempFilePath wx.playVoice({ filePath: tempFilePath @@ -223,7 +223,7 @@ wx.startRecord({ }) wx.getBackgroundAudioPlayerState({ - success: function (res) { + success: function (res: any) { var status = res.status var dataUrl = res.dataUrl var currentPosition = res.currentPosition @@ -246,11 +246,11 @@ wx.seekBackgroundAudio({ wx.stopBackgroundAudio() wx.startRecord({ - success: function (res) { + success: function (res: any) { var tempFilePath = res.tempFilePath wx.saveFile({ tempFilePath: tempFilePath, - success: function (res) { + success: function (res: any) { var savedFilePath = res.savedFilePath } }) @@ -265,7 +265,7 @@ Page({ sourceType: ['album', 'camera'], maxDuration: 60, camera: ['front', 'back'], - success: function (res) { + success: function (res: any) { that.setData({ src: res.tempFilePath }) @@ -286,7 +286,7 @@ try { wx.getStorage({ key: 'key', - success: function (res) { + success: function (res: any) { console.log(res.data) } }) @@ -309,7 +309,7 @@ try { wx.getLocation({ type: 'wgs84', - success: function (res) { + success: function (res: any) { var latitude = res.latitude var longitude = res.longitude var speed = res.speed @@ -319,7 +319,7 @@ wx.getLocation({ wx.getLocation({ type: 'gcj02', //返回可以用于wx.openLocation的经纬度 - success: function(res) { + success: function(res: any) { var latitude = res.latitude var longitude = res.longitude wx.openLocation({ @@ -331,13 +331,13 @@ wx.getLocation({ }) wx.getNetworkType({ - success: function(res) { + success: function(res: any) { var networkType = res.networkType // 返回网络类型2g,3g,4g,wifi } }) wx.getSystemInfo({ - success: function(res) { + success: function(res: any) { console.log(res.model) console.log(res.pixelRatio) console.log(res.windowWidth) @@ -347,13 +347,37 @@ wx.getSystemInfo({ } }) -wx.onAccelerometerChange(function(res) { +const updateManager = wx.getUpdateManager() + +// updateManager.onCheckForUpdate(function (res: any) { +// //请求完新版本信息的回调 +// console.log(res.hasUpdate) +// }) + +updateManager.onUpdateReady(function () { + // wx.showModal({ + // title: '更新提示', + // content: '新版本已经准备好,是否重启应用?', + // success(res: any) { + // if (res.confirm) { + // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 + // updateManager.applyUpdate() + // } + // } + // }) +}) + +updateManager.onUpdateFailed(function () { + // 新版本下载失败 +}) + +wx.onAccelerometerChange(function(res: any) { console.log(res.x) console.log(res.y) console.log(res.z) }) -wx.onCompassChange(function (res) { +wx.onCompassChange(function (res: any) { console.log(res.direction) }) @@ -519,7 +543,7 @@ Page({ onReady: function() { var context = wx.createContext() wx.chooseImage({ - success: function(res) { + success: function(res: any) { context.drawImage(res.tempFilePaths[0], 0, 0) wx.drawCanvas({ canvasId: 1, @@ -632,7 +656,7 @@ Page({ App({ onLaunch: function() { wx.login({ - success: function(res) { + success: function(res: any) { if (res.code) { //发起网络请求 wx.request({ @@ -651,7 +675,7 @@ App({ wx.getUserInfo({ - success: function(res) { + success: function(res: any) { var userInfo = res.userInfo var nickName = userInfo.nickName var avatarUrl = userInfo.avatarUrl @@ -668,8 +692,14 @@ wx.requestPayment({ 'package': '', 'signType': 'MD5', 'paySign': '', - 'success':function(res){ + 'success':function(res: any){ }, - 'fail':function(res){ + 'fail':function(res: any){ } }) + +App({ + onLaunch(options){ + console.log(options.query) + } +})