index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. userReadId: ''
  44. },
  45. /**
  46. * 组件的方法列表
  47. */
  48. methods: {
  49. async upload() {
  50. if (this.data.uploadState) {
  51. return
  52. }
  53. this.setData({
  54. uploadFlag: true
  55. })
  56. const uploadTask = wx.uploadFile({
  57. url: 'https://reader-api.ai160.com//file/upload',
  58. filePath: this.data.readDetail.tempFilePath,
  59. name: '朗读录音',
  60. header: {
  61. uid: wx.getStorageSync('uid')
  62. },
  63. success: (res) => {
  64. const formateRes = JSON.parse(res.data);
  65. let audioPath = formateRes.data;
  66. this.uploadWorks(audioPath);
  67. },
  68. fail: () => {
  69. wx.showToast({
  70. title: '上传失败请重试',
  71. icon: 'error',
  72. duration: 2000
  73. })
  74. this.setData({
  75. uploadFlag: false
  76. })
  77. }
  78. })
  79. uploadTask.onProgressUpdate((res) => {
  80. this.setData({
  81. percent: res.progress
  82. })
  83. })
  84. await userEvent({
  85. action: 'WXUPLOAD',
  86. })
  87. },
  88. cancelMask() {
  89. this.setData({
  90. uploadSuccess: false
  91. })
  92. },
  93. async uploadWorks(audio) {
  94. const data = {
  95. exampleId: this.data.readDetail.id,
  96. audioPath: audio,
  97. originVideo: this.data.readDetail.originVideo,
  98. activityId: this.properties.activityId
  99. };
  100. let res
  101. if (this.properties.readingType == 'readMatch') {
  102. res = await publishRankWorks(data)
  103. } else {
  104. res = await publishWorks(data)
  105. }
  106. wx.setStorageSync('shareVideoId', res.id)
  107. let _data = this.data.readDetail
  108. let scoreRes = await postWorksScore({
  109. userReadId: res.id,
  110. complete: _data.integrity,
  111. accuracy: _data.accuracy,
  112. speed: _data.fluency,
  113. intonation: _data.tone,
  114. score: _data.myOverall,
  115. })
  116. console.log(scoreRes, 'scoreRes');
  117. this.setData({
  118. uploadFlag: false,
  119. uploadState: true,
  120. uploadSuccess: true,
  121. userReadId: res.id,
  122. })
  123. wx.disableAlertBeforeUnload();
  124. },
  125. jumpWork() {
  126. wx.redirectTo({
  127. url: '/pages/userWorks/index'
  128. })
  129. }
  130. },
  131. })