> For the complete documentation index, see [llms.txt](https://tongzonghuas-organization.gitbook.io/wgappsdk-1/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tongzonghuas-organization.gitbook.io/wgappsdk-1/useage.md).

# Useage

外部 H5 调用 \`坦克/战舰 APP\` 内部方法

SDK 会暴露出 wgappsdk 对象，里面包含 callNative 方法，通过对该方法的不同参数来操作 APP

### 使用方法

* 引入 npm 包（推荐）

```bash
npm install wgappsdk
# 或者
yarn add wgappsdk
```

```javascript
import { callNative, onAppMessage, JumpMethod, JumpType } from 'wgappsdk';

// 显示标题
await callNative(JumpMethod.SHOWTITLE, { title: "页面标题" });

// 打开H5页面
await callNative(JumpMethod.OPENH5URL, { url: "https://example.com" });

// 获取网络状态
const netStatus = await callNative(JumpMethod.GETNETSTATUS);

// 分享链接
await callNative(JumpMethod.JUMPTOSHARE, {
  project: "forum",
  type: ShareType.URL,
  title: "分享标题",
  desc: "分享描述",
  iconUrl: "https://example.com/icon.png",
  url: "https://example.com"
});

// 监听原生消息
onAppMessage((appData) => {
  switch (appData.biz) {
    case "pushComment":
      // 处理评论消息
      console.log(appData.data);
      break;
    case "pushSelected":
      // 处理选择消息
      console.log(appData.data.selectedVal);
      break;
    // ... 处理其他消息类型
  }
});
```

* 引入 cdn 连接

<pre class="language-html"><code class="lang-html"><strong>https://wg.360.cn/wgappsdk.min.js
</strong></code></pre>

测试页面：<https://zs.wows.360.cn/sdktest.html>

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>sdk test</title>
  <style>
    div {
      width: 100px;
      margin-bottom: 10px;
      padding: 10px 15px;
      border: 1px solid;
      border-radius: 10px;
    }

    .userInfo {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin-bottom: 10px;
    }

    .avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      margin-right: 10px;
    }

    span {
      margin-right: 10px;
    }
  </style>
</head>

<body>
  <section class="userInfo"> </section>
  <div class="login">login</div>
  <div class="getUserInfo">获取APP用户信息</div>
  <div class="openh5">open h5</div>
  <div class="openwebh5">外部浏览器打开 h5 页面</div>
  <div class="openShop">打开商城页面(只对Android有效)</div>
  <div class="share">share</div>
  <div class="close">close</div>
  <script src="https://s0.ssl.qhres2.com/!0da0d140/theme/js/jquery-2.1.4.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/eruda"></script>
  <!-- 引入 SDK -->
  <script src="https://s0.ssl.qhres2.com/!3cdf87fe/wgappsdk.min.js"></script>
  <script>
    eruda.init();
    // 给 webview 添加标题
    wgappsdk.callNative('showTitle', { title: 'wgappsdk' })

    document.querySelector('.login').addEventListener('click', async () => {
      // 唤起 APP 原生登录框
      wgappsdk.callNative('jumpToLogin');
    })

    document.querySelector('.getUserInfo').addEventListener('click', async () => {
      getUserInfo()
    })

    document.querySelector('.close').addEventListener('click', async () => {
      // 关闭 webview 页面
      wgappsdk.callNative('finishWeb');
    })

    document.querySelector('.openh5').addEventListener('click', async () => {
      // 通过 APP 打开页面
      wgappsdk.callNative('jumpToUi', { jump_type: 'openclient', jump_data: 'wotbox://client/webview?isFullScreen=1&url=https%3A%2F%2Fzs.wows.360.cn%2Fmatrix_ship_test.html%23%2FpostDetail%2F1340' });
    })

    document.querySelector('.openwebh5').addEventListener('click', async () => {
      // 通过 APP 打开外部浏览器
      wgappsdk.callNative('jumpToUi', { jump_type: 'openbrowser', jump_data: 'https://beta.wot.360.cn/m/wci/2023/tickets/' });
    })

    document.querySelector('.openShop').addEventListener('click', async () => {
      // 打开安卓 APP 商城
      wgappsdk.callNative('jumpToUi', { jump_type: 'openclient', jump_data: 'wows://client/mainactivity?tab=0&child_tab=2' });
    })

    document.querySelector('.share').addEventListener('click', async () => {
      // 唤起 APP 原生分享功能
      wgappsdk.callNative('jumpToShare', { type: 'url', title: '战舰助手APP SDK 测试', desc: '测试测试testesttesttest', url: location.href, iconUrl: "https://s3.ssl.qhres2.com/static/a85197ff4c92dd7e.ico", });
    })

    // 设置 APP 头部图表颜色和按钮是否显示
    wgappsdk.callNative('titleBarStyleMotify', {
      icon_is_white: '0',
      right_icon: {
        show_share_icon: '1',
        show_search_icon: '1'
      }
    });

    getUserInfo()

    // 获取当前APP用户信息
    async function getUserInfo() {
      const res = await wgappsdk.callNative('getAppUserInfo');
      const userRes = JSON.parse(res)
      if (userRes.errno === 0 && userRes.data) {
        const userInfo = userRes.data;
        $('.userInfo').html(`
          <img src="${userInfo.user_info.face_url}" class="avatar">
          <span class="username">${userInfo.currentUserInfo.nickname || userInfo.user_info.user_name || userInfo.user_info.nick_name}</span>
          <p>军团：<span style="color: ${userInfo.currentUserInfo.clan_color}">[${userInfo.currentUserInfo.clan_tag}]</span>-${userInfo.currentUserInfo.clan_name}</p>
          <span>军团ID：${userInfo.currentUserInfo.clan_id}</span>
          <span>wg_id：${userInfo.currentUserInfo.wg_id}</span>
          <span>qid：${userInfo.currentUserInfo.qid}</span>
        `)
      } else {
        wgappsdk.callNative('jumpToLogin');
      }
    }
  </script>
</body>

</html>
```
