import { $wuxActionSheet } from '../../../../wux/index' const config = require('../../../../utils/config') const app = getApp() import { topicSubmit, noticeSubmit } from '../../../../api/clerkOnline' Page({ data: { // 此页面 页面内容距最顶部的距离 height: app.globalData.navHeight, issueContent: '',//话题,通知内容 titleContent: '',//通知的标题 uploadImageList: [], imageId: 1, navigationBarText: '', uploadType: '1',//上传类型:1话题,2通知 isUploading: false, //true正在上传 isPublish: false,//true正在发送请求 placeholder: '请输入内容2000字以内', placeholderTitle: '请输入标题40字以内', imageListlenght: 3, }, onLoad: function (options) { if (options.type == 'topic') { //发布话题 this.setData({ navigationBarText: '我有话说', imageListlenght: 3, }) } else if (options.type == 'inform') { // 发布通知 this.setData({ navigationBarText: '发布通知', imageListlenght: 20, }) } }, // 双向绑定 内容输入框 bindTextareaInput(e: any) { this.setData({ issueContent: e.detail.value }) }, bindcontentblur(e: any){ this.setData({ issueContent: e.detail.value }) }, bindTitleInput(e: any) { this.setData({ titleContent: e.detail.value }) }, bindTitleblur(e:any){ this.setData({ titleContent: e.detail.value }) }, // 选择图片 上传弹窗 - 上传图片方式 - 选择图片 - 上传图片 - 回调赋值 chooseImage() { const _this = this console.log('::::::::' + this.data.isUploading) let number_a = 0; if (this.data.isUploading < 20) { number_a = this.data.imageListlenght - _this.data.uploadImageList.length } if (this.data.isUploading) { wx.showToast({ title: '图片正在上传', icon: 'none' }) console.log('禁止点击~~~') return false; } $wuxActionSheet().showSheet({ buttons: [ { text: '拍照' }, { text: '从相册中获取' }, ], className: 'dialog-class', buttonClicked(index: any) { console.log('index', index) _this.setData({ isUploading: true }) if (index === 0) { console.log('选择图片') wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['camera'], success(res) { const uploadImageList = [..._this.data.uploadImageList] if (res.tempFiles[0].size <= 5 * 1024 * 1024) { uploadImageList.push({ uploaded: false, ossUrl: '', imgUrl: res.tempFiles[0].path, imageId: ++_this.data.imageId }) } else { wx.showToast({ title: '图片上限5M,请压缩后重试~', icon: 'none', duration: 1500 }) return false } _this.setData({ uploadImageList, }) wx.uploadFile({ url: `${config.config.apiUrl}oss/file/upload`, filePath: res.tempFilePaths[0], name: 'file', formData: { uploadType: _this.data.uploadType }, header: { 'Content-type': 'application/json;', 'Authorization': wx.getStorageSync('token') }, success(fileRes) { uploadImageList[uploadImageList.length - 1].uploaded = true uploadImageList[uploadImageList.length - 1].ossUrl = JSON.parse(fileRes.data).data.url wx.hideToast();//关闭提示框 _this.setData({ uploadImageList, isUploading: false }) }, fail() { wx.hideToast();//关闭提示框 _this.setData({ uploadImageList: [], isUploading: false }) wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) } }) }, fail(res: any) { wx.hideToast();//关闭提示框 console.log('取消的函数', res) _this.setData({ isUploading: false, }) } }) } else if (index === 1) { console.log("本地选择") console.log('number_a::::' + number_a) wx.chooseImage({ // count: 3 - _this.data.uploadImageList.length, count: number_a, sizeType: ['original', 'compressed'], sourceType: ['album'], success(res) { let imgIndex = 0;//上传完成的第几张照片 console.log('lll', res.tempFiles) const uploadImageList: any = [] const endIndex = _this.data.uploadImageList.length console.log('2222' + endIndex) res.tempFiles.forEach(item => { if (item.size <= 5 * 1024 * 1024) { uploadImageList.push({ uploaded: false, ossUrl: '', imgUrl: item.path, imageId: ++_this.data.imageId }) } else { wx.showToast({ title: '图片上限5M,请压缩后重试~', icon: 'none', duration: 1500 }) } }) _this.setData({ uploadImageList: [..._this.data.uploadImageList, ...uploadImageList] }) const endIndex2 = res.tempFiles.length console.log('endIndex2', _this.data.uploadImageList.length) uploadImageList.forEach((item: any, index: any) => { return (async function (index) { await wx.uploadFile({ url: `${config.config.apiUrl}oss/file/upload`, filePath: res.tempFilePaths[index], name: 'file', formData: { uploadType: _this.data.uploadType }, header: { 'Content-type': 'application/json', 'Authorization': wx.getStorageSync('token') }, success(fileRes) { imgIndex = imgIndex + 1; uploadImageList[index].uploaded = true uploadImageList[index].ossUrl = JSON.parse(fileRes.data).data.url _this.data.uploadImageList = _this.data.uploadImageList.slice(0, endIndex) _this.setData({ uploadImageList: [..._this.data.uploadImageList, ...uploadImageList] }) console.log('index', index) wx.hideToast();//关闭提示框 if (imgIndex == uploadImageList.length) { _this.setData({ isUploading: false, }) } }, fail() { wx.hideToast();//关闭提示框 _this.setData({ uploadImageList: [], isUploading: false }) wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) } }) })(index) }) }, fail(res: any) { console.log('取消的函数', res) wx.hideToast();//关闭提示框 _this.setData({ isUploading: false, }) } }) } return true }, cancelText: '取消', cancel() { wx.hideToast();//关闭提示框 _this.setData({ isUploading: false }) }, destructiveButtonClicked() { }, }) }, // 删除选中的图片 deleteImage(e: any) { const index = this.data.uploadImageList.findIndex(item => item.imageId === e.currentTarget.dataset.imageid) if (index > -1) { this.data.uploadImageList.splice(index, 1) this.setData({ uploadImageList: this.data.uploadImageList }) } }, // 发布话题 async gettopicSubmit() { let images = []; for (var i = 0; i < this.data.uploadImageList.length; i++) { const imageUrl = this.data.uploadImageList[i].ossUrl images.push(imageUrl) } console.log(':::::::::' + JSON.stringify(images)) let obj = { topicContent: this.data.issueContent, images: images } try { let res: any = await topicSubmit(obj) console.log('res', res) wx.setStorageSync("isBackOpen",false) wx.hideLoading() wx.navigateBack({ delta: 1 }) } catch{ } }, // 发布通知 async getnoticeSubmit() { console.log("~~~~~" + JSON.stringify(this.data.uploadImageList)) let images = []; for (var i = 0; i < this.data.uploadImageList.length; i++) { const imageUrl = this.data.uploadImageList[i].ossUrl images.push(imageUrl) } console.log(':::::::::' + JSON.stringify(images)) let obj = { noticeTitle: this.data.titleContent, noticeContent: this.data.issueContent, images: images } try { let res: any = await noticeSubmit(obj) console.log('res', res) wx.setStorageSync("isBackOpen",false) wx.hideLoading() wx.navigateBack({ delta: 1 }) } catch{ } }, // 发布 async goPublish() { if (this.data.isUploading) { wx.showModal({ title: '', //提示的标题, content: '图片正在发布中,请稍等', //提示的内容, showCancel: false, //是否显示取消按钮, confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符, confirmColor: '#3CC51F', //确定按钮的文字颜色, success: res => { if (res.confirm) { console.log('用户点击确定') } else if (res.cancel) { console.log('用户点击取消') } } }) return } wx.showLoading({ title: '发布中', mask: true }) if (this.data.isPublish) { return } if (this.data.navigationBarText == '我有话说') { if (!this.data.issueContent) { wx.showToast({ title: '请输入发布内容', icon: 'none' }) return } this.setData({ isPublish: true, }) await this.gettopicSubmit() } else if (this.data.navigationBarText == '发布通知') { if (!this.data.titleContent && !this.data.issueContent) { wx.showToast({ title: '请输入通知标题及内容', icon: 'none' }) return } else if (!this.data.titleContent && this.data.issueContent) { wx.showToast({ title: '请输入通知标题', icon: 'none' }) return }else if(this.data.titleContent && !this.data.issueContent){ wx.showToast({ title: '请输入通知内容', icon: 'none' }) return } this.setData({ isPublish: true, }) await this.getnoticeSubmit() } }, })