import { HttpException , HttpService , Inject , Injectable } from '@nestjs/common' ; import { WeChatUtil } from '../../utile/wechat.util' ; @ Injectable ( ) export class WechatGrouping { constructor ( @ Inject ( WeChatUtil ) private readonly wechatUtil , @ Inject ( HttpService ) private readonly httpService ) { } param { string } appid param parameter returns { Promise < { code : number ; message : string } > } async createUserLabel ( 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/tags/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 queryUserLabel ( appid ) { if ( ! appid ) { throw new HttpException ( 'appid不存在！！' , 500 ) ; } const accessToken = await this . wechatUtil . ensureAccessToken ( appid ) ; const url = `https://api.weixin.qq.com/cgi-bin/tags/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 param parameter returns { Promise < { code : number ; message : string } > } async updateUserLabel ( 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/tags/update?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 param parameter returns { Promise < { code : number ; message : string } > } async deleteUserLabel ( 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/tags/delete?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 param parameter returns { Promise < { code : number ; message : string } > } async batchAdditionUser ( 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/tags/members/batchtagging?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 param parameter returns { Promise < { code : number ; message : string } > } async batchCancellation ( 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/tags/members/batchuntagging?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 param parameter returns { Promise < { code : number ; message : string ; data : any } > } async queryUserLabelList ( 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/tags/getidlist?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 } ; } }