index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. var direction = diff === 1 || diff === -2 ? 'up' : 'down';
  98. this.triggerEvent('change', {
  99. activeId: curQueue[current].id,
  100. index: current,
  101. _last,
  102. nextQueue,
  103. direction
  104. });
  105. if (direction === 'up') {
  106. if (this.data._invalidDown === 0) {
  107. var change = (_change + 1) % 3;
  108. var add = nextQueue.shift();
  109. var remove = curQueue[change];
  110. if (add) {
  111. prevQueue.push(remove);
  112. curQueue[change] = add;
  113. this.data._change = change;
  114. } else {
  115. this.data._invalidUp += 1;
  116. }
  117. } else {
  118. this.data._invalidDown -= 1;
  119. }
  120. }
  121. if (direction === 'down') {
  122. if (this.data._invalidUp === 0) {
  123. var _change2 = _change;
  124. var _remove = curQueue[_change2];
  125. var _add = prevQueue.pop();
  126. if (_add) {
  127. curQueue[_change2] = _add;
  128. nextQueue.unshift(_remove);
  129. this.data._change = (_change2 - 1 + 3) % 3;
  130. } else {
  131. this.data._invalidDown += 1;
  132. }
  133. } else {
  134. this.data._invalidUp -= 1;
  135. }
  136. }
  137. var circular = true;
  138. if (nextQueue.length === 0 && current !== 0) {
  139. circular = false;
  140. }
  141. if (prevQueue.length === 0 && current !== 2) {
  142. circular = false;
  143. }
  144. this.setData({
  145. curQueue: curQueue,
  146. circular: circular
  147. });
  148. },
  149. playCurrent: function playCurrent(current) {
  150. console.log('playCurrent', current)
  151. // return; // 注掉自动播放
  152. this.data._videoContexts.forEach(function (ctx, index) {
  153. index !== current ? ctx.pause() : ctx.play();
  154. });
  155. },
  156. onPlay: function onPlay(e) {
  157. console.log('播放记录', e)
  158. httpRequestApi.playLogReport({
  159. userReadId: e.currentTarget.dataset.id,
  160. playStopTime: 1000
  161. }).success(res => {
  162. console.log('播放记录', res)
  163. })
  164. // this.trigger(e, 'play');
  165. },
  166. onPlayList: function onPlayList(e) {
  167. console.log('lastVideoId', this.data.lastVideoId);
  168. if (this.data.lastVideoId && e.target.id !== this.data.lastVideoId) {
  169. const lastVideo = wx.createVideoContext(this.data.lastVideoId, this)
  170. lastVideo.pause();
  171. this.setData({
  172. lastVideoId: e.target.id
  173. })
  174. }
  175. if (!this.data.lastVideoId) {
  176. this.setData({
  177. lastVideoId: e.target.id
  178. })
  179. }
  180. this.triggerEvent('onPlay')
  181. },
  182. onPause: function onPause(e) {
  183. this.trigger(e, 'pause');
  184. },
  185. onEnded: function onEnded(e) {
  186. console.log('播放结束', e)
  187. // this.trigger(e, 'ended');
  188. },
  189. onError: function onError(e) {
  190. this.trigger(e, 'error');
  191. },
  192. onTimeUpdate: function onTimeUpdate(e) {
  193. this.trigger(e, 'timeupdate');
  194. },
  195. onWaiting: function onWaiting(e) {
  196. this.trigger(e, 'wait');
  197. },
  198. onProgress: function onProgress(e) {
  199. this.trigger(e, 'progress');
  200. },
  201. onLoadedMetaData: function onLoadedMetaData(e) {
  202. this.trigger(e, 'loadedmetadata');
  203. },
  204. onTimeUpdate: function onTimeUpdate(e) {
  205. },
  206. openComment: function openComment(e) {
  207. console.log(e)
  208. this.trigger(e, 'openComment')
  209. },
  210. // 点击头部
  211. headTap: function headTap(e) {
  212. if (!wx.getStorageSync('user').wechatName) {
  213. wx.navigateTo({
  214. url: `../../pages/login/login`
  215. });
  216. return;
  217. }
  218. let uid = e.target.dataset.uid ? e.target.dataset.uid : e.currentTarget.dataset.uid;
  219. // this.trigger(e, 'headTap')
  220. console.log('点击头像', e)
  221. wx.navigateTo({
  222. url: `../../pages/myworks/myworks?uid=${uid}`,
  223. fail: (err) => {
  224. console.log('跳转错误', err)
  225. wx.navigateTo({
  226. url: `../../../pages/myworks/myworks?uid=${uid}`
  227. });
  228. }
  229. });
  230. },
  231. // 去朗读
  232. goToReading: function goToReading(e) {
  233. this.trigger(e, 'goToReading')
  234. },
  235. // 收藏课程
  236. collectTap: function collectClass(e) {
  237. if (!wx.getStorageSync('user').wechatName) {
  238. wx.navigateTo({
  239. url: `../../pages/login/login`
  240. });
  241. return;
  242. }
  243. console.log('收藏按钮', e);
  244. const data = {
  245. targetCode: e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id,
  246. favoritesType: e.target.dataset.type ? e.target.dataset.type : e.currentTarget.dataset.type
  247. }
  248. const index = e.target.dataset.index ? e.target.dataset.index : e.currentTarget.dataset.index;
  249. console.log('视频index', index);
  250. if (this.data.curQueue.length <= 0) {
  251. let str = `videoList[${index}].isFavorite`
  252. httpRequestApi.collectClass(data).success((res) => {
  253. this.setData({
  254. [str]: !this.data.videoList[index].isFavorite
  255. })
  256. });
  257. } else {
  258. let str = `curQueue[${index}].isFavorite`
  259. httpRequestApi.collectClass(data).success((res) => {
  260. this.setData({
  261. [str]: !this.data.curQueue[index].isFavorite
  262. })
  263. });
  264. }
  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. if (this.isSwiper) {
  283. let likeStr = `curQueue[${index}].isLike`;
  284. let likeNumStr = `curQueue[${index}].likes`;
  285. httpRequestApi.likeWorks(id).success((res) => {
  286. this.setData({
  287. [likeStr]: true,
  288. [likeNumStr]: this.data.curQueue[index].likes + 1
  289. })
  290. });
  291. } else {
  292. let likeStr = `videoList[${index}].isLike`;
  293. let likeNumStr = `videoList[${index}].likes`;
  294. httpRequestApi.likeWorks(id).success((res) => {
  295. this.setData({
  296. [likeStr]: true,
  297. [likeNumStr]: this.data.videoList[index].likes + 1
  298. })
  299. });
  300. }
  301. },
  302. delete: function (e) {
  303. console.log('删除', e)
  304. let data = {
  305. id: e.currentTarget.dataset.id,
  306. status: 'DEL'
  307. }
  308. httpRequestApi.putWork(data).success(res => {
  309. console.log('已删除', res)
  310. this.triggerEvent('delHideMyWork');
  311. })
  312. },
  313. hide: function (e) {
  314. console.log('隐藏', e)
  315. const status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : e.target.dataset.status
  316. console.log('当前状态', status)
  317. let data = {
  318. id: e.currentTarget.dataset.id,
  319. status: status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  320. }
  321. httpRequestApi.putWork(data).success(res => {
  322. console.log('已隐藏', res)
  323. this.triggerEvent('delHideMyWork');
  324. })
  325. },
  326. openShare: function (e) {
  327. const obj = e.currentTarget.dataset
  328. console.log('分享', obj)
  329. console.log('分享', e)
  330. if (1) {
  331. this.shareDialog = this.selectComponent("#share-dialog");
  332. const data = {
  333. avatar: obj.avatar,
  334. author: obj.author,
  335. iconImg: obj.iconImg,
  336. title: obj.title,
  337. path: `pages/index/index`,
  338. scene: obj.id,
  339. productId: 1
  340. // tip: this.data.tip,
  341. }
  342. // console.log(data)
  343. this.setData({
  344. noScroll: 'noScroll'
  345. })
  346. this.shareDialog.share(data);
  347. }
  348. this.triggerEvent('openShare',e)
  349. },
  350. trigger: function trigger(e, type) {
  351. if (!wx.getStorageSync('user').wechatName) {
  352. wx.navigateTo({
  353. url: `../../pages/login/login`
  354. });
  355. return;
  356. }
  357. var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  358. // var detail = e.detail;
  359. var activeId = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
  360. this.triggerEvent(type, {
  361. activeId: activeId
  362. }, {
  363. bubbles: false
  364. });
  365. /* this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
  366. activeId: activeId
  367. }), ext)); */
  368. }
  369. }
  370. });