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.

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