index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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: 'default'
  28. },
  29. loop: {
  30. type: Boolean,
  31. value: true
  32. },
  33. videoList: {
  34. type: Array,
  35. value: [],
  36. observer: function observer() {
  37. console.log('this.isSwiper', this.data.isSwiper)
  38. if (!this.data.isSwiper) return;
  39. var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  40. this._videoListChanged(newVal);
  41. }
  42. },
  43. nextMargin: {
  44. type: String,
  45. value: '400rpx'
  46. }
  47. },
  48. data: {
  49. nextQueue: [],
  50. prevQueue: [],
  51. curQueue: [],
  52. circular: false,
  53. // nextMargin: '400rpx',
  54. _last: 0,
  55. // _last: 1,
  56. _change: -1,
  57. _invalidUp: 0,
  58. _invalidDown: 0,
  59. _videoContexts: [],
  60. inputValue: '',
  61. showControl: 0
  62. },
  63. lifetimes: {
  64. attached: function attached() {
  65. this.data._videoContexts = [wx.createVideoContext('video_0', this), wx.createVideoContext('video_1', this), wx.createVideoContext('video_2', this)];
  66. }
  67. },
  68. methods: {
  69. _videoListChanged: function _videoListChanged(newVal) {
  70. var _this = this;
  71. var data = this.data;
  72. console.log('newVal', newVal)
  73. newVal.forEach(function (item) {
  74. data.nextQueue.push(item);
  75. });
  76. if (data.curQueue.length === 0) {
  77. this.setData({
  78. curQueue: data.nextQueue.splice(0, 3)
  79. }, function () {
  80. // _this.playCurrent(1);
  81. _this.playCurrent(0);
  82. });
  83. }
  84. },
  85. animationfinish: function animationfinish(e) {
  86. var _data = this.data,
  87. _last = _data._last,
  88. _change = _data._change,
  89. curQueue = _data.curQueue,
  90. prevQueue = _data.prevQueue,
  91. nextQueue = _data.nextQueue;
  92. var current = e.detail.current;
  93. var diff = current - _last;
  94. if (diff === 0) return;
  95. this.data._last = current;
  96. this.playCurrent(current);
  97. this.triggerEvent('change', {
  98. activeId: curQueue[current].id,
  99. index: current,
  100. _last,
  101. nextQueue,
  102. });
  103. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  104. if (direction === 'up') {
  105. if (this.data._invalidDown === 0) {
  106. var change = (_change + 1) % 3;
  107. var add = nextQueue.shift();
  108. var remove = curQueue[change];
  109. if (add) {
  110. prevQueue.push(remove);
  111. curQueue[change] = add;
  112. this.data._change = change;
  113. } else {
  114. this.data._invalidUp += 1;
  115. }
  116. } else {
  117. this.data._invalidDown -= 1;
  118. }
  119. }
  120. if (direction === 'down') {
  121. if (this.data._invalidUp === 0) {
  122. var _change2 = _change;
  123. var _remove = curQueue[_change2];
  124. var _add = prevQueue.pop();
  125. if (_add) {
  126. curQueue[_change2] = _add;
  127. nextQueue.unshift(_remove);
  128. this.data._change = (_change2 - 1 + 3) % 3;
  129. } else {
  130. this.data._invalidDown += 1;
  131. }
  132. } else {
  133. this.data._invalidUp -= 1;
  134. }
  135. }
  136. var circular = true;
  137. if (nextQueue.length === 0 && current !== 0) {
  138. circular = false;
  139. }
  140. if (prevQueue.length === 0 && current !== 2) {
  141. circular = false;
  142. }
  143. this.setData({
  144. curQueue: curQueue,
  145. circular: circular
  146. });
  147. },
  148. playCurrent: function playCurrent(current) {
  149. console.log('playCurrent', current)
  150. // return; // 注掉自动播放
  151. this.data._videoContexts.forEach(function (ctx, index) {
  152. index !== current ? ctx.pause() : ctx.play();
  153. });
  154. },
  155. onPlay: function onPlay(e) {
  156. console.log('播放记录', e)
  157. httpRequestApi.playLogReport({
  158. userReadId: e.currentTarget.dataset.id,
  159. playStopTime: 1000
  160. }).success(res => {
  161. console.log('播放记录', res)
  162. })
  163. // this.trigger(e, 'play');
  164. },
  165. onPlayList: function onPlayList(e) {
  166. console.log('lastVideoId', this.data.lastVideoId);
  167. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  168. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  169. lastVideo.pause();
  170. this.setData({
  171. lastVideoId: e.target.id
  172. })
  173. }
  174. if (!this.data.lastVideoId) {
  175. this.setData({
  176. lastVideoId: e.target.id
  177. })
  178. }
  179. },
  180. onPause: function onPause(e) {
  181. this.trigger(e, 'pause');
  182. },
  183. onEnded: function onEnded(e) {
  184. console.log('播放结束', e)
  185. // this.trigger(e, 'ended');
  186. },
  187. onError: function onError(e) {
  188. this.trigger(e, 'error');
  189. },
  190. onTimeUpdate: function onTimeUpdate(e) {
  191. this.trigger(e, 'timeupdate');
  192. },
  193. onWaiting: function onWaiting(e) {
  194. this.trigger(e, 'wait');
  195. },
  196. onProgress: function onProgress(e) {
  197. this.trigger(e, 'progress');
  198. },
  199. onLoadedMetaData: function onLoadedMetaData(e) {
  200. this.trigger(e, 'loadedmetadata');
  201. },
  202. onTimeUpdate: function onTimeUpdate(e) {
  203. },
  204. openComment: function openComment(e) {
  205. console.log(e)
  206. this.trigger(e, 'openComment')
  207. },
  208. // 点击头部
  209. headTap: function headTap(e) {
  210. if (!wx.getStorageSync('user').wechatName) {
  211. wx.navigateTo({
  212. url: `../../pages/login/login`
  213. });
  214. return;
  215. }
  216. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  217. // this.trigger(e, 'headTap')
  218. console.log('点击头像', e)
  219. wx.navigateTo({
  220. url: `../../pages/myworks/myworks?uid=${uid}`,
  221. fail: (err) => {
  222. console.log('跳转错误', err)
  223. wx.navigateTo({
  224. url: `../../../pages/myworks/myworks?uid=${uid}`
  225. });
  226. }
  227. });
  228. },
  229. // 去朗读
  230. goToReading: function goToReading(e) {
  231. this.trigger(e, 'goToReading')
  232. },
  233. // 收藏课程
  234. collectTap: function collectClass(e) {
  235. if (!wx.getStorageSync('user').wechatName) {
  236. wx.navigateTo({
  237. url: `../../pages/login/login`
  238. });
  239. return;
  240. }
  241. console.log('收藏按钮', e);
  242. const data = {
  243. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  244. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  245. }
  246. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  247. console.log('视频index', index);
  248. if (this.data.curQueue.length <= 0) {
  249. let str = `videoList[${index}].isFavorite`
  250. httpRequestApi.collectClass(data).success((res) => {
  251. this.setData({
  252. [str]: !this.data.videoList[index].isFavorite
  253. })
  254. });
  255. } else {
  256. let str = `curQueue[${index}].isFavorite`
  257. httpRequestApi.collectClass(data).success((res) => {
  258. this.setData({
  259. [str]: !this.data.curQueue[index].isFavorite
  260. })
  261. });
  262. }
  263. },
  264. collectTapList: function collectTapList(e) {
  265. },
  266. // 点赞
  267. likeTap: function likeTap(e) {
  268. if (!wx.getStorageSync('user').wechatName) {
  269. wx.navigateTo({
  270. url: `../../pages/login/login`
  271. });
  272. return;
  273. }
  274. const isLike = e.target.dataset.islike ? e.target.dataset.islike : e.currentTarget.dataset.islike;
  275. console.log('isLike', isLike)
  276. if (isLike) {
  277. return;
  278. }
  279. const id = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  280. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  281. console.log('视频index', index);
  282. let likeStr = `curQueue[${index}].isLike`;
  283. let likeNumStr = `curQueue[${index}].likes`;
  284. httpRequestApi.likeWorks(id).success((res) => {
  285. this.setData({
  286. [likeStr]: true,
  287. [likeNumStr]: this.data.curQueue[index].likes + 1
  288. })
  289. });
  290. },
  291. delete: function (e) {
  292. console.log('删除', e)
  293. let data = {
  294. id: e.currentTarget.dataset.id,
  295. status: 'DEL'
  296. }
  297. httpRequestApi.putWork(data).success(res => {
  298. console.log('已删除', res)
  299. this.triggerEvent('delHideMyWork');
  300. })
  301. },
  302. hide: function (e) {
  303. console.log('隐藏', e)
  304. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  305. console.log('当前状态', status)
  306. let data = {
  307. id: e.currentTarget.dataset.id,
  308. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  309. }
  310. httpRequestApi.putWork(data).success(res => {
  311. console.log('已隐藏', res)
  312. this.triggerEvent('delHideMyWork');
  313. })
  314. },
  315. openShare: function (e) {
  316. const obj = e.currentTarget.dataset
  317. console.log('分享', obj)
  318. console.log('分享', e)
  319. if (1) {
  320. this.shareDialog = this.selectComponent("#share-dialog");
  321. const data = {
  322. avatar: obj.avatar,
  323. author: obj.author,
  324. iconImg: obj.iconImg,
  325. title: obj.title,
  326. path: `pages/index/index`,
  327. scene: obj.id,
  328. productId: 1
  329. // tip: this.data.tip,
  330. }
  331. // console.log(data)
  332. this.setData({
  333. noScroll: 'noScroll'
  334. })
  335. this.shareDialog.share(data);
  336. }
  337. },
  338. trigger: function trigger(e, type) {
  339. if (!wx.getStorageSync('user').wechatName) {
  340. wx.navigateTo({
  341. url: `../../pages/login/login`
  342. });
  343. return;
  344. }
  345. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  346. // var detail = e.detail;
  347. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  348. this.triggerEvent(type, {
  349. activeId: activeId
  350. }, {
  351. bubbles: false
  352. });
  353. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  354. activeId: activeId
  355. }), ext)); */
  356. }
  357. }
  358. });