You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/server/plugins/com.msgbyte.wxpusher/services/wxpusher.service.dev.ts

143 lines
3.5 KiB
TypeScript

import { TcService, TcDbService } from 'tailchat-server-sdk';
import type {
WXPusherUserDocument,
WXPusherUserModel,
} from '../models/wxpusher-user';
import got from 'got';
import type { TcContext } from 'tailchat-server-sdk';
/**
* wxpusher
*
* Add support for wxpusher to notify user
*/
interface WxpusherService
extends TcService,
TcDbService<WXPusherUserDocument, WXPusherUserModel> {}
class WxpusherService extends TcService {
get serviceName() {
return 'plugin:com.msgbyte.wxpusher';
}
get appToken() {
return process.env.WXPUSHER_APP_TOKEN;
}
/**
*
*/
get serverAvailable(): boolean {
return Boolean(this.appToken);
}
onInit() {
this.registerLocalDb(require('../models/wxpusher-user').default);
this.registerAvailableAction(() => this.serverAvailable);
if (!this.serverAvailable) {
console.warn(
'[plugin:com.msgbyte.wxpusher] require env: WXPUSHER_APP_TOKEN'
);
return;
}
this.registerAction('getWXPusherUserId', this.getWXPusherUserId);
this.registerAction('createQRCode', this.createQRCode);
this.registerAction('callback', this.callback, {
params: {
action: 'string',
data: 'any',
},
});
this.registerAuthWhitelist(['/callback']);
}
async getWXPusherUserId(ctx: TcContext) {
const userId = ctx.meta.userId;
return await this.findUserWxpusherUid(userId);
}
async createQRCode(ctx: TcContext) {
const userId = ctx.meta.userId;
const json = await got
.post('https://wxpusher.zjiecode.com/api/fun/create/qrcode', {
json: {
appToken: this.appToken, // 必填appToken,前面有说明,应用的标志
extra: userId, // 必填二维码携带的参数最长64位
validTime: 1800, // 可选二维码的有效期默认30分钟最长30天单位是秒
},
})
.json();
return json;
}
async callback(
ctx: TcContext<{
action: string;
data: any;
}>
) {
const { action, data } = ctx.params;
if (action === 'app_subscribe') {
this.logger.info('data', data);
// Reference: https://wxpusher.zjiecode.com/docs/#/?id=subscribe-callback
const userId = data.extra;
const wxpusherUserId = data.uid;
const record = await this.adapter.model.findOne({ userId });
if (!record) {
// 新增
await this.adapter.model.create({
userId,
wxpusherUserId,
});
} else {
record.wxpusherUserId = wxpusherUserId;
await record.save();
}
}
return true;
}
/**
* wxpusherid
*/
async findUserWxpusherUid(userId: string): Promise<string | null> {
const user = await this.adapter.model.findOne({ userId });
if (!user) {
return null;
}
const uid = user.wxpusherUserId;
return uid;
}
/**
*
*/
async sendMessage(userId: string, content: string) {
const uid = await this.findUserWxpusherUid(userId);
if (!uid) {
console.warn('This user not bind wxpusher, skip!');
return;
}
await got.post('https://wxpusher.zjiecode.com/api/send/message', {
json: {
appToken: this.appToken,
content,
contentType: 1, //内容类型 1表示文字 2表示html(只发送body标签内部的数据即可不包括body标签) 3表示markdown
uids: [uid],
},
});
}
}
export default WxpusherService;