const app = getApp() Page({ data: { toastShow: false, toastType: 'storyEnter', protocol: false, play: false, fileList: [] as any, form: { UserName: '', TreatmentExperience: '', Telephone: '', ImgSrc: [] as any, AudioSrc: [] as any, AllRead: 1, }, time: 0, timeName: '0秒', timer: 0, VoiceContent: [] as { size: string | number; url: string }[], StoryLeadIn: '0' as string | undefined, userInfo: {}, }, onLoad(options) { const that = this that.setData({ StoryLeadIn: options.storyleadin, }) const SystemInfo = app.globalSystemInfo if (SystemInfo) { const { bottom } = SystemInfo.capsulePosition that.setData({ bottom, }) } app.waitLogin().then(() => { app.mpBehavior({ PageName: 'PG_PATIENTSUBMITTEDSTORIES' }) app.getUserInfo(that, true) }) that.record = wx.getRecorderManager() that.record.onStart((_res) => { that.setData({ play: true, timer: setInterval(() => { that.setData({ time: that.data.time + 1, }) this.formatTime() }, 1000), }) wx.setKeepScreenOn({ keepScreenOn: true, }) }) that.record.onStop((res) => { that.setData({ play: false, time: 0, timeName: '0秒', }) clearInterval(that.data.timer) that.uplodaAudio(res) wx.setKeepScreenOn({ keepScreenOn: false, }) }) that.record.onError(({ errMsg }) => { clearInterval(that.data.timer) if (errMsg === 'operateRecorder:fail auth deny') { wx.showModal({ //====> 授权 title: '提示', content: '您未授权录音,功能将无法使用', showCancel: true, confirmText: '授权', confirmColor: '#cf5375', success(res) { if (res.confirm) { //确认则打开设置页面(重点) wx.openSetting({ success: (res) => { console.log(res.authSetting) if (!res.authSetting['scope.record']) { //未设置录音授权 console.log('未设置录音授权') wx.showModal({ title: '提示', content: '您未授权录音,功能将无法使用', confirmColor: '#cf5375', showCancel: false, success(_res) {}, }) } else { //第二次才成功授权 console.log('设置录音授权成功') } }, fail() { console.log('授权设置录音失败') }, }) } else if (res.cancel) { console.log('cancel') } }, fail() { console.log('open fail') }, }) } }) that.record.onPause((res) => { // that.record.resume(); that.handlePlayEnd() }) }, record: {} as WechatMiniprogram.RecorderManager, handleBack() { wx.navigateBack({ fail() { wx.reLaunch({ url: '/pages/index/index', }) }, }) }, handleSetData(e) { this.setData({ fileList: [...this.data.fileList, ...e.detail], }) }, handleDelData(e) { const index = e.detail.index this.setData({ fileList: this.data.fileList.filter((item, i) => i !== index), }) }, handleInput(e) { const { key } = e.currentTarget.dataset let value = e.detail.value if (['Telephone'].includes(key)) { if (value.length <= 11) { value = e.detail.value } else { value = this.data.form[key] } } this.setData({ [`form.${key}`]: value, }) }, handlePlayStart() { this.record.start({ duration: 180000, }) }, handlePlayEnd() { this.record.stop() }, uplodaAudio({ tempFilePath, duration }) { const that = this const url = `${app.globalData.upFileUrl}?r=file-service/upload-audio` wx.showLoading({ title: '上传中', mask: true, }) wx.uploadFile({ filePath: tempFilePath, name: 'file', url, header: { novocarewaf: 1 }, formData: { convCommon: 1, }, success(res) { wx.hideLoading() const data = JSON.parse(res.data) that.setData({ VoiceContent: [ ...that.data.VoiceContent, { size: duration / 1000, url: data.data.Url, }, ], }) that.pageScrollToBottom() }, fail() { wx.showToast({ icon: 'none', title: '上传失败', }) }, }) }, handleAudioDel(e) { const { index } = e.currentTarget.dataset const that = this wx.showModal({ title: '确认删除?', confirmColor: '#cf5375', success(res) { if (res.confirm) { that.setData({ VoiceContent: that.data.VoiceContent.filter((item, i) => i !== index), }) } }, }) }, handleSwitchChange() { this.setData({ 'form.AllRead': this.data.form.AllRead == 1 ? 2 : 1, }) }, submit() { app.mpBehavior({ PageName: 'BTN_PATIENTSUBMISSIONSTORYSUBMIT' }) const { form, fileList, VoiceContent } = this.data let toast = '' if (!form.TreatmentExperience) { toast = '请输入您的治疗经验和故事' } if (form.Telephone) { if (!/^(?:(?:\+|00)86)?1(?:3\d|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8\d|9[189])\d{8}$/.test(form.Telephone)) { toast = '手机号格式不正确' } } if (this.data.play) { toast = '录音中不能提交' } if (toast) { wx.showToast({ title: toast, icon: 'none', }) return } // Safety if (fileList.length) { form.ImgSrc = fileList.map((item) => decodeURI(item.fileUrl.replace(/^https?:\/\//, ''))) } if (VoiceContent.length) { form.AudioSrc = VoiceContent.map((item) => { return { shichang: item.size, url: decodeURI(item.url.replace(/^https?:\/\//, '')), } }) } if (!this.data.protocol) { this.setData({ toastShow: true, toastType: 'storyEnter', }) return } wx.ajax({ method: 'POST', url: '?r=igg4/story/submit-story', data: { ...form, StoryLeadIn: this.data.StoryLeadIn, }, loading: true, }).then((res) => { console.log('res: ', res) wx.redirectTo({ url: `/pages/storyEnterResult/index?id=${res.Id}`, }) }) }, handleToastOk(e) { if (!e.detail.protocol) { wx.showToast({ title: '请先同意审核和编辑后公开发布', icon: 'none', }) return } this.setData({ protocol: e.detail.protocol, }) this.handleToastCancel() this.submit() }, handleToastCancel() { this.setData({ toastShow: false, toastType: '', }) }, formatTime() { const time = this.data.time let timeName = '' if (time > 60) { timeName = `${Math.floor(time / 60)}分${time % 60}秒` } else { timeName = `${time % 60}秒` } this.setData({ timeName, }) }, pageScrollToBottom() { wx.createSelectorQuery() .select('#page') .boundingClientRect((rect) => { wx.pageScrollTo({ scrollTop: rect.bottom, }) }) .exec() }, })