const app = getApp() Page({ data: { popupShow: false, popupType: '', popupParams: { position: 'bottom', } as any, topicShow: false, replayShow: false, fileList: [] as any[], topicList: [] as any[], categoryList: [] as any[], selectedTopicIds: [] as number[], selectedTopics: [] as any[], selectedCategoryIds: [] as number[], title: '', content: '', visibility: 1, isEdit: false, editPostId: '', }, onLoad(options: any) { if (options && options.id) { this.setData({ isEdit: true, editPostId: options.id }) } app.waitLogin({ type: 1 }).then(() => { this.getTopicList() this.getCategoryList() if (this.data.isEdit) { this.getPostDetail() } }) }, getTopicList() { wx.ajax({ method: 'GET', url: '?r=psvt/topic/top-list', }).then((res: any) => { this.setData({ topicList: res.list || [], selectedTopics: this.computeSelectedTopics(this.data.selectedTopicIds, res.list || []), }) }) }, getCategoryList() { wx.ajax({ method: 'GET', url: '?r=psvt/wall/index-category', }).then((res: any) => { this.setData({ categoryList: res.list || [], }) }) }, getPostDetail() { const { editPostId } = this.data wx.ajax({ method: 'GET', url: '?r=psvt/post/detail', data: { id: editPostId }, }).then((res: any) => { if (!res) return const fileList = (res.attachments || []).map((item: any) => { if (item.attachmentType == 2) { return { id: item.id, fileType: 'video', url: item.videoCoverUrl || '', videoUrl: item.attachmentUrl || '', } } return { id: item.id, fileType: 'image', url: item.attachmentUrl || '', } }) this.setData({ title: res.title || '', content: res.content || '', visibility: res.visibility || 1, selectedTopicIds: (res.topics || []).map((t: any) => t.topicId), selectedTopics: this.computeSelectedTopics( (res.topics || []).map((t: any) => t.topicId), this.data.topicList, ), selectedCategoryIds: (res.categories || []).map((c: any) => c.categoryId), fileList, }) }) }, onTitleInput(e: any) { this.setData({ title: e.detail.value }) }, onContentInput(e: any) { this.setData({ content: e.detail.value }) }, handleTopicTap(e: any) { const id = Number(e.currentTarget.dataset.id) const selectedTopicIds = [...this.data.selectedTopicIds] const index = selectedTopicIds.indexOf(id) if (index > -1) { selectedTopicIds.splice(index, 1) } else { selectedTopicIds.push(id) } this.setData({ selectedTopicIds, selectedTopics: this.computeSelectedTopics(selectedTopicIds, this.data.topicList), }) }, handleRemoveTopic(e: any) { const id = Number(e.currentTarget.dataset.id) const selectedTopicIds = this.data.selectedTopicIds.filter((tid) => tid !== id) this.setData({ selectedTopicIds, selectedTopics: this.computeSelectedTopics(selectedTopicIds, this.data.topicList), }) }, computeSelectedTopics(ids: number[], list: any[]) { return ids.map((id) => list.find((t) => String(t.id) === String(id))).filter((t) => t) }, showTopicPopup() { this.setData({ topicShow: true }) }, topicPopupClose() { this.setData({ topicShow: false }) }, handleCategoryTap(e: any) { const id = Number(e.currentTarget.dataset.id) const selectedCategoryIds = [...this.data.selectedCategoryIds] const index = selectedCategoryIds.indexOf(id) if (index > -1) { selectedCategoryIds.splice(index, 1) } else { selectedCategoryIds.push(id) } this.setData({ selectedCategoryIds }) }, handleVisibilityChange(e: any) { this.setData({ visibility: Number(e.currentTarget.dataset.value) }) }, buildAttachments() { const { fileList } = this.data return fileList.map((item: any, index: number) => { const isVideo = item.fileType === 'video' const attachment: any = { attachmentType: isVideo ? 2 : 1, attachmentUrl: isVideo ? item.videoUrl : item.url, sortOrder: index, } if (isVideo) { attachment.videoCoverUrl = item.url } if (item.id) { attachment.id = item.id } return attachment }) }, handleSaveDraft() { this.submitPost(0) }, handleSubmit() { this.submitPost(1) }, submitPost(postStatus: number) { const { title, content, visibility, selectedTopicIds, selectedCategoryIds, isEdit, editPostId } = this.data if (!title.trim()) { wx.showToast({ title: '请添加标题', icon: 'none' }) return } const data: any = { title, content, postStatus, visibility, topicIds: selectedTopicIds, categoryIds: selectedCategoryIds, attachments: this.buildAttachments(), } if (isEdit) { data.postId = Number(editPostId) } wx.ajax({ method: 'POST', url: isEdit ? '?r=psvt/my-post/update' : '?r=psvt/post/create', data, }).then(() => { wx.showToast({ title: postStatus === 0 ? '草稿已保存' : '发布成功', icon: 'success', }) setTimeout(() => { wx.navigateBack() }, 1000) }) }, handlePopupOk() { const { popupType } = this.data if (popupType === 'argument') { wx.ajax({ method: 'POST', url: '?r=wtx/user/agree-guest-privacy', data: { WorkerId: app.globalData.scene?.workerId || '', }, }).then(() => { this.setData({ popupShow: false, popupType: '', popupParams: {}, }) const waitBindDoctorId = app.globalData.waitBindDoctorId if (waitBindDoctorId) { this.handleBindDoctor(waitBindDoctorId) } }) } if (popupType === 'conformBindDoctorConform') { this.setData({ popupShow: false, }) wx.ajax({ method: 'POST', url: '?r=wtx/account/wait-bind-doctor', data: { doctorId: app.globalData.waitBindDoctorId, }, }).then(() => { wx.navigateTo({ url: `/patient/pages/login/index`, }) }) } }, handlePopupCancel() { const { popupType } = this.data if (popupType === 'argument') { wx.exitMiniProgram() } if (popupType === 'conformBindDoctorConform') { this.setData({ popupShow: false, }) wx.ajax({ method: 'POST', url: '?r=wtx/account/wait-bind-doctor', data: { doctorId: app.globalData.waitBindDoctorId, }, }).then(() => { app.globalData.waitBindDoctorId = '' }) } }, handleWechatWork() { wx.navigateTo({ url: '/pages/wechatWork/index', }) }, handleSetData(e) { const { fileList } = this.data this.setData({ fileList: [...fileList, ...e.detail], }) }, handleDeleteFile(e) { const { index } = e.detail const { fileList } = this.data this.setData({ fileList: fileList.filter((_, i) => i !== index), }) }, handleBack() { wx.navigateBack() }, }) export {}