|
| 1 | +const { getGtk, getGuid } = require('../../../util/loginUtils'); |
| 2 | +const { refreshData } = require('../../../config/user-info'); |
| 3 | + |
| 4 | +module.exports = async ({ method = 'get', params = {}, option = {} }) => { |
| 5 | + const { ptqrtoken, qrsig } = params; |
| 6 | + if (!ptqrtoken || !qrsig) return { body: '参数错误' }; |
| 7 | + const url = `https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3A%2F%2Fgraph.qq.com%2Foauth2.0%2Flogin_jump&ptqrtoken=${ptqrtoken}&ptredirect=0&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-0-1711022193435&js_ver=23111510&js_type=1&login_sig=du-YS1h8*0GqVqcrru0pXkpwVg2DYw-DtbFulJ62IgPf6vfiJe*4ONVrYc5hMUNE&pt_uistyle=40&aid=716027609&daid=383&pt_3rd_aid=100497308&&o1vId=3674fc47871e9c407d8838690b355408&pt_js_version=v1.48.1`; |
| 8 | + const response = await fetch(url, { headers: { Cookie: `qrsig=${qrsig}` }}); |
| 9 | + const { data = '' } = await response.text(); |
| 10 | + let allCookie = []; |
| 11 | + const setCookie = cookies => { |
| 12 | + allCookie = [...allCookie, ...cookies.map(i => i.split(';')[0]).filter(i => i.split('=')[1])]; |
| 13 | + }; |
| 14 | + const refresh = data.includes('已失效') |
| 15 | + if (!data.includes('登录成功')) return { status: 200, isOk: false, refresh, message: refresh && '二维码已失效' || '未扫描二维码' }; |
| 16 | + |
| 17 | + // 获取p_skey 与gtk |
| 18 | + const checkSigUrl = data.match(/(?:'((?:https?|ftp):\/\/[^\s/$.?#].[^\s]*)')/g)[0].replaceAll('\'', ''); |
| 19 | + const checkSigRes = await fetch(checkSigUrl, { redirect: 'manual', headers: { Cookie: allCookie.join('; ') } }); |
| 20 | + const p_skey = checkSigRes.headers.get('Set-Cookie').match(/p_skey=([^;]+)/)[1]; |
| 21 | + const gtk = getGtk(p_skey); |
| 22 | + setCookie(checkSigRes.headers.get('Set-Cookie').split(';, ')); |
| 23 | + |
| 24 | + // authorize |
| 25 | + const authorizeUrl = 'https://graph.qq.com/oauth2.0/authorize'; |
| 26 | + const getAuthorizeData = (gtk) => { |
| 27 | + let data = new FormData() |
| 28 | + data.append('response_type', 'code') |
| 29 | + data.append('client_id', 100497308) |
| 30 | + data.append('redirect_uri', 'https://y.qq.com/portal/wx_redirect.html?login_type=1&surl=https://y.qq.com/') |
| 31 | + data.append('scope', 'get_user_info,get_app_friends') |
| 32 | + data.append('state', 'state') |
| 33 | + data.append('switch', '') |
| 34 | + data.append('from_ptlogin', 1) |
| 35 | + data.append('src', 1) |
| 36 | + data.append('update_auth', 1) |
| 37 | + data.append('openapi', '1010_1030') |
| 38 | + data.append('g_tk', gtk) |
| 39 | + data.append('auth_time', new Date) |
| 40 | + data.append('ui', getGuid()) |
| 41 | + return data |
| 42 | + }; |
| 43 | + |
| 44 | + const authorizeRes = await fetch(authorizeUrl, { |
| 45 | + redirect: 'manual', |
| 46 | + method: 'POST', |
| 47 | + body: getAuthorizeData(gtk), |
| 48 | + headers: { |
| 49 | + Cookie: allCookie.join('; ') |
| 50 | + } |
| 51 | + }); |
| 52 | + const code = authorizeRes.headers.get('Location').match(/[?&]code=([^&]+)/)[1]; |
| 53 | + |
| 54 | + // login |
| 55 | + const getFcgReqData = (g_tk, code) => { |
| 56 | + const data = { |
| 57 | + comm: { |
| 58 | + "g_tk":g_tk, |
| 59 | + "platform":"yqq", |
| 60 | + "ct":24, |
| 61 | + "cv":0 |
| 62 | + }, req: { |
| 63 | + "module":"QQConnectLogin.LoginServer", |
| 64 | + "method":"QQLogin", |
| 65 | + "param": { |
| 66 | + "code":code |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return JSON.stringify(data) |
| 71 | + }; |
| 72 | + |
| 73 | + const loginUrl = 'https://u.y.qq.com/cgi-bin/musicu.fcg'; |
| 74 | + const loginRes = await fetch(loginUrl, { |
| 75 | + method: 'POST', |
| 76 | + body: getFcgReqData(gtk, code), |
| 77 | + headers: { |
| 78 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 79 | + Cookie: allCookie.join('; ') |
| 80 | + } |
| 81 | + }); |
| 82 | + setCookie(loginRes.headers.get('Set-Cookie').split(';, ')); |
| 83 | + global.userInfo = { ...refreshData(allCookie.join('; ')) } |
| 84 | + |
| 85 | + return { status: 200, isOk: true, message: '登录成功' }; |
| 86 | +}; |
0 commit comments