index.js 20 KB

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