index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (!wx.getStorageSync('user').wechatName) {
  178. wx.navigateTo({
  179. url: `../../pages/login/login`
  180. });
  181. return;
  182. }
  183. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  184. // this.trigger(e, 'headTap')
  185. console.log('点击头像', e)
  186. wx.navigateTo({
  187. url: `../../pages/user/myworks/myworks?uid=${uid}`
  188. });
  189. },
  190. // 去朗读
  191. goToReading: function goToReading(e) {
  192. this.trigger(e, 'goToReading')
  193. },
  194. // 收藏课程
  195. collectTap: function collectClass(e) {
  196. if (!wx.getStorageSync('user').wechatName) {
  197. wx.navigateTo({
  198. url: `../../pages/login/login`
  199. });
  200. return;
  201. }
  202. console.log('收藏按钮', e);
  203. const data = {
  204. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  205. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  206. }
  207. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  208. console.log('视频index', index);
  209. let str = `curQueue[${index}].isFavorite`
  210. httpRequestApi.collectClass(data).success((res) => {
  211. this.setData({
  212. [str]: true
  213. })
  214. });
  215. },
  216. // 点赞
  217. likeTap: function likeTap(e) {
  218. if (!wx.getStorageSync('user').wechatName) {
  219. wx.navigateTo({
  220. url: `../../pages/login/login`
  221. });
  222. return;
  223. }
  224. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  225. console.log('isLike', isLike)
  226. if (isLike) {
  227. return;
  228. }
  229. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  230. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  231. console.log('视频index', index);
  232. let likeStr = `curQueue[${index}].isLike`;
  233. let likeNumStr = `curQueue[${index}].likes`;
  234. httpRequestApi.likeWorks(id).success((res) => {
  235. this.setData({
  236. [likeStr]: true,
  237. [likeNumStr]: this.data.curQueue[index].likes + 1
  238. })
  239. });
  240. },
  241. trigger: function trigger(e, type) {
  242. if (!wx.getStorageSync('user').wechatName) {
  243. wx.navigateTo({
  244. url: `../../pages/login/login`
  245. });
  246. return;
  247. }
  248. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  249. // var detail = e.detail;
  250. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  251. this.triggerEvent(type, {
  252. activeId: activeId
  253. }, {
  254. bubbles: false
  255. });
  256. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  257. activeId: activeId
  258. }), ext)); */
  259. }
  260. }
  261. });