const QQMapWX = require('../../utils/qqmap-wx-jssdk') import { $wuxActionSheet } from '../../../../dist/index' const config = require('../../../../utils/config') import { addIssue } from '../../utils/api' Page({ data: { issueContent: '', addressContent: '', latitude: '', longitude: '', qqMapWX: '', uploadImageList: [], imageId: 1, dialogVisible: false, publishIssuePrevious: 0, placeholderShow:true, submitEvent:false, isFirstNum:0,//0:初始状态,1:提交一次审核违规不通过 isConReview:false,//true 强制发送 }, onLoad () { this.data.qqMapWX = new QQMapWX({ key: 'CMJBZ-4DECI-JXGGN-5B4WU-QLV2H-B5BEJ' }) this.getLocation().then(() => { this.reverseLocation() }) }, // 重新定位 getPosition(){ this.getLocation().then(() => { this.reverseLocation() }) }, // 解决ios占位符遮挡问题 bindTextareaFocus(){ this.setData({ placeholderShow:false, }) }, // 解决ios占位符遮挡问题 bindTextareaBlur(e){ this.setData({ issueContent: e.detail.value }) if(this.data.issueContent.length==0){ this.setData({ placeholderShow:true }) } }, // 双向绑定 内容输入框 bindTextareaInput (e) { this.setData({ issueContent: e.detail.value }) }, // 双向绑定 地址输入框 bindAddressInput (e) { this.setData({ addressContent: e.detail.value }) }, // 获取经纬度 getLocation () { return new Promise((resolve, reject) => { const _this = this wx.getLocation({ success (res) { if (res.latitude && res.longitude) { _this.setData({ latitude: res.latitude, longitude: res.longitude }) resolve(true) } }, fail (err) { reject(err) } }) }) }, // 逆地址解析 reverseLocation () { const _this = this this.data.qqMapWX.reverseGeocoder({ location: { latitude: _this.data.latitude, longitude: _this.data.longitude }, success (res) { _this.setData({ addressContent: res.result.address }) }, fail (err) { console.debug(err) } }) }, // 选择图片 上传弹窗 - 上传图片方式 - 选择图片 - 上传图片 - 回调赋值 chooseImage () { const _this = this $wuxActionSheet().showSheet({ buttons: [ { text: '拍照' }, { text: '从相册中获取' }, ], className: 'dialog-class', buttonClicked(index) { if (index === 0) { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['camera'], success (res) { let deleteLength = _this.data.uploadImageList.length 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.BASEURL()}group/topic/upload`, filePath: res.tempFilePaths[0], name: 'file', header: { 'Content-type': 'multipart/form-data' }, success (fileRes){ if(!JSON.parse(fileRes.data).data){ wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) // 删除 const index = _this.data.uploadImageList.findIndex(item => item.imageId === _this.data.imageId) if (index > -1) { _this.data.uploadImageList.splice(index, 1) _this.setData({ uploadImageList: _this.data.uploadImageList }) } }else { uploadImageList[uploadImageList.length - 1].uploaded = true uploadImageList[uploadImageList.length - 1].ossUrl = JSON.parse(fileRes.data).data _this.setData({ uploadImageList }) } }, fail (fileRes) { _this.setData({ uploadImageList: [] }) wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) } }) } }) } else if (index === 1) { wx.chooseImage({ count: 3 - _this.data.uploadImageList.length, sizeType: ['original', 'compressed'], sourceType: ['album'], success (res) { let deleteLength = _this.data.uploadImageList.length const uploadImageList = [] const endIndex = _this.data.uploadImageList.length 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] }) uploadImageList.forEach((item, index) => { return (function (index) { wx.uploadFile({ url: `${config.BASEURL()}group/topic/upload`, filePath: res.tempFilePaths[index], name: 'file', header: { 'Content-type': 'multipart/form-data' }, success (fileRes){ if(!JSON.parse(fileRes.data).data){ wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) _this.data.uploadImageList.splice(deleteLength, _this.data.uploadImageList.length - deleteLength) _this.setData({ uploadImageList: _this.data.uploadImageList }) }else{ uploadImageList[index].uploaded = true uploadImageList[index].ossUrl = JSON.parse(fileRes.data).data _this.data.uploadImageList = _this.data.uploadImageList.slice(0, endIndex) _this.setData({ uploadImageList: [..._this.data.uploadImageList, ...uploadImageList] }) } }, fail (fileRes) { _this.setData({ uploadImageList: [] }) wx.showToast({ title: '图片上传失败,请重试~', icon: 'none', duration: 1500 }) } }) })(index) }) } }) } return true }, cancelText: '取消', cancel() {}, destructiveButtonClicked() {}, }) }, // 删除选中的图片 deleteImage (e) { 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 }) } }, throttlePublisgIssue() { let now = new Date() if (now - this.data.publishIssuePrevious > 2000) { console.log(1) this.publishIssue() this.data.publishIssuePrevious = now } }, // 发布议题 publishIssue () { if (!this.data.issueContent) { wx.showToast({ title: '请填写议题内容', icon: 'none', duration: 1500 }) return false } else if (!this.data.addressContent) { wx.showToast({ title: '请填写地址', icon: 'none', duration: 1500 }) return false } const imagesList = [] if (this.data.uploadImageList.length > 0) { const isUploadDown = this.data.uploadImageList.some(item => !item.uploaded) if (isUploadDown) { wx.showToast({ title: '请等待图片上传完成', icon: 'none', duration: 1000 }) return false } } if (this.data.uploadImageList.length > 0) { this.data.uploadImageList.forEach(item => { imagesList.push(item.ossUrl) }) } const para = { eventContent: this.data.issueContent, issueAddress: this.data.addressContent, issueLongitude: this.data.longitude, issueLatitude: this.data.latitude, images: imagesList, isConReview:this.data.isConReview } wx.showLoading({ title: '加载中...', mask:true }) addIssue(para).then(res => { console.log('发布议题', res) this.setData({ dialogVisible: !this.data.dialogVisible, isConReview:false }) wx.hideLoading() }).catch(err => { let {code} =err if(code==533){ this.checkTitle() }else{ wx.showToast({ title: err, icon: 'none', duration: 6000, mask:true, complete: () => { setTimeout(() => { wx.hideLoading() }, 2000) } }) } }) }, checkTitle(){ let that = this if(this.data.isFirstNum==0){ this.setData({ isFirstNum:++this.data.isFirstNum }) // wx.hideLoading() // wx.showToast({ // title:'内容存在违规信息,请修改后重新提交!', // icon:'none', // duration: 3000, // mask:true // }) }else if(this.data.isFirstNum==1){ wx.hideLoading() wx.showToast({ title:'', icon:'none', duration: 100, mask:true }) wx.showModal({ title: '提示', content: '您提交的内容再次被判定为违规,您确定是否要提交?', success (res) { if (res.confirm) { that.setData({ isConReview:true }) that.publishIssue () } else if (res.cancel) { } } }) } }, // 知道了 确认按钮 confirmDialog () { wx.switchTab({ url: '/pages/discussion/discussion' }) } })