# ciam-miniapp-sdk

> ##### 1. SDK实例化 - 代码示例 ``` javascript import { WXAppletClient } from 'ciam-miniapp-sdk'; const authSDK = new WXAppletClient({ clientId: 'your-cliendId', authSourceId: 'your-authSourceId', userDomain: 'your-userDomain', agreement

Latest version **1.1.0** (published 2022-01-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install ciam-miniapp-sdk
pnpm add ciam-miniapp-sdk
yarn add ciam-miniapp-sdk
bun add ciam-miniapp-sdk
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-01-24 |
| First published | 2021-08-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 2 |
| Unpacked size | 340 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | shingao |
| Maintainers | shingao |

## Links

- npm: https://www.npmjs.com/package/ciam-miniapp-sdk
- npm.io page: https://npm.io/package/ciam-miniapp-sdk

## Dependencies (2)

- [wx-axios](https://npm.io/package/wx-axios.md) ^0.0.7
- [ciam-js-sdk](https://npm.io/package/ciam-js-sdk.md) ^1.0.0

## Recent versions

- 1.1.0 (latest) — 2022-01-24
- 1.0.10-beta.2 (beta) — 2022-01-17
- 1.0.10-beta.1 — 2022-01-17
- 1.0.9 — 2021-11-10
- 1.0.8 — 2021-11-10
- 1.0.7 — 2021-11-04
- 1.0.6 — 2021-10-09
- 1.0.6-beta.4 — 2021-09-03
- 1.0.6-beta.3 — 2021-08-24
- 1.0.6-beta.2 — 2021-08-24
- 1.0.6-beta.1 — 2021-08-24
- 1.0.5-beta.2 — 2021-08-24
- 1.0.5-beta.1 — 2021-08-24
- 1.0.5-beta.0 — 2021-08-24
- 1.0.5 — 2021-08-24
- … 5 more at https://npm.io/package/ciam-miniapp-sdk/versions

## README

####  使用SDK完成集成开发

##### 1. SDK实例化
- 代码示例
``` javascript
import { WXAppletClient }  from 'ciam-miniapp-sdk';
const authSDK = new WXAppletClient({
      clientId: 'your-cliendId',
      authSourceId: 'your-authSourceId',
      userDomain: 'your-userDomain',
	       agreements: [
        {
            "name": "用户协议|隐私协议|附加协议|...", // 必传
            "url": "https://qq.com", // 必传，最大长度1024
            "version": "1.0" // 必传，最大长度10
        },
        {
            "name": "用户协议|隐私协议|附加协议|...", // 必传
            "url": "https://baidu.com", // 必传，最大长度1024
            "version": "2.0.0" // 必传，最大长度10
        }
      ]
    });
```
- 初始化参数说明


| 参数名 | 类型 | 是否必填 | 长度限制 |描述 |
| ------ | ------ |  ------ | ------ | ------ |
| clientId | string | 是 |  -  |管理端添加的小程序应用ID |
| authSourceId | string | 是|  -  |管理端添加的小程序认证源ID |
| userDomain | string | 是|  -  | 租户域名（自定义域名获取） |
| agreements |  array[Agreement]  | 否 |   -  |如协议流程开启则开发者需要上传协议签署

- Agreement参数说明


| 参数名 | 类型 | 是否必填 | 长度限制 |描述 |
| ------ | ------ |  ------ | ------ | ------ |
| name | string | 是|  100  | 协议名称，如用户协议、隐私协议、附加协议 |
| url | string | 是|  1024 |协议链接地址 |
| version | string | 是|  10 | 协议版本号 |


##### 2. 静默授权登录loginCode
- 代码示例
``` javascript
async bindLoginBySlience() {
    try {
      await authSDK.loginCode();
    }
    catch(err) {
      console.log('bindLoginBySlience error', err)
    }
    const userInfo = authSDK.getUser();
    if (userInfo) {
	      ...
	   }
  },
```
- 参数说明
无

- 返回值: 类型为Promise[User|null]，User结构如下
> 首次登录时，静默授权只可获取wechatOpenid和wechatUnionId的值，其他信息需要在用户信息完善后获取。

``` javascript
{
   sub: "69bc2a4d-e575-41cf-bbc6-996e0911e2ec",
   username: "xxx",
   name: "xxx",
   gender: "female",
   phoneNumber: "+86-13612345678",
   email: "xxx@qq.com",
   nickname: "xxxx",
   wechatUnionId: "xxxxx",
   wechatOpenid: "xxxxx"
}
```

##### 3. 用户信息授权登录 loginUser
> 首次登录时，用户信息授权只可获取下面在官网api中提供的部分信息，其他信息需要在用户信息完善后获取。

`注意该方法需要页面产生点击事件（例如button上bindtap的回调中）后才可调用`
官方说明见：[获取用户信息](https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html) 
- 代码示例
``` javascript
async bindLoginByUserInfo() {
    try {
      await authSDK.loginUser();
    }
    catch(err) {
      console.log('bindLoginByUserInfo error', err)
    }
    const userInfo = authSDK.getUser();
    if (userInfo) {
      ...
    }
  },
```

- 参数说明
无

- 返回值: 类型为Promise[User|null]，User结构如下
``` javascript
{
   sub: "69bc2a4d-e575-41cf-bbc6-996e0911e2ec",
   username: "xxx",
   name: "xxx",
   gender: "female",
   phoneNumber: "+86-13612345678",
   email: "xxx@qq.com",
   nickname: "xxxx",
   wechatUnionId: "xxxxx",
   wechatOpenid: "xxxxx"
}
```

##### 4. 用户手机号授权登录loginPhone
> 首次登录时，手机号授权只可获取下面在官网api中提供的部分信息，其他信息需要在用户信息完善后获取。

`注意该方法需要页面产生点击事件（例如button上bindtap的回调中）后才可调用`
`注意该方法已更新为官方最新版本的获取手机号接口，开发时请检查开发者工具是否更新以及小程序基础库版本是否在2.21.2以上，暂不支持旧版本调用。`官方说明见：[获取手机号](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html) 
- 代码示例
```
async bindLoginByPhone(e) {
   
    const {code} = e.detail;

    if (!code) {
      console.error('未获取到code,请检查开发者工具是否更新以及小程序基础库版本是否在2.21.2以上');
      return;
    }
    try { 
      await authSDK.loginPhone(code);
    }
    catch(err) {
      console.log('bindLoginByPhone error', err)
    }

    const userInfo = authSDK.getUser();
   
    if (userInfo) {
      ...
    }
   
  },
```

- 参数说明
无

- 返回值
``` javascript
{
   sub: "69bc2a4d-e575-41cf-bbc6-996e0911e2ec",
   username: "xxx",
   name: "xxx",
   gender: "female",
   phoneNumber: "+86-13612345678",
   email: "xxx@qq.com",
   nickname: "xxxx",
   wechatUnionId: "xxxxx",
   wechatOpenid: "xxxxx"
}
```

##### 5. 获取登录状态 checkLoginState
- 代码示例
``` javascript
const isLogin = authSDK.checkLoginState();
    if (!isLogin) {
      return;
    }
```

- 参数说明
无

-返回值
``` javascript
true/false
```

##### 6. 退出登录logout
- 代码示例
``` javascript
await authSDK.logout();
```

- 参数说明
无

- 返回值: 类型为Boolean，结果如下
``` javascript
true/false
```

---
_Source: https://npm.io/package/ciam-miniapp-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
