-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetTokenDaluon.js
More file actions
75 lines (62 loc) · 2.09 KB
/
getTokenDaluon.js
File metadata and controls
75 lines (62 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const axios = require("axios");
const fs = require("fs");
const pLimit = require("p-limit");
const BASE_URL = "http://live-rest-api-lb-271378262.ap-southeast-1.elb.amazonaws.com";
const FILE_PATH = "tokens.json";
// const time = "1751365760069"; // oris
// const time = "1751529734750"; // test1
// const time = "1751530317891"; // test1
const time = "1751531995463"; // test1
const password = "Orics@727965";
const from = 1;
const to = 19978;
const limit = pLimit(30);
async function loginUser(userNo, tokens) {
// const username = `0107_${time}_test${10000 + userNo}`; // oris
const username = `0107_${time}_test1${10000 + userNo}`; // test1
try {
const res = await axios.post(`${BASE_URL}/customer`, {
username,
password,
});
const token = res?.data?.data?.accessToken;
if (token) {
tokens.push(token);
const count = tokens.length;
console.log(`✅ Token OK: ${username} (${count})`);
fs.writeFileSync(FILE_PATH, JSON.stringify(tokens, null, 2), "utf-8");
} else {
console.warn(`⚠️ Không lấy được token cho ${username}`);
}
} catch (err) {
console.error(`❌ Lỗi login ${username}:`, err?.response?.data || err?.message);
}
}
async function getOrCreateTokens() {
let tokens = [];
// Đọc file nếu có
if (fs.existsSync(FILE_PATH)) {
try {
const content = fs.readFileSync(FILE_PATH, "utf-8");
const parsed = JSON.parse(content);
if (Array.isArray(parsed)) {
tokens = parsed;
if (tokens.length >= to - from + 1) {
console.log("✅ Đã sử dụng lại token từ file.");
return tokens;
}
}
} catch (err) {
console.warn("⚠️ File tokens.json bị lỗi, sẽ tạo lại.");
}
}
console.log("🔐 Đang tạo token mới...");
const promises = [];
for (let userNo = from + tokens.length; userNo <= to; userNo++) {
promises.push(limit(() => loginUser(userNo, tokens)));
}
await Promise.all(promises);
console.log(`✅ Đã hoàn tất, tổng cộng ${tokens.length} token.`);
return tokens;
}
getOrCreateTokens();