index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. "use strict";
  2. import httpRequestApi from '../../utils/APIClient';
  3. Component({
  4. options: {
  5. addGlobalClass: true,
  6. pureDataPattern: /^_/
  7. },
  8. properties: {
  9. ifHeadTap:{
  10. type: Boolean,
  11. value: true
  12. },
  13. isSwiper: {
  14. type: Boolean,
  15. value: true
  16. },
  17. duration: {
  18. type: Number,
  19. value: 500
  20. },
  21. easingFunction: {
  22. type: String,
  23. value: 'default'
  24. },
  25. loop: {
  26. type: Boolean,
  27. value: true
  28. },
  29. videoList: {
  30. type: Array,
  31. value: [],
  32. observer: function observer() {
  33. console.log('this.isSwiper', this.data.isSwiper)
  34. if (!this.data.isSwiper) return;
  35. var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  36. this._videoListChanged(newVal);
  37. }
  38. },
  39. nextMargin: {
  40. type: String,
  41. value: '400rpx'
  42. }
  43. },
  44. data: {
  45. nextQueue: [],
  46. prevQueue: [],
  47. curQueue: [],
  48. circular: false,
  49. // nextMargin: '400rpx',
  50. _last: 1,
  51. _change: -1,
  52. _invalidUp: 0,
  53. _invalidDown: 0,
  54. _videoContexts: [],
  55. inputValue: '',
  56. showControl: 0
  57. },
  58. lifetimes: {
  59. attached: function attached() {
  60. this.data._videoContexts = [wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)];
  61. }
  62. },
  63. methods: {
  64. _videoListChanged: function _videoListChanged(newVal) {
  65. var _this = this;
  66. var data = this.data;
  67. console.log('newVal', newVal)
  68. newVal.forEach(function (item) {
  69. data.nextQueue.push(item);
  70. });
  71. if (data.curQueue.length === 0) {
  72. this.setData({
  73. curQueue: data.nextQueue.splice(0, 3)
  74. }, function () {
  75. _this.playCurrent(1);
  76. });
  77. }
  78. },
  79. animationfinish: function animationfinish(e) {
  80. var _data = this.data,
  81. _last = _data._last,
  82. _change = _data._change,
  83. curQueue = _data.curQueue,
  84. prevQueue = _data.prevQueue,
  85. nextQueue = _data.nextQueue;
  86. var current = e.detail.current;
  87. var diff = current - _last;
  88. if (diff === 0) return;
  89. this.data._last = current;
  90. this.playCurrent(current);
  91. this.triggerEvent('change', {
  92. activeId: curQueue[current].id
  93. });
  94. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  95. if (direction === 'up') {
  96. if (this.data._invalidDown === 0) {
  97. var change = (_change + 1) % 3;
  98. var add = nextQueue.shift();
  99. var remove = curQueue[change];
  100. if (add) {
  101. prevQueue.push(remove);
  102. curQueue[change] = add;
  103. this.data._change = change;
  104. } else {
  105. this.data._invalidUp += 1;
  106. }
  107. } else {
  108. this.data._invalidDown -= 1;
  109. }
  110. }
  111. if (direction === 'down') {
  112. if (this.data._invalidUp === 0) {
  113. var _change2 = _change;
  114. var _remove = curQueue[_change2];
  115. var _add = prevQueue.pop();
  116. if (_add) {
  117. curQueue[_change2] = _add;
  118. nextQueue.unshift(_remove);
  119. this.data._change = (_change2 - 1 + 3) % 3;
  120. } else {
  121. this.data._invalidDown += 1;
  122. }
  123. } else {
  124. this.data._invalidUp -= 1;
  125. }
  126. }
  127. var circular = true;
  128. if (nextQueue.length === 0 && current !== 0) {
  129. circular = false;
  130. }
  131. if (prevQueue.length === 0 && current !== 2) {
  132. circular = false;
  133. }
  134. this.setData({
  135. curQueue: curQueue,
  136. circular: circular
  137. });
  138. },
  139. playCurrent: function playCurrent(current) {
  140. return; // 注掉自动播放
  141. this.data._videoContexts.forEach(function (ctx, index) {
  142. index !== current ? ctx.pause() : ctx.play();
  143. });
  144. },
  145. onPlay: function onPlay(e) {
  146. this.trigger(e, 'play');
  147. },
  148. onPause: function onPause(e) {
  149. this.trigger(e, 'pause');
  150. },
  151. onEnded: function onEnded(e) {
  152. this.trigger(e, 'ended');
  153. },
  154. onError: function onError(e) {
  155. this.trigger(e, 'error');
  156. },
  157. onTimeUpdate: function onTimeUpdate(e) {
  158. this.trigger(e, 'timeupdate');
  159. },
  160. onWaiting: function onWaiting(e) {
  161. this.trigger(e, 'wait');
  162. },
  163. onProgress: function onProgress(e) {
  164. this.trigger(e, 'progress');
  165. },
  166. onLoadedMetaData: function onLoadedMetaData(e) {
  167. this.trigger(e, 'loadedmetadata');
  168. },
  169. onTimeUpdate: function onTimeUpdate(e) {
  170. },
  171. openComment: function openComment(e) {
  172. console.log(e)
  173. this.trigger(e, 'openComment')
  174. },
  175. // 点击头部
  176. headTap: function headTap(e) {
  177. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  178. // this.trigger(e, 'headTap')
  179. console.log('点击头像',e)
  180. wx.navigateTo({
  181. url: `../../pages/user/myworks/myworks?uid=${uid}`
  182. });
  183. },
  184. // 去朗读
  185. goToReading: function goToReading(e) {
  186. this.trigger(e, 'goToReading')
  187. },
  188. // 收藏课程
  189. collectTap: function collectClass(e) {
  190. console.log('收藏按钮', e);
  191. const data = {
  192. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  193. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  194. }
  195. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  196. console.log('视频index', index);
  197. let str = `curQueue[${index}].isFavorite`
  198. httpRequestApi.collectClass(data).success((res) => {
  199. this.setData({
  200. [str]: true
  201. })
  202. });
  203. },
  204. // 点赞
  205. likeTap: function likeTap(e) {
  206. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  207. console.log('isLike',isLike)
  208. if (isLike) {
  209. return;
  210. }
  211. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  212. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  213. console.log('视频index', index);
  214. let likeStr = `curQueue[${index}].isLike`;
  215. let likeNumStr = `curQueue[${index}].likes`;
  216. httpRequestApi.likeWorks(id).success((res) => {
  217. this.setData({
  218. [likeStr]: true,
  219. [likeNumStr]: this.data.curQueue[index].likes + 1
  220. })
  221. });
  222. },
  223. trigger: function trigger(e, type) {
  224. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  225. // var detail = e.detail;
  226. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  227. this.triggerEvent(type, {
  228. activeId: activeId
  229. }, {
  230. bubbles: false
  231. });
  232. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  233. activeId: activeId
  234. }), ext)); */
  235. }
  236. }
  237. });