doTask.js 10 KB

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