index.js 18 KB

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