import { HttpException , HttpService , Inject , Injectable } from '@nestjs/common' ; @ Injectable ( ) export class WechatTemplate { constructor ( @ Inject ( HttpService ) private readonly httpService ) { } param { string } openid param { string } templateId param { string } accessToken param parameter returns { Promise < AxiosResponse < any > > } async sendTemplate ( openid , templateId , accessToken , parameter ) { try { if ( ! openid ) { throw new HttpException ( 'openid不存在！！' , 500 ) ; } if ( ! templateId ) { throw new HttpException ( 'templateId不存在！！' , 500 ) ; } if ( ! accessToken ) { throw new HttpException ( 'accessToken不存在！！' , 500 ) ; } const url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' + accessToken + ' ' ; const body = JSON . stringify ( { touser : openid , template_id : templateId , data : parameter } ) ; const data = await this . httpService . post ( url , body ) . toPromise ( ) ; return data ; } catch ( error ) { throw new HttpException ( '发送失败，错误信息： ' + error . toString ( ) , 500 ) ; } } }