import { HttpException , HttpService , Inject , Injectable } from '@nestjs/common' ; import { WeChatUtil } from '../../utile/wechat.util' ; @ Injectable ( ) export class WechatMenu { constructor ( @ Inject ( WeChatUtil ) private readonly wechatUtil , @ Inject ( HttpService ) private readonly httpService ) { } param { string } appid param parameter returns { Promise < { code : number ; message : string } > } async createMenu ( appid , parameter ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = ` https://api.weixin.qq.com/cgi-bin/menu/create?access_token=${ accessToken }` ; const { data } = await this . httpService . post ( url , parameter ) . toPromise ( ) ; if ( data . errcode ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '创建菜单成功!' } ; } param { string } appid returns { Promise < { code : number ; message : string ; data : any } > } async queryMenu ( appid ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/menu/get?access_token=${ accessToken }` ; const { data } = await this . httpService . get ( url ) . toPromise ( ) ; if ( data . errcode ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '查询菜单成功!' , data } ; } param { string } appid returns { Promise < { code : number ; message : string ; data : any } > } async deleteMenu ( appid ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=${ accessToken }` ; const { data } = await this . httpService . get ( url ) . toPromise ( ) ; if ( data . errcode !== 0 ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '删除菜单成功!' , data } ; } param { string } appid param parameter returns { Promise < { code : number ; message : string ; menuid : string } > } async createPersonalizedMenu ( appid , parameter ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=${ accessToken }` ; const { data } = await this . httpService . post ( url , parameter ) . toPromise ( ) ; if ( data . errcode ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '创建个性化菜单成功!' , menuid : data . menuid } ; } param { string } appid param { string } parameter returns { Promise < { code : number ; message : string ; menuid : any } > } async deletePersonalizedMenu ( appid , parameter ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=${ accessToken }` ; const { data } = await this . httpService . post ( url , parameter ) . toPromise ( ) ; if ( data . errcode !== 0 ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '删除个性化菜单成功!' } ; } param { string } appid param { string } parameter returns { Promise < { code : number ; message : string ; data : any } > } async testPersonalizedMenu ( appid , parameter ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/menu/trymatch?access_token=${ accessToken }` ; const { data } = await this . httpService . post ( url , parameter ) . toPromise ( ) ; if ( data . errcode ) { throw new HttpException ( data . errmsg , data . errcode ) ; } return { code : 200 , message : '测试成功!' , data } ; } }