@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
89 lines • 3.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loginMixin = void 0;
const wechaty_puppet_1 = require("@juzi/wechaty-puppet");
const loginMixin = (mixinBase) => {
wechaty_puppet_1.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);
wechaty_puppet_1.log.warn('WechatyLoginMixin', 'get isLoggedIn puppet instance is not ready yet');
// https://github.com/wechaty/wechaty/issues/1878
return false;
}
}
__loginMixinInited = false;
constructor(...args) {
wechaty_puppet_1.log.verbose('WechatyLoginMixin', 'constructor()');
super(...args);
}
async init() {
wechaty_puppet_1.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) {
wechaty_puppet_1.log.verbose('WechatyLoginMixin', 'logout()');
await this.puppet.logout(reason);
}
/**
* @deprecated: use `isLoggedIn` property instead. will be removed after Dec 31, 2022
*/
logonoff() {
wechaty_puppet_1.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() {
wechaty_puppet_1.log.warn('WechatyLoginMixin', 'userSelf() deprecated: use currentUser instead.\n%s', new Error().stack);
return this.currentUser;
}
}
return LoginMixin;
};
exports.loginMixin = loginMixin;
//# sourceMappingURL=login-mixin.js.map