| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- *示例
- * this.playPriceAudio(123.32);
- */
- export default {
- onLunch() {},
- data() {
- return {
- playing: false,
- priceHanderList: []
- };
- },
- methods: {
- playPriceAudio(price) {
- this.priceHanderList.push(() => {
- return new Promise((resolve, reject) => {
- this.formatPriceAudio(price)
- .then(res => {
- resolve(res);
- })
- .catch(err => {
- reject(err);
- });
- });
- });
- if (this.playing) {
- } else {
- this.playAllPriceAudio();
- }
- },
- async playAllPriceAudio() {
- try {
- this.playing = true;
- await this.formatAudioPromise(this.priceHanderList);
- this.playing = false;
- return true;
- } catch (error) {
- this.playing = false;
- }
- },
- getPriceAutioSrcByType(type) {
- let src = `${this.$constant.imgUrl}/audio/${type}.wav`;
- return src;
- },
- async formatPriceAudio(num) {
- let priceHandler = [];
- let count = String(num);
- let countList = count.split(".");
- let intNum = countList[0];
- let floatNum = countList[1];
- if (intNum >= 0) {
- let priceList = [];
- for (const item of intNum) {
- priceList.push(item);
- }
- let individual = priceList.pop();
- if (individual) {
- priceHandler.unshift(this.createPlayHandler(individual));
- let ten = priceList.pop();
- if (ten) {
- priceHandler.unshift(this.createPlayHandler("shi"));
- priceHandler.unshift(this.createPlayHandler(ten));
- let hundred = priceList.pop();
- if (hundred) {
- priceHandler.unshift(this.createPlayHandler("bai"));
- priceHandler.unshift(this.createPlayHandler(hundred));
- let thousand = priceList.pop();
- if (thousand) {
- priceHandler.unshift(this.createPlayHandler("qian"));
- priceHandler.unshift(this.createPlayHandler(thousand));
- if (priceList.length > 0) {
- priceHandler.unshift(this.createPlayHandler("wan"));
- for (const item of priceList) {
- priceList = [this.createPlayHandler(item)].concat(priceList);
- }
- }
- }
- }
- }
- } else {
- if (floatNum >= 0) {
- priceHandler.unshift(this.createPlayHandler("0"));
- }
- }
- }
- if (floatNum >= 0) {
- priceHandler.push(this.createPlayHandler("."));
- for (const item of floatNum) {
- priceHandler.push(this.createPlayHandler(item));
- }
- }
- priceHandler.push(this.createPlayHandler("yuan"));
- priceHandler.unshift(this.createPlayHandler("ccb"));
- const outBack = async () => {
- return new Promise(resolve => {
- setTimeout(function() {
- resolve();
- }, 500);
- });
- };
- priceHandler.push(outBack);
- return this.formatAudioPromise(priceHandler);
- },
- async formatAudioPromise(handlerList) {
- try {
- const handlerItem = handlerList.shift();
- if (handlerItem) {
- await handlerItem();
- if (handlerList.length > 0) {
- return this.formatAudioPromise(handlerList);
- }
- }
- return true;
- } catch (error) {
- return Promise.reject(error);
- }
- },
- createPlayHandler(type) {
- return () => {
- return new Promise((resolve, reject) => {
- const innerAudioContext = uni.createInnerAudioContext();
- innerAudioContext.autoplay = true;
- innerAudioContext.src = this.getPriceAutioSrcByType(type);
- innerAudioContext.onPlay(() => {
- console.log("开始播放", type);
- });
- innerAudioContext.onError(res => {
- console.log(res.errMsg);
- console.log(res.errCode);
- reject(res);
- });
- innerAudioContext.onEnded(err => {
- console.log("开始结束", type);
- innerAudioContext.destroy();
- resolve(err);
- });
- });
- };
- }
- }
- };
|