接到一个制作仅展示静态页面的小程序的要求。我看了下具体的要求,其实页面无需用到后端,资源也不多,算上视频两三个G而已,如果为此专门租用一个服务器,就算是微信云开发我觉得都亏了(而且对于学生党服务器也太贵太麻烦了),所以开始想办法不用服务器储存需要用到的图片,视频等静态资源。最后是整出来了。

首先是图片资源:

        这个简单,解决方案就是将图片上传到如QQ空间,微博等地方,再右键获取图片链接,填入image组件的src属性中就好了。

        我用的是微博。Tips:用微博上传图片时建议用手机端进行,经过实测pc端缺少了减少水印的功能,而且上传的图片会被强制压缩,而手机端可以去除水印,还能上传原图(上传时记得设置成仅自己可见!!不然真的会有阅读量的!好羞耻QAQ)。ps:用微博上传的视频,右键获取连接后,链接一段时间后会失效的,所以储存视频需要用到别的方法。

其次是视频资源:

        这个就有些复杂了,最后的解决方法是将需要用到的资源上传到腾讯视频,拿到视频地址url,再通过一些操作就可以拿到填入video组件的src属性的最终url了。如下:

js界面,初始数据:

   /**
     * 页面的初始数据
     */
    data: {
      videos:{
        video1:{},
        video2:{},
      }
    },

js界面,函数:(更改只需改video_init

video_init:async function(){
      let url_1 = 'https://v.qq.com/x/page/d35055e6cqi.html' //视频1
      let url_2 = 'https://v.qq.com/x/page/t3505tytxy9.html' //视频2
      wx.showLoading({
          title: '加载中...'
      })
      //拿数据
      let result1=await this.getVideo(url_1).then((res)=>{return res;});
      //设置数据
      let temp1=this.getResult(result1,'video1');
      //重复
      let result2=await this.getVideo(url_2).then((res)=>{return res;});
      let temp2=this.getResult(result2,'video2');
       //setData
      this.setData({
         ['videos.video1']:temp1,
         ['videos.video2']:temp2,
      })
      wx.hideLoading();
  },
//接收视频所在网页url,返回一个对象,如下:
  getVideo:async function(url){
      let result={//返回的对象的数据格式
          video_source:'',//放入video组件src属性的部分
          video_direction:'',//这个属性也没必要,可以删掉
      }
      return new Promise(function(resovle,reject){
          if (url.indexOf('v.qq.com') !== -1) {
              var key = url.match(/http(s?):\/\/(.+?)\/(\w+?).html/)
              if (key && key[3]) {
                  wx.request({
                      url: 'https://h5vv.video.qq.com/getinfo?otype=json&platform=11001&sphttps=1&vid=' + key[3],
                      success: function(res) {
                          var data = res.data
                          data = data.slice(13, data.length - 1)
                          try {
                              var ret = JSON.parse(data);
                              result.video_source=ret.vl.vi[0].ul.ui[0].url + ret.vl.vi[0].fn + '?vkey=' + ret.vl.vi[0].fvkey;
                              result.video_direction= 90;
                              resovle(result);
                          } catch (e) {
                              wx.showToast({
                                  title: '视频地址解析失败'
                              })
                              throw "error!";
                          }
                      }
                  })
              }
          }
      })
      
  },
  //储存video_source的数据可能很复杂,一层套一层的,比如item:{videos:{video1:{}}}所以
  //增加了一步,用于封装设置数据的操作,免得接下来在setData函数中写的太复杂。
  getResult(res,name){
      let temp1=this.data.videos[name]
      temp1.video_source=res.video_source;
      temp1.video_direction=res.video_direction;
      return temp1;
  },

wxml页面:

<video direction='{{videos.video1.video_direction}}' src='{{videos.video1.video_source}}' style="width: 100%;height: 300rpx;"/>
<video direction='{{videos.video2.video_direction}}' src='{{videos.video2.video_source}}' style="width: 100%;height: 300rpx;"/>

最终就可以播放啦!

最后需要感谢一位大哥!关于视频的相关代码是我对这位大哥的代码进行封装整理出来的:

在这个网址的评论区:

 小程序播放第三方网址 | 微信开放社区 (qq.com)

“我有办法”

        英雄登场!

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐