Skip to content

Commit

Permalink
迁移项目
Browse files Browse the repository at this point in the history
  • Loading branch information
adaex committed Mar 30, 2021
1 parent b4ad5c3 commit 10411cf
Show file tree
Hide file tree
Showing 18 changed files with 573 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Build

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:

build-check:

name: Build check

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- run: yarn
- run: yarn build
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Release

on:
release:
types:
- created

jobs:

publish-npm:

name: NPM Publish

if: github.repository_owner == 'coajs'

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://registry.npmjs.org

- name: Build
run: |
yarn
npm --no-git-tag-version version ${{ github.event.release.tag_name }}
npm run build
- name: Publish
env:
NODE_AUTH_TOKEN: ${{ secrets.AEX_NPM_TOKEN }}
run: |
cd dist
npm publish
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea/
/node_modules/
/dist/

.DS_Store
npm-debug.log
yarn-error.log
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 COA

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
111 changes: 110 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,110 @@
# coa-ali-pop
# coa-ali-pop

[![GitHub license](https://img.shields.io/badge/license-MIT-green.svg?style=flat-square)](LICENSE)
[![npm version](https://img.shields.io/npm/v/coa-ali-pop.svg?style=flat-square)](https://www.npmjs.org/package/coa-ali-pop)
[![npm downloads](https://img.shields.io/npm/dm/coa-ali-pop.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-ali-pop)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-ali-pop/pulls)

极简的阿里云POP SDK for Node.js

来源于一个实际生产项目,将用到的API和业务解耦后封装成此库。

后续会根据实际使用情况优化和扩充新的服务。如果急需用到其他接口,可以直接使用阿里云官方POP SDK [@alicloud/pop-core](https://github.com/aliyun/openapi-core-nodejs-sdk)

## 特征

- **轻量** 相对于官方的SDK,无任何第三方依赖,更轻量简洁
- **COA友好** 配合COA使用,效率更高,报错信息更友好
- **TypeScript** 使用TypeScript编写,类型约束,无需查看文档,IDE友好

## 用法

### 安装

```shell
yarn add coa-ali-pop
```

### 使用

#### 短信

```typescript
import { CoaAliOssBin, CoaAliPopSmsService } from 'coa-ali-pop'

// 短信配置
const smsConfig = {
accessKeyId: 'LTAI4FkXXXXXXXXZpgGmbT',
accessKeySecret: 'T7PSOeuWIVXXXXXXXXyyqNCAPlv1',
endpoint: 'https://dysmsapi.aliyuncs.com',
apiVersion: '2017-05-25',
}

// 创建一个Bin配置实例
const smsBin = new CoaAliOssBin(smsConfig)

// 创建一个服务实例
const smsService = new CoaAliPopSmsService(smsBin)

// 通过一个模版给某个手机号发送短信消息
smsService.send('15010001001', { signName: 'XXX科技', templateCode: 'XXXXXX', param: { 'name': '张三' } })
```

#### 隐私号

```typescript
// @ts-nocheck
import { CoaAliOssBin, CoaAliPopPlsService } from 'coa-ali-pop'

// 虚拟号配置
const plsConfig = {
accessKeyId: 'LTAI4FkXXXXXXXLZpgGmbT',
accessKeySecret: 'T7PSOeuWIVgXXXXXXXFyyqNCAPlv1',
endpoint: 'https://dyplsapi.aliyuncs.com',
apiVersion: '2017-05-25'
}


// 创建一个Bin配置实例
const plsBin = new CoaAliOssBin(plsConfig)

// 创建一个服务实例
const plsService = new CoaAliPopPlsService(plsBin)

// 绑定Axn分机号,详见 https://help.aliyun.com/document_detail/110259.html
await plsService.bindAxnExtension('15010001001', Date.now() + 10 * 60 * 100)
```

#### 文字识别

```typescript
// @ts-nocheck
import { CoaAliOssBin, CoaAliPopOcrService } from 'coa-ali-pop'

// 文字识别配置
const ocrConfig = {
accessKeyId: 'LTAI4FkrXXXXXXLZpgGmbT',
accessKeySecret: 'T7PSOeuWIVgpXXXXXXyyqNCAPlv1',
endpoint: 'https://dyplsapi.aliyuncs.com',
apiVersion: '2017-05-25'
}

// 创建一个Bin配置实例
const ocrBin = new CoaAliOssBin(ocrConfig)

// 创建一个服务实例
const ocrService = new CoaAliPopOcrService(ocrBin)

// 二维码识别 详见 https://help.aliyun.com/document_detail/155008.html
await ocrService.recognizeQrCode('path/qr-code.png')

// 身份证识别 https://help.aliyun.com/document_detail/151899.html
await ocrService.recognizeIdentityCard('path/identity-card-face.png', 'face')
await ocrService.recognizeIdentityCard('path/identity-card-back.png', 'back')

// 银行卡识别 https://help.aliyun.com/document_detail/151893.html
await ocrService.recognizeBankCard('path/bank-card.png')

// 营业执照识别 https://help.aliyun.com/document_detail/151895.html
await ocrService.recognizeBusinessLicense('path/business-license.png')
```
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "coa-ali-pop",
"version": "0.0.0",
"description": "极简的阿里云POP SDK for Node.js",
"keywords": [
"coa",
"coajs",
"coa-ali-pop",
"ali-pop",
"ali",
"pop"
],
"license": "MIT",
"author": "Aex",
"homepage": "https://github.com/coajs",
"repository": {
"type": "git",
"url": "https://github.com/coajs/coa-ali-pop.git"
},
"scripts": {
"dev": "tsc -w",
"build": "rm -rf dist && tsc && cp package.json *.md dist && rm -rf dist/test",
"publish-prerelease": "yarn build && yarn version --prerelease && cp package.json *.md dist && cd dist && rm -rf test && npm publish",
"test": "node dist/test"
},
"dependencies": {
"coa-error": "^1.1.3",
"coa-helper": "^1.1.1",
"coa-secure": "^1.1.2"
},
"devDependencies": {
"@types/node": "^14.14.37",
"typescript": "^4.2.3"
}
}
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// libs
export { CoaAliPop } from './typings'
export { CoaAliPopBin } from './libs/CoaAliPopBin'

// services
export { CoaAliPopSmsService } from './services/CoaAliPopSmsService'
export { CoaAliPopOcrService } from './services/CoaAliPopOcrService'
export { CoaAliPopPlsService } from './services/CoaAliPopPlsService'
78 changes: 78 additions & 0 deletions src/libs/CoaAliPopBin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { CoaError } from 'coa-error'
import { $, _, Axios, axios } from 'coa-helper'
import { secure } from 'coa-secure'
import { CoaAliPop } from '../typings'

export class CoaAliPopBin {

readonly config: CoaAliPop.Config

constructor (config: CoaAliPop.Config) {
this.config = config
}

// POST请求
public async post (body: any, errorCodeType: 'statusCode' | 'bodyCode') {

const requestString = this.signRequestString(body, 'POST')

const response = await axios.post(this.config.endpoint, requestString).catch(e => e.response || e)

return this.responseResult(response, errorCodeType)
}

// 生成请求字符串
signRequestString (params: any, method: string) {

_.defaults(params, {
Format: 'JSON',
SignatureMethod: 'HMAC-SHA1',
SignatureVersion: '1.0',
SignatureNonce: this.createNonceString(),
Timestamp: new Date().toISOString(),
AccessKeyId: this.config.accessKeyId,
Version: this.config.apiVersion,
})

const sortQueryString = this.getSortQueryString(params)
const stringToSign = `${method}&${this.encode('/')}&${this.encode(sortQueryString)}`
const signature = secure.sha1_hmac(stringToSign, this.config.accessKeySecret + '&', 'base64')

return `Signature=${this.encode(signature)}&${sortQueryString}`
}

// 生成随机字符串
protected createNonceString () {
return Math.random().toString(36).substr(2, 15)
}

// Signature encode
protected encode (str: string) {
return encodeURIComponent(str)
.replace(/\!/g, '%21')
.replace(/\'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
}

// 获取排序过的querystring
protected getSortQueryString (object: any) {
const paramList = [] as string[]
_.forEach(object, (v, k) => {
if (v) paramList.push(this.encode(k) + '=' + this.encode(v))
})
paramList.sort()
return paramList.join('&')
}

// 处理结果
protected responseResult ({ status, statusText, data }: Axios.AxiosResponse, errorCodeType: string) {
if (status !== 200)
CoaError.throw('AliPop.Error.' + status, _.toString(data.Message) || _.toString(statusText) || '阿里云:服务请求错误')
if (errorCodeType === 'bodyCode' && data.Code !== 'OK')
CoaError.throw('AliPop.Error.' + data.Code, _.toString(data.Message) || '阿里云:服务返回错误')
return $.camelCaseKeys(data)
}

}
10 changes: 10 additions & 0 deletions src/libs/CoaAliPopServiceBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CoaAliPopBin } from './CoaAliPopBin'

export class CoaAliPopServiceBase {

readonly bin: CoaAliPopBin

constructor (bin: CoaAliPopBin) {
this.bin = bin
}
}
31 changes: 31 additions & 0 deletions src/services/CoaAliPopOcrService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CoaAliPopServiceBase } from '../libs/CoaAliPopServiceBase'

// 文字识别相关
// 接口文档 https://help.aliyun.com/document_detail/146692.html
export class CoaAliPopOcrService extends CoaAliPopServiceBase {

// 二维码识别 https://help.aliyun.com/document_detail/155008.html
async recognizeQrCode (url: string) {
const params = { Action: 'RecognizeQrCode', 'Tasks.1.ImageURL': url }
return await this.bin.post(params, 'statusCode')
}

// 身份证识别 https://help.aliyun.com/document_detail/151899.html
async recognizeIdentityCard (url: string, side: 'back' | 'face') {
const params = { Action: 'RecognizeIdentityCard', ImageURL: url, Side: side }
return await this.bin.post(params, 'statusCode')
}

// 营业执照识别 https://help.aliyun.com/document_detail/151895.html
async recognizeBusinessLicense (url: string) {
const params = { Action: 'RecognizeBusinessLicense', ImageURL: url }
return await this.bin.post(params, 'statusCode')
}

// 银行卡识别 https://help.aliyun.com/document_detail/151893.html
async recognizeBankCard (url: string) {
const params = { Action: 'RecognizeBankCard', ImageURL: url }
return await this.bin.post(params, 'statusCode')
}

}
Loading

0 comments on commit 10411cf

Please sign in to comment.