mini-jsdk
v0.1.1
Published
NNW小程序jsdk统一
Downloads
10
Readme
miniSDK API 介绍
小程序 JSDK
支付宝辅助 API: https://opendocs.alipay.com/mini/component/web-view
微信辅助 API: https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
版本
V:0.0.3 增加 npm 引入
支付宝环境需要在 HTML 先引入: https://appx/web-view.min.js 微信环境需要在 HTML 先引入: https://res2.wx.qq.com/open/js/jweixin-1.6.0.js
import miniSDK from 'mini-jsdk'; const jsdk = miniSDK(wx|| my)
miniSDK.getEnv
描述
获取当前环境
示例代码
miniSDK.getEnv(function(res) {
console.log(res.miniprogram) // true
});
入参
入参为 Function 类型
回调函数返回值(res)
| 名称 | 类型 | 描述 | | :---------: | :-----: | :------------------: | | miniprogram | Boolean | true: 小程序运行环境 |
miniSDK.chooseImage
描述
拍照或从手机相册中选择图片。
示例代码
miniSDK.chooseImage({
sourceType: ['camera','album'],
count: 2,
success: (res) => {
console.log(res)
},
fail:()=>{
console.log('fail)
}
})
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :--------: | :----------: | :---: | :----------------------------------------------: | | count | Number | 否 | 最大可选照片数,默认 1 张。 | | sizeType | StringArray | 否 | original 原图,compressed 压缩图,默认二者都有。 | | sourceType | String Array | 否 | 相册选取或者拍照,默认 ['camera','album']。 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
success 回调函数
入参为 Object 类型,属性如下:
| 名称 | 类型 | 描述 | | :---------: | :----------: | :--------------: | | apFilePaths | String Array | 图片的路径数组。 |
fail 回调函数
| 错误码 | 描述 | | :----: | :------------: | | 11 | 用户取消操作。 |
miniSDK.navigateToMiniProgram
描述
web-view 跳转其它小程
示例代码
miniSDK.navigateToMiniProgram({
path: "/pages/example/example",
appId: "wx9db2c16d0633c2e7",
extraData:null,
success() {},
fail() {},
complete() {}
});
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :-------: | :------: | :---: | :--------------------------------------------: | | path | String | 是 | 需要跳转小程序 page 的地址, | | appId | String | 是 | 需要跳转的小程序 APPID | | extraData | Object | 否 | 跳转携带的参数 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
出参
暂时无法获取
miniSDK.navigateToAlipayPage
描述
小程序中跳转到支付宝官方业务或运营活动页面。 小程序之间相互跳转,请使用专有接口: miniSDK.navigateToMiniProgram。
示例代码
miniSDK.navigateToAlipayPage({
path: "/pages/example/example",
appCode: "wx9db2c16d0633c2e7",
appParams:null,
success() {},
fail() {},
complete() {}
});
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :-------: | :------: | :---------------------: | :----------------------------------------------------------------------------------------------------------------: | | path | String | 是(和 appCode 二选一) | 要跳转的支付宝业务、运营活动 scheme 或 url,如果 url 中带有参数,请务必先将整个 url 做 encode 处理。 | | appCode | String | 是(和 path 二选一) | 要跳转的支付宝官方业务,例如付款码,appCode: 'payCode',详见下方 appCode | | appParams | Object | 否 | appCode 配套参数,需要和 appCode 搭配使用。目前已开放的跳转页面,均无需配置 appParams 参数,appParams 为 {} 即可。 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
appCode 参数说明
入参为 Object 类型,属性如下: | appCode | appParams | 描述 | | :--------: | :-------: | :------------------: | | alipayScan | {} | 打开支付宝扫一扫。 | | redPacket | {} | 打开收到的红包页面。 | | collectOil | {} | 打开爱攒油页面。 | 详见:https://opendocs.alipay.com/mini/api/navigatetoalipaypage
出参
暂时无法获取
miniSDK.navigateBack
描述
返回、或者回到小程序首页
示例代码
miniSDK.navigateBack()
入参
无
miniSDK.switchTab
描述
跳转到指定 tabBar 页面,并关闭其他所有非 tabBar 页面。
示例代码
miniSDK.switchTab()
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :------: | :------: | :---: | :----------------------------------------------------------: | | url | String | 否 | 需要跳转 tabbar 的地址,默认:/pages/invoice/invList/invList | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
success 回调函数
无描述
fail 回调函数
无描述
mini.navigateTo
描述
保留当前页面,跳转到应用内的某个指定页面。
示例代码
miniSDK.navigateTo()
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :------: | :------: | :---: | :--------------------------------------------: | | url | String | 是 | 需要跳转小程序 page 的地址 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
success 回调函数
无描述
fail 回调函数
无描述
mini.redirectTo
描述
关闭当前页面,跳转到应用内的某个指定页面。
示例代码
miniSDK.redirectTo()
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :------: | :------: | :---: | :--------------------------------------------: | | url | String | 是 | 需要跳转小程序 page 的地址 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
success 回调函数
无描述
fail 回调函数
无描述
miniSDK.reLaunch
描述
关闭当前所有页面,跳转到应用内的某个指定页面。
示例代码
miniSDK.reLaunch()
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :------: | :------: | :---: | :--------------------------------------------: | | url | String | 是 | 需要跳转小程序 page 的地址 | | success | Function | 否 | 调用成功的回调函数。 | | fail | Function | 否 | 调用失败的回调函数。 | | complete | Function | 否 | 调用结束的回调函数(调用成功、失败都会执行)。 |
success 回调函数
无描述
fail 回调函数
无描述
miniSDK.tradePay
描述
唤起支付(仅支持使用该 API 唤起支付,不支持使用 H5 进行支付)
示例代码
// 支付宝
miniSDK.tradePay({
tradeNO:"1212121212122",// 接入小程序支付时传入此参数。此参数为支付宝交易号,注意参数有大小写区分。
orderStr:'',// 完整的支付参数拼接成的字符串,从服务端获取。
callbackUrl = "https://www.baicu.com", // 支付回调跳转地址PS:支付宝不会执行
success:() => undefined,
fail:() => undefined
})
// 微信
miniSDK.tradePay({
orderStr:"{timeStamp:'',nonceStr:'',package:'',signType:'',paySign:''}",
tradeNO:'',// 订单id扩展字段
callbackUrl = "https://www.baicu.com", // 支付回调跳转地址
success = () => undefined,// PS:微信不会执行
fail = () => undefined// PS:微信不会执行
})
入参
入参为 Object 类型,属性如下:
| 属性 | 类型 | 必填 | 描述 | | :---------: | :------: | :---: | :------------------------------------------------------------------------------------------------------------------: | | tradeNO | String | 是 | 接入小程序支付时传入此参数。此参数为支付宝交易号,微信订单号,注意参数有大小写区分。 | | orderStr | String | 是 | 完整的支付参数拼接成的字符串,从服务端获取。例子:("{timeStamp:'',nonceStr:'',package:'',signType:'',paySign:''}") | | callbackUrl | String | 是 | 微信支付成功回调跳转地址。PS:支付宝不会执行该方法 | | success | Function | 否 | 调用成功的回调函数,PS:微信不回调用该方法。 | | fail | Function | 否 | 调用失败的回调函数, PS:微信不回调用该方法, |
callbackUrl 回调地址
微信小程序会使用改地址重新加载 web-view,并携带参数 payresult
- payresult='success'支付成功
- payresult='fail'支付失败
success 回调函数
入参为 Object 类型,属性如下:
| 名称 | 类型 | 描述 | | :--------: | :----: | :----------------------: | | resultCode | String | 支付结果码,详见下表。。 |
| 结果码 | 描述 | | :----: | :----------------------------------------------------------------------------------: | | 9000 | 订单处理成功。 | | 8000 | 正在处理中。支付结果未知(有可能已经支付成功),请查询商户订单列表中订单的支付状态。 | | 4000 | 订单处理失败。 | | 6001 | 用户中途取消。 | | 6002 | 网络连接出错。 | | 6004 | 处理结果未知(有可能已经成功),请查询商户订单列表中订单状态。 | | 99 | 用户点击忘记密码导致快捷界面退出(only iOS)。 |
fail 回调函数
无描述
miniSDK.startAPVerify
描述
唤起支付支付宝实名认证
示例代码
miniSDK.startAPVerify({
certname: '', // 姓名
certno: '', // 身份号码
success:(res){
},
fail: (fail) => undefined
})
success 回调函数
参考 支付宝小程序实名认证
- 入参为 Object 类型,属性如下:
| 名称 | 类型 | 描述 | | :--------------: | :----: | :--------------------------------------------: | | result.certifyId | String | 本次认证流水号 certifyId | | result.errorCode | String | 业务异常错误码 | | resultStatus | String | 认证流程结果状态码, 详见以下 ResultStatus 定义 |
| 结果码 | 描述 | | :----: | :--------------------------: | | 9000 | 订单处理成功。 | | 6002 | 网络异常 | | 6001 | 用户取消了业务流程,主动退出 | | 4000 | 业务异常 |
miniSDK.chooseInvoiceTitle
描述
获取支付宝抬头
示例代码
miniSDK.chooseInvoiceTitle({
success:(res){
},
fail: (fail) => undefined
})
success 回调函数
- 入参为 Array 类型,[{invoiceName,invoiceCode,invoiceAddress,invoiceTelephone, bankName,account }]属性如下:
| 名称 | 类型 | 描述 | | :--------------: | :----: | :--------: | | invoiceName | String | 抬头名称 | | invoiceCode | String | 税号 | | invoiceAddress | String | 地址 | | invoiceTelephone | String | 电话 | | bankName | String | 开户行银行 | | account | String | 账号 |
miniSDK.getLocation
描述
小程序定位
示例代码
miniSDK.getLocation({
type:0,
cacheTimeout:30,
success:(res) => undefined,
fail:(fail) => undefined
})
success 回调函数
参考 支付宝小程序定位
- 入参为 Object 类型,属性如下:
| 名称 | 类型 | 描述 | | :----------------: | :----: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | | longitude | String | 经度 | | latitude | String | 纬度 | | accuracy | String | 精确度,单位米 (m)。 | | horizontalAccuracy | String | 精确度,单位米 (m)。 | | country | String | 国家(type>0 生效) | | countryCode | String | 国家编号 (type>0 生效) | | province | String | 省份(type>0 生效) | | city | String | 城市(type>0 生效) | | cityAdcode | String | 城市级别的地区代码(type>0 生效) | | district | String | 区县(type>0 生效) | | districtAdcode | String | 区县级别的地区代码(type>0 生效) | | streetNumber | Object | 需要街道级别逆地理的才会有的字段,街道门牌信息,结构是:{street, number} (type>1 生效)。 | | pois | Array | 需要 POI (Point of Interest,兴趣点,在地理信息系统中,一个 POI 可以是一栋房子、一个商铺、一个邮筒、一个公交站等)级别的地理位置才会有的字段,定位点附近的 POI 信息,结构是:{name, address}(type>2 生效)。 |
错误码
| 错误码 | 描述 | | :----: | :------------------------: | | 11 | 请确认定位相关权限已开启。 | | 12 | 网络异常,请稍后再试。 | | 13 | 定位失败,请稍后再试。 | | 14 | 业务定位超时。 | | 2001 | 用户拒绝给小程序授权。 |