Skip to content

Commit

Permalink
feat(grouping): Add grouping Function
Browse files Browse the repository at this point in the history
  • Loading branch information
lei3265018 committed Nov 29, 2018
1 parent 9771639 commit 4cefef2
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WeChatUtil } from './utile/wechat.util';
import { WechatTemplate } from './modules/template/wechat.template';
import { TestController } from './controller/test.controller';
import { WechatMenu } from './modules/menu/wechat.menu';
import { WechatGrouping } from './modules/grouping/wechat.grouping';

@Module({
imports: [CacheModule.register({
Expand All @@ -22,6 +23,6 @@ import { WechatMenu } from './modules/menu/wechat.menu';
{
provide: APP_INTERCEPTOR,
useClass: ErrorsInterceptor
}, AppService, WeChatUtil, WechatTemplate, WechatMenu],
}, AppService, WeChatUtil, WechatTemplate, WechatMenu, WechatGrouping],
})
export class AppModule {}
54 changes: 53 additions & 1 deletion src/controller/test.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Body, Controller, Inject, Post, Req } from '@nestjs/common';
import { WeChatUtil } from '../utile/wechat.util';
import { WechatTemplate } from '../modules/template/wechat.template';
import { WechatMenu } from '../modules/menu/wechat.menu';
import { WechatGrouping } from '../modules/grouping/wechat.grouping';

@Controller('test')
export class TestController {
Expand All @@ -11,7 +12,9 @@ export class TestController {
@Inject(WechatTemplate)
private readonly wechatTemplate: WechatTemplate,
@Inject(WechatMenu)
private readonly wechatMenu: WechatMenu
private readonly wechatMenu: WechatMenu,
@Inject(WechatGrouping)
private readonly wechatGrouping: WechatGrouping,
) {}


Expand Down Expand Up @@ -79,4 +82,53 @@ export class TestController {
const data = await this.wechatMenu.testPersonalizedMenu(appid, parameter);
return data;
}

@Post('createUserLabel')
async createUserLabel(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.createUserLabel(appid, parameter);
return data;
}

@Post('queryUserLabel')
async queryUserLabel(@Req() req, @Body() body: {appid: string}) {
const { appid } = body;
const data = await this.wechatGrouping.queryUserLabel(appid);
return data;
}

@Post('updateUserLabel')
async updateUserLabel(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.updateUserLabel(appid, parameter);
return data;
}

@Post('deleteUserLabel')
async deleteUserLabel(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.deleteUserLabel(appid, parameter);
return data;
}

@Post('batchAdditionUser')
async batchAdditionUser(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.batchAdditionUser(appid, parameter);
return data;
}

@Post('batchCancellation')
async batchCancellation(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.batchCancellation(appid, parameter);
return data;
}

@Post('queryUserLabelList')
async queryUserLabelList(@Req() req, @Body() body: {appid: string, parameter: any}) {
const {appid, parameter} = body;
const data = await this.wechatGrouping.queryUserLabelList(appid, parameter);
return data;
}
}
165 changes: 165 additions & 0 deletions src/modules/grouping/wechat.grouping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { HttpException, HttpService, Inject, Injectable } from '@nestjs/common';
import { WeChatUtil } from '../../utile/wechat.util';

@Injectable()
export class WechatGrouping {
constructor(
@Inject(WeChatUtil) private readonly wechatUtil: WeChatUtil,
@Inject(HttpService) private readonly httpService: HttpService
) {}

/**
* 创建用户分组标签,一个公众号,最多可以创建100个标签。
* parameter 格式:{ "tag" : { "name" : "广东"//标签名 } }
* 返回参数:{ "tag":{ "id":134,//标签id "name":"广东" } }
* @param {string} appid (必传)
* @param parameter(必传)
* @returns {Promise<{code: number; message: string}>}
*/
async createUserLabel(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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: string) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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};
}

/**
* 编辑公众号以建立好的标签
* parameter 格式:{ "tag" : { "id" : 134, "name" : "广东人" } }
* @param {string} appid (必传)
* @param parameter (必传)
* @returns {Promise<{code: number; message: string}>}
*/
async updateUserLabel(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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: '修改用户标签成功!'};
}

/**
* 删除公众号里的标签
* 请注意,当某个标签下的粉丝超过10w时,后台不可直接删除标签。
* 此时,开发者可以对该标签下的openid列表,先进行取消标签的操作,直到粉丝数不超过10w后,才可直接删除该标签。
* parameter 格式:{ "tag":{ "id" : 134 } }
* @param {string} appid (必传)
* @param parameter (必传)
* @returns {Promise<{code: number; message: string}>}
*/
async deleteUserLabel(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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: '删除用户标签成功!'};
}

// async queryUserLabelFans(appid: string, parameter: any) {
// if (!appid) {
// throw new HttpException('appid不存在!!', 500);
// }
// const accessToken: string = 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};
// }

/**
* 批量为用户打标签,标签功能目前支持公众号为用户打上最多20个标签。
* parameter 格式:{"openid_list" : [//粉丝列表 "ocYxcuAEy30bX0NXmGn4ypqx3tI0", "ocYxcuBt0mRugKZ7tGAHPnUaOW7Y" ], "tagid" : 134 }
* @param {string} appid (必传)
* @param parameter (必传)
* @returns {Promise<{code: number; message: string}>}
*/
async batchAdditionUser(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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: '批量为用户添加标签成功!'};
}

/**
* 批量为用户取消标签
* parameter 格式:{"openid_list" : [//粉丝列表 "ocYxcuAEy30bX0NXmGn4ypqx3tI0", "ocYxcuBt0mRugKZ7tGAHPnUaOW7Y" ], "tagid" : 134 }
* @param {string} appid (必传)
* @param parameter (必传)
* @returns {Promise<{code: number; message: string}>}
*/
async batchCancellation(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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: '批量为用户取消标签成功!'};
}

/**
* 获取用户身上的标签列表
* parameter 格式:{ "openid" : "ocYxcuBt0mRugKZ7tGAHPnUaOW7Y" }
* @param {string} appid (必传)
* @param parameter (必传)
* @returns {Promise<{code: number; message: string; data: any}>}
*/
async queryUserLabelList(appid: string, parameter: any) {
if (!appid) {
throw new HttpException('appid不存在!!', 500);
}
const accessToken: string = 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};
}

}

0 comments on commit 4cefef2

Please sign in to comment.