index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. index:current,
  94. _last,
  95. nextQueue,
  96. });
  97. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  98. if (direction === 'up') {
  99. if (this.data._invalidDown === 0) {
  100. var change = (_change + 1) % 3;
  101. var add = nextQueue.shift();
  102. var remove = curQueue[change];
  103. if (add) {
  104. prevQueue.push(remove);
  105. curQueue[change] = add;
  106. this.data._change = change;
  107. } else {
  108. this.data._invalidUp += 1;
  109. }
  110. } else {
  111. this.data._invalidDown -= 1;
  112. }
  113. }
  114. if (direction === 'down') {
  115. if (this.data._invalidUp === 0) {
  116. var _change2 = _change;
  117. var _remove = curQueue[_change2];
  118. var _add = prevQueue.pop();
  119. if (_add) {
  120. curQueue[_change2] = _add;
  121. nextQueue.unshift(_remove);
  122. this.data._change = (_change2 - 1 + 3) % 3;
  123. } else {
  124. this.data._invalidDown += 1;
  125. }
  126. } else {
  127. this.data._invalidUp -= 1;
  128. }
  129. }
  130. var circular = true;
  131. if (nextQueue.length === 0 && current !== 0) {
  132. circular = false;
  133. }
  134. if (prevQueue.length === 0 && current !== 2) {
  135. circular = false;
  136. }
  137. this.setData({
  138. curQueue: curQueue,
  139. circular: circular
  140. });
  141. },
  142. playCurrent: function playCurrent(current) {
  143. return; // 注掉自动播放
  144. this.data._videoContexts.forEach(function (ctx, index) {
  145. index !== current ? ctx.pause() : ctx.play();
  146. });
  147. },
  148. onPlay: function onPlay(e) {
  149. this.trigger(e, 'play');
  150. },
  151. onPause: function onPause(e) {
  152. this.trigger(e, 'pause');
  153. },
  154. onEnded: function onEnded(e) {
  155. this.trigger(e, 'ended');
  156. },
  157. onError: function onError(e) {
  158. this.trigger(e, 'error');
  159. },
  160. onTimeUpdate: function onTimeUpdate(e) {
  161. this.trigger(e, 'timeupdate');
  162. },
  163. onWaiting: function onWaiting(e) {
  164. this.trigger(e, 'wait');
  165. },
  166. onProgress: function onProgress(e) {
  167. this.trigger(e, 'progress');
  168. },
  169. onLoadedMetaData: function onLoadedMetaData(e) {
  170. this.trigger(e, 'loadedmetadata');
  171. },
  172. onTimeUpdate: function onTimeUpdate(e) {
  173. },
  174. openComment: function openComment(e) {
  175. console.log(e)
  176. this.trigger(e, 'openComment')
  177. },
  178. // 点击头部
  179. headTap: function headTap(e) {
  180. if (!wx.getStorageSync('user').wechatName) {
  181. wx.navigateTo({
  182. url: `../../pages/login/login`
  183. });
  184. return;
  185. }
  186. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  187. // this.trigger(e, 'headTap')
  188. console.log('点击头像', e)
  189. wx.navigateTo({
  190. url: `../../pages/user/myworks/myworks?uid=${uid}`
  191. });
  192. },
  193. // 去朗读
  194. goToReading: function goToReading(e) {
  195. this.trigger(e, 'goToReading')
  196. },
  197. // 收藏课程
  198. collectTap: function collectClass(e) {
  199. if (!wx.getStorageSync('user').wechatName) {
  200. wx.navigateTo({
  201. url: `../../pages/login/login`
  202. });
  203. return;
  204. }
  205. console.log('收藏按钮', e);
  206. const data = {
  207. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  208. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  209. }
  210. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  211. console.log('视频index', index);
  212. let str = `curQueue[${index}].isFavorite`
  213. httpRequestApi.collectClass(data).success((res) => {
  214. this.setData({
  215. [str]: true
  216. })
  217. });
  218. },
  219. // 点赞
  220. likeTap: function likeTap(e) {
  221. if (!wx.getStorageSync('user').wechatName) {
  222. wx.navigateTo({
  223. url: `../../pages/login/login`
  224. });
  225. return;
  226. }
  227. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  228. console.log('isLike', isLike)
  229. if (isLike) {
  230. return;
  231. }
  232. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  233. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  234. console.log('视频index', index);
  235. let likeStr = `curQueue[${index}].isLike`;
  236. let likeNumStr = `curQueue[${index}].likes`;
  237. httpRequestApi.likeWorks(id).success((res) => {
  238. this.setData({
  239. [likeStr]: true,
  240. [likeNumStr]: this.data.curQueue[index].likes + 1
  241. })
  242. });
  243. },
  244. trigger: function trigger(e, type) {
  245. if (!wx.getStorageSync('user').wechatName) {
  246. wx.navigateTo({
  247. url: `../../pages/login/login`
  248. });
  249. return;
  250. }
  251. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  252. // var detail = e.detail;
  253. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  254. this.triggerEvent(type, {
  255. activeId: activeId
  256. }, {
  257. bubbles: false
  258. });
  259. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  260. activeId: activeId
  261. }), ext)); */
  262. }
  263. }
  264. });