index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. noMoreWork: {
  22. type: Boolean,
  23. value: true
  24. },
  25. duration: {
  26. type: Number,
  27. value: 500
  28. },
  29. easingFunction: {
  30. type: String,
  31. value: 'easeInOutCubic' // easeInCubic 缓入 easeOutCubic 缓出 easeInOutCubic 缓入缓出 default linear
  32. },
  33. loop: {
  34. type: Boolean,
  35. value: true
  36. },
  37. videoList: {
  38. type: Array,
  39. value: [],
  40. observer: function observer() {
  41. if (!this.data.isSwiper) return;
  42. var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  43. this._videoListChanged(newVal);
  44. }
  45. },
  46. nextMargin: {
  47. type: String,
  48. value: '400rpx'
  49. }
  50. },
  51. data: {
  52. nextQueue: [],
  53. prevQueue: [],
  54. curQueue: [],
  55. circular: false,
  56. // nextMargin: '400rpx',
  57. _last: 0,
  58. // _last: 1,
  59. _change: -1,
  60. _invalidUp: 0,
  61. _invalidDown: 0,
  62. _videoContexts: [],
  63. inputValue: '',
  64. showControl: 0
  65. },
  66. lifetimes: {
  67. attached: function attached() {
  68. this.data._videoContexts = [wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)];
  69. }
  70. },
  71. methods: {
  72. _videoListChanged: function _videoListChanged(newVal) {
  73. var _this = this;
  74. var data = this.data;
  75. console.log('newVal', newVal)
  76. newVal.forEach(function (item) {
  77. data.nextQueue.push(item);
  78. });
  79. if (data.curQueue.length === 0) {
  80. this.setData({
  81. curQueue: data.nextQueue.splice(0, 3)
  82. }, function () {
  83. _this.playCurrent(1);
  84. // _this.playCurrent(0);
  85. });
  86. }
  87. },
  88. animationfinish: function animationfinish(e) {
  89. var _data = this.data,
  90. _last = _data._last,
  91. _change = _data._change,
  92. curQueue = _data.curQueue,
  93. prevQueue = _data.prevQueue,
  94. nextQueue = _data.nextQueue;
  95. var current = e.detail.current;
  96. var diff = current - _last;
  97. if (diff === 0) return;
  98. this.data._last = current;
  99. this.playCurrent(current);
  100. this.triggerEvent('change', {
  101. activeId: curQueue[current].id,
  102. // index: current,
  103. // _last,
  104. // nextQueue,
  105. });
  106. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  107. if (direction === 'up') {
  108. if (this.data._invalidDown === 0) {
  109. var change = (_change + 1) % 3;
  110. var add = nextQueue.shift();
  111. var remove = curQueue[change];
  112. if (add) {
  113. prevQueue.push(remove);
  114. curQueue[change] = add;
  115. this.data._change = change;
  116. } else {
  117. this.data._invalidUp += 1;
  118. }
  119. } else {
  120. this.data._invalidDown -= 1;
  121. }
  122. }
  123. if (direction === 'down') {
  124. if (this.data._invalidUp === 0) {
  125. var _change2 = _change;
  126. var _remove = curQueue[_change2];
  127. var _add = prevQueue.pop();
  128. if (_add) {
  129. curQueue[_change2] = _add;
  130. nextQueue.unshift(_remove);
  131. this.data._change = (_change2 - 1 + 3) % 3;
  132. } else {
  133. this.data._invalidDown += 1;
  134. }
  135. } else {
  136. this.data._invalidUp -= 1;
  137. }
  138. }
  139. var circular = true;
  140. if (nextQueue.length === 0 && current !== 0) {
  141. circular = false;
  142. }
  143. if (prevQueue.length === 0 && current !== 2) {
  144. circular = false;
  145. }
  146. this.setData({
  147. curQueue: curQueue,
  148. circular: circular
  149. });
  150. },
  151. playCurrent: function playCurrent(current) {
  152. console.log('playCurrent', current)
  153. // return; // 注掉自动播放
  154. this.data._videoContexts.forEach(function (ctx, index) {
  155. index !== current ? ctx.pause() : ctx.play();
  156. });
  157. },
  158. onPlay: function onPlay(e) {
  159. console.log('播放记录', e)
  160. httpRequestApi.playLogReport({
  161. userReadId: e.currentTarget.dataset.id,
  162. playStopTime: 1000
  163. }).success(res => {
  164. console.log('播放记录', res)
  165. })
  166. // this.trigger(e, 'play');
  167. },
  168. onPlayList: function onPlayList(e) {
  169. console.log('lastVideoId', this.data.lastVideoId);
  170. console.log('lastVideoId', e);
  171. httpRequestApi.playLogReport({
  172. userReadId: e.target.dataset.id,
  173. playStopTime: 1000
  174. }).success(res => {
  175. console.log('播放记录', res)
  176. })
  177. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  178. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  179. lastVideo.stop();
  180. this.setData({
  181. lastVideoId: e.target.id
  182. })
  183. }
  184. if (!this.data.lastVideoId) {
  185. this.setData({
  186. lastVideoId: e.target.id
  187. })
  188. }
  189. this.triggerEvent('onPlay')
  190. },
  191. onPause: function onPause(e) {
  192. this.trigger(e, 'pause');
  193. },
  194. onEnded: function onEnded(e) {
  195. console.log('播放结束', e)
  196. // this.trigger(e, 'ended');
  197. },
  198. onError: function onError(e) {
  199. console.log('视频出错', e)
  200. // this.trigger(e, 'error');
  201. },
  202. onTimeUpdate: function onTimeUpdate(e) {
  203. this.trigger(e, 'timeupdate');
  204. },
  205. onWaiting: function onWaiting(e) {
  206. this.trigger(e, 'wait');
  207. },
  208. onProgress: function onProgress(e) {
  209. this.trigger(e, 'progress');
  210. },
  211. onLoadedMetaData: function onLoadedMetaData(e) {
  212. this.trigger(e, 'loadedmetadata');
  213. },
  214. onTimeUpdate: function onTimeUpdate(e) {
  215. },
  216. openComment: function openComment(e) {
  217. console.log(e)
  218. this.trigger(e, 'openComment')
  219. },
  220. // 展示视频
  221. showVideo: function showVideo(e) {
  222. let index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  223. console.log('showVideo')
  224. const str = `videoList[${index}].videoShow`;
  225. if (this.data.lastIndex || this.data.lastIndex === 0) {
  226. const lastStr = `videoList[${this.data.lastIndex}].videoShow`;
  227. this.setData({
  228. lastIndex: index,
  229. [lastStr]: false,
  230. [str]: true
  231. })
  232. } else {
  233. this.setData({
  234. lastIndex: index,
  235. [str]: true
  236. })
  237. }
  238. },
  239. // 点击头部
  240. headTap: function headTap(e) {
  241. if (!wx.getStorageSync('user').wechatName) {
  242. wx.navigateTo({
  243. url: `../../pages/login/login`
  244. });
  245. return;
  246. }
  247. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  248. // this.trigger(e, 'headTap')
  249. console.log('点击头像', e)
  250. wx.navigateTo({
  251. url: `../../pages/myworks/myworks?uid=${uid}`,
  252. fail: (err) => {
  253. console.log('跳转错误', err)
  254. wx.navigateTo({
  255. url: `../../../pages/myworks/myworks?uid=${uid}`
  256. });
  257. }
  258. });
  259. },
  260. // 去朗读
  261. goToReading: function goToReading(e) {
  262. this.trigger(e, 'goToReading')
  263. },
  264. // 收藏课程
  265. collectTap: function collectClass(e) {
  266. if (!wx.getStorageSync('user').wechatName) {
  267. wx.navigateTo({
  268. url: `../../pages/login/login`
  269. });
  270. return;
  271. }
  272. console.log('收藏按钮', e);
  273. const data = {
  274. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  275. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  276. }
  277. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  278. console.log('视频index', index);
  279. if (this.data.curQueue.length <= 0) {
  280. let str = `videoList[${index}].isFavorite`
  281. httpRequestApi.collectClass(data).success((res) => {
  282. let isCollect = res.data.data.status === 'NORMAL' ? true : false;
  283. // this.setData({
  284. // [str]: isCollect
  285. // })
  286. this.triggerEvent('collectTap',{
  287. index,
  288. isCollect
  289. })
  290. });
  291. } else {
  292. let str = `curQueue[${index}].isFavorite`
  293. httpRequestApi.collectClass(data).success((res) => {
  294. this.setData({
  295. [str]: !this.data.curQueue[index].isFavorite
  296. })
  297. });
  298. }
  299. },
  300. // 点赞
  301. likeTap: function likeTap(e) {
  302. if (!wx.getStorageSync('user').wechatName) {
  303. wx.navigateTo({
  304. url: `../../pages/login/login`
  305. });
  306. return;
  307. }
  308. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  309. console.log('isLike', isLike)
  310. if (isLike) {
  311. return;
  312. }
  313. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  314. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  315. console.log('视频index', index);
  316. if (this.data.isSwiper) {
  317. let likeStr = `curQueue[${index}].isLike`;
  318. let likeNumStr = `curQueue[${index}].likes`;
  319. httpRequestApi.likeWorks(id).success((res) => {
  320. this.setData({
  321. [likeStr]: true,
  322. [likeNumStr]: this.data.curQueue[index].likes + 1
  323. })
  324. });
  325. } else {
  326. let likeStr = `videoList[${index}].isLike`;
  327. let likeNumStr = `videoList[${index}].likes`;
  328. httpRequestApi.likeWorks(id).success((res) => {
  329. // this.setData({
  330. // [likeStr]: true,
  331. // [likeNumStr]: this.data.videoList[index].likes + 1
  332. // })
  333. this.triggerEvent('likeTap',{
  334. index,
  335. })
  336. });
  337. }
  338. },
  339. delete: function (e) {
  340. console.log('删除', e)
  341. wx.showModal({
  342. title: '确认删除吗?',
  343. content: '作品将被永久删除,无法找回。',
  344. confirmText: '确认',
  345. cancelText: '取消',
  346. success: (res) => {
  347. if (res.confirm) {
  348. /* let data = {
  349. id: e.currentTarget.dataset.id,
  350. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  351. }
  352. httpRequestApi.putWork(data).success(res => {
  353. console.log('已隐藏', res)
  354. this.triggerEvent('delHideMyWork');
  355. }) */
  356. let data = {
  357. id: e.currentTarget.dataset.id,
  358. status: 'DEL'
  359. }
  360. httpRequestApi.putWork(data).success(res => {
  361. console.log('已删除', res)
  362. this.triggerEvent('delHideMyWork');
  363. })
  364. } else if (res.cancel) {
  365. console.log('用户点击取消')
  366. }
  367. }
  368. })
  369. },
  370. hide: function (e) {
  371. console.log('隐藏', e)
  372. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  373. console.log('当前状态', status)
  374. let data = {
  375. id: e.currentTarget.dataset.id,
  376. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  377. }
  378. httpRequestApi.putWork(data).success(res => {
  379. console.log('已隐藏', res)
  380. if (res.data.data.status === 'DISABLE') {
  381. wx.showToast({
  382. title: '该作品仅自己可见',
  383. icon: 'none',
  384. duration: 2000
  385. })
  386. }
  387. this.triggerEvent('delHideMyWork');
  388. })
  389. },
  390. openShare: function (e) {
  391. const obj = e.currentTarget.dataset
  392. console.log('分享的dataset', obj)
  393. console.log('分享', e)
  394. if (1) {
  395. this.shareDialog = this.selectComponent("#share-dialog");
  396. const data = {
  397. avatar: obj.avatar,
  398. author: obj.author,
  399. iconImg: obj.iconImg,
  400. title: obj.title,
  401. path: `pages/index/index`,
  402. scene: obj.id,
  403. type:obj.type,
  404. grade:obj.grade,
  405. productId: 1,
  406. shareImg:obj.shareImg
  407. // tip: this.data.tip,
  408. }
  409. // console.log(data)
  410. this.setData({
  411. noScroll: 'noScroll'
  412. })
  413. this.shareDialog.share(data);
  414. }
  415. this.triggerEvent('openShare', e)
  416. },
  417. trigger: function trigger(e, type) {
  418. if (!wx.getStorageSync('user').wechatName) {
  419. wx.navigateTo({
  420. url: `../../pages/login/login`
  421. });
  422. return;
  423. }
  424. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  425. // var detail = e.detail;
  426. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  427. this.triggerEvent(type, {
  428. activeId: activeId
  429. }, {
  430. bubbles: false
  431. });
  432. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  433. activeId: activeId
  434. }), ext)); */
  435. }
  436. }
  437. });