建立資料中心後,更改登入功能
再登入後將用戶資料記錄在案
變更 dataCenter 對應的 action
dataCenter.js
const GetInstanceClass = require("./component/GetInstanceClass");
/**
* @description 資料都存在這邊
*
* @class DataCenter
*/
class DataCenter extends GetInstanceClass {
constructor() {
super();
this.state = {
user: {
id: "X",
username: "X",
name: "遊客"
}
};
}
/**
* @description 獲取對應資料
*
* @param {string} key
* @return {any} state data
* @memberof DataCenter
*/
getData(key) {
switch (key) {
case "user":
return this.state["user"];
default:
break;
}
}
/**
* @description 設定 state 資料
*
* @param {string} key
* @param {any} data state data
* @memberof DataCenter
*/
setData(key, data) {
switch (key) {
case "setUser":
this.state["user"] = data;
break;
default:
break;
}
}
/**
* @description 訂閱資料變更後要執行的程式,先預留到時候再寫
*
* @param {string} key state data
* @param {function} callBack
* @memberof DataCenter
*/
subscription(key, callBack) {
}
}
let dataCenter = DataCenter.getInstance();
module.exports = dataCenter;
接著更改 telegram 對應的 login 函式
interface\telegramItem.js
const { TelegramClient } = require("telegram");
const { StringSession } = require("telegram/sessions");
const { Logger } = require("telegram/extensions");
const input = require("input");
const GetInstanceClass = require("../component/GetInstanceClass");
const dataCeneter = require("../dataCenter");
Logger.setLevel("none"); // 這邊可以隱藏很多關於 telegram 的運作細節
class TelegramItem extends GetInstanceClass {
constructor() {
super();
this.client = null;
}
/**
* @description 初始化 telegram 實體
*
* @memberof TelegramItem
*/
init() {
this.client = new TelegramClient(
new StringSession(""),
Number(process.env.telegramId), // 這邊輸入你的 id
process.env.telegramHash, // 輸入你的 hash
{ connectionRetries: 5 }
);
}
/**
* @description 登入 telegram
*
* @return {*}
* @memberof TelegramClass
*/
async login() {
await this.client.start({
phoneNumber: async () => await input.text("請輸入電話?"),
password: async () => await input.text("請輸入密碼?"),
phoneCode: async () => await input.text("請輸入驗證碼?"),
onError: (err) => console.log("錯誤訊息 : " + err.errorMessage),
});
// console.log(); // Save this string to avoid logging in again
console.log("");
console.log("\x1b[36m", "成功登入");
console.log();
let user = await this.client.getMe();
dataCeneter.setData("setUser", {
id: user.id,
username: user.username,
name: user.lastName + user.firstName
})
this.client.session.save();
// 登入成功寄給你的 telegram 一個訊息
await this.client.sendMessage("me", {
message: "node sevret is online! " + new Date().toLocaleString(),
});
return true;
}
}
const telegramItem = TelegramItem.getInstance();
telegramItem.init();
module.exports = telegramItem;
然後更改你好前面對應的名稱
view\viewPrinter.js
const consoleItem = require("../interface/consoleItem");
const input = require("input");
const GetInstanceClass = require("../component/GetInstanceClass");
const dataCenter = require("../dataCenter");
class ViewPrinter extends GetInstanceClass{
constructor() {
super();
}
/**
* @description 歡迎你
*
* @memberof ViewPrinter
*/
async welcome() {
return new Promise((resolve) => {
let str = [
"=======================================================",
"=======================================================",
"============ ============",
"============ Welcome Node Telegram ============",
"============ ============",
"=======================================================",
"=======================================================",
];
console.log("\x1b[36m");
for (let i = 0; i < str.length; i++) {
setTimeout(() => {
console.log(str[i]);
if (i === str.length - 1) {
console.log("\x1b[0m");
resolve();
}
}, i * 100);
}
});
}
/**
* @description 主要畫面選單
*
* @memberof ViewPrinter
*/
async mainMenu() {
let option = ["登入", "離開"];
let selectOption = await input.select(dataCenter.getData("user").name + " 你好:", option);
return selectOption;
}
/**
* @description 按下去任何按鍵來繼續程式
*
* @return {*}
* @memberof InterfaceClass
*/
async pressToContinue() {
return new Promise((resolve) => {
const handler = () => {
process.stdin.setRawMode(false);
process.stdin.pause();
process.stdout.write("\n");
resolve();
};
process.stdin.resume();
process.stdin.setRawMode(true);
process.stdin.once("data", handler);
});
}
/**
* @description 清空畫面
*
* @memberof ViewPrinter
*/
async clearView() {
process.stdout.write(
process.platform === "win32" ? "\x1Bc" : "\x1B[2J\x1B[3J\x1B[H"
);
}
}
let viewPrinter = ViewPrinter.getInstance();
module.exports = viewPrinter;
這樣就有登入記憶 user 的功能了