59 | type Property = Component.Property
60 | type Method = Component.Method
61 |
62 | type DefinitionFilter = Component.DefinitionFilter
63 | type Lifetimes = Component.Lifetimes
64 |
65 | type OtherOption = Omit
66 | }
67 | /** 注册一个 `behavior`,接受一个 `Object` 类型的参数。*/
68 | declare let Behavior: WechatMiniprogram.Behavior.Constructor
69 |
--------------------------------------------------------------------------------
/demo/qiniu-ts-demo/typings/types/wx/index.d.ts:
--------------------------------------------------------------------------------
1 | /*! *****************************************************************************
2 | Copyright (c) 2022 Tencent, Inc. All rights reserved.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of
5 | this software and associated documentation files (the "Software"), to deal in
6 | the Software without restriction, including without limitation the rights to
7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 | of the Software, and to permit persons to whom the Software is furnished to do
9 | so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be included in all
12 | copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | SOFTWARE.
21 | ***************************************************************************** */
22 |
23 | ///
24 | ///
25 | ///
26 | ///
27 | ///
28 | ///
29 | ///
30 |
31 | declare namespace WechatMiniprogram {
32 | type IAnyObject = Record
33 | type Optional = F extends (arg: infer P) => infer R ? (arg?: P) => R : F
34 | type OptionalInterface = { [K in keyof T]: Optional }
35 | interface AsyncMethodOptionLike {
36 | success?: (...args: any[]) => void
37 | }
38 | type PromisifySuccessResult<
39 | P,
40 | T extends AsyncMethodOptionLike
41 | > = P extends {
42 | success: any
43 | }
44 | ? void
45 | : P extends { fail: any }
46 | ? void
47 | : P extends { complete: any }
48 | ? void
49 | : Promise>[0]>
50 |
51 | // TODO: Extract real definition from `lib.dom.d.ts` to replace this
52 | type IIRFilterNode = any
53 | type WaveShaperNode = any
54 | type ConstantSourceNode = any
55 | type OscillatorNode = any
56 | type GainNode = any
57 | type BiquadFilterNode = any
58 | type PeriodicWaveNode = any
59 | type BufferSourceNode = any
60 | type ChannelSplitterNode = any
61 | type ChannelMergerNode = any
62 | type DelayNode = any
63 | type DynamicsCompressorNode = any
64 | type ScriptProcessorNode = any
65 | type PannerNode = any
66 | type AnalyserNode = any
67 | type AudioListener = any
68 | type WebGLTexture = any
69 | type WebGLRenderingContext = any
70 | }
71 |
72 | declare let console: WechatMiniprogram.Console
73 |
74 | declare let wx: WechatMiniprogram.Wx
75 | /** 引入模块。返回模块通过 `module.exports` 或 `exports` 暴露的接口。 */
76 | declare function require(
77 | /** 需要引入模块文件相对于当前文件的相对路径,或 npm 模块名,或 npm 模块路径。不支持绝对路径 */
78 | module: string
79 | ): any
80 | /** 引入插件。返回插件通过 `main` 暴露的接口。 */
81 | declare function requirePlugin(
82 | /** 需要引入的插件的 alias */
83 | module: string
84 | ): any
85 | /** 插件引入当前使用者小程序。返回使用者小程序通过 [插件配置中 `export` 暴露的接口](https://developers.weixin.qq.com/miniprogram/dev/framework/plugin/using.html#%E5%AF%BC%E5%87%BA%E5%88%B0%E6%8F%92%E4%BB%B6)。
86 | *
87 | * 该接口只在插件中存在
88 | *
89 | * 最低基础库: `2.11.1` */
90 | declare function requireMiniProgram(): any
91 | /** 当前模块对象 */
92 | declare let module: {
93 | /** 模块向外暴露的对象,使用 `require` 引用该模块时可以获取 */
94 | exports: any
95 | }
96 | /** `module.exports` 的引用 */
97 | declare let exports: any
98 |
99 | /** [clearInterval(number intervalID)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/clearInterval.html)
100 | *
101 | * 取消由 setInterval 设置的定时器。 */
102 | declare function clearInterval(
103 | /** 要取消的定时器的 ID */
104 | intervalID: number
105 | ): void
106 | /** [clearTimeout(number timeoutID)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/clearTimeout.html)
107 | *
108 | * 取消由 setTimeout 设置的定时器。 */
109 | declare function clearTimeout(
110 | /** 要取消的定时器的 ID */
111 | timeoutID: number
112 | ): void
113 | /** [number setInterval(function callback, number delay, any rest)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/setInterval.html)
114 | *
115 | * 设定一个定时器。按照指定的周期(以毫秒计)来执行注册的回调函数 */
116 | declare function setInterval(
117 | /** 回调函数 */
118 | callback: (...args: any[]) => any,
119 | /** 执行回调函数之间的时间间隔,单位 ms。 */
120 | delay?: number,
121 | /** param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 */
122 | rest?: any
123 | ): number
124 | /** [number setTimeout(function callback, number delay, any rest)](https://developers.weixin.qq.com/miniprogram/dev/api/base/timer/setTimeout.html)
125 | *
126 | * 设定一个定时器。在定时到期以后执行注册的回调函数 */
127 | declare function setTimeout(
128 | /** 回调函数 */
129 | callback: (...args: any[]) => any,
130 | /** 延迟的时间,函数的调用会在该延迟之后发生,单位 ms。 */
131 | delay?: number,
132 | /** param1, param2, ..., paramN 等附加参数,它们会作为参数传递给回调函数。 */
133 | rest?: any
134 | ): number
135 |
--------------------------------------------------------------------------------
/sdk/qiniuUploader.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // 请参考demo的index.js中的initQiniu()方法,若在使用处对options进行了赋值,则此处config不需要赋默认值。init(options) 即updateConfigWithOptions(options),会对config进行赋值
3 | var config = {
4 | // bucket 所在区域。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
5 | qiniuRegion: '',
6 | // 七牛云bucket 外链前缀,外链在下载资源时用到
7 | qiniuBucketURLPrefix: '',
8 |
9 | // 获取uptoken方法三选一即可,执行优先级为:uptoken > uptokenURL > uptokenFunc。三选一,剩下两个置空。推荐使用uptokenURL,详情请见 README.md
10 | // 由其他程序生成七牛云uptoken,然后直接写入uptoken
11 | qiniuUploadToken: '',
12 | // 从指定 url 通过 HTTP GET 获取 uptoken,返回的格式必须是 json 且包含 uptoken 字段,例如: {"uptoken": "0MLvWPnyy..."}
13 | qiniuUploadTokenURL: '',
14 | // uptokenFunc 这个属性的值可以是一个用来生成uptoken的函数,详情请见 README.md
15 | qiniuUploadTokenFunction: function () { },
16 |
17 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
18 | // 微信自动生成的 filename较长,导致fileURL较长。推荐使用{qiniuShouldUseQiniuFileName: true} + "通过fileURL下载文件时,自定义下载名" 的组合方式。
19 | // 自定义上传key 需要两个条件:1. 此处shouldUseQiniuFileName值为false。 2. 通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义key。(请不要直接在sdk中修改options参数,修改方法请见demo的index.js)
20 | // 通过fileURL下载文件时,自定义下载名,请参考:七牛云“对象存储 > 产品手册 > 下载资源 > 下载设置 > 自定义资源下载名”(https://developer.qiniu.com/kodo/manual/1659/download-setting)。本sdk在README.md的"常见问题"板块中,有"通过fileURL下载文件时,自定义下载名"使用样例。
21 | qiniuShouldUseQiniuFileName: false
22 | }
23 |
24 | // init(options) 将七牛云相关配置初始化进本sdk
25 | // 在整个程序生命周期中,只需要 init(options); 一次即可
26 | // 如果需要变更七牛云配置,再次调用 init(options); 即可
27 | function init(options) {
28 | updateConfigWithOptions(options);
29 | }
30 |
31 | // 更新七牛云配置
32 | function updateConfigWithOptions(options) {
33 | if (options.region) {
34 | config.qiniuRegion = options.region;
35 | } else {
36 | console.error('qiniu uploader need your bucket region');
37 | }
38 | if (options.uptoken) {
39 | config.qiniuUploadToken = options.uptoken;
40 | } else if (options.uptokenURL) {
41 | config.qiniuUploadTokenURL = options.uptokenURL;
42 | } else if (options.uptokenFunc) {
43 | config.qiniuUploadTokenFunction = options.uptokenFunc;
44 | }
45 | if (options.domain) {
46 | config.qiniuBucketURLPrefix = options.domain;
47 | }
48 | config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
49 | }
50 |
51 | // 正式上传的前置方法,做预处理,应用七牛云配置
52 | function upload(filePath, success, fail, options, progress, cancelTask) {
53 | if (null == filePath) {
54 | console.error('qiniu uploader need filePath to upload');
55 | return;
56 | }
57 | if (options) {
58 | updateConfigWithOptions(options);
59 | }
60 | if (config.qiniuUploadToken) {
61 | doUpload(filePath, success, fail, options, progress, cancelTask);
62 | } else if (config.qiniuUploadTokenURL) {
63 | getQiniuToken(function () {
64 | doUpload(filePath, success, fail, options, progress, cancelTask);
65 | });
66 | } else if (config.qiniuUploadTokenFunction) {
67 | config.qiniuUploadToken = config.qiniuUploadTokenFunction();
68 | if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
69 | console.error('qiniu UploadTokenFunction result is null, please check the return value');
70 | return
71 | }
72 | doUpload(filePath, success, fail, options, progress, cancelTask);
73 | } else {
74 | console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
75 | return;
76 | }
77 | }
78 |
79 | // 正式上传
80 | function doUpload(filePath, success, fail, options, progress, cancelTask) {
81 | if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
82 | console.error('qiniu UploadToken is null, please check the init config or networking');
83 | return
84 | }
85 | var url = uploadURLFromRegionCode(config.qiniuRegion);
86 | var fileName = filePath.split('//')[1];
87 | // 自定义上传key(即自定义上传文件名)。通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义文件名称。如果options非空,则使用options中的key作为fileName
88 | if (options && options.key) {
89 | fileName = options.key;
90 | }
91 | var formData = {
92 | 'token': config.qiniuUploadToken
93 | };
94 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
95 | if (!config.qiniuShouldUseQiniuFileName) {
96 | formData['key'] = fileName
97 | }
98 | var uploadTask = wx.uploadFile({
99 | url: url,
100 | filePath: filePath,
101 | name: 'file',
102 | formData: formData,
103 | success: function (res) {
104 | var dataString = res.data
105 | // // this if case is a compatibility with wechat server returned a charcode, but was fixed
106 | // if(res.data.hasOwnProperty('type') && res.data.type === 'Buffer'){
107 | // dataString = String.fromCharCode.apply(null, res.data.data)
108 | // }
109 | try {
110 | var dataObject = JSON.parse(dataString);
111 | // 拼接fileURL
112 | var fileURL = config.qiniuBucketURLPrefix + '/' + dataObject.key;
113 | dataObject.fileURL = fileURL;
114 | // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
115 | dataObject.imageURL = fileURL;
116 | console.log(dataObject);
117 | if (success) {
118 | success(dataObject);
119 | }
120 | } catch (e) {
121 | console.log('parse JSON failed, origin String is: ' + dataString)
122 | if (fail) {
123 | fail(e);
124 | }
125 | }
126 | },
127 | fail: function (error) {
128 | console.error(error);
129 | if (fail) {
130 | fail(error);
131 | }
132 | }
133 | })
134 |
135 | // 文件上传进度
136 | uploadTask.onProgressUpdate((res) => {
137 | progress && progress(res)
138 | })
139 |
140 | // 中断文件上传
141 | cancelTask && cancelTask(() => {
142 | uploadTask.abort()
143 | })
144 | }
145 |
146 | // 获取七牛云uptoken, url为后端服务器获取七牛云uptoken接口
147 | function getQiniuToken(callback) {
148 | wx.request({
149 | url: config.qiniuUploadTokenURL,
150 | success: function (res) {
151 | var token = res.data.uptoken;
152 | if (token && token.length > 0) {
153 | config.qiniuUploadToken = token;
154 | if (callback) {
155 | callback();
156 | }
157 | } else {
158 | console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
159 | }
160 | },
161 | fail: function (error) {
162 | console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
163 | }
164 | })
165 | }
166 |
167 | // 选择七牛云文件上传接口,文件向匹配的接口中传输。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
168 | function uploadURLFromRegionCode(code) {
169 | var uploadURL = null;
170 | switch (code) {
171 | case 'ECN': uploadURL = 'https://up.qiniup.com'; break;
172 | case 'NCN': uploadURL = 'https://up-z1.qiniup.com'; break;
173 | case 'SCN': uploadURL = 'https://up-z2.qiniup.com'; break;
174 | case 'NA': uploadURL = 'https://up-na0.qiniup.com'; break;
175 | case 'ASG': uploadURL = 'https://up-as0.qiniup.com'; break;
176 | default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
177 | }
178 | return uploadURL;
179 | }
180 |
181 | module.exports = {
182 | init: init,
183 | upload: upload,
184 | }
185 | })();
186 |
--------------------------------------------------------------------------------
/demo/qiniu-demo/utils/qiniuUploader.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // 请参考demo的index.js中的initQiniu()方法,若在使用处对options进行了赋值,则此处config不需要赋默认值。init(options) 即updateConfigWithOptions(options),会对config进行赋值
3 | var config = {
4 | // bucket 所在区域。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
5 | qiniuRegion: '',
6 | // 七牛云bucket 外链前缀,外链在下载资源时用到
7 | qiniuBucketURLPrefix: '',
8 |
9 | // 获取uptoken方法三选一即可,执行优先级为:uptoken > uptokenURL > uptokenFunc。三选一,剩下两个置空。推荐使用uptokenURL,详情请见 README.md
10 | // 由其他程序生成七牛云uptoken,然后直接写入uptoken
11 | qiniuUploadToken: '',
12 | // 从指定 url 通过 HTTP GET 获取 uptoken,返回的格式必须是 json 且包含 uptoken 字段,例如: {"uptoken": "0MLvWPnyy..."}
13 | qiniuUploadTokenURL: '',
14 | // uptokenFunc 这个属性的值可以是一个用来生成uptoken的函数,详情请见 README.md
15 | qiniuUploadTokenFunction: function () { },
16 |
17 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
18 | // 微信自动生成的 filename较长,导致fileURL较长。推荐使用{qiniuShouldUseQiniuFileName: true} + "通过fileURL下载文件时,自定义下载名" 的组合方式。
19 | // 自定义上传key 需要两个条件:1. 此处shouldUseQiniuFileName值为false。 2. 通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义key。(请不要直接在sdk中修改options参数,修改方法请见demo的index.js)
20 | // 通过fileURL下载文件时,自定义下载名,请参考:七牛云“对象存储 > 产品手册 > 下载资源 > 下载设置 > 自定义资源下载名”(https://developer.qiniu.com/kodo/manual/1659/download-setting)。本sdk在README.md的"常见问题"板块中,有"通过fileURL下载文件时,自定义下载名"使用样例。
21 | qiniuShouldUseQiniuFileName: false
22 | }
23 |
24 | // init(options) 将七牛云相关配置初始化进本sdk
25 | // 在整个程序生命周期中,只需要 init(options); 一次即可
26 | // 如果需要变更七牛云配置,再次调用 init(options); 即可
27 | function init(options) {
28 | updateConfigWithOptions(options);
29 | }
30 |
31 | // 更新七牛云配置
32 | function updateConfigWithOptions(options) {
33 | if (options.region) {
34 | config.qiniuRegion = options.region;
35 | } else {
36 | console.error('qiniu uploader need your bucket region');
37 | }
38 | if (options.uptoken) {
39 | config.qiniuUploadToken = options.uptoken;
40 | } else if (options.uptokenURL) {
41 | config.qiniuUploadTokenURL = options.uptokenURL;
42 | } else if (options.uptokenFunc) {
43 | config.qiniuUploadTokenFunction = options.uptokenFunc;
44 | }
45 | if (options.domain) {
46 | config.qiniuBucketURLPrefix = options.domain;
47 | }
48 | config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
49 | }
50 |
51 | // 正式上传的前置方法,做预处理,应用七牛云配置
52 | function upload(filePath, success, fail, options, progress, cancelTask) {
53 | if (null == filePath) {
54 | console.error('qiniu uploader need filePath to upload');
55 | return;
56 | }
57 | if (options) {
58 | updateConfigWithOptions(options);
59 | }
60 | if (config.qiniuUploadToken) {
61 | doUpload(filePath, success, fail, options, progress, cancelTask);
62 | } else if (config.qiniuUploadTokenURL) {
63 | getQiniuToken(function () {
64 | doUpload(filePath, success, fail, options, progress, cancelTask);
65 | });
66 | } else if (config.qiniuUploadTokenFunction) {
67 | config.qiniuUploadToken = config.qiniuUploadTokenFunction();
68 | if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
69 | console.error('qiniu UploadTokenFunction result is null, please check the return value');
70 | return
71 | }
72 | doUpload(filePath, success, fail, options, progress, cancelTask);
73 | } else {
74 | console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
75 | return;
76 | }
77 | }
78 |
79 | // 正式上传
80 | function doUpload(filePath, success, fail, options, progress, cancelTask) {
81 | if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
82 | console.error('qiniu UploadToken is null, please check the init config or networking');
83 | return
84 | }
85 | var url = uploadURLFromRegionCode(config.qiniuRegion);
86 | var fileName = filePath.split('//')[1];
87 | // 自定义上传key(即自定义上传文件名)。通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义文件名称。如果options非空,则使用options中的key作为fileName
88 | if (options && options.key) {
89 | fileName = options.key;
90 | }
91 | var formData = {
92 | 'token': config.qiniuUploadToken
93 | };
94 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
95 | if (!config.qiniuShouldUseQiniuFileName) {
96 | formData['key'] = fileName
97 | }
98 | var uploadTask = wx.uploadFile({
99 | url: url,
100 | filePath: filePath,
101 | name: 'file',
102 | formData: formData,
103 | success: function (res) {
104 | var dataString = res.data
105 | // // this if case is a compatibility with wechat server returned a charcode, but was fixed
106 | // if(res.data.hasOwnProperty('type') && res.data.type === 'Buffer'){
107 | // dataString = String.fromCharCode.apply(null, res.data.data)
108 | // }
109 | try {
110 | var dataObject = JSON.parse(dataString);
111 | // 拼接fileURL
112 | var fileURL = config.qiniuBucketURLPrefix + '/' + dataObject.key;
113 | dataObject.fileURL = fileURL;
114 | // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
115 | dataObject.imageURL = fileURL;
116 | console.log(dataObject);
117 | if (success) {
118 | success(dataObject);
119 | }
120 | } catch (e) {
121 | console.log('parse JSON failed, origin String is: ' + dataString)
122 | if (fail) {
123 | fail(e);
124 | }
125 | }
126 | },
127 | fail: function (error) {
128 | console.error(error);
129 | if (fail) {
130 | fail(error);
131 | }
132 | }
133 | })
134 |
135 | // 文件上传进度
136 | uploadTask.onProgressUpdate((res) => {
137 | progress && progress(res)
138 | })
139 |
140 | // 中断文件上传
141 | cancelTask && cancelTask(() => {
142 | uploadTask.abort()
143 | })
144 | }
145 |
146 | // 获取七牛云uptoken, url为后端服务器获取七牛云uptoken接口
147 | function getQiniuToken(callback) {
148 | wx.request({
149 | url: config.qiniuUploadTokenURL,
150 | success: function (res) {
151 | var token = res.data.uptoken;
152 | if (token && token.length > 0) {
153 | config.qiniuUploadToken = token;
154 | if (callback) {
155 | callback();
156 | }
157 | } else {
158 | console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
159 | }
160 | },
161 | fail: function (error) {
162 | console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
163 | }
164 | })
165 | }
166 |
167 | // 选择七牛云文件上传接口,文件向匹配的接口中传输。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
168 | function uploadURLFromRegionCode(code) {
169 | var uploadURL = null;
170 | switch (code) {
171 | case 'ECN': uploadURL = 'https://up.qiniup.com'; break;
172 | case 'NCN': uploadURL = 'https://up-z1.qiniup.com'; break;
173 | case 'SCN': uploadURL = 'https://up-z2.qiniup.com'; break;
174 | case 'NA': uploadURL = 'https://up-na0.qiniup.com'; break;
175 | case 'ASG': uploadURL = 'https://up-as0.qiniup.com'; break;
176 | default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
177 | }
178 | return uploadURL;
179 | }
180 |
181 | module.exports = {
182 | init: init,
183 | upload: upload,
184 | }
185 | })();
186 |
--------------------------------------------------------------------------------
/sdk/qiniuUploader.ts:
--------------------------------------------------------------------------------
1 | type TokenFunction = () => string;
2 | type AnyFunction = (...args: any[]) => any;
3 |
4 | export type RegionCode = 'ECN' | 'NCN' | 'SCN' | 'NA' | 'ASG' | '';
5 |
6 |
7 | interface QiniuConfig {
8 | qiniuRegion?: RegionCode;
9 | qiniuBucketURLPrefix?: string;
10 | qiniuUploadToken?: string;
11 | qiniuUploadTokenURL?: string;
12 | qiniuUploadTokenFunction?: TokenFunction;
13 | qiniuShouldUseQiniuFileName?: boolean;
14 | }
15 |
16 |
17 | /**
18 | * 针对使用七牛云 API 的数据结构
19 | */
20 | export interface QiniuOptions {
21 | key?: string;
22 | region?: RegionCode;
23 | domain?: string;
24 | uptoken?: string;
25 | uptokenURL?: string;
26 | uptokenFunc?: TokenFunction;
27 | shouldUseQiniuFileName?: boolean;
28 | }
29 |
30 | /**
31 | * 针对 upload 函数的数据结构
32 | */
33 | export interface QiniuUploadOptions {
34 | filePath: string;
35 | success?: AnyFunction;
36 | fail?: AnyFunction;
37 | options?: QiniuOptions | null;
38 | progress?: WechatMiniprogram.UploadTaskOnProgressUpdateCallback;
39 | cancelTask?: AnyFunction;
40 | before?: () => void;
41 | complete?: WechatMiniprogram.UploadFileCompleteCallback;
42 | }
43 |
44 |
45 | const config: QiniuConfig = {
46 | // bucket 所在区域。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
47 | qiniuRegion: '',
48 | // 七牛云bucket 外链前缀,外链在下载资源时用到
49 | qiniuBucketURLPrefix: '',
50 |
51 | // 获取uptoken方法三选一即可,执行优先级为:uptoken > uptokenURL > uptokenFunc。三选一,剩下两个置空。推荐使用uptokenURL,详情请见 README.md
52 | // 直接写入uploadtoken
53 | qiniuUploadToken: '',
54 | // 从指定 URL 获取 uploadtoken,数据机构见
55 | qiniuUploadTokenURL: '',
56 | // uptokenFunc 这个属性的值可以是一个用来生成uptoken的函数,详情请见 README.md
57 | qiniuUploadTokenFunction: function () { return ''; },
58 |
59 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
60 | // 微信自动生成的 filename较长,导致fileURL较长。推荐使用{qiniuShouldUseQiniuFileName: true} + "通过fileURL下载文件时,自定义下载名" 的组合方式。
61 | // 自定义上传key 需要两个条件:1. 此处shouldUseQiniuFileName值为false。 2. 通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义key。(请不要直接在sdk中修改options参数,修改方法请见demo的index.js)
62 | // 通过fileURL下载文件时,自定义下载名,请参考:七牛云“对象存储 > 产品手册 > 下载资源 > 下载设置 > 自定义资源下载名”(https://developer.qiniu.com/kodo/manual/1659/download-setting)。本sdk在README.md的"常见问题"板块中,有"通过fileURL下载文件时,自定义下载名"使用样例。
63 | qiniuShouldUseQiniuFileName: false
64 | };
65 |
66 |
67 | /**
68 | * 在整个程序生命周期中,只需要 init 一次即可。如果需要变更参数,再调用 init 即可
69 | * @param options
70 | */
71 | export function init(options: QiniuOptions) {
72 | updateConfigWithOptions(options);
73 | }
74 |
75 | /**
76 | * 将用户提供的 options 整合到 config
77 | * @param options
78 | */
79 | function updateConfigWithOptions(options: QiniuOptions) {
80 | if (options.region) {
81 | config.qiniuRegion = options.region;
82 | } else {
83 | console.error('qiniu uploader need your bucket region');
84 | }
85 |
86 | if (options.uptoken) {
87 | config.qiniuUploadToken = options.uptoken;
88 | } else if (options.uptokenURL) {
89 | config.qiniuUploadTokenURL = options.uptokenURL;
90 | } else if (options.uptokenFunc) {
91 | config.qiniuUploadTokenFunction = options.uptokenFunc;
92 | }
93 |
94 | if (options.domain) {
95 | config.qiniuBucketURLPrefix = options.domain;
96 | }
97 | if (options.shouldUseQiniuFileName) {
98 | config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName;
99 | }
100 | }
101 |
102 | export function upload(uploadOptions: QiniuUploadOptions) {
103 | const { filePath, options } = uploadOptions;
104 | if (!filePath) {
105 | console.error('qiniu uploader need filePath to upload');
106 | return;
107 | }
108 | if (options) {
109 | updateConfigWithOptions(options);
110 | }
111 | if (config.qiniuUploadToken) {
112 | doUpload(uploadOptions);
113 | } else if (config.qiniuUploadTokenURL) {
114 | getQiniuToken(() => {
115 | doUpload(uploadOptions);
116 | });
117 | } else if (config.qiniuUploadTokenFunction) {
118 | config.qiniuUploadToken = config.qiniuUploadTokenFunction();
119 | if (!config.qiniuUploadToken) {
120 | console.error('qiniu UploadTokenFunction result is null, please check the return value');
121 | return;
122 | }
123 | doUpload(uploadOptions);
124 | } else {
125 | console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
126 | return;
127 | }
128 | }
129 |
130 | function doUpload(uploadOptions: QiniuUploadOptions) {
131 | if (!config.qiniuUploadToken) {
132 | console.error('qiniu UploadToken is null, please check the init config or networking');
133 | return;
134 | }
135 | const { filePath, success, fail, options, progress, cancelTask, before, complete } = uploadOptions;
136 |
137 | const url = uploadURLFromRegionCode(config?.qiniuRegion || 'ECN');
138 | if (!url) {
139 | console.error('qiniu region code is null, please check the init config or networking');
140 | return;
141 | }
142 |
143 | let fileName = filePath.split('//')[1];
144 | if (options && options.key) {
145 | fileName = options.key;
146 | }
147 | let formData: { token: string; key?: string } = {
148 | token: config.qiniuUploadToken
149 | };
150 | if (!config.qiniuShouldUseQiniuFileName) {
151 | formData['key'] = fileName;
152 | }
153 | before && before();
154 | const uploadTask = wx.uploadFile({
155 | url: url,
156 | filePath: filePath,
157 | name: 'file',
158 | formData: formData,
159 | success: function (res) {
160 | let dataString = res.data;
161 | try {
162 | const dataObject = JSON.parse(dataString);
163 | //do something
164 | const fileURL = config.qiniuBucketURLPrefix + '/' + dataObject.key;
165 | dataObject.fileURL = fileURL;
166 | // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
167 | dataObject.imageURL = fileURL;
168 | console.log(dataObject);
169 | if (success) {
170 | success(dataObject);
171 | }
172 | } catch (e) {
173 | console.log('parse JSON failed, origin String is: ' + dataString);
174 | fail && fail(e);
175 | }
176 | },
177 | fail: function (error) {
178 | console.error(error);
179 | fail && fail(error);
180 | },
181 | complete: function (err) {
182 | complete && complete(err);
183 | }
184 | });
185 |
186 | uploadTask.onProgressUpdate(res => {
187 | progress && progress(res);
188 | });
189 |
190 | cancelTask && cancelTask(() => {
191 | uploadTask.abort();
192 | });
193 | }
194 |
195 | /**
196 | * 从指定 URL
197 | * @param callback
198 | */
199 | function getQiniuToken(callback: () => void) {
200 | wx.request({
201 | url: config.qiniuUploadTokenURL || '',
202 | success: (res: any) => {
203 | let token = res.data.uptoken;
204 | if (token && token.length > 0) {
205 | config.qiniuUploadToken = token;
206 | callback && callback();
207 | } else {
208 | console.error(
209 | 'qiniuUploader cannot get your token, please check the uptokenURL or server'
210 | );
211 | }
212 | },
213 | fail: function (error) {
214 | console.error('qiniu UploadToken is null, please check the init config or networking: ');
215 | console.error(error);
216 | }
217 | });
218 | }
219 |
220 |
221 |
222 | function uploadURLFromRegionCode(code: RegionCode): string | null {
223 | let uploadURL = null;
224 | switch (code) {
225 | case 'ECN':
226 | uploadURL = 'https://up.qiniup.com';
227 | break;
228 | case 'NCN':
229 | uploadURL = 'https://up-z1.qiniup.com';
230 | break;
231 | case 'SCN':
232 | uploadURL = 'https://up-z2.qiniup.com';
233 | break;
234 | case 'NA':
235 | uploadURL = 'https://up-na0.qiniup.com';
236 | break;
237 | case 'ASG':
238 | uploadURL = 'https://up-as0.qiniup.com';
239 | break;
240 | default:
241 | console.error(
242 | 'please make the region is with one of [ECN, SCN, NCN, NA, ASG]'
243 | );
244 | }
245 | return uploadURL;
246 | }
--------------------------------------------------------------------------------
/demo/qiniu-ts-demo/miniprogram/utils/qiniuUploader.ts:
--------------------------------------------------------------------------------
1 | type TokenFunction = () => string;
2 | type AnyFunction = (...args: any[]) => any;
3 |
4 | export type RegionCode = 'ECN' | 'NCN' | 'SCN' | 'NA' | 'ASG' | '';
5 |
6 |
7 | interface QiniuConfig {
8 | qiniuRegion?: RegionCode;
9 | qiniuBucketURLPrefix?: string;
10 | qiniuUploadToken?: string;
11 | qiniuUploadTokenURL?: string;
12 | qiniuUploadTokenFunction?: TokenFunction;
13 | qiniuShouldUseQiniuFileName?: boolean;
14 | }
15 |
16 |
17 | /**
18 | * 针对使用七牛云 API 的数据结构
19 | */
20 | export interface QiniuOptions {
21 | key?: string;
22 | region?: RegionCode;
23 | domain?: string;
24 | uptoken?: string;
25 | uptokenURL?: string;
26 | uptokenFunc?: TokenFunction;
27 | shouldUseQiniuFileName?: boolean;
28 | }
29 |
30 | /**
31 | * 针对 upload 函数的数据结构
32 | */
33 | export interface QiniuUploadOptions {
34 | filePath: string;
35 | success?: AnyFunction;
36 | fail?: AnyFunction;
37 | options?: QiniuOptions | null;
38 | progress?: WechatMiniprogram.UploadTaskOnProgressUpdateCallback;
39 | cancelTask?: AnyFunction;
40 | before?: () => void;
41 | complete?: WechatMiniprogram.UploadFileCompleteCallback;
42 | }
43 |
44 |
45 | const config: QiniuConfig = {
46 | // bucket 所在区域。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
47 | qiniuRegion: '',
48 | // 七牛云bucket 外链前缀,外链在下载资源时用到
49 | qiniuBucketURLPrefix: '',
50 |
51 | // 获取uptoken方法三选一即可,执行优先级为:uptoken > uptokenURL > uptokenFunc。三选一,剩下两个置空。推荐使用uptokenURL,详情请见 README.md
52 | // 直接写入uploadtoken
53 | qiniuUploadToken: '',
54 | // 从指定 URL 获取 uploadtoken,数据机构见
55 | qiniuUploadTokenURL: '',
56 | // uptokenFunc 这个属性的值可以是一个用来生成uptoken的函数,详情请见 README.md
57 | qiniuUploadTokenFunction: function () { return ''; },
58 |
59 | // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
60 | // 微信自动生成的 filename较长,导致fileURL较长。推荐使用{qiniuShouldUseQiniuFileName: true} + "通过fileURL下载文件时,自定义下载名" 的组合方式。
61 | // 自定义上传key 需要两个条件:1. 此处shouldUseQiniuFileName值为false。 2. 通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义key。(请不要直接在sdk中修改options参数,修改方法请见demo的index.js)
62 | // 通过fileURL下载文件时,自定义下载名,请参考:七牛云“对象存储 > 产品手册 > 下载资源 > 下载设置 > 自定义资源下载名”(https://developer.qiniu.com/kodo/manual/1659/download-setting)。本sdk在README.md的"常见问题"板块中,有"通过fileURL下载文件时,自定义下载名"使用样例。
63 | qiniuShouldUseQiniuFileName: false
64 | };
65 |
66 |
67 | /**
68 | * 在整个程序生命周期中,只需要 init 一次即可。如果需要变更参数,再调用 init 即可
69 | * @param options
70 | */
71 | export function init(options: QiniuOptions) {
72 | updateConfigWithOptions(options);
73 | }
74 |
75 | /**
76 | * 将用户提供的 options 整合到 config
77 | * @param options
78 | */
79 | function updateConfigWithOptions(options: QiniuOptions) {
80 | if (options.region) {
81 | config.qiniuRegion = options.region;
82 | } else {
83 | console.error('qiniu uploader need your bucket region');
84 | }
85 |
86 | if (options.uptoken) {
87 | config.qiniuUploadToken = options.uptoken;
88 | } else if (options.uptokenURL) {
89 | config.qiniuUploadTokenURL = options.uptokenURL;
90 | } else if (options.uptokenFunc) {
91 | config.qiniuUploadTokenFunction = options.uptokenFunc;
92 | }
93 |
94 | if (options.domain) {
95 | config.qiniuBucketURLPrefix = options.domain;
96 | }
97 | if (options.shouldUseQiniuFileName) {
98 | config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName;
99 | }
100 | }
101 |
102 | export function upload(uploadOptions: QiniuUploadOptions) {
103 | const { filePath, options } = uploadOptions;
104 | if (!filePath) {
105 | console.error('qiniu uploader need filePath to upload');
106 | return;
107 | }
108 | if (options) {
109 | updateConfigWithOptions(options);
110 | }
111 | if (config.qiniuUploadToken) {
112 | doUpload(uploadOptions);
113 | } else if (config.qiniuUploadTokenURL) {
114 | getQiniuToken(() => {
115 | doUpload(uploadOptions);
116 | });
117 | } else if (config.qiniuUploadTokenFunction) {
118 | config.qiniuUploadToken = config.qiniuUploadTokenFunction();
119 | if (!config.qiniuUploadToken) {
120 | console.error('qiniu UploadTokenFunction result is null, please check the return value');
121 | return;
122 | }
123 | doUpload(uploadOptions);
124 | } else {
125 | console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
126 | return;
127 | }
128 | }
129 |
130 | function doUpload(uploadOptions: QiniuUploadOptions) {
131 | if (!config.qiniuUploadToken) {
132 | console.error('qiniu UploadToken is null, please check the init config or networking');
133 | return;
134 | }
135 | const { filePath, success, fail, options, progress, cancelTask, before, complete } = uploadOptions;
136 |
137 | const url = uploadURLFromRegionCode(config?.qiniuRegion || 'ECN');
138 | if (!url) {
139 | console.error('qiniu region code is null, please check the init config or networking');
140 | return;
141 | }
142 |
143 | let fileName = filePath.split('//')[1];
144 | if (options && options.key) {
145 | fileName = options.key;
146 | }
147 | let formData: { token: string; key?: string } = {
148 | token: config.qiniuUploadToken
149 | };
150 | if (!config.qiniuShouldUseQiniuFileName) {
151 | formData['key'] = fileName;
152 | }
153 | before && before();
154 | const uploadTask = wx.uploadFile({
155 | url: url,
156 | filePath: filePath,
157 | name: 'file',
158 | formData: formData,
159 | success: function (res) {
160 | let dataString = res.data;
161 | try {
162 | const dataObject = JSON.parse(dataString);
163 | //do something
164 | const fileURL = config.qiniuBucketURLPrefix + '/' + dataObject.key;
165 | dataObject.fileURL = fileURL;
166 | // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
167 | dataObject.imageURL = fileURL;
168 | console.log(dataObject);
169 | if (success) {
170 | success(dataObject);
171 | }
172 | } catch (e) {
173 | console.log('parse JSON failed, origin String is: ' + dataString);
174 | fail && fail(e);
175 | }
176 | },
177 | fail: function (error) {
178 | console.error(error);
179 | fail && fail(error);
180 | },
181 | complete: function (err) {
182 | complete && complete(err);
183 | }
184 | });
185 |
186 | uploadTask.onProgressUpdate(res => {
187 | progress && progress(res);
188 | });
189 |
190 | cancelTask && cancelTask(() => {
191 | uploadTask.abort();
192 | });
193 | }
194 |
195 | /**
196 | * 从指定 URL
197 | * @param callback
198 | */
199 | function getQiniuToken(callback: () => void) {
200 | wx.request({
201 | url: config.qiniuUploadTokenURL || '',
202 | success: (res: any) => {
203 | let token = res.data.uptoken;
204 | if (token && token.length > 0) {
205 | config.qiniuUploadToken = token;
206 | callback && callback();
207 | } else {
208 | console.error(
209 | 'qiniuUploader cannot get your token, please check the uptokenURL or server'
210 | );
211 | }
212 | },
213 | fail: function (error) {
214 | console.error('qiniu UploadToken is null, please check the init config or networking: ');
215 | console.error(error);
216 | }
217 | });
218 | }
219 |
220 |
221 |
222 | function uploadURLFromRegionCode(code: RegionCode): string | null {
223 | let uploadURL = null;
224 | switch (code) {
225 | case 'ECN':
226 | uploadURL = 'https://up.qiniup.com';
227 | break;
228 | case 'NCN':
229 | uploadURL = 'https://up-z1.qiniup.com';
230 | break;
231 | case 'SCN':
232 | uploadURL = 'https://up-z2.qiniup.com';
233 | break;
234 | case 'NA':
235 | uploadURL = 'https://up-na0.qiniup.com';
236 | break;
237 | case 'ASG':
238 | uploadURL = 'https://up-as0.qiniup.com';
239 | break;
240 | default:
241 | console.error(
242 | 'please make the region is with one of [ECN, SCN, NCN, NA, ASG]'
243 | );
244 | }
245 | return uploadURL;
246 | }
--------------------------------------------------------------------------------
/demo/qiniu-ts-demo/typings/types/wx/lib.wx.app.d.ts:
--------------------------------------------------------------------------------
1 | /*! *****************************************************************************
2 | Copyright (c) 2022 Tencent, Inc. All rights reserved.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy of
5 | this software and associated documentation files (the "Software"), to deal in
6 | the Software without restriction, including without limitation the rights to
7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 | of the Software, and to permit persons to whom the Software is furnished to do
9 | so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be included in all
12 | copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 | SOFTWARE.
21 | ***************************************************************************** */
22 |
23 | declare namespace WechatMiniprogram.App {
24 | interface ReferrerInfo {
25 | /** 来源小程序或公众号或App的 appId
26 | *
27 | * 以下场景支持返回 referrerInfo.appId:
28 | * - 1020(公众号 profile 页相关小程序列表): appId
29 | * - 1035(公众号自定义菜单):来源公众号 appId
30 | * - 1036(App 分享消息卡片):来源应用 appId
31 | * - 1037(小程序打开小程序):来源小程序 appId
32 | * - 1038(从另一个小程序返回):来源小程序 appId
33 | * - 1043(公众号模板消息):来源公众号 appId
34 | */
35 | appId: string
36 | /** 来源小程序传过来的数据,scene=1037或1038时支持 */
37 | extraData?: any
38 | }
39 |
40 | type SceneValues =
41 | | 1001
42 | | 1005
43 | | 1006
44 | | 1007
45 | | 1008
46 | | 1011
47 | | 1012
48 | | 1013
49 | | 1014
50 | | 1017
51 | | 1019
52 | | 1020
53 | | 1023
54 | | 1024
55 | | 1025
56 | | 1026
57 | | 1027
58 | | 1028
59 | | 1029
60 | | 1030
61 | | 1031
62 | | 1032
63 | | 1034
64 | | 1035
65 | | 1036
66 | | 1037
67 | | 1038
68 | | 1039
69 | | 1042
70 | | 1043
71 | | 1044
72 | | 1045
73 | | 1046
74 | | 1047
75 | | 1048
76 | | 1049
77 | | 1052
78 | | 1053
79 | | 1056
80 | | 1057
81 | | 1058
82 | | 1059
83 | | 1064
84 | | 1067
85 | | 1069
86 | | 1071
87 | | 1072
88 | | 1073
89 | | 1074
90 | | 1077
91 | | 1078
92 | | 1079
93 | | 1081
94 | | 1082
95 | | 1084
96 | | 1089
97 | | 1090
98 | | 1091
99 | | 1092
100 | | 1095
101 | | 1096
102 | | 1097
103 | | 1099
104 | | 1102
105 | | 1124
106 | | 1125
107 | | 1126
108 | | 1129
109 |
110 | interface LaunchShowOption {
111 | /** 打开小程序的路径 */
112 | path: string
113 | /** 打开小程序的query */
114 | query: IAnyObject
115 | /** 打开小程序的场景值
116 | * - 1001:发现栏小程序主入口,「最近使用」列表(基础库2.2.4版本起包含「我的小程序」列表)
117 | * - 1005:微信首页顶部搜索框的搜索结果页
118 | * - 1006:发现栏小程序主入口搜索框的搜索结果页
119 | * - 1007:单人聊天会话中的小程序消息卡片
120 | * - 1008:群聊会话中的小程序消息卡片
121 | * - 1011:扫描二维码
122 | * - 1012:长按图片识别二维码
123 | * - 1013:扫描手机相册中选取的二维码
124 | * - 1014:小程序模板消息
125 | * - 1017:前往小程序体验版的入口页
126 | * - 1019:微信钱包(微信客户端7.0.0版本改为支付入口)
127 | * - 1020:公众号 profile 页相关小程序列表
128 | * - 1023:安卓系统桌面图标
129 | * - 1024:小程序 profile 页
130 | * - 1025:扫描一维码
131 | * - 1026:发现栏小程序主入口,「附近的小程序」列表
132 | * - 1027:微信首页顶部搜索框搜索结果页「使用过的小程序」列表
133 | * - 1028:我的卡包
134 | * - 1029:小程序中的卡券详情页
135 | * - 1030:自动化测试下打开小程序
136 | * - 1031:长按图片识别一维码
137 | * - 1032:扫描手机相册中选取的一维码
138 | * - 1034:微信支付完成页
139 | * - 1035:公众号自定义菜单
140 | * - 1036:App 分享消息卡片
141 | * - 1037:小程序打开小程序
142 | * - 1038:从另一个小程序返回
143 | * - 1039:摇电视
144 | * - 1042:添加好友搜索框的搜索结果页
145 | * - 1043:公众号模板消息
146 | * - 1044:带 shareTicket 的小程序消息卡片 [详情](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share.html)
147 | * - 1045:朋友圈广告
148 | * - 1046:朋友圈广告详情页
149 | * - 1047:扫描小程序码
150 | * - 1048:长按图片识别小程序码
151 | * - 1049:扫描手机相册中选取的小程序码
152 | * - 1052:卡券的适用门店列表
153 | * - 1053:搜一搜的结果页
154 | * - 1056:聊天顶部音乐播放器右上角菜单
155 | * - 1057:钱包中的银行卡详情页
156 | * - 1058:公众号文章
157 | * - 1059:体验版小程序绑定邀请页
158 | * - 1064:微信首页连Wi-Fi状态栏
159 | * - 1067:公众号文章广告
160 | * - 1069:移动应用
161 | * - 1071:钱包中的银行卡列表页
162 | * - 1072:二维码收款页面
163 | * - 1073:客服消息列表下发的小程序消息卡片
164 | * - 1074:公众号会话下发的小程序消息卡片
165 | * - 1077:摇周边
166 | * - 1078:微信连Wi-Fi成功提示页
167 | * - 1079:微信游戏中心
168 | * - 1081:客服消息下发的文字链
169 | * - 1082:公众号会话下发的文字链
170 | * - 1084:朋友圈广告原生页
171 | * - 1089:微信聊天主界面下拉,「最近使用」栏(基础库2.2.4版本起包含「我的小程序」栏)
172 | * - 1090:长按小程序右上角菜单唤出最近使用历史
173 | * - 1091:公众号文章商品卡片
174 | * - 1092:城市服务入口
175 | * - 1095:小程序广告组件
176 | * - 1096:聊天记录
177 | * - 1097:微信支付签约页
178 | * - 1099:页面内嵌插件
179 | * - 1102:公众号 profile 页服务预览
180 | * - 1124:扫“一物一码”打开小程序
181 | * - 1125:长按图片识别“一物一码”
182 | * - 1126:扫描手机相册中选取的“一物一码”
183 | * - 1129:微信爬虫访问 [详情](https://developers.weixin.qq.com/miniprogram/dev/reference/configuration/sitemap.html)
184 | */
185 | scene: SceneValues
186 | /** shareTicket,详见 [获取更多转发信息]((转发#获取更多转发信息)) */
187 | shareTicket: string
188 | /** 当场景为由从另一个小程序或公众号或App打开时,返回此字段 */
189 | referrerInfo?: ReferrerInfo
190 | }
191 |
192 | interface PageNotFoundOption {
193 | /** 不存在页面的路径 */
194 | path: string
195 | /** 打开不存在页面的 query */
196 | query: IAnyObject
197 | /** 是否本次启动的首个页面(例如从分享等入口进来,首个页面是开发者配置的分享页面) */
198 | isEntryPage: boolean
199 | }
200 |
201 | interface Option {
202 | /** 生命周期回调—监听小程序初始化
203 | *
204 | * 小程序初始化完成时触发,全局只触发一次。
205 | */
206 | onLaunch(options: LaunchShowOption): void
207 | /** 生命周期回调—监听小程序显示
208 | *
209 | * 小程序启动,或从后台进入前台显示时
210 | */
211 | onShow(options: LaunchShowOption): void
212 | /** 生命周期回调—监听小程序隐藏
213 | *
214 | * 小程序从前台进入后台时
215 | */
216 | onHide(): void
217 | /** 错误监听函数
218 | *
219 | * 小程序发生脚本错误,或者 api
220 | */
221 | onError(/** 错误信息,包含堆栈 */ error: string): void
222 | /** 页面不存在监听函数
223 | *
224 | * 小程序要打开的页面不存在时触发,会带上页面信息回调该函数
225 | *
226 | * **注意:**
227 | * 1. 如果开发者没有添加 `onPageNotFound` 监听,当跳转页面不存在时,将推入微信客户端原生的页面不存在提示页面。
228 | * 2. 如果 `onPageNotFound` 回调中又重定向到另一个不存在的页面,将推入微信客户端原生的页面不存在提示页面,并且不再回调 `onPageNotFound`。
229 | *
230 | * 最低基础库: 1.9.90
231 | */
232 | onPageNotFound(options: PageNotFoundOption): void
233 | /**
234 | * 小程序有未处理的 Promise 拒绝时触发。也可以使用 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html) 绑定监听。注意事项请参考 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html)。
235 | * **参数**:与 [wx.onUnhandledRejection](https://developers.weixin.qq.com/miniprogram/dev/api/base/app/app-event/wx.onUnhandledRejection.html) 一致
236 | */
237 | onUnhandledRejection: OnUnhandledRejectionCallback
238 | /**
239 | * 系统切换主题时触发。也可以使用 wx.onThemeChange 绑定监听。
240 | *
241 | * 最低基础库: 2.11.0
242 | */
243 | onThemeChange: OnThemeChangeCallback
244 | }
245 |
246 | type Instance = Option & T
247 | type Options = Partial