doTask.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // pages/doTask/doTask.js
  2. import VODUpload from '../../utils/aliyun-upload-sdk-1.0.1.min'
  3. const app = getApp()
  4. const util = require("../../utils/util")
  5. const urlDef = require("../../utils/urls")
  6. const recordManager = wx.getRecorderManager() //录音对象
  7. const audioContext = wx.createInnerAudioContext() //音频播放对象
  8. var uploader = null //阿里云视频上传对象
  9. const urls = urlDef.urls;
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. talkFlag: false,
  16. closeName: '开始录音',
  17. videoList: [],
  18. videoFlag: 0,
  19. imgList: [],
  20. audioList:[],
  21. audioPause: false
  22. },
  23. getRecord: function () {
  24. const that = this
  25. if (that.data.talkFlag) {
  26. recordManager.stop()
  27. recordManager.onStop((res) => { //监听录音停止的事件
  28. console.log("监听录音停止事件",res)
  29. if (res.duration < 1000) {
  30. wx.showToast({
  31. title: '录音时间太短',
  32. icon: none
  33. })
  34. return;
  35. } else {
  36. console.log(res)
  37. var tempFilePath = res.tempFilePath; // 文件临时路径
  38. console.log("文件临时路径", tempFilePath)
  39. wx.uploadFile({
  40. filePath: tempFilePath,
  41. name: 'name',
  42. url: urls.file_upload,
  43. header: util.getHeaders(),
  44. success(res) {
  45. wx.showToast({
  46. title: '上传成功',
  47. })
  48. const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
  49. console.log(rs)
  50. const list = [rs.data[0].url]
  51. that.setData({
  52. audioList:that.data.audioList.concat(list)
  53. })
  54. },
  55. fail(res) {
  56. wx.showToast({
  57. title: '上传失败',
  58. icon: 'none'
  59. })
  60. },
  61. complete(res) {
  62. wx.hideLoading({
  63. success: (res) => {},
  64. })
  65. }
  66. })
  67. }
  68. });
  69. that.setData({
  70. talkFlag: false,
  71. closeName: '开始录音'
  72. })
  73. } else {
  74. wx.getSetting({
  75. success(res) {
  76. if (!res.authSetting['scope.record']) {
  77. wx.authorize({
  78. scope: 'scope.record',
  79. success() {
  80. // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
  81. const options = {}
  82. recordManager.start(options)
  83. },
  84. fail(){
  85. wx.openSetting({
  86. withSubscriptions: true,
  87. })
  88. }
  89. })
  90. } else {
  91. that.setData({
  92. talkFlag: true,
  93. closeName: '正在录音,再次点击结束录音'
  94. })
  95. const options = {}
  96. recordManager.start(options)
  97. }
  98. }
  99. })
  100. }
  101. },
  102. showAudio:function(e){
  103. const index = e.currentTarget.dataset.index
  104. if(this.data.curAudioIndex != index){
  105. this.setData({
  106. curAudioIndex: index,
  107. })
  108. audioContext.src = this.data.audioList[index]
  109. audioContext.play()
  110. audioContext.onEnded((res)=>{
  111. this.setData({
  112. curAudioIndex: '-1',
  113. })
  114. })
  115. }else{
  116. if(this.data.audioPause){
  117. audioContext.play()
  118. this.setData({
  119. audioPause: false
  120. })
  121. } else {
  122. audioContext.pause()
  123. this.setData({
  124. audioPause: true
  125. })
  126. }
  127. }
  128. },
  129. delAudio:function(e){
  130. const index = e.currentTarget.dataset.index
  131. this.data.audioList.splice(index,1);
  132. this.setData({
  133. audioList: this.data.audioList
  134. })
  135. },
  136. uploadTap: function () {
  137. const that = this
  138. wx.chooseImage({
  139. count: 6,
  140. sourceType: ['album', 'camera'],
  141. success(res) {
  142. wx.showLoading({
  143. title: '上传中...',
  144. })
  145. for (var i in res.tempFilePaths) {
  146. wx.uploadFile({
  147. filePath: res.tempFilePaths[i],
  148. name: 'name',
  149. url: urls.file_upload,
  150. header: util.getHeaders(),
  151. success(res) {
  152. wx.showToast({
  153. title: '上传成功',
  154. })
  155. const rs = JSON.parse(JSON.parse(JSON.stringify(res.data)))
  156. const list = [rs.data[0].url]
  157. that.setData({
  158. imgList: that.data.imgList.concat(list)
  159. })
  160. },
  161. fail(res) {
  162. wx.showToast({
  163. title: '上传失败',
  164. icon: none
  165. })
  166. },
  167. complete(res) {
  168. wx.hideLoading({
  169. success: (res) => {},
  170. })
  171. }
  172. })
  173. }
  174. }
  175. })
  176. },
  177. showImg: function (e) {
  178. const index = e.currentTarget.dataset.index
  179. wx.previewImage({
  180. current: this.data.imgList[index],
  181. urls: this.data.imgList,
  182. })
  183. },
  184. delImg: function (e) {
  185. const index = e.currentTarget.dataset.index
  186. this.data.imgList.splice(index, 1);
  187. this.setData({
  188. imgList: this.data.imgList
  189. })
  190. },
  191. upLoadVideo: function () {
  192. const that = this
  193. wx.chooseVideo({
  194. camera: ['album'],
  195. success(res) {
  196. var file = {
  197. url: res.tempFilePath,
  198. coverUrl: res.thumbTempFilePath
  199. };
  200. var userData = '{"Vod":{}}';
  201. const urls = urlDef.urls;
  202. let createUrl = urls.video_create_upload + '?title=' + res.tempFilePath + '&fileName=' + res.tempFilePath;
  203. console.log('createUploadVideo ... ');
  204. util.apiPost(createUrl).then((rs) => {
  205. let d = JSON.parse(rs.data);
  206. // console.log('createUploadVideo ' + JSON.stringify(d));
  207. that.setData({
  208. uploadAuth: d.UploadAuth,
  209. uploadAddress: d.UploadAddress,
  210. videoId: d.VideoId,
  211. })
  212. uploader.addFile(file, null, null, null, userData)
  213. uploader.startUpload();
  214. // uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
  215. }, e => {
  216. console.log('createUploadVideo error ' + JSON.stringify(e));
  217. });
  218. }
  219. })
  220. },
  221. showVideo: function (e) {
  222. const index = e.currentTarget.dataset.index
  223. const curVideo = this.data.videoList[index].src
  224. this.setData({
  225. curVideo
  226. })
  227. this.videoContext.requestFullScreen({ // 设置全屏时视频的方向,不指定则根据宽高比自动判断。
  228. direction: 90 // 屏幕逆时针90度
  229. });
  230. },
  231. // 监听视频是否为全屏,否则直接关闭
  232. bindfullscreenchange: function () {
  233. if (this.data.videoFlag == 0) {
  234. this.setData({
  235. videoFlag: 1
  236. })
  237. } else {
  238. this.setData({
  239. videoFlag: 0,
  240. curVideo: ""
  241. })
  242. }
  243. },
  244. delVideo: function (e) {
  245. const index = e.currentTarget.dataset.index
  246. this.data.videoList.splice(index, 1);
  247. this.setData({
  248. videoList: this.data.videoList
  249. })
  250. },
  251. /**
  252. * 生命周期函数--监听页面加载
  253. */
  254. onLoad: function (options) {
  255. this.videoContext = wx.createVideoContext('play-video'); // 创建 video 上下文 VideoContext 对象。
  256. const that = this
  257. const createUpLoad = new VODUpload({
  258. //阿里账号ID,必须有值
  259. userId: "WaWQOn6gXod13WLEp8cr4ljUdvcbXJ",
  260. //网络原因失败时,重新上传次数,默认为3
  261. retryCount: 3,
  262. //网络原因失败时,重新上传间隔时间,默认为2秒
  263. retryDuration: 2,
  264. //开始上传
  265. 'onUploadstarted': function (uploadInfo) {
  266. wx.showLoading({
  267. title: '上传中...',
  268. mask: true
  269. })
  270. // console.log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object)
  271. //上传方式1,需要根据uploadInfo.videoId是否有值,调用点播的不同接口获取uploadauth和uploadAddress,如果videoId有值,调用刷新视频上传凭证接口,否则调用创建视频上传凭证接口
  272. if (uploadInfo.videoId) {
  273. //如果uploadInfo.videoId存在,调用刷新视频上传凭证接口
  274. } else {
  275. //如果uploadInfo.videoId不存在,调用获取视频上传地址和凭证接口
  276. }
  277. //从点播服务获取的uploadAuth、uploadAddress和videoId,设置SDK
  278. console.log(that.data)
  279. uploader.setUploadAuthAndAddress(uploadInfo, that.data.uploadAuth, that.data.uploadAddress, that.data.videoId);
  280. },
  281. //文件上传成功
  282. 'onUploadSucceed': function (uploadInfo) {
  283. // console.log("上传成功:" + JSON.stringify(uploadInfo))
  284. var json = {
  285. src: uploadInfo.url,
  286. img: uploadInfo.coverUrl
  287. }
  288. var list = [json]
  289. that.setData({
  290. videoList: that.data.videoList.concat(list)
  291. })
  292. wx.showToast({
  293. title: '上传成功',
  294. })
  295. // console.log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
  296. },
  297. //文件上传失败
  298. 'onUploadFailed': function (uploadInfo, code, message) {
  299. // console.log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
  300. wx.showToast({
  301. title: '上传失败',
  302. })
  303. },
  304. //文件上传进度,单位:字节
  305. 'onUploadProgress': function (uploadInfo, totalSize, loadedPercent) {
  306. // console.log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
  307. },
  308. //上传凭证超时
  309. 'onUploadTokenExpired': function (uploadInfo) {
  310. wx.showToast({
  311. title: '上传超时',
  312. })
  313. console.log("onUploadTokenExpired");
  314. //实现时,根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
  315. //从点播服务刷新的uploadAuth,设置到SDK里
  316. uploader.resumeUploadWithAuth(uploadAuth);
  317. },
  318. //全部文件上传结束
  319. 'onUploadEnd': function (uploadInfo) {
  320. console.log("onUploadEnd: uploaded all the files");
  321. }
  322. });
  323. uploader = createUpLoad
  324. },
  325. /**
  326. * 生命周期函数--监听页面初次渲染完成
  327. */
  328. onReady: function () {
  329. },
  330. /**
  331. * 生命周期函数--监听页面显示
  332. */
  333. onShow: function () {
  334. },
  335. /**
  336. * 生命周期函数--监听页面隐藏
  337. */
  338. onHide: function () {
  339. },
  340. /**
  341. * 生命周期函数--监听页面卸载
  342. */
  343. onUnload: function () {
  344. },
  345. /**
  346. * 页面相关事件处理函数--监听用户下拉动作
  347. */
  348. onPullDownRefresh: function () {
  349. },
  350. /**
  351. * 页面上拉触底事件的处理函数
  352. */
  353. onReachBottom: function () {
  354. },
  355. /**
  356. * 用户点击右上角分享
  357. */
  358. onShareAppMessage: function () {
  359. }
  360. })