const uploadJS = require('../../mixin/upload.js') const { postAddNews } = require('./api/index') Page({ /** * 页面的初始数据 */ data: { form: { 'track_content': '', 'track_img_url': [] }, track_img_url_max: 6 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, ...uploadJS, uploadCallBack(res) { const temp = res.map(item => { return { 'url': item.url, 'formkey': item.formkey } }) let tempForm = {} let formkey = '' if (temp.length > 0) { formkey = temp[0].formkey } switch (formkey) { case 'track_img_url': const formkeyData = this.data.form[formkey] tempForm[`form.${formkey}`] = formkeyData.concat(...temp) break default: } if (Object.keys(tempForm).length > 0) { this.setData(tempForm) } }, handleInput(e) { const { value } = e.detail this.setData({ 'form.track_content': value.trim() }) }, handleCancel() { wx.navigateBack({ delta: 1 }) }, getForm() { const { track_content, track_img_url } = this.data.form return { track_content: track_content, track_img_url: track_img_url.map(item => item.url) } }, verify() { let errorList = [] const { track_content, track_img_url } = this.data.form if (!track_content) { errorList.push('请写下此刻的想法') } if (track_img_url.length < 1) { errorList.push('请上传图片') } return errorList }, async onSubmit() { const temp = this.getForm() const verifyList = this.verify() if (verifyList.length >= 2) { wx.showToast({ title: verifyList[0], icon: 'none' }) return } try { const { status, msg } = await postAddNews(temp) if (status) { const page = getCurrentPages() if (page.length >= 2 && page[page.length - 2].route === 'pages/news/news') { page[page.length - 2].refreshOrderList && page[page.length - 2].refreshOrderList() } wx.switchTab({ url: '/pages/news/news' }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} } })