关于node.js:node练习

4次阅读

共计 953 个字符,预计需要花费 3 分钟才能阅读完成。

1、node 实现登录

/**
 * 登录零碎
 * @param {String} username 用户名
 * @param {String} password 明码
 */
async function login(username, password) {const res = await fetch(`${BASE_URL}/websys/xxx/login.do`, {
        credentials: 'include',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Referer': 'http://xxx.com.cn/websys/xxx/index.html'
        },
        body: qs.stringify({
            name: username,
            pwd: password
        })
    })
    const matchArr = res.headers.get('set-cookie').match(new RegExp('sys_auth?=([^;]+)'));
    const sys_auth = matchArr && matchArr[1];

    if (!sys_auth) {throw new Error('登录谬误,请确认用户名或明码是否正确');
    }

    cookie = `SITE=alm01; ws_auth=${ws_auth};` // 全局保留登录 token
}

/**
 * 根据登录 token 申请后续接口
 */
async function getPlans(planId) {return await fetch(`${BASE_URL}/websys/xxx/${planId}/plan/?planList`, {
            credentials: 'include',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Cookie': cookie
            },
            body: qs.stringify({
                pageSize: 10,
                pageNo: 5,
            })
        })
        .then(res => res.json())
        .then(data => data.plans.rows);
}

2、node 操作 excel

正文完
 0