mattersHandle.js 3.1 KB
import api from '~/api/api'
import { getMatterDetailFields, normalizeMatterItem } from '~/utils/matterItem'

Page({
  data: {
    navBarHeight: 0,
    task: null,
    reply: '',
    detailFields: [],
    submitting: false,
    isReadonly: false,
    loading: true,
  },

  onLoad(options) {
    const id = options?.id
    if (!id) {
      wx.showToast({ title: '缺少任务信息', icon: 'none' })
      setTimeout(() => wx.navigateBack(), 1500)
      return
    }
    this.taskId = id
    this.loadDetail()
  },

  async loadDetail() {
    this.setData({ loading: true })
    wx.showLoading({ title: '加载中', mask: true })
    try {
      const res = await api.getTaskCenterDetail({ id: this.taskId })
      if (res?.code !== '0' && res?.code !== 0) {
        wx.showToast({
          title: res?.message || '加载失败',
          icon: 'none',
        })
        setTimeout(() => wx.navigateBack(), 1500)
        return
      }
      const task = normalizeMatterItem(res.data || {}, 0)
      this.setData({
        task,
        detailFields: getMatterDetailFields(task.taskType),
        isReadonly: task.completed,
        reply: task.replyText || '',
        loading: false,
      })
    } catch (e) {
      wx.showToast({ title: '加载失败', icon: 'none' })
      setTimeout(() => wx.navigateBack(), 1500)
    } finally {
      wx.hideLoading()
      this.setData({ loading: false })
    }
  },

  navbarReady(e) {
    this.setData({ navBarHeight: e.detail.height })
  },

  onBack() {
    wx.navigateBack()
  },

  onReplyInput(e) {
    this.setData({ reply: e.detail.value || '' })
  },

  async submit() {
    const { reply, task, submitting } = this.data
    if (submitting || !task) return

    const content = (reply || '').trim()
    if (!content) {
      wx.showToast({ title: '请输入专项任务回复', icon: 'none' })
      return
    }

    if (content.length > 800) {
      wx.showToast({ title: '回复内容不能超过800字', icon: 'none' })
      return
    }

    this.setData({ submitting: true })
    wx.showLoading({ title: '提交中', mask: true })
    try {
      const res = await api.submitTaskCenter({
        id: task.id,
        replyContent: content,
      })
      if (res?.code === '0' || res?.code === 0) {
        this.onSubmitSuccess()
      } else {
        wx.showToast({
          title: res?.message || res?.msg || '提交失败',
          icon: 'none',
        })
      }
    } catch (e) {
      wx.showToast({ title: '提交失败', icon: 'none' })
    } finally {
      wx.hideLoading()
      this.setData({ submitting: false })
    }
  },

  onSubmitSuccess() {
    const { task } = this.data
    wx.hideLoading()
    this.setData({ submitting: false })
    wx.showToast({ title: '提交成功', icon: 'success' })
    this.notifyRefresh()
    setTimeout(() => {
      if (task?.taskType === 'staff-replace-access') {
        wx.redirectTo({
          url: '/pages/mattersSubmitResult/mattersSubmitResult',
        })
        return
      }
      wx.navigateBack()
    }, 1000)
  },

  notifyRefresh() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel?.emit?.('refreshList')
  },
})