@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
86 lines • 2.78 kB
JavaScript
import { log } from '@juzi/wechaty-puppet';
const loginMixin = (mixinBase) => {
log.verbose('WechatyLoginMixin', 'loginMixin(%s)', mixinBase.name);
class LoginMixin extends mixinBase {
get authQrCode() {
return this.puppet.authQrCode;
}
/**
* The current user
*
* @returns {ContactSelfInterface}
* @example
* const contact = bot.currentUser
* console.log(`Bot is ${contact.name()}`)
*/
get currentUser() {
return this.ContactSelf
.load(this.puppet.currentUserId);
}
/**
* Get the logon / logoff state
*
* @returns {boolean}
* @example
* if (bot.isLoggedin) {
* console.log('Bot logged in')
* } else {
* console.log('Bot not logged in')
* }
*/
get isLoggedIn() {
try {
// the `this.puppet` might not be initialized yet
return this.puppet.isLoggedIn;
}
catch (e) {
this.emit('error', e);
log.warn('WechatyLoginMixin', 'get isLoggedIn puppet instance is not ready yet');
// https://github.com/wechaty/wechaty/issues/1878
return false;
}
}
__loginMixinInited = false;
constructor(...args) {
log.verbose('WechatyLoginMixin', 'constructor()');
super(...args);
}
async init() {
log.verbose('WechatyLoginMixin', 'init()');
await super.init();
if (this.__loginMixinInited) {
return;
}
this.__loginMixinInited = true;
}
/**
* Logout the bot
*
* @returns {Promise<void>}
* @example
* await bot.logout()
*/
async logout(reason) {
log.verbose('WechatyLoginMixin', 'logout()');
await this.puppet.logout(reason);
}
/**
* @deprecated: use `isLoggedIn` property instead. will be removed after Dec 31, 2022
*/
logonoff() {
log.warn('WechatyLoginMixin', 'logonoff() is deprecated: use `isLoggedIn` property instead.\n%s', new Error().stack);
return this.isLoggedIn;
}
/**
* Will be removed after Dec 31, 2022
* @deprecated use {@link Wechaty#currentUser} instead
*/
userSelf() {
log.warn('WechatyLoginMixin', 'userSelf() deprecated: use currentUser instead.\n%s', new Error().stack);
return this.currentUser;
}
}
return LoginMixin;
};
export { loginMixin, };
//# sourceMappingURL=login-mixin.js.map