cybertron-utils
Version:
cybertron components manage
48 lines (44 loc) • 1.15 kB
JavaScript
const axios = require('axios');
const chalk = require('chalk');
const { get, set } = require('../utils/config');
module.exports = {
command: 'login',
desc: '登陆',
handler(argv) {
const values = {};
const arr = [{
key: 'username',
text: '请输入账号:'
},
{
key: 'password',
text: '请输入密码:'
}];
let index = 0;
const url = get('url');
process.stdout.write(arr[index].text);
process.stdin.on('data', async (input) => {
//输入的字符最后肯定是一个回车符
input = input.toString().trim();
if (!input) {
return process.stdout.write(arr[index].text);
}
values[arr[index].key] = input;
index++;
if (index == arr.length) {
const res = await axios.post(`${url}/user/login`, values).then(res => res.data).catch(e => {
console.log(e);
return { code: 404, message: '请检查源地址是否有问题!' }
});
if (res.code == 200) {
set('token', res.data);
console.log(chalk.green(res.message))
} else {
console.log(chalk.red(res.message))
}
process.exit(0);
}
process.stdout.write(arr[index].text);
});
}
};