# defineAppShareModel

> 定义页面分享内容

### defineAppShareModel

> 定义 app 点击转发时候的分享卡片内容

#### 请求参数 方式 1

| 参数       | 类型                          | 必填 | 说明         |
| ---------- | ----------------------------- | ---- | ------------ |
| `title`    | `string`                      | 是   | 分享标题     |
| `content`  | `string`                      | 否   | 分享内容     |
| `imageUrl` | `string`                      | 是   | 分享图片 URL |
| `url`      | `string`                      | 是   | 分享链接 URL |
| `success`  | `() => void`                  | 否   | 成功回调     |
| `fail`     | `(error: BridgeCode) => void` | 否   | 失败回调     |
| `complete` | `() => void`                  | 否   | 完成回调     |

#### 请求参数 方式 2

| 参数         | 类型                          | 必填 | 说明                                         |
| ------------ | ----------------------------- | ---- | -------------------------------------------- |
| `onShareApp` | `() => AppShareModel`         | 是   | 点击分享时触发的函数，返回分享模型（方式 2） |
| `success`    | `() => void`                  | 否   | 成功回调                                     |
| `fail`       | `(error: BridgeCode) => void` | 否   | 失败回调                                     |
| `complete`   | `() => void`                  | 否   | 完成回调                                     |

#### 返回值

```js
Promise<void>
```

#### 示例代码

```js
import { defineAppShareModel } from '@kbapp/js-bridge'

// 方式1：直接提供分享模型
defineAppShareModel({
    title: '分享标题',
    content: '分享内容',
    imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
    url: 'http://www.kaiba315.com.cn/',
    success() {
        console.log('设置成功')
    },
    fail(error) {
        console.log('设置失败', error)
    },
})

// 方式2：提供分享模型函数（点击分享时动态生成分享内容）
defineAppShareModel({
    onShareApp() {
        // 点击页面右上角或分享按钮时触发该回调，动态生成分享内容
        return {
            title: '动态生成的分享标题',
            content: '动态生成的分享内容',
            imageUrl: 'https://static.kaiba315.com.cn/kaiba-logo.png',
            url: 'http://www.kaiba315.com.cn/?t=' + Date.now(),
        }
    },
    success() {
        console.log('设置成功')
    },
    fail(error) {
        console.log('设置失败', error)
    },
})
```

> 支持 Promise 风格调用
