74 lines
2.1 KiB
JavaScript
Executable File
74 lines
2.1 KiB
JavaScript
Executable File
const os = require('os')
|
|
const sd = require('silly-datetime')
|
|
const request = require('request')
|
|
const config = require('./config')
|
|
let UserInfo = {}
|
|
|
|
/**
|
|
* 获取用户电脑的基本信息
|
|
*/
|
|
const GetUserInfo = () =>{
|
|
UserInfo = {
|
|
homedir: os.homedir() || 'null',
|
|
hostname: os.hostname() || 'null',
|
|
filepath: __filename,
|
|
addtime: sd.format(new Date(), 'YYYY-MM-DD HH:mm'),
|
|
cpu: os.cpus()[0].model || 'null',
|
|
totalmem: bytesToSize(os.totalmem() || 'null' ),
|
|
platform: os.platform() || 'null',
|
|
release: os.release() || 'null'
|
|
}
|
|
select(UserInfo.hostname)
|
|
}
|
|
|
|
/**
|
|
* 收集用户的电脑基本信息,不涉及个人隐私,只为更好提供服务。
|
|
* @param {*object} info 用户信息
|
|
*/
|
|
const insert = (info) => {
|
|
request({
|
|
url: config.url,
|
|
method: "POST",
|
|
json: true,
|
|
headers: config.headers,
|
|
body: info
|
|
}, (error, response, body) => {
|
|
// if (!error && response.statusCode == 201){
|
|
// console.log('success')
|
|
// }
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 查找对应 hostname 的主机用户在云端数据库是否存在,
|
|
* 存在表示已经不是第一次 whtml init 初始化项目
|
|
* 所以不需要再次收集用户信息,不存在开始收集信息
|
|
* @param {*string} name
|
|
*/
|
|
const select = (name) => {
|
|
var par = encodeURIComponent(JSON.stringify({ "hostname": name }))
|
|
request({
|
|
url: `${config.url}?where=${par}`,
|
|
method: "GET",
|
|
json: true,
|
|
headers: config.headers
|
|
}, (error, response, body) => {
|
|
if (body.results.length == 0){
|
|
insert(UserInfo)
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 将字节转换为G
|
|
* @param {*string} bytes 字节数
|
|
*/
|
|
const bytesToSize = (bytes) => {
|
|
if (bytes === 0) return '0 B'
|
|
var k = 1024
|
|
sizes = ['B','KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
return (bytes / Math.pow(k, i)) + ' ' + sizes[i] //return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
|
|
}
|
|
|
|
module.exports = { GetUserInfo } |