aliyun.service.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { SendCodeDto } from './../sms/dto/sms.dto'
  2. import { Inject, Injectable, InternalServerErrorException, Logger, NotAcceptableException } from '@nestjs/common'
  3. import aliyunConfig from './config/aliyun.config'
  4. import { ConfigType } from '@nestjs/config'
  5. import Dysmsapi20170525, * as $Dysmsapi20170525 from '@alicloud/dysmsapi20170525'
  6. import OpenApi, * as $OpenApi from '@alicloud/openapi-client'
  7. import Util, * as $Util from '@alicloud/tea-util'
  8. import * as $tea from '@alicloud/tea-typescript'
  9. import { SendSmsResponse } from '@alicloud/dysmsapi20170525'
  10. import * as randomstring from 'randomstring'
  11. import OSS from 'ali-oss'
  12. import { buffer } from 'stream/consumers'
  13. import { AfsClient, AuthenticateSigRequest, AuthenticateSigResponse } from './afs'
  14. @Injectable()
  15. export class AliyunService {
  16. constructor(
  17. @Inject(aliyunConfig.KEY)
  18. private readonly aliyunConfiguration: ConfigType<typeof aliyunConfig>
  19. ) {}
  20. public async sendCode(data: SendCodeDto) {
  21. await this.authSig(data.sessionId, data.sig, data.token)
  22. if (!/^1[3-9]\d{9}$/.test(data.phone)) {
  23. throw new InternalServerErrorException('手机号码格式不正确')
  24. }
  25. let config = new $OpenApi.Config({
  26. accessKeyId: this.aliyunConfiguration.accessKeyId,
  27. accessKeySecret: this.aliyunConfiguration.accessKeySecret
  28. })
  29. config.endpoint = `dysmsapi.aliyuncs.com`
  30. let client = new Dysmsapi20170525(config)
  31. const code = randomstring.generate({ length: 4, charset: 'numeric' })
  32. let sendSmsRequest = new $Dysmsapi20170525.SendSmsRequest({
  33. phoneNumbers: data.phone,
  34. signName: this.aliyunConfiguration.smsSign,
  35. templateCode: this.aliyunConfiguration.smsTemplateCode,
  36. templateParam: JSON.stringify({ code })
  37. })
  38. try {
  39. const res: SendSmsResponse = await client.sendSmsWithOptions(sendSmsRequest, new $Util.RuntimeOptions({}))
  40. if (res.statusCode === 200 && res.body.code == 'OK') {
  41. return { phone: data.phone, code }
  42. } else {
  43. throw new InternalServerErrorException(res.body.message)
  44. }
  45. } catch (error) {
  46. Logger.error(error.message)
  47. throw new InternalServerErrorException(error.message)
  48. }
  49. }
  50. public async uploadFile(path: string, data: Buffer) {
  51. let client = new OSS({
  52. endpoint: this.aliyunConfiguration.ossEndPoint,
  53. accessKeyId: this.aliyunConfiguration.accessKeyId,
  54. accessKeySecret: this.aliyunConfiguration.accessKeySecret,
  55. bucket: this.aliyunConfiguration.ossBucket
  56. })
  57. try {
  58. const result = await client.put(path, data)
  59. Logger.log(result)
  60. if (result.res.status === 200) {
  61. return { url: result.url.replace('http:', 'https:') }
  62. } else {
  63. throw new InternalServerErrorException(result.res.statusMessage)
  64. }
  65. } catch (e) {
  66. Logger.error(e)
  67. throw new InternalServerErrorException(e.message)
  68. }
  69. }
  70. public async authSig(sessionId: string, sig: string, token: string) {
  71. var client = new AfsClient({
  72. accessKeyId: this.aliyunConfiguration.accessKeyId,
  73. accessKeySecret: this.aliyunConfiguration.accessKeySecret,
  74. endpoint: 'afs.aliyuncs.com',
  75. regionId: 'cn-hangzhou',
  76. toMap: null
  77. })
  78. // AuthenticateSigRequest
  79. var data = new AuthenticateSigRequest({
  80. scene: 'nc_activity_h5',
  81. sessionId: sessionId,
  82. sig: sig,
  83. appKey: 'FFFF0N0000000000B53B',
  84. token: token,
  85. remoteIp: '192.168.0.11'
  86. })
  87. try {
  88. const res: AuthenticateSigResponse = await client.authenticateSig(data)
  89. if (res.body.code === 100) {
  90. return
  91. } else {
  92. Logger.error(res.body.msg)
  93. throw new NotAcceptableException('验证失败')
  94. }
  95. } catch (e) {
  96. Logger.error(e)
  97. throw new InternalServerErrorException(e.message)
  98. }
  99. }
  100. }