| 77 | }); |
| 78 | |
| 79 | async function BahamutLogin(retry = 3, interval = 1000) { //登录函数,拿到Set-Cookie |
| 80 | |
| 81 | //登录成功: {"success":true,"userid":"DGIE","nickname":"coco","gold":152769,"gp":0,"avatar":"https:\/\/avatar2.bahamut.com.tw\/avataruserpic\/dgie.png","avatar_s":"https:\/\/avatar2.bahamut.com.tw\/avataruserpic\/dgie_s.png","lv":6} |
| 82 | //账号错误: {"code":0,"message":"查無此人:SDFOUGB"} |
| 83 | //密码错误: {"code":0,"message":"帳號、密碼或驗證碼錯誤!"} |
| 84 | //验证码错误: {"code":0,"message":"驗證碼錯誤"} |
| 85 | |
| 86 | for (let i = 0; i < retry; i++) { //循环登录(默认三次) |
| 87 | if (i > 0) { |
| 88 | $.log('', `🔶尝试第${i+1}次登录...`); |
| 89 | await $.wait(interval); //延迟一秒 |
| 90 | }; |
| 91 | const reqUrl = { |
| 92 | url: 'https://api.gamer.com.tw/mobile_app/user/v3/do_login.php', //登录接口 |
| 93 | headers: { //请求头 |
| 94 | 'Cookie': 'ckAPP_VCODE=6666' //Cookie中的ckAPP_VCODE为必须 |
| 95 | }, |
| 96 | //请求体放入用户名和密码,并把它uri编码 |
| 97 | body: `uid=${encodeURIComponent($.uid)}&passwd=${encodeURIComponent($.pwd)}&vcode=6666${$.totp?`&twoStepAuth=${TOTP($.totp)}`:``}` |
| 98 | }; |
| 99 | const res = await $.http.post(reqUrl) //使用post请求查询 (兼容函数实际上返回Promise实例对象,以便后续调用时可以实现顺序执行异步函数) |
| 100 | .then(async (resp) => { //请求成功的处理 |
| 101 | const body = JSON.parse(resp.body); //解析响应体json为对象 |
| 102 | if (body.userid) { //如果成功返回用户信息 |
| 103 | $.BAHARUNE = JSON.stringify(resp.headers).split(/(BAHARUNE=\w+)/)[1]; |
| 104 | return `✅巴哈姆特登录成功`; |
| 105 | } else { //否则登录失败 (例如密码错误) |
| 106 | const failMsg = body.error ? body.error.message : null; //判断签到失败原因 |
| 107 | throw new Error(`${body.message||failMsg||'原因未知'}`); //带上原因抛出异常 |
| 108 | } |
| 109 | }).catch((err) => `❌登录失败\n❌${err.message || err}`); |
| 110 | $.log('', res.message || res); |
| 111 | if (res === `✅巴哈姆特登录成功`) { |
| 112 | break; //登录成功则跳出循环 |
| 113 | } else if (retry == i + 1) { //如果最后一次重试仍登录失败 |
| 114 | throw new Error(res.message || res); //抛出错误, 被调用该函数时的catch捕获, 脚本结束. |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | function BahamutSign() { //查询巴哈姆特签到Token |
| 120 | return $.http.get({ //使用get方法 (Promise实例对象) 查询签到Token |