index.js 19 KB

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