yxy-h5-bridge

1.0.10 • Public • Published

h5与App 交互(支持promise)

一、集成

npm 集成

npm i yxy-h5-bridge
2、 具体调用页面(vue项目示例)
import {NativeService} from 'yxy-h5-bridge'

data() {
    return {
      nativeService: NativeService.prototype
    }
  }

二、使用(获取用户信息示例)

try {
  let res = await this.nativeService.todoNative('getAppUserInfo', {})
  this.showToast(JSON.stringify(res))
}catch (e) {
  console.log('获取信息错误', e)
}
      

三、交互接口

1、获取用户信息

  let res = await this.nativeServicetodoNative('getAppUserInfo', {})

2、获取定位

  let res = await this.nativeServicetodoNative('getPosition', {})

3、扫码

  let res = await this.nativeServicetodoNative('getScan', {})

4、拨打电话

  let res = await this.nativeServicetodoNative('call', {
    phone: '13312341234'
  })

5、微信支付

支付示例参数

let res = await this.nativeServicetodoNative('wxpay', {
    "appid": "wxf7835d88f93668e2",  // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
    "noncestr": "c5sEwbaNPiXAF3iv", // 随机字符串
    "package": "Sign=WXPay",        // 固定值
    "partnerid": "148*****52",      // 微信支付商户号
    "prepayid": "wx202254********************fbe90000", // 统一下单订单号
    "timestamp": 1597935292,        // 时间戳(单位:秒)
    "sign": "A842B45937F6EFF60DEC7A2EAA52D5A0"
  })

6、分享

分享示例参数

let res = await this.nativeService.todoNative('share', {
        "title": "专题 | 新春走基层",                                                                 //标题
        "imageUrl": "http://img.yuxinet.cn/2023/01/11/89af8282-542d-4f39-b4b9-dd4eda594f89.jpg",    //图标
        "href": "http://app.n2022.yuxinet.cn/h5/#/pages/subject/index?id=1062661189016358912",      //跳转链接
        "id": "1062661189016358912",                                                                //id统计分享需要(可选)
        "siteId": ""                                                                                //cms站点id(可选)
      })

7、获取app详细信息

获取app详细信息示例参数(无参)

let res = await this.nativeService.todoNative('appInfo', {})

返回示例

{
    "appLanguage": "zh-Hans",
    "appName": "玉溪+",
    "appVersion": "1.2.479",
    "appVersionCode": 12479,
    "appWgtVersion": "1.2.479",
    "brand": "huawei",
    "deviceBrand": "huawei",
    "deviceId": "325EF32A00D1099047817D19CC6E864B",
    "deviceModel": "EVA-TL00",
    "deviceOrientation": "portrait",
    "devicePixelRatio": 3,
    "deviceType": "phone",
    "language": "zh-CN",
    "model": "EVA-TL00",
    "oaid": "fff7bf97-75ef-8df1-f7ee-efbf5ffd537f",
    "osAndroidAPILevel": 26,
    "osLanguage": "zh-CN",
    "osName": "android",
    "osTheme": "light",
    "osVersion": "8.0.0",
    "pixelRatio": 3,
    "platform": "android",
    "romName": "EMUI",
    "romVersion": "EmotionUI_8.0.0",
    "screenHeight": 597,
    "screenWidth": 360,
    "statusBarHeight": 24,
    "system": "Android 8.0.0"
}

8、获取cookie

获取app cookie示例参数

    try {
        let res = await this.nativeService.todoNative('getCookie', {
        })

        this.showToast(res)
      }catch (e) {
        this.showToast(JSON.stringify(e))
      }

返回示例

"LOGIN_USER_KEY=4f6543d7-570a-4100-8181-06ab738a3686; Path=/; HttpOnly, VISITOR_INFO_KEY=; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/"

9、验证用户信息(获取当前用户登录的会员信息接口) 请求curl示例(参数只有header里面的Cookie)

curl --location --request GET 'http://14.205.92.213:9002/cms-member/api/getCurrentMember' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Accept: */*' \
--header 'Host: 14.205.92.213:9002' \
--header 'Connection: keep-alive' \
--header 'Cookie: LOGIN_USER_KEY=xxxx-xxx-xxx-xx-xxxxx'

img.png

10、获取用户信息(加密) 请求加密用户信息示例

    try {
        let res = await this.nativeService.todoNative('getAppEncryptUserInfo', {})
        //this.showToast(JSON.stringify(res))
        //解密后信息
        const userData = JSON.parse(decrypt(res))
        this.showToast(JSON.stringify(userData))

      }catch (e) {
        console.log('获取信息错误', e)
      }

sm4解密

需要安装第三方依赖 npm i sm-crypto
https://www.npmjs.com/package/sm-crypto

const sm4 = require('sm-crypto').sm4
const encryptData = '0e395deb10f6e8a17e17823e1fd9bd98a1bff1df508b5b8a1efb79ec633d1bb129432ac1b74972dbe97bab04f024e89c' // 可以为 16 进制串或字节数组
const key = '0123456789abcdeffedcba9876543210' // 可以为 16 进制串或字节数组,要求为 128 比特

let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8 字符串,默认使用 pkcs#7 填充(传 pkcs#5 也会走 pkcs#7 填充)
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式

11、获取用户信息-实名信息(加密) 请求加密用户信息示例

    try {
        let res = await this.nativeService.todoNative('getAppEncryptRealUserInfo', {})
        //this.showToast(JSON.stringify(res))
        //解密后信息
        const userData = JSON.parse(decrypt(res))
        this.showToast(JSON.stringify(userData))

      }catch (e) {
        console.log('获取信息错误', e)
        this.showToast(JSON.stringify(e))
      }

身份证信息base64编码,需要decode解码一下

12、显示/隐藏分享按钮 请求示例

try {
        let res = await this.nativeService.todoNative('showShare', {
          showShare: true //true: 显示分享按钮 false: 隐藏分享按钮
        })

        this.showToast(JSON.stringify(res))
      }catch (e) {
        this.showToast(JSON.stringify(e))
      }

13、跳转商城 请求示例

try {
        let res = await this.nativeService.todoNative('goToMall', {})
        this.showToast(JSON.stringify(res))
      }catch (e) {
        console.log('获取信息错误', e)
        this.showToast(JSON.stringify(e))
      }

Readme

Keywords

Package Sidebar

Install

npm i yxy-h5-bridge

Weekly Downloads

0

Version

1.0.10

License

ISC

Unpacked Size

133 kB

Total Files

10

Last publish

Collaborators

  • zxl56123