youzanyun-devtool-worker
Version:
- web - ws - proxy
77 lines (76 loc) • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const spring4js_nodejs_1 = require("spring4js-nodejs");
let UserService = class UserService {
constructor() {
this._changeUserEvent = new spring4js_nodejs_1.Emitter();
this.onChangeUserEvent = this._changeUserEvent.registerEvent;
this.userInfo = {};
this.sidCache = '';
}
async start() {
let dataDir = this.configService.getDataDir();
let authPath = path_1.default.resolve(dataDir, "auth.json");
this.authPath = authPath;
let exists = await fs_extra_1.default.pathExists(authPath);
if (exists) {
try {
this.userInfo = await fs_extra_1.default.readJson(authPath);
}
catch (e) {
}
}
}
async changeUserInfo(sid, userName) {
this.sidCache = sid;
this._changeUserEvent.fire({
success: true,
userName,
});
}
async setUserInfo(userName) {
await fs_extra_1.default.writeJson(this.authPath, { sid: this.sidCache, userName });
this.userInfo = await fs_extra_1.default.readJson(this.authPath);
let container = global.container;
let requestService = await container.getServiceInstance('requestService');
await requestService.setRequestCookie(this.sidCache);
}
async getSid() {
return this.userInfo.sid;
}
async getUserInfo() {
const _a = this.userInfo, { sid } = _a, otherInfo = tslib_1.__rest(_a, ["sid"]);
return otherInfo;
}
async getUserInfoFromServer(projectId) {
let container = global.container;
let requestService = await container.getServiceInstance('requestService');
let pro = await this.projectService.getProjectById(projectId);
const resData = await requestService.get('https://diy.youzanyun.com/api/custom/devtool/app/auth', {
params: {
appName: pro.proName
},
});
if (resData.code == 200) {
return resData.data;
}
throw new Error(resData.message || resData.msg);
}
async logout() {
await fs_extra_1.default.remove(this.authPath);
return true;
}
};
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], UserService.prototype, "configService", void 0);
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], UserService.prototype, "projectService", void 0);
UserService = tslib_1.__decorate([
spring4js_nodejs_1.Service()
], UserService);
exports.default = UserService;