Files
Mycontent/miniprogram/pages/super-article-editor/super-article-editor.js
2026-04-29 19:08:02 +08:00

89 lines
2.4 KiB
JavaScript

const app = getApp()
Page({
data: {
statusBarHeight: 44,
title: '',
content: '',
saving: false,
meNickname: '',
},
onLoad() {
this.setData({
statusBarHeight: app.globalData.statusBarHeight || 44,
meNickname: String(app.globalData.userInfo?.nickname || '我').trim() || '我',
})
this.ensureSuperIdentity()
},
async ensureSuperIdentity() {
const userId = app.globalData.userInfo?.id
if (!app.globalData.isLoggedIn || !userId) {
wx.showToast({ title: '请先登录', icon: 'none' })
setTimeout(() => wx.navigateBack(), 400)
return
}
try {
const res = await app.request({
url: `/api/miniprogram/my/super-stats?userId=${encodeURIComponent(userId)}`,
silent: true,
})
if (!(res?.success && res.data?.isSuperIndividual === true)) {
wx.showToast({ title: '仅超级个体可发文章', icon: 'none' })
setTimeout(() => wx.navigateBack(), 400)
}
} catch (_) {
wx.showToast({ title: '身份校验失败', icon: 'none' })
setTimeout(() => wx.navigateBack(), 400)
}
},
onTitleInput(e) {
this.setData({ title: e.detail.value || '' })
},
onContentInput(e) {
this.setData({ content: e.detail.value || '' })
},
insertMentionSelf() {
const nick = this.data.meNickname || '我'
this.setData({ content: `${this.data.content}@${nick} ` })
},
insertHashLink() {
this.setData({ content: `${this.data.content}#链接(https://)` })
},
async submitArticle() {
if (this.data.saving) return
const userId = app.globalData.userInfo?.id
const title = String(this.data.title || '').trim()
const content = String(this.data.content || '').trim()
if (!title || !content) {
wx.showToast({ title: '请填写标题和正文', icon: 'none' })
return
}
this.setData({ saving: true })
try {
const res = await app.request({
url: '/api/miniprogram/super/articles',
method: 'POST',
data: { userId, title, content },
})
if (!res?.success) throw new Error(res?.error || '发布失败')
wx.showToast({ title: '发布成功', icon: 'success' })
setTimeout(() => wx.navigateBack(), 400)
} catch (e) {
wx.showToast({ title: e.message || '发布失败', icon: 'none' })
} finally {
this.setData({ saving: false })
}
},
goBack() {
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/my/my' }) })
},
})