index.js 8.5 KB

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