index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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: false
  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. console.log('数据更新111111', arguments)
  42. let newValue = arguments[0];
  43. let oldValue = arguments[1];
  44. if (newValue && oldValue && (this.data.activeIndex || this.data.activeIndex === 0)) {
  45. let newVideoShow = newValue[this.data.activeIndex].videoShow;
  46. let oldVideoShow = oldValue[this.data.activeIndex].videoShow;
  47. if (newVideoShow !== oldVideoShow && oldVideoShow) {
  48. console.log('把值设置为true')
  49. newValue[this.data.activeIndex].videoShow = true;
  50. this.setData({
  51. })
  52. }
  53. }
  54. let newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  55. this.updateSonList(newVal);
  56. }
  57. },
  58. nextMargin: {
  59. type: String,
  60. value: '400rpx'
  61. }
  62. },
  63. data: {
  64. sonVideoList: [], // 子组件里的视频组件
  65. activeType: '',
  66. nextQueue: [],
  67. prevQueue: [],
  68. curQueue: [],
  69. circular: false,
  70. // nextMargin: '400rpx',
  71. _last: 0,
  72. // _last: 1,
  73. _change: -1,
  74. _invalidUp: 0,
  75. _invalidDown: 0,
  76. _videoContexts: [],
  77. inputValue: '',
  78. showControl: 0,
  79. flowerNum: 0
  80. },
  81. lifetimes: {
  82. attached: function attached() {
  83. this.data._videoContexts = [wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)];
  84. }
  85. },
  86. /* observers: {
  87. '**': function() {
  88. // 每次 setData 都触发
  89. console.log('数据更新333333',this.data)
  90. },
  91. }, */
  92. methods: {
  93. _videoListChanged: function _videoListChanged(newVal) {
  94. var _this = this;
  95. var data = this.data;
  96. console.log('newVal', newVal)
  97. newVal.forEach(function (item) {
  98. data.nextQueue.push(item);
  99. });
  100. if (data.curQueue.length === 0) {
  101. this.setData({
  102. curQueue: data.nextQueue.splice(0, 3)
  103. }, function () {
  104. _this.playCurrent(1);
  105. // _this.playCurrent(0);
  106. });
  107. }
  108. },
  109. updateSonList: function updateSonList(newVal) {
  110. this.setData({
  111. sonVideoList: newVal,
  112. activeIndex: ''
  113. })
  114. },
  115. animationfinish: function animationfinish(e) {
  116. var _data = this.data,
  117. _last = _data._last,
  118. _change = _data._change,
  119. curQueue = _data.curQueue,
  120. prevQueue = _data.prevQueue,
  121. nextQueue = _data.nextQueue;
  122. var current = e.detail.current;
  123. var diff = current - _last;
  124. if (diff === 0) return;
  125. this.data._last = current;
  126. this.playCurrent(current);
  127. this.triggerEvent('change', {
  128. activeId: curQueue[current].id,
  129. // index: current,
  130. // _last,
  131. // nextQueue,
  132. });
  133. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  134. if (direction === 'up') {
  135. if (this.data._invalidDown === 0) {
  136. var change = (_change + 1) % 3;
  137. var add = nextQueue.shift();
  138. var remove = curQueue[change];
  139. if (add) {
  140. prevQueue.push(remove);
  141. curQueue[change] = add;
  142. this.data._change = change;
  143. } else {
  144. this.data._invalidUp += 1;
  145. }
  146. } else {
  147. this.data._invalidDown -= 1;
  148. }
  149. }
  150. if (direction === 'down') {
  151. if (this.data._invalidUp === 0) {
  152. var _change2 = _change;
  153. var _remove = curQueue[_change2];
  154. var _add = prevQueue.pop();
  155. if (_add) {
  156. curQueue[_change2] = _add;
  157. nextQueue.unshift(_remove);
  158. this.data._change = (_change2 - 1 + 3) % 3;
  159. } else {
  160. this.data._invalidDown += 1;
  161. }
  162. } else {
  163. this.data._invalidUp -= 1;
  164. }
  165. }
  166. var circular = true;
  167. if (nextQueue.length === 0 && current !== 0) {
  168. circular = false;
  169. }
  170. if (prevQueue.length === 0 && current !== 2) {
  171. circular = false;
  172. }
  173. this.setData({
  174. curQueue: curQueue,
  175. circular: circular
  176. });
  177. },
  178. playCurrent: function playCurrent(current) {
  179. console.log('playCurrent', current)
  180. // return; // 注掉自动播放
  181. this.data._videoContexts.forEach(function (ctx, index) {
  182. index !== current ? ctx.pause() : ctx.play();
  183. });
  184. },
  185. onPlay: function onPlay(e) {
  186. console.log('播放记录', e)
  187. httpRequestApi.playLogReport({
  188. userReadId: e.currentTarget.dataset.id,
  189. playStopTime: 1000
  190. }).success(res => {
  191. console.log('播放记录', res)
  192. })
  193. // this.trigger(e, 'play');
  194. },
  195. onPlayList: function onPlayList(e) {
  196. console.log('lastVideoId', this.data.lastVideoId);
  197. console.log('lastVideoId', e);
  198. httpRequestApi.playLogReport({
  199. userReadId: e.target.dataset.id,
  200. playStopTime: 1000
  201. }).success(res => {
  202. console.log('播放记录', res)
  203. })
  204. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  205. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  206. lastVideo.stop();
  207. this.setData({
  208. lastVideoId: e.target.id
  209. })
  210. }
  211. if (!this.data.lastVideoId) {
  212. this.setData({
  213. lastVideoId: e.target.id
  214. })
  215. }
  216. this.triggerEvent('onPlay')
  217. },
  218. onPause: function onPause(e) {
  219. this.trigger(e, 'pause');
  220. },
  221. onEnded: function onEnded(e) {
  222. console.log('播放结束', e)
  223. // this.trigger(e, 'ended');
  224. },
  225. onError: function onError(e) {
  226. console.log('视频出错', e)
  227. // this.trigger(e, 'error');
  228. },
  229. onTimeUpdate: function onTimeUpdate(e) {
  230. this.trigger(e, 'timeupdate');
  231. },
  232. onWaiting: function onWaiting(e) {
  233. this.trigger(e, 'wait');
  234. },
  235. onProgress: function onProgress(e) {
  236. this.trigger(e, 'progress');
  237. },
  238. onLoadedMetaData: function onLoadedMetaData(e) {
  239. this.trigger(e, 'loadedmetadata');
  240. },
  241. onTimeUpdate: function onTimeUpdate(e) {
  242. },
  243. openComment: function openComment(e) {
  244. console.log(e)
  245. let index = e.currentTarget.dataset.index;
  246. this.setData({
  247. activeIndex:index
  248. })
  249. this.trigger(e, 'openComment')
  250. },
  251. // 展示视频
  252. showVideo: function showVideo(e) {
  253. let index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  254. console.log('showVideo')
  255. const str = `sonVideoList[${index}].videoShow`;
  256. if (this.data.lastIndex || this.data.lastIndex === 0) {
  257. const lastStr = `sonVideoList[${this.data.lastIndex}].videoShow`;
  258. this.setData({
  259. lastIndex: index,
  260. [lastStr]: false,
  261. [str]: true
  262. })
  263. } else {
  264. this.setData({
  265. lastIndex: index,
  266. [str]: true
  267. })
  268. }
  269. },
  270. // 点击头部
  271. headTap: function headTap(e) {
  272. if (!wx.getStorageSync('user').wechatName) {
  273. wx.navigateTo({
  274. url: `../../pages/login/login`
  275. });
  276. return;
  277. }
  278. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  279. // this.trigger(e, 'headTap')
  280. console.log('点击头像', e)
  281. wx.navigateTo({
  282. url: `../../pages/myworks/myworks?uid=${uid}`,
  283. fail: (err) => {
  284. console.log('跳转错误', err)
  285. wx.navigateTo({
  286. url: `../../../pages/myworks/myworks?uid=${uid}`
  287. });
  288. }
  289. });
  290. },
  291. // 去朗读
  292. goToReading: function goToReading(e) {
  293. this.trigger(e, 'goToReading')
  294. },
  295. // 收藏课程
  296. collectTap: function collectClass(e) {
  297. if (!wx.getStorageSync('user').wechatName) {
  298. wx.navigateTo({
  299. url: `../../pages/login/login`
  300. });
  301. return;
  302. }
  303. console.log('收藏按钮', e);
  304. const data = {
  305. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  306. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  307. }
  308. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  309. this.setData({
  310. activeIndex:index
  311. })
  312. console.log('视频index', index);
  313. if (this.data.curQueue.length <= 0) {
  314. let str = `videoList[${index}].isFavorite`
  315. httpRequestApi.collectClass(data).success((res) => {
  316. let isCollect = res.data.data.status === 'NORMAL' ? true : false;
  317. // this.setData({
  318. // [str]: isCollect
  319. // })
  320. if (res.data.count > 0) {
  321. this.setData({
  322. flowerNum: res.data.count
  323. })
  324. this.flowerAnimationHandler();
  325. }
  326. this.triggerEvent('collectTap', {
  327. index,
  328. isCollect
  329. })
  330. });
  331. } else {
  332. let str = `curQueue[${index}].isFavorite`;
  333. let str2 = `curQueue[${index}].favoritesAmount`
  334. httpRequestApi.collectClass(data).success((res) => {
  335. console.log('收藏数', this.data.curQueue[index].favoritesAmount)
  336. this.setData({
  337. [str]: !this.data.curQueue[index].isFavorite,
  338. [str2]: res.data.data.status === 'NORMAL' ? this.data.curQueue[index].favoritesAmount + 1 : this.data.curQueue[index].favoritesAmount - 1
  339. })
  340. });
  341. }
  342. },
  343. // 点赞
  344. likeTap: function likeTap(e) {
  345. if (!wx.getStorageSync('user').wechatName) {
  346. wx.navigateTo({
  347. url: `../../pages/login/login`
  348. });
  349. return;
  350. }
  351. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  352. console.log('isLike', isLike)
  353. if (isLike) {
  354. return;
  355. }
  356. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  357. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  358. this.setData({
  359. addComeOut: index,
  360. activeIndex: index,
  361. activeType: 'like'
  362. })
  363. setTimeout(() => {
  364. if (this.data.addComeOut) {
  365. this.setData({
  366. addComeOut: ''
  367. })
  368. }
  369. }, 1100)
  370. console.log('视频index', index);
  371. if (this.data.isSwiper) {
  372. let likeStr = `curQueue[${index}].isLike`;
  373. let likeNumStr = `curQueue[${index}].likes`;
  374. httpRequestApi.likeWorks(id).success((res) => {
  375. this.setData({
  376. [likeStr]: true,
  377. [likeNumStr]: this.data.curQueue[index].likes + 1
  378. })
  379. });
  380. } else {
  381. let likeStr = `videoList[${index}].isLike`;
  382. let likeNumStr = `videoList[${index}].likes`;
  383. httpRequestApi.likeWorks(id).success((res) => {
  384. console.log('小红花个数', res.data.count)
  385. if (res.data.count > 0) {
  386. this.setData({
  387. flowerNum: res.data.count
  388. })
  389. this.flowerAnimationHandler();
  390. }
  391. this.triggerEvent('likeTap', {
  392. index,
  393. })
  394. });
  395. }
  396. },
  397. addShareAmount: function (e) {
  398. console.log('+++++1', e.detail.index);
  399. this.triggerEvent('addShareAmount', {
  400. index: e.detail.index
  401. })
  402. },
  403. // 下载视频
  404. download: function (e) {
  405. // console.log(e.currentTarget.dataset)
  406. wx.showLoading({
  407. title: '保存到本地',
  408. mask: true
  409. })
  410. const {
  411. url
  412. } = e.currentTarget.dataset;
  413. wx.downloadFile({
  414. url,
  415. success(res) {
  416. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  417. if (res.statusCode === 200) {
  418. console.log(res.tempFilePath)
  419. wx.saveVideoToPhotosAlbum({
  420. filePath: res.tempFilePath,
  421. success(res) {
  422. wx.hideLoading()
  423. wx.showToast({
  424. title: '保存成功',
  425. icon: 'success',
  426. duration: 2000,
  427. mask: true
  428. })
  429. },
  430. fail(error) {
  431. console.log(error)
  432. }
  433. })
  434. }
  435. },
  436. fail() {
  437. wx.hideLoading()
  438. wx.showToast({
  439. title: '网络不给力',
  440. icon: 'error',
  441. duration: 2000,
  442. mask: true
  443. })
  444. }
  445. })
  446. },
  447. delete: function (e) {
  448. console.log('删除', e)
  449. wx.showModal({
  450. title: '确认删除吗?',
  451. content: '作品将被永久删除,无法找回。',
  452. confirmText: '确认',
  453. cancelText: '取消',
  454. success: (res) => {
  455. if (res.confirm) {
  456. /* let data = {
  457. id: e.currentTarget.dataset.id,
  458. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  459. }
  460. httpRequestApi.putWork(data).success(res => {
  461. console.log('已隐藏', res)
  462. this.triggerEvent('delHideMyWork');
  463. }) */
  464. let data = {
  465. id: e.currentTarget.dataset.id,
  466. status: 'DEL'
  467. }
  468. httpRequestApi.putWork(data).success(res => {
  469. console.log('已删除', res)
  470. this.triggerEvent('delHideMyWork');
  471. })
  472. } else if (res.cancel) {
  473. console.log('用户点击取消')
  474. }
  475. }
  476. })
  477. },
  478. hide: function (e) {
  479. console.log('隐藏', e)
  480. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  481. console.log('当前状态', status)
  482. let data = {
  483. id: e.currentTarget.dataset.id,
  484. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  485. }
  486. httpRequestApi.putWork(data).success(res => {
  487. console.log('已隐藏', res)
  488. if (res.data.data.status === 'DISABLE') {
  489. wx.showToast({
  490. title: '该作品仅自己可见',
  491. icon: 'none',
  492. duration: 2000
  493. })
  494. }
  495. this.triggerEvent('delHideMyWork');
  496. })
  497. },
  498. openShare: function (e) {
  499. this.triggerEvent('openShare', e)
  500. const obj = e.currentTarget.dataset
  501. this.shareDialog = this.selectComponent("#share-dialog");
  502. const data = {
  503. avatar: obj.avatar,
  504. author: obj.author,
  505. iconImg: obj.iconImg,
  506. title: obj.title,
  507. path: `pages/index/index`,
  508. scene: obj.id,
  509. type: obj.type,
  510. grade: obj.grade,
  511. productId: 1,
  512. shareImg: obj.shareImg,
  513. index: obj.index
  514. }
  515. this.setData({
  516. noScroll: 'noScroll'
  517. })
  518. this.shareDialog.share(data);
  519. // this.shareDialog.shareFriend();
  520. },
  521. flowerAnimationHandler: function () {
  522. this.flowerBox = this.selectComponent("#flower-toast");
  523. console.log('this.flower', this.flowerBox)
  524. this.flowerBox.comeOut();
  525. },
  526. trigger: function trigger(e, type) {
  527. if (!wx.getStorageSync('user').wechatName) {
  528. wx.navigateTo({
  529. url: `../../pages/login/login`
  530. });
  531. return;
  532. }
  533. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  534. // var detail = e.detail;
  535. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  536. var activeIndex = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  537. this.triggerEvent(type, {
  538. activeId: activeId,
  539. activeIndex: activeIndex
  540. }, {
  541. bubbles: false
  542. });
  543. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  544. activeId: activeId
  545. }), ext)); */
  546. }
  547. }
  548. });