index.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <template>
  2. <div class="page" :style="{ height: height }">
  3. <div class="swiper" :style="containerStyle">
  4. <block v-for="(item, index) in videoList" :key="index">
  5. <video-new
  6. class="video"
  7. :video_id="item.video_id"
  8. :nickname="item.nickname"
  9. :video_describe="item.video_describe"
  10. :cover_url="item.cover_url"
  11. :video_url="item.video_url"
  12. :dianzan="item.dianzan"
  13. :is_dianzan="item.is_dianzan"
  14. :pinglun="item.pinglun"
  15. :zhuanfa="item.zhuanfa"
  16. :height="height"
  17. :width="width"
  18. :play="item.flag"
  19. :index="index"
  20. :cur_index="cur_index"
  21. :style="{ height: height, width: width }"
  22. ></video-new>
  23. </block>
  24. </div>
  25. <cover-view
  26. class="cover-view-marks"
  27. :style="{ height: height, width: width }"
  28. @click.stop="clickVideo"
  29. @touchmove="ListTouchMove"
  30. @touchend.stop="ListTouchEnd"
  31. @touchstart="ListTouchStart">
  32. </cover-view>
  33. <cover-view class="wrapper" :style="{ width: width }">
  34. <cover-view class="title-center" :style="{ width: width }">
  35. <!-- <text class="title" :class="video_type == 'recommend' ? 'active-title' : ''" @click="switchTtype('recommend')">推荐</text>-->
  36. <!-- <text class="title" :class="video_type == 'follow' ? 'active-title' : ''" @click="switchTtype('follow')">关注</text>-->
  37. </cover-view>
  38. </cover-view>
  39. </div>
  40. </template>
  41. <script>
  42. import videoNew from './app-goods-video.vue';
  43. export default {
  44. components: {
  45. videoNew
  46. },
  47. data() {
  48. return {
  49. video_type: 'recommend',
  50. translateX: '10',
  51. height: '667px',
  52. cur_index: null,
  53. width: '',
  54. sysheight: 0,
  55. containerStyle: '',
  56. distance: 0,
  57. listTouchStartY: 0,
  58. listTouchDirection: null,
  59. scroll: false,
  60. videoList: [],
  61. clicktime:0,
  62. };
  63. },
  64. onLoad() {
  65. let res = uni.getSystemInfoSync();
  66. this.height = `${res.windowHeight}px`;
  67. this.width = `${res.windowWidth}px`;
  68. this.sysheight = res.windowHeight;
  69. console.log(res);
  70. this.getVideoList();
  71. },
  72. onHide() {
  73. console.log('界面隐藏,暂停播放',`video_${this.videoList[this.cur_index].video_id}`);
  74. this.videoCtx = uni.createVideoContext(`video_${this.videoList[this.cur_index].video_id}`, this);
  75. this.videoCtx.pause();
  76. },
  77. onShow() {
  78. console.log('界面显示')
  79. if (this.cur_index !== null && this.videoList[this.cur_index].flag) {
  80. console.log('开启播放',`video_${this.videoList[this.cur_index].video_id}`);
  81. this.videoCtx = uni.createVideoContext(`video_${this.videoList[this.cur_index].video_id}`, this);
  82. this.videoCtx.play();
  83. }
  84. },
  85. onReady() {},
  86. methods: {
  87. switchTtype(type) {
  88. console.log(type);
  89. this.video_type = type;
  90. this.getVideoList(true);
  91. },
  92. getVideoList(refresh) {
  93. uni.request({
  94. url: 'https://api.52170.xin/video',
  95. dataType: 'json',
  96. success: res => {
  97. console.log(res);
  98. if (refresh) this.videoList = [];
  99. for (let item of res.data.data.list) {
  100. this.videoList.push({
  101. video_id: item.video_id,
  102. nickname: item.nickname,
  103. video_describe: item.video_describe,
  104. cover_url: item.cover_url,
  105. video_url: item.video_url,
  106. dianzan: item.dianzan,
  107. pinglun: item.pinglun,
  108. zhuanfa: item.zhuanfa,
  109. is_dianzan: item.is_dianzan,
  110. flag: false
  111. });
  112. }
  113. try {
  114. if (this.cur_index == null) this.cur_index = 0;
  115. this.videoList[this.cur_index].flag = true;
  116. setTimeout(cur_video_id => {
  117. this.videoCtx = uni.createVideoContext(`video_${this.videoList[this.cur_index].video_id}`, this);
  118. this.videoCtx.play();
  119. }, 100);
  120. } catch (e) {
  121. console.log(e);
  122. }
  123. }
  124. });
  125. },
  126. clickVideo(e) {
  127. if(this.clicktime > 0 && e.timeStamp - this.clicktime < 200){
  128. clearInterval(this.han);
  129. if(!this.videoList[this.cur_index].is_dianzan){
  130. this.videoList[this.cur_index].is_dianzan = true;
  131. }
  132. }else{
  133. let _this = this;
  134. this.han = setTimeout(function() {
  135. _this.videoList[_this.cur_index].flag = !_this.videoList[_this.cur_index].flag;
  136. }, 200);
  137. }
  138. this.clicktime = e.timeStamp;
  139. return;
  140. },
  141. ListTouchStart(e) {
  142. console.log(1, e);
  143. this.listTouchStartY = e.changedTouches[0].pageY;
  144. this.moverOnoff = false;
  145. },
  146. ListTouchMove(e) {
  147. console.log(2, e);
  148. this.listTouchDirection = this.listTouchStartY - e.changedTouches[0].pageY > 10 ? -1 : 0;
  149. this.listTouchDirection = e.changedTouches[0].pageY - this.listTouchStartY > 10 ? 1 : this.listTouchDirection;
  150. const distance = this.distance + e.changedTouches[0].pageY - this.listTouchStartY;
  151. if (distance > 0) return;
  152. if (Math.abs(this.listTouchStartY - e.changedTouches[0].pageY) < 2) {
  153. this.listTouchDirection = null;
  154. }
  155. },
  156. ListTouchEnd(e) {
  157. console.log(3, e);
  158. this.handelVideo(e);
  159. },
  160. handelVideo(e){
  161. if (this.cur_index == 0 && this.listTouchDirection == 1) {
  162. console.log('刷新视频');
  163. this.getVideoList(true);
  164. return;
  165. }
  166. try {
  167. this.translateX = 10;
  168. } catch (e) {
  169. console.log('错误');
  170. console.log(e);
  171. }
  172. if (this.scroll) return;
  173. if (!this.listTouchDirection) {
  174. return;
  175. }
  176. const destination = this.listTouchDirection * this.sysheight + this.distance;
  177. if (destination > 0 || destination < -this.sysheight * (this.videoList.length - 1)) return;
  178. for (let item of this.videoList) {
  179. item.flag = false;
  180. }
  181. this.animate(destination, this.listTouchDirection);
  182. this.cur_index = this.cur_index - this.listTouchDirection;
  183. this.videoList[this.cur_index].flag = true;
  184. console.log('切换第' + this.cur_index + '个');
  185. //移动到最后一个加载新的列表
  186. if (this.videoList.length == this.cur_index + 1) {
  187. this.getVideoList();
  188. }
  189. this.listTouchDirection = null;
  190. },
  191. animate(des, direc) {
  192. console.log(34534, des, direc);
  193. let { distance } = this;
  194. let promise = new Promise((resolve, reject) => {
  195. this.scroll = true;
  196. const temp = setInterval(() => {
  197. if ((direc === -1 && des < distance) || (direc === 1 && des > distance)) {
  198. distance += 100 * direc;
  199. this.containerStyle = `transform:translateY(${distance}px)`;
  200. } else {
  201. clearInterval(temp);
  202. distance = des;
  203. this.distance = des;
  204. this.containerStyle = `transform:translateY(${distance}px)`;
  205. this.scroll = false;
  206. resolve();
  207. }
  208. }, 20);
  209. });
  210. return promise;
  211. },
  212. tapMsg(e) {
  213. console.log(5, e);
  214. },
  215. tapShare(e) {
  216. console.log(6, e);
  217. },
  218. tapLove(e) {
  219. console.log(7, e);
  220. }
  221. }
  222. };
  223. </script>
  224. <style>
  225. .page {
  226. flex: 1;
  227. overflow: hidden;
  228. background-color: #000;
  229. }
  230. .swiper {
  231. position: relative;
  232. }
  233. .cover-view-marks {
  234. position: fixed;
  235. top: 0;
  236. right: 0;
  237. z-index: 9998;
  238. }
  239. .wrapper {
  240. position: fixed;
  241. top: 80upx;
  242. background-color: transparent;
  243. flex-direction: row;
  244. width: 100%;
  245. z-index: 9999;
  246. }
  247. .video {
  248. width: 100%;
  249. height: 100%;
  250. position: relative;
  251. }
  252. .title {
  253. line-height: 80upx;
  254. color: #eee;
  255. font-size: 18px;
  256. margin: 0 10px;
  257. }
  258. .title-center {
  259. flex-direction: row;
  260. height: 45px;
  261. line-height: 30px;
  262. justify-content: center;
  263. align-items: center;
  264. //#ifndef APP-PLUS-NVUE
  265. display: flex;
  266. //#endif
  267. }
  268. .active-title {
  269. border-bottom-width: 2px;
  270. font-weight: bold;
  271. border-style: solid;
  272. border-bottom-color: #ffffff;
  273. color: #ffffff;
  274. }
  275. .img {
  276. width: 50upx;
  277. height: 50upx;
  278. margin-top: 18upx;
  279. }
  280. </style>