diff --git a/src/App.vue b/src/App.vue index dbeb0b3..01d6720 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,49 +1,54 @@ - +
+
+ 取消 + 我已登录 +
@@ -235,7 +239,7 @@ import readFile from '@/utils/readFile.js' import paresClip from '@/utils/paresClip.js' import readSQLite from '@/utils/readSQLite.js' import { Loading } from 'element-ui' -import { getBookshelf, getBookMarkList, getReviewList } from '@/utils/weread.js' +import { getNotebooklist, getBookMarkList, getReviewList } from '@/utils/weread.js' import axios from 'axios' export default { name: 'Options', @@ -271,8 +275,6 @@ export default { showDialog: false, cookie: null, loading: false, - logout: false, - firstLoad: false, } }, watch: { @@ -307,43 +309,21 @@ export default { this.options.noEmptyLine = true } }, + getNotebooklist(){ + getNotebooklist() + .then(res => { + this.showDialog = false + this.loading = false + this.handleBooksData(res) + }) + .catch((err) => { + this.loading = false + }) + }, importWeRead () { this.loading = true this.showDialog = true - - const { session } = require('electron').remote - const filter = { - urls: ['https://weread.qq.com/*'] - } - session.defaultSession.webRequest.onBeforeSendHeaders( - filter, - (details, callback) => { - if(!this.firstLoad){ - const cookie = details.requestHeaders.Cookie - this.cookie = cookie - if (!this.logout) { - getBookshelf(cookie) - .then(res => { - this.firstLoad = true - this.showDialog = false - this.loading = false - this.handleBooksData(res) - }) - .catch(({ errmsg }) => { - this.loading = false - this.firstLoad = false - if (errmsg === '登录超时') { - this.logout = true - window.location.reload() - } - }) - } else { - this.loading = false - } - } - callback(details) - } - ) + this.getNotebooklist() }, importAppleBooks () { const loadingInstance = Loading.service({ @@ -368,7 +348,7 @@ export default { }) }, handleBooksData (bookList) { - this.bookList = bookList + this.bookList = bookList.reverse() const data = bookList[0] if(data.bookId) this.selectChange(data.title) this.options.book = data.title diff --git a/src/utils/weread.js b/src/utils/weread.js index 7185785..4b93aef 100644 --- a/src/utils/weread.js +++ b/src/utils/weread.js @@ -1,11 +1,35 @@ -import { get_bookshelf, get_bookmarklist, get_reviewlist } from '@/utils/wereadRequest.js' +import { get_bookshelf, get_bookmarklist, get_reviewlist, get_notebooklist } from '@/utils/wereadRequest.js' -export function getBookshelf(cookie) { +function clearCookies(){ + const { session } = require('electron').remote + return new Promise((r, j) => { + session.defaultSession.cookies.get({ url: 'https://weread.qq.com/' }).then(res => { + try { + for (var i = 0; i < res.length; i++) { + session.defaultSession.cookies.remove( + 'https://weread.qq.com/', + res[i].name, + (error) => { + //判断是否error + console.log('cookie 清除出错') + } + ) + } + r() + } catch (error) { + j() + } + }).catch(() => j()) + }) +} + + +export function getNotebooklist() { return new Promise((resolve, reject) => { - get_bookshelf(cookie).then(res => { - const list = res.recentBooks.map(i => { + get_notebooklist().then(res => { + const list = res.books.map(i => { return { - title: i.title, + title: i.book.title, loaded: false, bookId: i.bookId, texts: [] @@ -13,6 +37,7 @@ export function getBookshelf(cookie) { }) resolve(list) }).catch(e => { + if(e === 401) clearCookies() reject(e) }) }) @@ -36,9 +61,9 @@ export function getBookMarkList(bookId) { }) } -export function getReviewList(params, cookie) { +export function getReviewList(params, ) { return new Promise((resolve, reject) => { - get_reviewlist(params, cookie).then(res => { + get_reviewlist(params).then(res => { let list = res.reviews.map(i => { return { text: i.review.abstract, diff --git a/src/utils/wereadRequest.js b/src/utils/wereadRequest.js index e67d4b5..c6acaf1 100644 --- a/src/utils/wereadRequest.js +++ b/src/utils/wereadRequest.js @@ -1,25 +1,27 @@ import axios from 'axios' axios.defaults.baseURL = 'https://i.weread.qq.com' -function getUid (cookie) { - let uid - cookie.split(';').forEach(ele => { - let [key, value] = ele.trim().split('=') - if (key === 'wr_vid') { - uid = Number(value) - } +function getUserVid() { + return new Promise((r, j) => { + const { session } = require('electron').remote + session.defaultSession.cookies.get({ url: 'https://weread.qq.com/' }).then(res => { + if (res && res.length) { + const vid = res.find(i => i.name === 'wr_vid') + if (vid) r(vid.value) + j(false) + } + j(false) + }) }) - return uid } + const headers = { Host: 'i.weread.qq.com', Connection: 'keep-alive', 'Upgrade-Insecure-Requests': 1, - 'User-Agent': - 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', - Accept: - 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', } @@ -78,18 +80,23 @@ export const get_bestbookmarks = bookId => { }) } // 获取书架上的书籍列表 -export const get_bookshelf = (cookie) => { +export const get_bookshelf = async () => { + const userVid = await getUserVid() return axios({ method: 'get', url: '/shelf/friendCommon', headers, params: { - userVid: getUid(cookie) + userVid } }) } // 获取你的所有有笔记本书单 -export const get_notebooklist = () => { +export const get_notebooklist = async () => { + const userVid = await getUserVid().catch(e => {}) + if (!userVid) { + return Promise.reject(401) + } return axios({ method: 'get', headers, @@ -109,14 +116,15 @@ export const get_bookinfo = bookId => { } // 获取某本书的批注 -export const get_reviewlist = (params, cookie) => { +export const get_reviewlist = async (params) => { + const userVid = await getUserVid() return axios({ method: 'get', url: '/web/review/list', baseURL: 'https://weread.qq.com', headers, params: { - userVid: getUid(cookie), + userVid, ...params } })