index.js 17 KB

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