index.js 11 KB

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