25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

517 lines
16 KiB

  1. const app = getApp()
  2. const navigationBarHeight = (getApp().statusBarHeight + 44) + "px"
  3. const config = require("../../config/config")
  4. const Http = require("../../utils/HttpBasics.js")
  5. Page({
  6. data: {
  7. direction:-90,
  8. screenFlag: false,
  9. showAddBox: {},
  10. signal: app.globalData.platform,
  11. classTitle: "",//课程标题
  12. showIos: false,//ios字符提示
  13. showHie: false,//提示框
  14. anthology: 0,//选集
  15. classNumber: "",//有多少集
  16. imgHttps: app.globalData.imgHttps,
  17. extraClasses: 'addBox-transition ',
  18. navigationBarHeight,
  19. videoContext: "",
  20. ifpay: 0,//是否支付 0免费 1付费
  21. goPayFlag: false,//支付按钮显隐
  22. btnSuspend: true,//控制播放按钮
  23. columnIndex: 1,
  24. addFlag: true,
  25. thenIndex: 0,//播放选合集
  26. popupNum: 0,//弹出选集
  27. popupIndex: 0,////播放选集
  28. showPopup: false,//选集弹框
  29. id: "",//课程id
  30. data: {},//数据源
  31. iscollect: false,//是否收藏
  32. mp4Url: "",
  33. tiemLsit: [
  34. {//不预览
  35. value: -1,
  36. name: 0
  37. },
  38. {//试看时间
  39. value: 1,
  40. name: 1000000000000000000//无限大=》免费观看
  41. }, {
  42. value: 2,
  43. name: 30,
  44. }, {
  45. value: 3,
  46. name: 60,
  47. }, {
  48. value: 4,
  49. name: 180,
  50. }
  51. , {
  52. value: 5,
  53. name: 300,
  54. }],
  55. },
  56. // goIndent(e){
  57. // console.log(e.dataset.url);
  58. // },
  59. itiRecording(){//防止录屏初始化函数
  60. if(this.data.signal=='ios'){
  61. this.onRecord()
  62. }else{
  63. this.disable()
  64. }
  65. },
  66. disable: function() {// 防止录屏录制 安卓
  67. tt.disableUserScreenRecord({
  68. success(res) {
  69. console.log("disableUserScreenRecord 调用成功", res)
  70. },
  71. fail(res) {
  72. console.log("disableUserScreenRecord 调用失败", res);
  73. },
  74. })
  75. },
  76. onRecord: function () { // 防止录屏录制 ios
  77. const callback = (res) => {
  78. if (res.state === 'start') {
  79. this.data.videoContext.pause()//暂停
  80. this.data.videoContext.exitFullScreen()//退出全屏
  81. this.setData({
  82. screenFlag: true,
  83. btnSuspend: false,//禁止播放
  84. })
  85. } else if (res.state === 'stop') {
  86. this.data.videoContext.play();
  87. this.setData({
  88. screenFlag: false,
  89. btnSuspend: true,//禁止播放
  90. })
  91. }
  92. }
  93. tt.onUserScreenRecord(callback)
  94. },
  95. showIos() {
  96. this.setData({
  97. showIos: true
  98. })
  99. },
  100. cancel(e) {
  101. this.setData({
  102. showHie: e.detail.data
  103. })
  104. },
  105. cancelIos(e) {
  106. this.setData({
  107. showIos: e.detail.data
  108. })
  109. },
  110. hiePopup() {
  111. this.setData({
  112. showPopup: false
  113. })
  114. },
  115. setShowPopup() {
  116. this.setData({
  117. showPopup: true
  118. })
  119. },
  120. setPopupIndex(e) {
  121. let index = e.currentTarget.dataset.index
  122. this.setData({
  123. popupIndex: index
  124. })
  125. },
  126. setPopupNum(e) {
  127. let index = e.currentTarget.dataset.index
  128. this.setData({
  129. popupNum: index
  130. })
  131. },
  132. setThenIndex(e) {
  133. tt.showLoading({
  134. title: '加载中...', // 内容
  135. });
  136. let index = e.currentTarget.dataset.index
  137. let id = e.currentTarget.dataset.id
  138. this.getVideoUrl(id)
  139. this.setData({
  140. anthology: index,
  141. goPayFlag: false,
  142. btnSuspend: true,
  143. })
  144. },
  145. siteVid(e) {//切换视频
  146. tt.showLoading({
  147. title: '加载中...', // 内容
  148. });
  149. let index = e.currentTarget.dataset.index
  150. let id = e.currentTarget.dataset.id
  151. this.getVideoUrl(id)
  152. this.setData({
  153. anthology: index,
  154. goPayFlag: false,
  155. btnSuspend: true,
  156. })
  157. },
  158. hieaddCheck() {
  159. let animation = tt.createAnimation({
  160. duration: 1000,
  161. timingFunction: "ease",
  162. });
  163. animation.translateX(400).step()
  164. this.setData({
  165. // extraClasses: 'addBox-transition',
  166. showAddBox: animation.export(),
  167. addFlag: true
  168. })
  169. },
  170. showAddCheck: function () {
  171. this.setData({
  172. addFlag: false
  173. })
  174. let animation = tt.createAnimation({
  175. duration: 1000,
  176. timingFunction: "ease",
  177. });
  178. animation.translateX(0).step()
  179. this.setData({
  180. showAddBox: animation.export(),
  181. })
  182. },
  183. setColumnIndex(e) {
  184. let index = e.currentTarget.dataset.index
  185. this.setData({
  186. columnIndex: index
  187. })
  188. },
  189. order() {//支付
  190. let _this = this
  191. Http.post({//下订单
  192. url: config.api.order,
  193. data: {
  194. couponId: this.data.id
  195. }
  196. }).then(res => {
  197. console.log(res.data, "订单成功");
  198. let id = res.data.id
  199. console.log(id);
  200. Http.post({//拿orderInfo
  201. url: config.api.pay,
  202. data: {
  203. orderId: res.data.id
  204. }
  205. }).then(item => {
  206. tt.pay({
  207. service: 5,
  208. orderInfo: {
  209. order_id: item.data.orderId,
  210. order_token: item.data.token,
  211. },
  212. success(res) {
  213. console.log(res.code, Http.address, "支付成功回调code");
  214. let id = res.id
  215. if (res.code === 0) {
  216. tt.request({
  217. url: Http.address + config.api.ifPayOk, // 目标服务器url
  218. method: "post",
  219. headers: {
  220. "Content-Type": "application/json;charset=UTF-8",
  221. token: app.globalData.token
  222. },
  223. data: {
  224. code: 0,
  225. orderId: id
  226. },
  227. success: (res) => {
  228. _this.setData({
  229. goPayFlag: false
  230. })
  231. _this.getData()
  232. },
  233. fail: (err) => {
  234. tt.showToast({
  235. title: err.message, // 内容
  236. icon: "none"
  237. });
  238. console.log("我失败; err", err);
  239. _this.getData()
  240. }
  241. });
  242. tt.showToast({
  243. title: '支付成功!', // 内容
  244. });
  245. // 支付成功处理逻辑,只有res.code=0时,才表示支付成功
  246. // 但是最终状态要以商户后端结果为准
  247. } else if (res.code === 1) {
  248. tt.showToast({
  249. title: '支付超时', // 内容
  250. icon: "none"
  251. });
  252. } else if (res.code === 2) {
  253. tt.showToast({
  254. title: '支付失败', // 内容
  255. icon: "none"
  256. });
  257. } else if (res.code === 3) {
  258. tt.showToast({
  259. title: '支付关闭', // 内容
  260. icon: "none"
  261. });
  262. } else if (res.code === 4) {
  263. tt.showToast({
  264. title: '支付取消', // 内容
  265. icon: "none"
  266. });
  267. } else if (res.code === 9) {
  268. tt.showToast({
  269. title: '订单状态开发者自行获取', // 内容
  270. icon: "none"
  271. });
  272. }
  273. },
  274. fail(res) {
  275. // handle fail
  276. },
  277. })
  278. }).catch(err => {
  279. tt.showToast({
  280. title: err.message ? err.message : err.data, // 内容
  281. icon: "none"
  282. });
  283. })
  284. }).catch(err => {
  285. if (err.code === 3013) {
  286. this.setData({
  287. showHie: true
  288. })
  289. } else {
  290. tt.showToast({
  291. title: err.message, // 内容
  292. icon: "none"
  293. });
  294. }
  295. })
  296. },
  297. goPay() {
  298. this.order()
  299. console.log("我支付了");
  300. this.setData({
  301. goPayFlag: false,
  302. btnSuspend: true,
  303. })
  304. },
  305. time(e) {
  306. let time = e.detail.currentTime.toFixed(0);
  307. if (!this.data.ifpay || this.data.data.view) {//已经支付过或免费
  308. if (this.data.data.columnCoupons) {//判断有没有集数
  309. this.setData({
  310. classNumber: this.data.data.columnCoupons.length
  311. })
  312. }
  313. return
  314. } else {//需要付费或已支付
  315. let tempTime = "" //预览时间
  316. if (this.data.data.columnCoupons) {//是不是集合
  317. this.setData({
  318. classNumber: this.data.data.columnCoupons.length
  319. })
  320. tempTime = this.data.tiemLsit.filter(item => {
  321. return this.data.data.columnCoupons[this.data.anthology].previewDuration == item.value
  322. })[0].name
  323. } else {
  324. tempTime = this.data.tiemLsit.filter(item => {
  325. return this.data.data.previewDuration == item.value
  326. })[0].name
  327. }
  328. if (time >= tempTime) {
  329. this.data.videoContext.pause()//暂停
  330. this.data.videoContext.exitFullScreen()//退出全屏
  331. this.setData({
  332. goPayFlag: true,
  333. btnSuspend: false,//禁止播放
  334. })
  335. }
  336. }
  337. },
  338. setCollect() {//设置收藏
  339. if (this.data.iscollect) {//已收藏
  340. Http.get({
  341. url: config.api.Fcollect,
  342. data: { id: this.data.id }
  343. }).then(res => {
  344. this.ifCollect()
  345. }).catch(err => {
  346. tt.showToast({
  347. title: err.message, // 内容
  348. icon: "none"
  349. });
  350. })
  351. } else {//未收藏
  352. Http.get({
  353. url: config.api.Tcollect,
  354. data: { id: this.data.id }
  355. }).then(res => {
  356. this.ifCollect()
  357. }).catch(err => {
  358. tt.showToast({
  359. title: err.message, // 内容
  360. icon: "none"
  361. });
  362. })
  363. }
  364. },
  365. ifCollect() {//是否收藏课程
  366. Http.get({
  367. url: config.api.iscollect,
  368. data: {
  369. id: this.data.id
  370. }
  371. }).then(res => {
  372. this.setData({
  373. iscollect: res.data
  374. })
  375. }).catch(err => {
  376. this.setData({
  377. iscollect: false
  378. })
  379. })
  380. },
  381. getData() {//课程详情
  382. Http.get({
  383. url: config.api.classDetails,
  384. data: { id: this.data.id }
  385. }).then(res => {
  386. this.setData({
  387. data: res.data,
  388. ifpay: res.data.isPayment,
  389. columnIndex: res.data.type == 1 ? 0 : 1
  390. })
  391. if (res.data.columnCoupons) {
  392. this.getVideoUrl(res.data.columnCoupons[0].videoId)
  393. } else {
  394. this.getVideoUrl(res.data.videoId)
  395. }
  396. }).catch(err => {
  397. tt.showToast({
  398. title: err.messagea, // 内容
  399. icon: "none"
  400. });
  401. })
  402. },
  403. getVideoUrl(id) {//获取视频url
  404. Http.get({
  405. url: config.api.getViodUrl,
  406. data: {
  407. videoId: id
  408. }
  409. }).then(res => {
  410. console.log(res.data.width,res.data.height);
  411. if((res.data.width)*1>=(res.data.height)*1){
  412. //横屏
  413. console.log("横屏");
  414. this.setData({
  415. direction:-90
  416. })
  417. }else{
  418. //竖屏
  419. console.log("竖屏");
  420. this.setData({
  421. direction:0
  422. })
  423. }
  424. this.setData({
  425. mp4Url: res.data.videoUrl
  426. })
  427. tt.hideLoading();
  428. }).catch(err => {
  429. t.hideLoading();
  430. tt.showToast({
  431. title: err.messagea, // 内容
  432. icon: "none"
  433. });
  434. })
  435. },
  436. onReady() {
  437. this.setData({
  438. videoContext: wx.createVideoContext('myVideo')
  439. })
  440. this.itiRecording()
  441. },
  442. onLoad(options) {
  443. if (app.globalData.token) {
  444. if (options.id) {
  445. this.setData({
  446. id: options.id,
  447. classTitle: options.title
  448. })
  449. this.ifCollect()
  450. this.getData()
  451. }
  452. } else {
  453. app.tokenCallback = token => {
  454. if (options.id) {
  455. this.setData({
  456. id: options.id,
  457. classTitle: options.title
  458. })
  459. this.ifCollect()
  460. this.getData()
  461. }
  462. }
  463. }
  464. },
  465. onShow() {
  466. let animation = tt.createAnimation({
  467. duration: 0,
  468. // timingFunction: "ease",
  469. });
  470. animation.translateX(400).step()
  471. this.setData({
  472. showAddBox: animation.export(),
  473. screenFlag:false,//初始化防录屏弹框
  474. })
  475. // this.itiRecording()
  476. },
  477. onShareAppMessage: function (shareOption) {
  478. console.log(this.data.data.dyTitle);
  479. let that = this
  480. if (shareOption.channel == 'video') { // 判断是是不是分享视频
  481. return {
  482. channel: 'video', // 必写 video
  483. //     templateId: '',   // 分享的模版 id (如果未设置就是默认,下面会说如何设置)
  484. title: '知播堂',     // 分享的标题
  485. //     desc: '我是简介',     // 分享的内容介绍目前没有用
  486. //     path: `/pages/index/index`,  // 分享的路径
  487. extra: {
  488. videoTopics: ["知播堂", that.data.data.dyTitle] // 只有抖音才会有的属性
  489. },
  490. }
  491. } else { // 右上角分享
  492. return {
  493. templateId: '',         //分享的模版 id
  494. title: '',            //分享的标题
  495. desc: '',           // 分享的内容
  496. //      path: ``, // 分享的路径
  497. }
  498. }
  499. },
  500. })