You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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