diff --git a/app.js b/app.js index e953dc4..63e0f1e 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,29 @@ -// app.js +import request from './utils/request'; + App({ onLaunch() { - console.log('App Launch'); + // Login + wx.login({ + success: res => { + // Send res.code to backend to swap for openId, sessionKey, unionId + if (res.code) { + request.get('/auth/miniLogin', { code: res.code }).then(data => { + // Assuming the token is in data.token or data itself + const token = data.token || data; + if (token && typeof token === 'string') { + wx.setStorageSync('token', token); + console.log('Login successful, token stored'); + } else { + console.warn('Login response did not contain a valid token string', data); + } + }).catch(err => { + console.error('Login failed', err); + }); + } else { + console.error('wx.login failed: ' + res.errMsg); + } + } + }); }, globalData: { userInfo: null diff --git a/assets/garden_banner.png b/assets/garden_banner.png deleted file mode 100644 index d4c22f4..0000000 Binary files a/assets/garden_banner.png and /dev/null differ diff --git a/assets/monstera_plant_1769757312755.png b/assets/monstera_plant_1769757312755.png deleted file mode 100644 index e989027..0000000 Binary files a/assets/monstera_plant_1769757312755.png and /dev/null differ diff --git a/assets/snake_plant_1769757638773.png b/assets/snake_plant_1769757638773.png deleted file mode 100644 index 187fec5..0000000 Binary files a/assets/snake_plant_1769757638773.png and /dev/null differ diff --git a/assets/succulent_garden_1769757406309.png b/assets/succulent_garden_1769757406309.png deleted file mode 100644 index 4d1b0c2..0000000 Binary files a/assets/succulent_garden_1769757406309.png and /dev/null differ diff --git a/pages/community/create/index.js b/pages/community/create/index.js index ed3e6bb..05adaf3 100644 --- a/pages/community/create/index.js +++ b/pages/community/create/index.js @@ -1,16 +1,15 @@ // pages/community/create/index.js -import { MOCK_POSTS } from '../../../utils/mockData'; +import request from '../../../utils/request'; Page({ data: { content: '', images: [], - canPublish: false, + canPublish: false, // Only depends on content autoFocus: true, location: '', selectedTopics: [], suggestedTopics: ['植物养护', '多肉日记', '绿植分享', '花卉美照', '阳台花园', '新手入门'], - hasDraft: false, showImageSheet: false, imageSheetItems: [ { label: '拍照', value: 'camera' }, @@ -19,60 +18,18 @@ Page({ }, onLoad() { - // Check for saved draft - this.loadDraft(); + // No draft loading }, onUnload() { - // Save draft if there's content - this.saveDraft(); - }, - - loadDraft() { - try { - const draft = wx.getStorageSync('post_draft'); - if (draft && (draft.content || draft.images.length > 0)) { - this.setData({ - content: draft.content || '', - images: draft.images || [], - selectedTopics: draft.selectedTopics || [], - canPublish: draft.content && draft.content.trim().length > 0, - hasDraft: true - }); - } - } catch (e) { - console.log('No draft found'); - } - }, - - saveDraft() { - if (this.data.content || this.data.images.length > 0) { - try { - wx.setStorageSync('post_draft', { - content: this.data.content, - images: this.data.images, - selectedTopics: this.data.selectedTopics - }); - } catch (e) { - console.log('Failed to save draft'); - } - } - }, - - clearDraft() { - try { - wx.removeStorageSync('post_draft'); - } catch (e) { - console.log('Failed to clear draft'); - } + // No draft saving }, onContentInput(e) { const content = e.detail.value; this.setData({ content, - canPublish: content.trim().length > 0, - hasDraft: false + canPublish: content.trim().length > 0 }); }, @@ -109,8 +66,7 @@ Page({ success: (res) => { const newImages = res.tempFiles.map(f => f.tempFilePath); this.setData({ - images: [...this.data.images, ...newImages], - hasDraft: false + images: [...this.data.images, ...newImages] }); } }); @@ -130,8 +86,7 @@ Page({ success: (res) => { const newImage = res.tempFiles[0].tempFilePath; this.setData({ - images: [...this.data.images, newImage], - hasDraft: false + images: [...this.data.images, newImage] }); } }); @@ -200,74 +155,74 @@ Page({ }); }, - insertEmoji() { - // Simple emoji picker simulation - const emojis = ['🌱', '🌿', '🍀', '🌵', '🌻', '🌺', '🌸', '🌼', '🪴', '🌲']; - wx.showActionSheet({ - itemList: emojis, - success: (res) => { - const emoji = emojis[res.tapIndex]; - this.setData({ - content: this.data.content + emoji, - canPublish: true - }); - } - }); - }, - handleCancel() { - if (this.data.content || this.data.images.length > 0) { - wx.showModal({ - title: '保存草稿', - content: '是否保存当前内容为草稿?', - cancelText: '不保存', - confirmText: '保存', - success: (res) => { - if (res.confirm) { - this.saveDraft(); - wx.showToast({ title: '已保存草稿', icon: 'success' }); - setTimeout(() => wx.navigateBack(), 500); - } else { - this.clearDraft(); - wx.navigateBack(); - } - } - }); - } else { - wx.navigateBack(); - } + wx.navigateBack(); }, - handlePublish() { - if (!this.data.canPublish) { + async handlePublish() { + if (!this.data.content || !this.data.content.trim()) { wx.showToast({ title: '请输入内容', icon: 'none' }); return; } - // Content already includes topics as hashtags - const finalContent = this.data.content.trim(); + wx.showLoading({ title: '发布中...', mask: true }); - // Create new post - const newPost = { - id: Date.now().toString(), - user: '我的花园', - content: finalContent, - images: this.data.images, - time: '刚刚', - likes: [], - comments: [] - }; + try { + // 1. Upload Images + const ossIds = []; + const images = this.data.images; - // Add to global mock data (at the beginning) - MOCK_POSTS.unshift(newPost); + if (images.length > 0) { + const uploadPromises = images.map(filePath => { + return request.upload(filePath).then(res => { + // Res structure: { file: { id: "...", url: "..." } } + return res && res.file ? res.file.id : null; + }); + }); - // Clear draft - this.clearDraft(); + const uploadedIds = await Promise.all(uploadPromises); - wx.showToast({ title: '发布成功', icon: 'success' }); + uploadedIds.forEach(id => { + if (id) ossIds.push(id); + }); - setTimeout(() => { - wx.navigateBack(); - }, 1000); + if (images.length > 0 && ossIds.length === 0) { + throw new Error('图片上传失败'); + } + } + + // 2. Publish Post + // Title is removed from UI. Using content snippet or default title. + const content = this.data.content.trim(); + const title = content.length > 20 ? content.substring(0, 20) + '...' : content; + + const payload = { + title: title || '新动态', // Fallback title + content: content, + location: this.data.location || '', + ossIds: ossIds + }; + + await request.post('/post/publish', payload); + + wx.hideLoading(); + wx.showToast({ title: '发布成功', icon: 'success' }); + + // Refresh previous page + const pages = getCurrentPages(); + const prevPage = pages[pages.length - 2]; + if (prevPage && prevPage.onRefresh) { + prevPage.onRefresh(); + } + + setTimeout(() => { + wx.navigateBack(); + }, 1000); + + } catch (err) { + wx.hideLoading(); + console.error('Publish failed', err); + wx.showToast({ title: '发布失败', icon: 'none' }); + } } }) diff --git a/pages/community/create/index.wxml b/pages/community/create/index.wxml index f6b18e3..e52630d 100644 --- a/pages/community/create/index.wxml +++ b/pages/community/create/index.wxml @@ -11,6 +11,8 @@ + +