index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. storeBindingsBehavior
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. import {
  8. userEvent
  9. } from '~/api/global'
  10. import {
  11. publishWorks,
  12. postWorksScore,
  13. publishRankWorks
  14. } from '~/api/works'
  15. Component({
  16. behaviors: [storeBindingsBehavior],
  17. storeBindings: {
  18. store,
  19. fields: {
  20. readDetail: 'readDetail'
  21. },
  22. },
  23. /**
  24. * 组件的属性列表
  25. */
  26. properties: {
  27. readingType: {
  28. type: String,
  29. value: ""
  30. },
  31. activityId: ''
  32. },
  33. /**
  34. * 组件的初始数据
  35. */
  36. data: {
  37. tempFilePath: '',
  38. uploadSuccess: false,
  39. uploadFlag: false,
  40. // 是否上传过
  41. uploadState: false,
  42. percent: 0,
  43. },
  44. /**
  45. * 组件的方法列表
  46. */
  47. methods: {
  48. async upload() {
  49. if (this.data.uploadState) {
  50. return
  51. }
  52. this.setData({
  53. uploadFlag: true
  54. })
  55. const uploadTask = wx.uploadFile({
  56. url: 'https://reader-api.ai160.com//file/upload',
  57. filePath: this.data.readDetail.tempFilePath,
  58. name: '朗读录音',
  59. header: {
  60. uid: wx.getStorageSync('uid')
  61. },
  62. success: (res) => {
  63. const formateRes = JSON.parse(res.data);
  64. let audioPath = formateRes.data;
  65. this.uploadWorks(audioPath);
  66. },
  67. fail: () => {
  68. wx.showToast({
  69. title: '上传失败请重试',
  70. icon: 'error',
  71. duration: 2000
  72. })
  73. this.setData({
  74. uploadFlag: false
  75. })
  76. }
  77. })
  78. uploadTask.onProgressUpdate((res) => {
  79. this.setData({
  80. percent: res.progress
  81. })
  82. })
  83. await userEvent({
  84. action: 'WXUPLOAD',
  85. })
  86. },
  87. cancelMask() {
  88. this.setData({
  89. uploadSuccess: false
  90. })
  91. },
  92. async uploadWorks(audio) {
  93. const data = {
  94. exampleId: this.data.readDetail.id,
  95. audioPath: audio,
  96. originVideo: this.data.readDetail.originVideo,
  97. activityId: this.properties.activityId
  98. };
  99. let res
  100. if (this.properties.readingType == 'readMatch') {
  101. res = await publishRankWorks(data)
  102. } else {
  103. res = await publishWorks(data)
  104. }
  105. wx.setStorageSync('shareVideoId', res.id)
  106. let _data = this.data.readDetail
  107. let scoreRes = await postWorksScore({
  108. userReadId: res.id,
  109. complete: _data.integrity,
  110. accuracy: _data.accuracy,
  111. speed: _data.fluency,
  112. intonation: _data.tone,
  113. score: _data.myOverall,
  114. })
  115. console.log(scoreRes, 'scoreRes');
  116. this.setData({
  117. uploadFlag: false,
  118. uploadState: true,
  119. uploadSuccess: true,
  120. })
  121. },
  122. },
  123. })