index.js 15 KB

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