import fs from 'fs'; import path from 'path'; import configUtil from '../utils/config.js'; import service from '../core/BaseService.js'; const __dirname = path.dirname(decodeURIComponent(new URL(import.meta.url).pathname.replace(/^\//, ''))); const configPath = path.join(__dirname, '../config.json'); class BindService extends service { constructor() { super(); this.generateCode = this.generateCode.bind(this); this.tokenInfo = this.tokenInfo.bind(this); this.getCurrentCode = this.getCurrentCode.bind(this); } generateCode(data) { const {app} = this; this.app.randomCode = data; this.app.emit('generateCode', data); } // 获取当前缓存的随机码(渲染进程挂载后调用,弥补 IPC 事件可能早于监听注册的时序问题) getCurrentCode() { const { app } = this; if (app.randomCode && app.randomCode.createdAt + app.randomCode.expiresIn * 1000 > Date.now()) { return app.randomCode.randomCode; } return null; } tokenInfo(data) { const {app} = this; app.config.token = data.jwtToken; app.config.refreshToken = data.refreshToken; app.config.tokenExpireTime = data.tokenExpireTime; configUtil.write(app.config); app.isBind = true; } }; export default BindService;