tangshi300/pages/record/record.js

258 lines
7.8 KiB
JavaScript

var t = require("../../miniprogram_npm/@vant/weapp/toast/toast"), e = require("../../lib/aes.js"), a = getApp(), o = wx.getBackgroundAudioManager(), i = wx.getRecorderManager(), n = wx.getFileSystemManager();
Page({
data: {
poetryId: void 0,
poetry: void 0,
tempFilePath: void 0,
pinyin: !0,
book: {},
path: "",
filePath: void 0,
pageIndex: 0,
pageUrl: "",
playing: !0,
voicePlaying: !1,
autoPlay: !1,
chinese: "",
trans: !1,
isIphone: !1,
recording: !1,
recordList: [],
rate: 1,
showVoice: !1,
hasVoice: !1,
voice: void 0,
showShare: !1,
options: [ {
name: "微信",
icon: "wechat",
openType: "share"
} ]
},
onLoad: function(t) {
this.audio = wx.createInnerAudioContext();
var e = wx.env.USER_DATA_PATH + "/poetry";
if (!this.isFileExit(e)) try {
n.mkdirSync(e, !0);
} catch (t) {
// t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
// console.error(t);
}
this.setData({
path: e
});
var o = a.getCos();
this.cos = o, this.handleAutoPlay(), this.handleRecorder(), this.setData({
poetryId: t.id
}), this.getPoetry(), this.getVoice();
},
onReady: function() {},
onShow: function() {},
onUnload: function() {
i.stop(), this.audio.destroy(), o.stop();
},
handleAutoPlay: function() {
var t = this;
t.audio.onEnded(function(e) {
t.setData({
playing: !1
});
}), t.audio.onPlay(function(e) {
t.setData({
playing: !0
});
}), t.audio.onError(function(e) {
t.setData({
playing: !1
});
}), o.onEnded(function(e) {
t.setData({
voicePlaying: !1
});
}), o.onPause(function(e) {
t.setData({
voicePlaying: !1
});
}), o.onPlay(function(e) {
t.setData({
voicePlaying: !0
});
}), o.onError(function(e) {
t.setData({
voicePlaying: !1
});
});
},
handleRecorder: function() {
var t = this;
i.onStart(function(e) {
t.setData({
recording: !0
});
}), i.onStop(function(e) {
t.setData({
recording: !1
});
var a = t.data.path + "/" + t.data.poetry.name + "#" + t.data.poetry.id + "#" + t.getDateStr() + "#.mp3";
t.handleVoice(e.tempFilePath, a);
}), i.onError(function(e) {
t.setData({
recording: !1
});
});
},
handleVoice: function(t, e) {
try {
n.copyFileSync(t, e);
} catch (t) {
// t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
// console.error(t);
}
this.setData({
tempFilePath: t,
filePath: e
}), this.audio.src = t, this.audio.play(), this.uploadFile(e);
},
uploadFile: function(t) {
var e = this, o = e.data.poetry;
this.cos.postObject({
Bucket: "baby-1251951688",
Region: "ap-shanghai",
Key: "user/poetry/" + a.globalData.openid + "/" + o.id + ".mp3",
FilePath: t,
onProgress: function(t) {
console.log(JSON.stringify(t));
}
}, function(t, a) {
console.log(t || a), a && a.Location && e.completePeiyin();
});
},
getPoetry: function() {
var t = this, o = t.data.poetryId, i = a.globalData.API_URL + "/h5/api/textbook/poetry/detail", n = {
id: o,
type: "album"
};
wx.request({
url: i,
data: n,
success: function(o) {
var i = e.decrypt(o.data, a.globalData.key), n = JSON.parse(i);
t.setData({
poetry: n
});
}
});
},
getVoice: function() {
var t = this, o = t.data.poetryId, i = a.globalData.API_URL + "/h5/api/textbook/poetry/voice/detail", n = {
openId: a.globalData.openid,
poetryId: o
};
wx.request({
url: i,
data: n,
success: function(o) {
var i = e.decrypt(o.data, a.globalData.key), n = JSON.parse(i);
t.setData({
voice: n
});
}
});
},
completePeiyin: function() {
var t = this, e = t.data.poetryId, o = a.globalData.API_URL + "/h5/api/textbook/poetry/voice", i = {
openId: a.globalData.openid,
poetryId: e
};
wx.request({
url: o,
data: i,
success: function(e) {
t.getVoice();
}
});
},
isFileExit: function(t) {
try {
return n.accessSync(t), !0;
} catch (t) {
// t = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(t);
// return console.error(t), !1;
}
},
help: function(t) {
this.setData({
showVoice: !0
});
},
choosePinyin: function(t) {
this.setData({
pinyin: !this.data.pinyin
});
},
confirmDialog: function() {
this.setData({
showVoice: !1
});
},
play: function(t) {
this.data.voicePlaying ? o.pause() : (o.src = this.data.poetry.voice, o.title = "小学古诗集",
o.play());
},
playRecord: function(e) {
var o = this.data.voice, i = this.data.filePath;
if (o && o.id) if (i) this.audio.src = i, this.audio.play(); else {
var n = "https://baby-1251951688.file.myqcloud.com/user/poetry/" + a.globalData.openid + "/" + this.data.poetryId + ".mp3?t=" + new Date().getTime();
this.audio.src = n, this.audio.play();
} else t("你还未配音,请配音后再播放");
},
shareRecord: function(e) {
var a = this.data.voice;
this.data.filePath;
a && a.id ? this.setData({
showShare: !0
}) : t("你还未配音,请配音后再分享");
},
onClose: function() {
this.setData({
showShare: !1
});
},
startRecorder: function(t) {
this.audio.stop(), o.stop(), i.start({
duration: 3e5,
sampleRate: 16e3,
numberOfChannels: 1,
encodeBitRate: 24e3,
format: "mp3"
});
},
stopRecorder: function() {
i.stop();
},
toHistory: function(t) {
i.stop(), o.stop();
t.currentTarget.dataset.id;
wx.navigateTo({
url: "../record-history/record-history"
});
},
getDateStr: function() {
var t = new Date(), e = t.getFullYear(), a = t.getMonth() + 1, o = t.getDate();
a < 10 && (a = "0" + a), o < 10 && (o = "0" + o);
var i = t.getHours(), n = t.getMinutes(), r = t.getSeconds(), s = t.getMilliseconds();
return i < 10 && (i = "0" + i), n < 10 && (n = "0" + n), r < 10 && (r = "0" + r),
s < 10 && (r = "0" + s), e + "-" + a + "-" + o + " " + i + ":" + n + ":" + r;
},
onShareAppMessage: function() {
var t = this.data.poetry, e = "配音作品《" + t.name + "》";
return {
title: e,
path: "/pages/poetry-share/poetry-share?id=" + t.id + "&openid=" + a.globalData.openid + "&title=" + e,
success: function(t) {
console.log(t);
}
};
}
});