index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. actions: {
  23. setReadDetail: 'setReadDetail'
  24. }
  25. },
  26. /**
  27. * 组件的属性列表
  28. */
  29. properties: {
  30. readingType: '',
  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. console.log('shareVideo', res);
  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. this.setData({
  117. uploadFlag: false,
  118. uploadState: true,
  119. uploadSuccess: true,
  120. })
  121. },
  122. },
  123. })