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 强制发送 audioManager: "", // 音频播放器 recorderManager: "", // recorderData: {}, // 录音文件数据 recorderTimeMax: "", // 00:00 recorderTimeCur: "", // 00:00 showRecord: false, // 是否显示录音sheet isRecord: 0, // 是否开始录音 0-未开始 1-正在录音 2-录音完成 recordingTime: 0, // 计时器 isPlayAudio: false, // 上传的音频播放状态 audioTimeline: 0, // 当前播放值 uploadRecord: { uploaded: true, url: "" }, recorderDuration: "", // 录音时长 time: 5, isCreateCity: "0", // 有奖举报的传1 默认传1 isShowTip: true }, onLoad (e) { console.log(e) this.data.qqMapWX = new QQMapWX({ key: "CMJBZ-4DECI-JXGGN-5B4WU-QLV2H-B5BEJ" }) this.getLocation().then(() => { this.reverseLocation() }) this.initRecordManager() if (e && e.isCreateCity && e.isCreateCity == "1") { wx.setNavigationBarTitle({ title: "有奖举报" }) this.setData({ isCreateCity: e.isCreateCity }) let timer = setInterval(() => { this.setData({ time: this.data.time - 1 }) if (this.data.time == 1) { clearTimeout(timer) } },1000) } else { wx.setNavigationBarTitle({ title: "随时讲" }) this.setData({ isShowTip: false, isCreateCity: "0" }) } }, closeTip() { if (this.data.time == 1) { this.setData({ isShowTip: false }) } }, onUnload () { this.data.audioManager.destroy() }, // 重新定位 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 () { if (this.data.uploadImageList.length > 2) { wx.showToast({ title: "最多上传3张照片", icon: "none" }) return } 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) { this.publishIssue() this.data.publishIssuePrevious = now } }, // 发布议题 publishIssue () { if (!this.data.issueContent) { wx.showToast({ title: this.data.isCreateCity == "1" ? "请填写内容" : "请填写议题内容", 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) }) } if (!this.data.uploadRecord.uploaded) { wx.showToast({ title: "请等待录音上传完成", icon: "none", duration: 1000 }) return false } const para = { eventContent: this.data.issueContent, issueAddress: this.data.addressContent, issueLongitude: this.data.longitude, issueLatitude: this.data.latitude, images: imagesList, isConReview:this.data.isConReview, voice: this.data.uploadRecord.url, duration: this.data.recorderDuration, isCreateCity: this.data.isCreateCity } 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" }) }, // 点击空白,隐藏sheet onHideSheet () { this.setData({ showRecord: false }) }, // 弹出录音 showRecordSheet () { this.setData({ showRecord: true, recorderData: {}, recorderTimeMax: "00:00", recorderTimeCur: "00:00", isRecord: 0, recordingTime: 0, uploadRecord: { uploaded: true, url: "" } }) }, // 删除录音 deleteRecord () { this.setData({ recorderData: {}, uploadRecord: { uploaded: true, url: "" } }) }, // 取消录音 onCancelRecord () { this.setData({ showRecord: false, recorderData: {}, uploadRecord: { uploaded: true, url: "" } }) }, // 确定录音 onConfirmRecord () { this.setRecordTimeline() this.setData({ showRecord: false, "uploadRecord.uploaded": false, "uploadRecord.url": "" }) wx.uploadFile({ url: `${config.BASEURL()}group/topic/upload`, filePath: this.data.recorderData.tempFilePath, name: "file", header: { "Content-type": "multipart/form-data" }, success: (fileRes) => { if (!JSON.parse(fileRes.data).data) { wx.showToast({ title: "录音上传失败,请重试~", icon: "none", duration: 1500 }) this.setData({ "uploadRecord.uploaded": true, "uploadRecord.url": "", recorderDuration: "", recorderData: {} }) } else { this.setData({ "uploadRecord.uploaded": true, "uploadRecord.url": JSON.parse(fileRes.data).data }) } }, fail: (fileRes) => { wx.showToast({ title: "录音上传失败,请重试~", icon: "none", duration: 1500 }) this.setData({ "uploadRecord.uploaded": true, "uploadRecord.url": "", recorderDuration: "", recorderData: {} }) } }) }, initRecordManager () { this.data.recorderManager = wx.getRecorderManager() this.data.recorderManager.onStop(res => { if (res.duration < 1000) { wx.showToast({ title: "录音时间太短", icon: "none" }) this.setData({ isRecord: 0, recordingTime: 0 }) } else { clearInterval(this.data.setInter) this.setData({ isRecord: 2, recordingTime: 0, recorderData: res }, () => { this.setRecordTimeline() }) } }) // 音频播放 this.data.audioManager = wx.createInnerAudioContext() this.data.audioManager.onPlay(() => { }) this.data.audioManager.onStop(() => { clearInterval(this.data.setInter) this.setData({ isPlayAudio: false, recordingTime: 0, recorderTimeCur: "00:00", audioTimeline: 0 }) }) this.data.audioManager.onEnded(() => { clearInterval(this.data.setInter) this.setData({ isPlayAudio: false, recordingTime: 0, recorderTimeCur: "00:00", audioTimeline: 0 }) }) this.data.audioManager.onError(() => { console.log("音频播放错误", this.data.audioManager.src) clearInterval(this.data.setInter) this.setData({ recordingTime: 0, recorderTimeCur: "00:00" }) }) }, // setRecordTimeline () { let sec = Math.ceil(this.data.recorderData.duration / 1000) this.data.recorderDuration = sec if (sec < 10) { this.setData({ recorderTimeMax: "00:0" + sec, recorderTimeCur: "00:00" }) } else if (sec > 59) { this.setData({ recorderTimeMax: "01:00", // 目前最长60秒 recorderTimeCur: "00:00" }) } else { this.setData({ recorderTimeMax: "00:" + sec, recorderTimeCur: "00:00" }) } }, // countRecordTimeline () { clearInterval(this.data.setInter) this.data.setInter = setInterval(() => { this.setData({ recordingTime: this.data.recordingTime + 1 }, () => { if (this.data.recordingTime < 10) { this.setData({ recorderTimeCur: "00:0" + this.data.recordingTime }) } else { this.setData({ recorderTimeCur: "00:" + this.data.recordingTime }) } if (!this.data.showRecord) { let max = Math.ceil(this.data.recorderData.duration / 1000) let value = parseFloat(this.data.recordingTime / max).toFixed(2) this.setData({ audioTimeline: value * 100 }) } }) }, 1000) }, //录音计时器 recordingTimer () { clearInterval(this.data.setInter) this.data.setInter = setInterval(() => { this.setData({ recordingTime: this.data.recordingTime + 1 }) }, 1000) }, // 长按录音 longpress时触发 recordStart () { console.log("开始录音") const options = { duration: 60000, //指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000) sampleRate: 16000, //采样率 numberOfChannels: 1, //录音通道数 encodeBitRate: 96000, //编码码率 format: "mp3", //音频格式,有效值 aac/mp3 frameSize: 50, //指定帧大小,单位 KB } this.data.recorderManager.start(options) // 开始录音 this.recordingTimer() this.setData({ isRecord: 1 }) }, // 松开发送 touchend (手指松开时)触发 recordStop () { console.log("停止录音") if (this.data.isRecord == 0) { return } this.data.recorderManager.stop() // 结束录音 clearInterval(this.data.setInter) this.setData({ isRecord: 2, recordingTime: 0 }) }, // 播放录音 onPlayRecord () { this.data.audioManager.src = this.data.recorderData.tempFilePath this.data.audioManager.play() this.countRecordTimeline() }, // 播放音频 onPlayAudio () { if (this.data.recorderData.tempFilePath) { this.setData({ isPlayAudio: true }) this.data.audioManager.src = this.data.recorderData.tempFilePath this.data.audioManager.play() this.countRecordTimeline() } }, // 暂停播放音频 onStopAudio () { this.setData({ isPlayAudio: false }) this.data.audioManager.stop() }, // sliderchange () { let max = Math.ceil(this.data.recorderData.duration / 1000) let value = parseFloat(this.data.recordingTime / max).toFixed(2) this.setData({ audioTimeline: value * 100 }) } })