index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. console.log('111222')
  294. this.trigger(e, 'goToReading')
  295. },
  296. // 收藏课程
  297. collectTap: function collectClass(e) {
  298. if (!wx.getStorageSync('user').wechatName) {
  299. wx.navigateTo({
  300. url: `../../pages/login/login`
  301. });
  302. return;
  303. }
  304. console.log('收藏按钮', e);
  305. const data = {
  306. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  307. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  308. }
  309. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  310. this.setData({
  311. activeIndex:index
  312. })
  313. console.log('视频index', index);
  314. if (this.data.curQueue.length <= 0) {
  315. let str = `videoList[${index}].isFavorite`
  316. httpRequestApi.collectClass(data).success((res) => {
  317. let isCollect = res.data.data.status === 'NORMAL' ? true : false;
  318. // this.setData({
  319. // [str]: isCollect
  320. // })
  321. if (res.data.count > 0) {
  322. this.setData({
  323. flowerNum: res.data.count
  324. })
  325. this.flowerAnimationHandler();
  326. }
  327. this.triggerEvent('collectTap', {
  328. index,
  329. isCollect
  330. })
  331. });
  332. } else {
  333. let str = `curQueue[${index}].isFavorite`;
  334. let str2 = `curQueue[${index}].favoritesAmount`
  335. httpRequestApi.collectClass(data).success((res) => {
  336. console.log('收藏数', this.data.curQueue[index].favoritesAmount)
  337. this.setData({
  338. [str]: !this.data.curQueue[index].isFavorite,
  339. [str2]: res.data.data.status === 'NORMAL' ? this.data.curQueue[index].favoritesAmount + 1 : this.data.curQueue[index].favoritesAmount - 1
  340. })
  341. });
  342. }
  343. },
  344. // 点赞
  345. likeTap: function likeTap(e) {
  346. if (!wx.getStorageSync('user').wechatName) {
  347. wx.navigateTo({
  348. url: `../../pages/login/login`
  349. });
  350. return;
  351. }
  352. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  353. console.log('isLike', isLike)
  354. if (isLike) {
  355. return;
  356. }
  357. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  358. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  359. this.setData({
  360. addComeOut: index,
  361. activeIndex: index,
  362. activeType: 'like'
  363. })
  364. setTimeout(() => {
  365. if (this.data.addComeOut) {
  366. this.setData({
  367. addComeOut: ''
  368. })
  369. }
  370. }, 1100)
  371. console.log('视频index', index);
  372. if (this.data.isSwiper) {
  373. let likeStr = `curQueue[${index}].isLike`;
  374. let likeNumStr = `curQueue[${index}].likes`;
  375. httpRequestApi.likeWorks(id).success((res) => {
  376. this.setData({
  377. [likeStr]: true,
  378. [likeNumStr]: this.data.curQueue[index].likes + 1
  379. })
  380. });
  381. } else {
  382. let likeStr = `videoList[${index}].isLike`;
  383. let likeNumStr = `videoList[${index}].likes`;
  384. httpRequestApi.likeWorks(id).success((res) => {
  385. console.log('小红花个数', res.data.count)
  386. if (res.data.count > 0) {
  387. this.setData({
  388. flowerNum: res.data.count
  389. })
  390. this.flowerAnimationHandler();
  391. }
  392. this.triggerEvent('likeTap', {
  393. index,
  394. })
  395. });
  396. }
  397. },
  398. addShareAmount: function (e) {
  399. console.log('+++++1', e.detail.index);
  400. this.triggerEvent('addShareAmount', {
  401. index: e.detail.index
  402. })
  403. },
  404. // 下载视频
  405. download: function (e) {
  406. // console.log(e.currentTarget.dataset)
  407. wx.showLoading({
  408. title: '保存到本地',
  409. mask: true
  410. })
  411. const {
  412. url
  413. } = e.currentTarget.dataset;
  414. wx.downloadFile({
  415. url,
  416. success(res) {
  417. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  418. if (res.statusCode === 200) {
  419. console.log(res.tempFilePath)
  420. wx.saveVideoToPhotosAlbum({
  421. filePath: res.tempFilePath,
  422. success(res) {
  423. wx.hideLoading()
  424. wx.showToast({
  425. title: '保存成功',
  426. icon: 'success',
  427. duration: 2000,
  428. mask: true
  429. })
  430. },
  431. fail(error) {
  432. console.log(error)
  433. }
  434. })
  435. }
  436. },
  437. fail() {
  438. wx.hideLoading()
  439. wx.showToast({
  440. title: '网络不给力',
  441. icon: 'error',
  442. duration: 2000,
  443. mask: true
  444. })
  445. }
  446. })
  447. },
  448. delete: function (e) {
  449. console.log('删除', e)
  450. wx.showModal({
  451. title: '确认删除吗?',
  452. content: '作品将被永久删除,无法找回。',
  453. confirmText: '确认',
  454. cancelText: '取消',
  455. success: (res) => {
  456. if (res.confirm) {
  457. /* let data = {
  458. id: e.currentTarget.dataset.id,
  459. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  460. }
  461. httpRequestApi.putWork(data).success(res => {
  462. console.log('已隐藏', res)
  463. this.triggerEvent('delHideMyWork');
  464. }) */
  465. let data = {
  466. id: e.currentTarget.dataset.id,
  467. status: 'DEL'
  468. }
  469. httpRequestApi.putWork(data).success(res => {
  470. console.log('已删除', res)
  471. this.triggerEvent('delHideMyWork');
  472. })
  473. } else if (res.cancel) {
  474. console.log('用户点击取消')
  475. }
  476. }
  477. })
  478. },
  479. hide: function (e) {
  480. console.log('隐藏', e)
  481. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  482. console.log('当前状态', status)
  483. let data = {
  484. id: e.currentTarget.dataset.id,
  485. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  486. }
  487. httpRequestApi.putWork(data).success(res => {
  488. console.log('已隐藏', res)
  489. if (res.data.data.status === 'DISABLE') {
  490. wx.showToast({
  491. title: '该作品仅自己可见',
  492. icon: 'none',
  493. duration: 2000
  494. })
  495. }
  496. this.triggerEvent('delHideMyWork');
  497. })
  498. },
  499. openShare: function (e) {
  500. this.triggerEvent('openShare', e)
  501. const obj = e.currentTarget.dataset
  502. const scene = obj.activity ? `${obj.id}&activity=true` : obj.id
  503. this.shareDialog = this.selectComponent("#share-dialog");
  504. const data = {
  505. avatar: obj.avatar,
  506. author: obj.author,
  507. iconImg: obj.iconImg,
  508. title: obj.title,
  509. path: `pages/index/index`,
  510. scene,
  511. type: obj.type,
  512. grade: obj.grade,
  513. productId: 1,
  514. shareImg: obj.shareImg,
  515. index: obj.index,
  516. activity: obj.activity || ''
  517. }
  518. this.setData({
  519. noScroll: 'noScroll'
  520. })
  521. this.shareDialog.share(data);
  522. // this.shareDialog.shareFriend();
  523. },
  524. flowerAnimationHandler: function () {
  525. this.flowerBox = this.selectComponent("#flower-toast");
  526. console.log('this.flower', this.flowerBox)
  527. this.flowerBox.comeOut();
  528. },
  529. trigger: function trigger(e, type) {
  530. if (!wx.getStorageSync('user').wechatName) {
  531. wx.navigateTo({
  532. url: `../../pages/login/login`
  533. });
  534. return;
  535. }
  536. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  537. // var detail = e.detail;
  538. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  539. var activeIndex = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  540. this.triggerEvent(type, {
  541. activeId: activeId,
  542. activeIndex: activeIndex
  543. }, {
  544. bubbles: false
  545. });
  546. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  547. activeId: activeId
  548. }), ext)); */
  549. }
  550. }
  551. });