index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import {
  2. getActivityInfo,
  3. saveActivity
  4. } from '~/api/activity'
  5. /*微信录音*/
  6. let recorderManager = wx.getRecorderManager();
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. state: false,
  13. tens: 3,
  14. bits: 0,
  15. fromWidth: 100,
  16. toWidth: 100,
  17. configure: {},
  18. avatar: '',
  19. percent: 0,
  20. tempFilePath: '',
  21. uploadState: false,
  22. // 此id保存模板使用
  23. id: ''
  24. },
  25. async onLoad(options) {
  26. let configure = await getActivityInfo(options.id)
  27. console.log(configure);
  28. this.setData({
  29. configure,
  30. avatar: configure.photoText,
  31. toWidth: '',
  32. fromWidth: configure.fromText.length * configure.fromFontSize,
  33. toWidth: configure.toText.length * configure.toFontSize,
  34. id: options.id,
  35. // 是否更改过头像
  36. changeAvatarFlag: false
  37. })
  38. },
  39. setInput({
  40. currentTarget
  41. }) {
  42. console.log(currentTarget.dataset.info);
  43. },
  44. changeAvatar: function (e) {
  45. const {
  46. avatarUrl
  47. } = e.detail
  48. wx.uploadFile({
  49. url: 'https://reader-api.ai160.com/file/upload',
  50. filePath: avatarUrl,
  51. name: '头像',
  52. header: {
  53. uid: wx.getStorageSync('uid')
  54. },
  55. success: (res) => {
  56. const newAvatar = JSON.parse(res.data).data;
  57. this.setData({
  58. changeAvatarFlag: true,
  59. avatar: newAvatar
  60. })
  61. }
  62. })
  63. },
  64. play() {
  65. return
  66. let dsq
  67. if (!this.data.state) {
  68. this.setData({
  69. state: true
  70. })
  71. let countDown = 30
  72. // 倒计时
  73. dsq = setInterval(item => {
  74. if (countDown == 1) {
  75. clearInterval(dsq)
  76. }
  77. if (countDown % 10 == 0) {
  78. this.setData({
  79. tens: --this.data.tens,
  80. bits: 9
  81. })
  82. } else {
  83. this.setData({
  84. bits: --this.data.bits
  85. })
  86. }
  87. --countDown
  88. }, 1000)
  89. const options = {
  90. sampleRate: 44100, //采样率
  91. numberOfChannels: 1, //录音通道数
  92. encodeBitRate: 192000, //编码码率
  93. format: 'mp3', //音频格式,有效值aac/mp3
  94. frameSize: 50 //指定帧大小,单位 KB
  95. };
  96. //开始录音,在开始录音回调中feed音频片
  97. recorderManager.start(options);
  98. //监听录音结束事件
  99. recorderManager.onStop((res) => {
  100. console.log('录音结束', res);
  101. this.setData({
  102. tempFilePath: res.tempFilePath,
  103. });
  104. this.uploadAudio(res.tempFilePath)
  105. });
  106. } else {
  107. console.log('结束');
  108. clearInterval(dsq)
  109. recorderManager.stop();
  110. this.setData({
  111. state: false,
  112. tens: 3,
  113. bits: 0
  114. })
  115. }
  116. },
  117. uploadAudio(recordSource) {
  118. wx.uploadFile({
  119. url: 'https://reader-api.ai160.com//file/upload',
  120. filePath: recordSource,
  121. name: '朗读录音',
  122. header: {
  123. uid: wx.getStorageSync('uid')
  124. },
  125. success: (res) => {
  126. const formateRes = JSON.parse(res.data);
  127. let audioPath = formateRes.data;
  128. console.log(formateRes);
  129. // this.shareWorks(audioPath);
  130. }
  131. })
  132. },
  133. bindKeyInput: function (e) {
  134. console.log(e);
  135. if (e.currentTarget.dataset.type == 'from') {
  136. this.setData({
  137. fromWidth: e.detail.cursor * this.data.configure.fromFontSize,
  138. 'configure.fromText': e.detail.value
  139. })
  140. } else if (e.currentTarget.dataset.type == 'to') {
  141. this.setData({
  142. toWidth: e.detail.cursor * this.data.configure.toFontSize,
  143. 'configure.toText': e.detail.value
  144. })
  145. }
  146. /* this.setData({
  147. inputValue: e.detail.value
  148. }) */
  149. },
  150. // 生成活动图片
  151. createActivityImg() {
  152. return new Promise((resolve, reject) => {
  153. let context = wx.createSelectorQuery();
  154. context
  155. .select('#share')
  156. .fields({
  157. node: true,
  158. size: true
  159. }).exec((res) => {
  160. const canvas = res[0].node;
  161. const ctx = canvas.getContext('2d');
  162. const dpr = wx.getSystemInfoSync().pixelRatio;
  163. canvas.width = res[0].width * dpr;
  164. canvas.height = res[0].height * dpr;
  165. ctx.scale(dpr, dpr);
  166. let avatar = canvas.createImage();
  167. wx.getImageInfo({
  168. src: this.data.avatar,
  169. success: function (res) {
  170. console.log(res);
  171. avatar.src = res.path
  172. },
  173. });
  174. avatar.onload = () => {
  175. ctx.drawImage(avatar, this.data.configure.templateBase.photoLeft / 2, this.data.configure.templateBase.photoTop / 2, this.data.configure.templateBase.photoWidth / 2, this.data.configure.templateBase.photoHeight / 2);
  176. let bgImg = canvas.createImage();
  177. wx.getImageInfo({
  178. src: this.data.configure.bgImg,
  179. success: function (res) {
  180. bgImg.src = res.path
  181. },
  182. });
  183. bgImg.onload = () => {
  184. ctx.drawImage(bgImg, 0, 0, 375, 300);
  185. ctx.font = `${this.data.configure.fromFontSize/2}px PingFang`;
  186. ctx.fillText(this.data.configure.toText, this.data.configure.templateBase.toLeft / 2, this.data.configure.templateBase.toTop / 2 + this.data.configure.fromFontSize / 2)
  187. ctx.fillText(this.data.configure.fromText, this.data.configure.templateBase.fromLeft / 2, this.data.configure.templateBase.fromTop / 2 + this.data.configure.toFontSize / 2)
  188. console.log(this.data.configure.toText, this.data.configure.templateBase.toLeft / 2, this.data.configure.templateBase.toTop / 2);
  189. console.log(this.data.configure.fromText, this.data.configure.templateBase.fromLeft / 2, this.data.configure.templateBase.fromTop / 2);
  190. }
  191. }
  192. setTimeout(() => {
  193. wx.canvasToTempFilePath({
  194. canvas: canvas,
  195. success(res) {
  196. console.log(res);
  197. resolve({
  198. title: '请欣赏我的课文朗读作品,点赞+评论。',
  199. path: `/pages/index/index`,
  200. imageUrl: res.tempFilePath
  201. })
  202. },
  203. fail(res) {
  204. reject()
  205. }
  206. }, this)
  207. }, 1000)
  208. })
  209. })
  210. },
  211. onShareAppMessage({
  212. from,
  213. }) {
  214. if (from == 'button') {
  215. const promise = new Promise(resolve => {
  216. this.createActivityImg().then(res => {
  217. resolve(res)
  218. })
  219. })
  220. return {
  221. title: '请欣赏我的课文朗读作品,点赞+评论。',
  222. path: `/pages/index/index?readId`,
  223. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png',
  224. promise
  225. }
  226. } else {
  227. return {
  228. title: '课文朗读,从未如此有趣。',
  229. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  230. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  231. }
  232. }
  233. }
  234. })