liufumingren 35fefaeddf 1
2026-07-28 23:46:46 +08:00

40 lines
1.2 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import configUtil from '../utils/config.js';
import service from '../core/BaseService.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
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;