| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | import * as $OpenApi from '@alicloud/openapi-client'; |
| 3 | import * as $Util from '@alicloud/tea-util'; |
| 4 | import { AlicloudSmsOptions } from './interfaces/alicloud-sms-options.interface'; |
| 5 | import { ConfigService } from '@nestjs/config'; |
| 6 | import Dypnsapi20170525, * as $Dypnsapi20170525 from '@alicloud/dypnsapi20170525'; |
| 7 | |
| 8 | @Injectable() |
| 9 | export class AlicloudPnsService { |
| 10 | private client: Dypnsapi20170525; |
| 11 | constructor(private configService: ConfigService) { |
| 12 | const options: AlicloudSmsOptions = this.configService.get('SMS_CONFIG'); |
| 13 | const config = new $OpenApi.Config({ |
| 14 | accessKeyId: options.config.accessKeyId, |
| 15 | accessKeySecret: options.config.accessKeySecret, |
| 16 | }); |
| 17 | |
| 18 | config.endpoint = `dypnsapi.aliyuncs.com`; |
| 19 | this.client = new Dypnsapi20170525(config); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * 获取一键登录的手机号 |
| 24 | * @param accessToken |
| 25 | * @param outId |
| 26 | * @returns |
| 27 | */ |
| 28 | async getOneKeyLoginPhone( |
| 29 | accessToken: string, |
| 30 | outId?: string, |
| 31 | ): Promise<string> { |
| 32 | const runtime = new $Util.RuntimeOptions({}); |
| 33 | const getMobileRequest = new $Dypnsapi20170525.GetMobileRequest({ |
| 34 | accessToken, |
| 35 | outId, |
| 36 | }); |
| 37 | try { |
| 38 | const res = await this.client.getMobileWithOptions( |
| 39 | getMobileRequest, |
| 40 | runtime, |
| 41 | ); |
| 42 | |
| 43 | if (res.statusCode !== 200) throw new Error('not 200' + res.statusCode); |
| 44 | if (res.body.code !== 'OK') throw new Error(res.body.message); |
| 45 | |
| 46 | return res.body.getMobileResultDTO.mobile; |
| 47 | } catch (error) { |
| 48 | console.log('------ getOneKeyLoginPhone ---- error', error); |
| 49 | return ''; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 |