index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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: 'easeInOutCubic' // easeInCubic 缓入 easeOutCubic 缓出 easeInOutCubic 缓入缓出 default linear
  28. },
  29. loop: {
  30. type: Boolean,
  31. value: true
  32. },
  33. videoList: {
  34. type: Array,
  35. value: [],
  36. observer: function observer() {
  37. if (!this.data.isSwiper) return;
  38. var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  39. this._videoListChanged(newVal);
  40. }
  41. },
  42. nextMargin: {
  43. type: String,
  44. value: '400rpx'
  45. }
  46. },
  47. data: {
  48. nextQueue: [],
  49. prevQueue: [],
  50. curQueue: [],
  51. circular: false,
  52. // nextMargin: '400rpx',
  53. _last: 0,
  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. // _this.playCurrent(0);
  81. });
  82. }
  83. },
  84. animationfinish: function animationfinish(e) {
  85. var _data = this.data,
  86. _last = _data._last,
  87. _change = _data._change,
  88. curQueue = _data.curQueue,
  89. prevQueue = _data.prevQueue,
  90. nextQueue = _data.nextQueue;
  91. var current = e.detail.current;
  92. var diff = current - _last;
  93. if (diff === 0) return;
  94. this.data._last = current;
  95. this.playCurrent(current);
  96. this.triggerEvent('change', {
  97. activeId: curQueue[current].id,
  98. // index: current,
  99. // _last,
  100. // nextQueue,
  101. });
  102. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  103. if (direction === 'up') {
  104. if (this.data._invalidDown === 0) {
  105. var change = (_change + 1) % 3;
  106. var add = nextQueue.shift();
  107. var remove = curQueue[change];
  108. if (add) {
  109. prevQueue.push(remove);
  110. curQueue[change] = add;
  111. this.data._change = change;
  112. } else {
  113. this.data._invalidUp += 1;
  114. }
  115. } else {
  116. this.data._invalidDown -= 1;
  117. }
  118. }
  119. if (direction === 'down') {
  120. if (this.data._invalidUp === 0) {
  121. var _change2 = _change;
  122. var _remove = curQueue[_change2];
  123. var _add = prevQueue.pop();
  124. if (_add) {
  125. curQueue[_change2] = _add;
  126. nextQueue.unshift(_remove);
  127. this.data._change = (_change2 - 1 + 3) % 3;
  128. } else {
  129. this.data._invalidDown += 1;
  130. }
  131. } else {
  132. this.data._invalidUp -= 1;
  133. }
  134. }
  135. var circular = true;
  136. if (nextQueue.length === 0 && current !== 0) {
  137. circular = false;
  138. }
  139. if (prevQueue.length === 0 && current !== 2) {
  140. circular = false;
  141. }
  142. this.setData({
  143. curQueue: curQueue,
  144. circular: circular
  145. });
  146. },
  147. playCurrent: function playCurrent(current) {
  148. console.log('playCurrent', current)
  149. // return; // 注掉自动播放
  150. this.data._videoContexts.forEach(function (ctx, index) {
  151. index !== current ? ctx.pause() : ctx.play();
  152. });
  153. },
  154. onPlay: function onPlay(e) {
  155. console.log('播放记录', e)
  156. httpRequestApi.playLogReport({
  157. userReadId: e.currentTarget.dataset.id,
  158. playStopTime: 1000
  159. }).success(res => {
  160. console.log('播放记录', res)
  161. })
  162. // this.trigger(e, 'play');
  163. },
  164. onPlayList: function onPlayList(e) {
  165. console.log('lastVideoId', this.data.lastVideoId);
  166. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  167. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  168. lastVideo.pause();
  169. this.setData({
  170. lastVideoId: e.target.id
  171. })
  172. }
  173. if (!this.data.lastVideoId) {
  174. this.setData({
  175. lastVideoId: e.target.id
  176. })
  177. }
  178. this.triggerEvent('onPlay')
  179. },
  180. onPause: function onPause(e) {
  181. this.trigger(e, 'pause');
  182. },
  183. onEnded: function onEnded(e) {
  184. console.log('播放结束', e)
  185. // this.trigger(e, 'ended');
  186. },
  187. onError: function onError(e) {
  188. this.trigger(e, 'error');
  189. },
  190. onTimeUpdate: function onTimeUpdate(e) {
  191. this.trigger(e, 'timeupdate');
  192. },
  193. onWaiting: function onWaiting(e) {
  194. this.trigger(e, 'wait');
  195. },
  196. onProgress: function onProgress(e) {
  197. this.trigger(e, 'progress');
  198. },
  199. onLoadedMetaData: function onLoadedMetaData(e) {
  200. this.trigger(e, 'loadedmetadata');
  201. },
  202. onTimeUpdate: function onTimeUpdate(e) {
  203. },
  204. openComment: function openComment(e) {
  205. console.log(e)
  206. this.trigger(e, 'openComment')
  207. },
  208. // 点击头部
  209. headTap: function headTap(e) {
  210. if (!wx.getStorageSync('user').wechatName) {
  211. wx.navigateTo({
  212. url: `../../pages/login/login`
  213. });
  214. return;
  215. }
  216. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  217. // this.trigger(e, 'headTap')
  218. console.log('点击头像', e)
  219. wx.navigateTo({
  220. url: `../../pages/myworks/myworks?uid=${uid}`,
  221. fail: (err) => {
  222. console.log('跳转错误', err)
  223. wx.navigateTo({
  224. url: `../../../pages/myworks/myworks?uid=${uid}`
  225. });
  226. }
  227. });
  228. },
  229. // 去朗读
  230. goToReading: function goToReading(e) {
  231. this.trigger(e, 'goToReading')
  232. },
  233. // 收藏课程
  234. collectTap: function collectClass(e) {
  235. if (!wx.getStorageSync('user').wechatName) {
  236. wx.navigateTo({
  237. url: `../../pages/login/login`
  238. });
  239. return;
  240. }
  241. console.log('收藏按钮', e);
  242. const data = {
  243. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  244. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  245. }
  246. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  247. console.log('视频index', index);
  248. if (this.data.curQueue.length <= 0) {
  249. let str = `videoList[${index}].isFavorite`
  250. httpRequestApi.collectClass(data).success((res) => {
  251. this.setData({
  252. [str]: !this.data.videoList[index].isFavorite
  253. })
  254. });
  255. } else {
  256. let str = `curQueue[${index}].isFavorite`
  257. httpRequestApi.collectClass(data).success((res) => {
  258. this.setData({
  259. [str]: !this.data.curQueue[index].isFavorite
  260. })
  261. });
  262. }
  263. },
  264. // 点赞
  265. likeTap: function likeTap(e) {
  266. if (!wx.getStorageSync('user').wechatName) {
  267. wx.navigateTo({
  268. url: `../../pages/login/login`
  269. });
  270. return;
  271. }
  272. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  273. console.log('isLike', isLike)
  274. if (isLike) {
  275. return;
  276. }
  277. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  278. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  279. console.log('视频index', index);
  280. if (this.data.isSwiper) {
  281. let likeStr = `curQueue[${index}].isLike`;
  282. let likeNumStr = `curQueue[${index}].likes`;
  283. httpRequestApi.likeWorks(id).success((res) => {
  284. this.setData({
  285. [likeStr]: true,
  286. [likeNumStr]: this.data.curQueue[index].likes + 1
  287. })
  288. });
  289. } else {
  290. let likeStr = `videoList[${index}].isLike`;
  291. let likeNumStr = `videoList[${index}].likes`;
  292. httpRequestApi.likeWorks(id).success((res) => {
  293. this.setData({
  294. [likeStr]: true,
  295. [likeNumStr]: this.data.videoList[index].likes + 1
  296. })
  297. });
  298. }
  299. },
  300. delete: function (e) {
  301. console.log('删除', e)
  302. let data = {
  303. id: e.currentTarget.dataset.id,
  304. status: 'DEL'
  305. }
  306. httpRequestApi.putWork(data).success(res => {
  307. console.log('已删除', res)
  308. this.triggerEvent('delHideMyWork');
  309. })
  310. },
  311. hide: function (e) {
  312. console.log('隐藏', e)
  313. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  314. console.log('当前状态', status)
  315. let data = {
  316. id: e.currentTarget.dataset.id,
  317. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  318. }
  319. httpRequestApi.putWork(data).success(res => {
  320. console.log('已隐藏', res)
  321. this.triggerEvent('delHideMyWork');
  322. })
  323. },
  324. openShare: function (e) {
  325. const obj = e.currentTarget.dataset
  326. console.log('分享', obj)
  327. console.log('分享', e)
  328. if (1) {
  329. this.shareDialog = this.selectComponent("#share-dialog");
  330. const data = {
  331. avatar: obj.avatar,
  332. author: obj.author,
  333. iconImg: obj.iconImg,
  334. title: obj.title,
  335. path: `pages/index/index`,
  336. scene: obj.id,
  337. productId: 1
  338. // tip: this.data.tip,
  339. }
  340. // console.log(data)
  341. this.setData({
  342. noScroll: 'noScroll'
  343. })
  344. this.shareDialog.share(data);
  345. }
  346. this.triggerEvent('openShare',e)
  347. },
  348. trigger: function trigger(e, type) {
  349. if (!wx.getStorageSync('user').wechatName) {
  350. wx.navigateTo({
  351. url: `../../pages/login/login`
  352. });
  353. return;
  354. }
  355. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  356. // var detail = e.detail;
  357. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  358. this.triggerEvent(type, {
  359. activeId: activeId
  360. }, {
  361. bubbles: false
  362. });
  363. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  364. activeId: activeId
  365. }), ext)); */
  366. }
  367. }
  368. });