| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { ref } from 'vue'
-
- export function useReadingProgress() {
- // 保存阅读进度
- const saveProgress = (novelId, chapterId, progress) => {
- try {
- const key = `reading_progress_${novelId}_${chapterId}`
- uni.setStorageSync(key, JSON.stringify(progress))
-
- // 同时保存到云端(如果有登录)
- if (uni.getStorageSync('token')) {
- uni.request({
- url: 'https://api.aiyadianzi.ltd/reading/progress',
- method: 'POST',
- data: {
- novelId,
- chapterId,
- ...progress
- },
- header: {
- 'Authorization': `Bearer ${uni.getStorageSync('token')}`
- }
- })
- }
- } catch (e) {
- console.error('保存阅读进度失败', e)
- }
- }
-
- // 加载阅读进度
- const loadProgress = (novelId, chapterId) => {
- return new Promise((resolve) => {
- try {
- // 先尝试从本地加载
- const key = `reading_progress_${novelId}_${chapterId}`
- const localData = uni.getStorageSync(key)
- if (localData) {
- resolve(JSON.parse(localData))
- return
- }
-
- // 如果登录了,尝试从云端加载
- if (uni.getStorageSync('token')) {
- uni.request({
- url: `https://api.aiyadianzi.ltd/reading/progress?novelId=${novelId}&chapterId=${chapterId}`,
- method: 'GET',
- header: {
- 'Authorization': `Bearer ${uni.getStorageSync('token')}`
- },
- success: (res) => {
- if (res.data.success && res.data.data) {
- resolve(res.data.data)
- } else {
- resolve(null)
- }
- },
- fail: () => resolve(null)
- })
- } else {
- resolve(null)
- }
- } catch (e) {
- console.error('加载阅读进度失败', e)
- resolve(null)
- }
- })
- }
-
- return {
- saveProgress,
- loadProgress
- }
- }
|