import { getPreferType, getMomentDetail, postMoment, updateMoment } from "../../../api/moment" import { getFactoryList } from "../../../api/ztb" const util = require('../../../utils/util') const event = require('../../../utils/event.js') Page({ /** * 页面的初始数据 */ data: { form: {}, requesting: false, factoryFlag: true, factoryList: [], typeList: [] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(!util.isEmpty(options.lableId)){ this.data.form.categoryId = Number(options.lableId) } if(options.factoryId){ this.data.form.millPaperId = options.factoryId this.data.factoryFlag = false this.setData({ ['form.millPaperName']: options.name }) } else { getFactoryList().then(result => { this.data.factoryList = [] for (let index = 0; index < result.data.records.length; index++) { const element = result.data.records[index] this.data.factoryList.push({value: element.paperMillId, text: element.name }) } }) } getPreferType().then(result => { this.data.typeList = [] for (let index = 0; index < result.data.length; index++) { const element = result.data[index] this.data.typeList.push({value: element.id, text: element.name }) } }) if(options.id){ wx.showLoading({ title: '加载中', mask: true }) getMomentDetail(options.id).then(result => { var fileList = [] if (result.data.imageUrlList && result.data.imageUrlList.length) { result.data.imageUrlList.forEach(element => { fileList.push({ url: element, status: 8 }) }) } result.data.imageUrls = result.data.imageUrlList this.setData({ form: result.data, fileList: fileList }) wx.hideLoading() }).catch(err => { wx.hideLoading() }) } }, bindInput: function (e) { this.data.form[e.target.id] = e.detail.value }, onImageChange: function (e) { if (e.detail) { var list = [] e.detail.forEach(element => { if (!util.isEmpty(element.url)) { list.push(element.url) } }) this.data.form['imageUrls'] = list } }, submitForm: function(){ if(this.data.requesting){ return } if(util.isEmpty(this.data.form.title)){ util.showToast('请输入标题') return } if(util.isEmpty(this.data.form.content)){ util.showToast('请输入内容') return } wx.showLoading({ title: '处理中', mask: true }) this.data.requesting = true if(util.isEmpty(this.data.form.id)){ postMoment(this.data.form).then(result => { wx.hideLoading() var what = 570 if(Number(this.data.form.categoryId) > 0){ what += this.data.form.categoryId } event.emit('EventMessage', { what, desc: 'PostChange' }) util.showBackToast('发布成功') }).catch(err => { wx.hideLoading() this.data.requesting = false util.showToast(err) }) } else { updateMoment(this.data.form.id, this.data.form).then(result => { wx.hideLoading() event.emit('EventMessage', { what: 501, desc: 'PostChange' }) util.showBackToast('编辑成功') }).catch(err => { wx.hideLoading() this.data.requesting = false util.showToast(err) }) } }, selectFactory: function (e) { if(!this.data.factoryFlag){ return } this.pickerView = this.pickerView || this.selectComponent('#picker-view') if(this.data.factoryList.length > 0){ this.pickerView.showPicker(this.data.factoryList, this.data.form.millPaperId, 0) } else { wx.showLoading({ title: '加载中', mask: true }) getPreferType().then(result => { this.data.typeList = [] for (let index = 0; index < result.data.length; index++) { const element = result.data[index] this.data.typeList.push({value: element.id, text: element.name }) } this.pickerView.showPicker(this.data.factoryList, this.data.form.millPaperId, 0) wx.hideLoading() }).catch(err => { wx.hideLoading() }) } }, selectType: function(){ this.pickerView = this.pickerView || this.selectComponent('#picker-view') if(this.data.typeList.length > 0){ this.pickerView.showPicker(this.data.typeList, this.data.form.categoryId, 1) } else { wx.showLoading({ title: '加载中', mask: true }) getPreferType().then(result => { tthis.data.typeList = [] for (let index = 0; index < result.data.length; index++) { const element = result.data[index] this.data.typeList.push({value: element.id, text: element.name }) } this.pickerView.showPicker(this.data.typeList, this.data.form.categoryId, 1) wx.hideLoading() }).catch(err => { wx.hideLoading() }) } }, onPickerChange: function ({detail}) { if(!detail){ return } if(detail.type == 1) { this.setData({ ['form.categoryId']: detail.value, ['form.typeName']: detail.text }) } else { this.setData({ ['form.millPaperId']: detail.value, ['form.millPaperName']: detail.text }) } } })