index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. console.log('lastVideoId', e);
  167. httpRequestApi.playLogReport({
  168. userReadId: e.target.dataset.id,
  169. playStopTime: 1000
  170. }).success(res => {
  171. console.log('播放记录', res)
  172. })
  173. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  174. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  175. lastVideo.stop();
  176. this.setData({
  177. lastVideoId: e.target.id
  178. })
  179. }
  180. if (!this.data.lastVideoId) {
  181. this.setData({
  182. lastVideoId: e.target.id
  183. })
  184. }
  185. this.triggerEvent('onPlay')
  186. },
  187. onPause: function onPause(e) {
  188. this.trigger(e, 'pause');
  189. },
  190. onEnded: function onEnded(e) {
  191. console.log('播放结束', e)
  192. // this.trigger(e, 'ended');
  193. },
  194. onError: function onError(e) {
  195. console.log('视频出错', e)
  196. // this.trigger(e, 'error');
  197. },
  198. onTimeUpdate: function onTimeUpdate(e) {
  199. this.trigger(e, 'timeupdate');
  200. },
  201. onWaiting: function onWaiting(e) {
  202. this.trigger(e, 'wait');
  203. },
  204. onProgress: function onProgress(e) {
  205. this.trigger(e, 'progress');
  206. },
  207. onLoadedMetaData: function onLoadedMetaData(e) {
  208. this.trigger(e, 'loadedmetadata');
  209. },
  210. onTimeUpdate: function onTimeUpdate(e) {
  211. },
  212. openComment: function openComment(e) {
  213. console.log(e)
  214. this.trigger(e, 'openComment')
  215. },
  216. // 展示视频
  217. showVideo: function showVideo(e) {
  218. let index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  219. console.log('showVideo')
  220. const str = `videoList[${index}].videoShow`;
  221. if (this.data.lastIndex || this.data.lastIndex === 0) {
  222. const lastStr = `videoList[${this.data.lastIndex}].videoShow`;
  223. this.setData({
  224. lastIndex: index,
  225. [lastStr]: false,
  226. [str]: true
  227. })
  228. } else {
  229. this.setData({
  230. lastIndex: index,
  231. [str]: true
  232. })
  233. }
  234. },
  235. // 点击头部
  236. headTap: function headTap(e) {
  237. if (!wx.getStorageSync('user').wechatName) {
  238. wx.navigateTo({
  239. url: `../../pages/login/login`
  240. });
  241. return;
  242. }
  243. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  244. // this.trigger(e, 'headTap')
  245. console.log('点击头像', e)
  246. wx.navigateTo({
  247. url: `../../pages/myworks/myworks?uid=${uid}`,
  248. fail: (err) => {
  249. console.log('跳转错误', err)
  250. wx.navigateTo({
  251. url: `../../../pages/myworks/myworks?uid=${uid}`
  252. });
  253. }
  254. });
  255. },
  256. // 去朗读
  257. goToReading: function goToReading(e) {
  258. this.trigger(e, 'goToReading')
  259. },
  260. // 收藏课程
  261. collectTap: function collectClass(e) {
  262. if (!wx.getStorageSync('user').wechatName) {
  263. wx.navigateTo({
  264. url: `../../pages/login/login`
  265. });
  266. return;
  267. }
  268. console.log('收藏按钮', e);
  269. const data = {
  270. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  271. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  272. }
  273. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  274. console.log('视频index', index);
  275. if (this.data.curQueue.length <= 0) {
  276. let str = `videoList[${index}].isFavorite`
  277. httpRequestApi.collectClass(data).success((res) => {
  278. let isCollect = res.data.data.status === 'NORMAL' ? true : false;
  279. // this.setData({
  280. // [str]: isCollect
  281. // })
  282. this.triggerEvent('collectTap',{
  283. index,
  284. isCollect
  285. })
  286. });
  287. } else {
  288. let str = `curQueue[${index}].isFavorite`
  289. httpRequestApi.collectClass(data).success((res) => {
  290. this.setData({
  291. [str]: !this.data.curQueue[index].isFavorite
  292. })
  293. });
  294. }
  295. },
  296. // 点赞
  297. likeTap: function likeTap(e) {
  298. if (!wx.getStorageSync('user').wechatName) {
  299. wx.navigateTo({
  300. url: `../../pages/login/login`
  301. });
  302. return;
  303. }
  304. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  305. console.log('isLike', isLike)
  306. if (isLike) {
  307. return;
  308. }
  309. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  310. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  311. console.log('视频index', index);
  312. if (this.data.isSwiper) {
  313. let likeStr = `curQueue[${index}].isLike`;
  314. let likeNumStr = `curQueue[${index}].likes`;
  315. httpRequestApi.likeWorks(id).success((res) => {
  316. this.setData({
  317. [likeStr]: true,
  318. [likeNumStr]: this.data.curQueue[index].likes + 1
  319. })
  320. });
  321. } else {
  322. let likeStr = `videoList[${index}].isLike`;
  323. let likeNumStr = `videoList[${index}].likes`;
  324. httpRequestApi.likeWorks(id).success((res) => {
  325. // this.setData({
  326. // [likeStr]: true,
  327. // [likeNumStr]: this.data.videoList[index].likes + 1
  328. // })
  329. this.triggerEvent('likeTap',{
  330. index,
  331. })
  332. });
  333. }
  334. },
  335. delete: function (e) {
  336. console.log('删除', e)
  337. wx.showModal({
  338. title: '确认删除吗?',
  339. content: '作品将被永久删除,无法找回。',
  340. confirmText: '确认',
  341. cancelText: '取消',
  342. success: (res) => {
  343. if (res.confirm) {
  344. /* let data = {
  345. id: e.currentTarget.dataset.id,
  346. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  347. }
  348. httpRequestApi.putWork(data).success(res => {
  349. console.log('已隐藏', res)
  350. this.triggerEvent('delHideMyWork');
  351. }) */
  352. let data = {
  353. id: e.currentTarget.dataset.id,
  354. status: 'DEL'
  355. }
  356. httpRequestApi.putWork(data).success(res => {
  357. console.log('已删除', res)
  358. this.triggerEvent('delHideMyWork');
  359. })
  360. } else if (res.cancel) {
  361. console.log('用户点击取消')
  362. }
  363. }
  364. })
  365. },
  366. hide: function (e) {
  367. console.log('隐藏', e)
  368. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  369. console.log('当前状态', status)
  370. let data = {
  371. id: e.currentTarget.dataset.id,
  372. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  373. }
  374. httpRequestApi.putWork(data).success(res => {
  375. console.log('已隐藏', res)
  376. if (res.data.data.status === 'DISABLE') {
  377. wx.showToast({
  378. title: '该作品仅自己可见',
  379. icon: 'none',
  380. duration: 2000
  381. })
  382. }
  383. this.triggerEvent('delHideMyWork');
  384. })
  385. },
  386. openShare: function (e) {
  387. const obj = e.currentTarget.dataset
  388. console.log('分享', obj)
  389. console.log('分享', e)
  390. if (1) {
  391. this.shareDialog = this.selectComponent("#share-dialog");
  392. const data = {
  393. avatar: obj.avatar,
  394. author: obj.author,
  395. iconImg: obj.iconImg,
  396. title: obj.title,
  397. path: `pages/index/index`,
  398. scene: obj.id,
  399. type:obj.type,
  400. grade:obj.grade,
  401. productId: 1
  402. // tip: this.data.tip,
  403. }
  404. // console.log(data)
  405. this.setData({
  406. noScroll: 'noScroll'
  407. })
  408. this.shareDialog.share(data);
  409. }
  410. this.triggerEvent('openShare', e)
  411. },
  412. trigger: function trigger(e, type) {
  413. if (!wx.getStorageSync('user').wechatName) {
  414. wx.navigateTo({
  415. url: `../../pages/login/login`
  416. });
  417. return;
  418. }
  419. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  420. // var detail = e.detail;
  421. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  422. this.triggerEvent(type, {
  423. activeId: activeId
  424. }, {
  425. bubbles: false
  426. });
  427. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  428. activeId: activeId
  429. }), ext)); */
  430. }
  431. }
  432. });