index.js 2.7 KB

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