priceAudio.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. *示例
  3. * this.playPriceAudio(123.32);
  4. */
  5. export default {
  6. onLunch() {},
  7. data() {
  8. return {
  9. playing: false,
  10. priceHanderList: []
  11. };
  12. },
  13. methods: {
  14. playPriceAudio(price) {
  15. this.priceHanderList.push(() => {
  16. return new Promise((resolve, reject) => {
  17. this.formatPriceAudio(price)
  18. .then(res => {
  19. resolve(res);
  20. })
  21. .catch(err => {
  22. reject(err);
  23. });
  24. });
  25. });
  26. if (this.playing) {
  27. } else {
  28. this.playAllPriceAudio();
  29. }
  30. },
  31. async playAllPriceAudio() {
  32. try {
  33. this.playing = true;
  34. await this.formatAudioPromise(this.priceHanderList);
  35. this.playing = false;
  36. return true;
  37. } catch (error) {
  38. this.playing = false;
  39. }
  40. },
  41. getPriceAutioSrcByType(type) {
  42. let src = `${this.$constant.imgUrl}/audio/${type}.wav`;
  43. return src;
  44. },
  45. async formatPriceAudio(num) {
  46. let priceHandler = [];
  47. let count = String(num);
  48. let countList = count.split(".");
  49. let intNum = countList[0];
  50. let floatNum = countList[1];
  51. if (intNum >= 0) {
  52. let priceList = [];
  53. for (const item of intNum) {
  54. priceList.push(item);
  55. }
  56. let individual = priceList.pop();
  57. if (individual) {
  58. priceHandler.unshift(this.createPlayHandler(individual));
  59. let ten = priceList.pop();
  60. if (ten) {
  61. priceHandler.unshift(this.createPlayHandler("shi"));
  62. priceHandler.unshift(this.createPlayHandler(ten));
  63. let hundred = priceList.pop();
  64. if (hundred) {
  65. priceHandler.unshift(this.createPlayHandler("bai"));
  66. priceHandler.unshift(this.createPlayHandler(hundred));
  67. let thousand = priceList.pop();
  68. if (thousand) {
  69. priceHandler.unshift(this.createPlayHandler("qian"));
  70. priceHandler.unshift(this.createPlayHandler(thousand));
  71. if (priceList.length > 0) {
  72. priceHandler.unshift(this.createPlayHandler("wan"));
  73. for (const item of priceList) {
  74. priceList = [this.createPlayHandler(item)].concat(priceList);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. } else {
  81. if (floatNum >= 0) {
  82. priceHandler.unshift(this.createPlayHandler("0"));
  83. }
  84. }
  85. }
  86. if (floatNum >= 0) {
  87. priceHandler.push(this.createPlayHandler("."));
  88. for (const item of floatNum) {
  89. priceHandler.push(this.createPlayHandler(item));
  90. }
  91. }
  92. priceHandler.push(this.createPlayHandler("yuan"));
  93. priceHandler.unshift(this.createPlayHandler("ccb"));
  94. const outBack = async () => {
  95. return new Promise(resolve => {
  96. setTimeout(function() {
  97. resolve();
  98. }, 500);
  99. });
  100. };
  101. priceHandler.push(outBack);
  102. return this.formatAudioPromise(priceHandler);
  103. },
  104. async formatAudioPromise(handlerList) {
  105. try {
  106. const handlerItem = handlerList.shift();
  107. if (handlerItem) {
  108. await handlerItem();
  109. if (handlerList.length > 0) {
  110. return this.formatAudioPromise(handlerList);
  111. }
  112. }
  113. return true;
  114. } catch (error) {
  115. return Promise.reject(error);
  116. }
  117. },
  118. createPlayHandler(type) {
  119. return () => {
  120. return new Promise((resolve, reject) => {
  121. const innerAudioContext = uni.createInnerAudioContext();
  122. innerAudioContext.autoplay = true;
  123. innerAudioContext.src = this.getPriceAutioSrcByType(type);
  124. innerAudioContext.onPlay(() => {
  125. console.log("开始播放", type);
  126. });
  127. innerAudioContext.onError(res => {
  128. console.log(res.errMsg);
  129. console.log(res.errCode);
  130. reject(res);
  131. });
  132. innerAudioContext.onEnded(err => {
  133. console.log("开始结束", type);
  134. innerAudioContext.destroy();
  135. resolve(err);
  136. });
  137. });
  138. };
  139. }
  140. }
  141. };