-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud.js
More file actions
238 lines (215 loc) · 7.14 KB
/
cloud.js
File metadata and controls
238 lines (215 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// ====================================================
// 云端数据层 · LeanCloud 接入
// 支持9人多端实时同步:打包清单 / 费用 / 笔记
// ====================================================
// 使用说明:
// 1. 前往 https://console.leancloud.cn 注册并创建应用
// 2. 进入「设置 → 应用凭证」复制 App ID 和 App Key
// 3. 将下方 LC_APP_ID / LC_APP_KEY / LC_SERVER_URL 替换为你的真实值
// 4. 在「数据存储 → 结构化数据」中确认以下 Class 存在:
// - TripState (存储打包/准备进度等状态)
// - Expense (费用记录)
// - Note (旅行笔记)
// - PackingItem (打包清单自定义项)
// ====================================================
const LC_APP_ID = 'YOUR_LEANCLOUD_APP_ID'; // ← 替换
const LC_APP_KEY = 'YOUR_LEANCLOUD_APP_KEY'; // ← 替换
const LC_SERVER_URL = 'https://please-replace.api.lncldglobal.com'; // ← 替换
// ---- 状态 ----
const cloudState = {
ready: false,
currentUser: null, // { name, color, avatar }
listeners: {},
};
// ---- 初始化 ----
function initCloud() {
if (typeof AV === 'undefined') {
console.warn('[Cloud] LeanCloud SDK 未加载,切换离线模式');
cloudState.ready = false;
return;
}
try {
AV.init({ appId: LC_APP_ID, appKey: LC_APP_KEY, serverURL: LC_SERVER_URL });
cloudState.ready = true;
console.log('[Cloud] LeanCloud 初始化成功');
} catch(e) {
console.warn('[Cloud] 初始化失败,切换离线模式', e);
cloudState.ready = false;
}
}
// ---- 用户身份 ----
function setCurrentUser(member) {
cloudState.currentUser = member;
localStorage.setItem('hainan_user', JSON.stringify(member));
console.log('[Cloud] 当前用户:', member.name);
}
function getStoredUser() {
try {
const raw = localStorage.getItem('hainan_user');
return raw ? JSON.parse(raw) : null;
} catch(e) { return null; }
}
function clearUser() {
cloudState.currentUser = null;
localStorage.removeItem('hainan_user');
}
// ====================================================
// 通用 CRUD
// ====================================================
// 读取某个 Class 下所有记录
async function cloudFetchAll(className) {
if (!cloudState.ready) return null;
try {
const query = new AV.Query(className);
query.limit(1000);
query.descending('createdAt');
const results = await query.find();
return results.map(r => ({ ...r.toJSON(), _id: r.id }));
} catch(e) {
console.warn('[Cloud] fetchAll error', className, e);
return null;
}
}
// 保存或更新一条记录(有 _id 则更新,否则新建)
async function cloudSave(className, data) {
if (!cloudState.ready) return null;
try {
let obj;
if (data._id) {
obj = AV.Object.createWithoutData(className, data._id);
} else {
const Cls = AV.Object.extend(className);
obj = new Cls();
}
const { _id, ...fields } = data;
Object.entries(fields).forEach(([k, v]) => obj.set(k, v));
if (cloudState.currentUser) obj.set('editor', cloudState.currentUser.name);
await obj.save();
return obj.id;
} catch(e) {
console.warn('[Cloud] save error', className, e);
return null;
}
}
// 删除一条记录
async function cloudDelete(className, objectId) {
if (!cloudState.ready) return false;
try {
const obj = AV.Object.createWithoutData(className, objectId);
await obj.destroy();
return true;
} catch(e) {
console.warn('[Cloud] delete error', className, e);
return false;
}
}
// ====================================================
// 打包清单同步
// ====================================================
async function syncPackingToCloud(packingData) {
if (!cloudState.ready) return;
// 用单条记录保存整个打包状态(覆盖式)
try {
const query = new AV.Query('TripState');
query.equalTo('key', 'packing');
let obj = await query.first();
if (!obj) {
const TripState = AV.Object.extend('TripState');
obj = new TripState();
obj.set('key', 'packing');
}
obj.set('data', JSON.stringify(packingData));
obj.set('editor', cloudState.currentUser?.name || 'unknown');
obj.set('updatedBy', cloudState.currentUser?.name || 'unknown');
await obj.save();
console.log('[Cloud] 打包清单已同步');
} catch(e) {
console.warn('[Cloud] syncPacking error', e);
}
}
async function fetchPackingFromCloud() {
if (!cloudState.ready) return null;
try {
const query = new AV.Query('TripState');
query.equalTo('key', 'packing');
const obj = await query.first();
if (obj) return JSON.parse(obj.get('data') || '{}');
return null;
} catch(e) {
console.warn('[Cloud] fetchPacking error', e);
return null;
}
}
// ====================================================
// 费用记录同步
// ====================================================
async function fetchExpensesFromCloud() {
return await cloudFetchAll('Expense');
}
async function saveExpenseToCloud(expense) {
return await cloudSave('Expense', expense);
}
async function deleteExpenseFromCloud(id) {
return await cloudDelete('Expense', id);
}
// ====================================================
// 旅行笔记同步
// ====================================================
async function fetchNotesFromCloud() {
return await cloudFetchAll('Note');
}
async function saveNoteToCloud(note) {
return await cloudSave('Note', note);
}
async function deleteNoteFromCloud(id) {
return await cloudDelete('Note', id);
}
// ====================================================
// 准备进度同步
// ====================================================
async function syncPrepToCloud(prepData) {
if (!cloudState.ready) return;
try {
const query = new AV.Query('TripState');
query.equalTo('key', 'prep');
let obj = await query.first();
if (!obj) {
const TripState = AV.Object.extend('TripState');
obj = new TripState();
obj.set('key', 'prep');
}
obj.set('data', JSON.stringify(prepData));
obj.set('editor', cloudState.currentUser?.name || 'unknown');
await obj.save();
} catch(e) {
console.warn('[Cloud] syncPrep error', e);
}
}
async function fetchPrepFromCloud() {
if (!cloudState.ready) return null;
try {
const query = new AV.Query('TripState');
query.equalTo('key', 'prep');
const obj = await query.first();
if (obj) return JSON.parse(obj.get('data') || '{}');
return null;
} catch(e) { return null; }
}
// ====================================================
// 离线降级:使用 localStorage
// ====================================================
function localSave(key, data) {
try { localStorage.setItem('hainan_' + key, JSON.stringify(data)); } catch(e) {}
}
function localLoad(key) {
try {
const raw = localStorage.getItem('hainan_' + key);
return raw ? JSON.parse(raw) : null;
} catch(e) { return null; }
}
// ====================================================
// 配置是否已填写检测
// ====================================================
function isCloudConfigured() {
return LC_APP_ID !== 'YOUR_LEANCLOUD_APP_ID' && LC_APP_KEY !== 'YOUR_LEANCLOUD_APP_KEY';
}