index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. spinShow: false,
  7. buyModel:false,
  8. code_url:'',
  9. system: "",
  10. userInfo:{},
  11. info:{},
  12. onPlay: false,
  13. item:{},
  14. courseId:0,
  15. mediaList: []
  16. },
  17. onLoad: function( options ){
  18. let courseId = +options.courseId||35;
  19. this.setData({courseId})
  20. app.getSystemInfo( system=>{
  21. this.setData({system})
  22. })
  23. //
  24. },
  25. onShow: function(){
  26. let courseId = this.data.courseId
  27. app.checkLogin( userInfo =>{
  28. this.setData({userInfo, from:0})
  29. this.loadData( courseId )
  30. })
  31. },
  32. loadData: function( courseId ) {
  33. let param ={ courseId}
  34. app.formPost('Study.GetWxCourseInfo', param).then(res => {
  35. if( res.code == 200){
  36. let {info, list} = res.data
  37. wx.setNavigationBarTitle({
  38. title: info.name
  39. })
  40. this.setData({info, mediaList: list})
  41. }
  42. })
  43. },
  44. onReady(res) {
  45. this.videoContext = wx.createVideoContext('myVideo')
  46. },
  47. gotoExam(){
  48. let courseId = this.data.courseId
  49. wx.navigateTo({
  50. url: `/pages/study/exam/index?courseId=${courseId}`,
  51. })
  52. },
  53. recalcXs( ){
  54. let medias = this.data.mediaList;
  55. let info = this.data.info
  56. let gxs = 0;
  57. for( let i in medias){
  58. if( medias[i].isFinish == 1){
  59. gxs+= medias[i].xs
  60. }
  61. }
  62. info.getXs = gxs
  63. this.setData({info})
  64. },
  65. // 拖动快进
  66. timeUpdate( e ){
  67. let that = this;
  68. let item = this.data.item
  69. var position = item.position
  70. var currentTime = parseInt(e.detail.currentTime)
  71. if( currentTime - position < 1 || item.isFinish ){
  72. return
  73. }
  74. //
  75. if( currentTime - position > 5 ){
  76. let videoContext = wx.createVideoContext('myVideo');
  77. videoContext.seek( position )
  78. wx.showToast({
  79. title: '未完整看完该视频,不能快进',
  80. icon: 'none',
  81. duration: 2000,
  82. })
  83. return;
  84. }else{
  85. item.position= currentTime
  86. }
  87. if( item.position - item.prev_tick > 4 || item.position == item.duration ){
  88. item.prev_tick = item.position;
  89. // 前一秒完成
  90. if( item.prev_tick >= e.detail.duration ){
  91. item.isFinish = 1
  92. }
  93. this.doTick( item )
  94. }
  95. that.setData({item})
  96. },
  97. doTick( item ){
  98. let param = {
  99. id: item.id,
  100. isFinish: item.isFinish||0,
  101. position: item.position,
  102. }
  103. let mediaList = this.data.mediaList;
  104. app.formPost('study.tick', param).then( res => {
  105. if( res.code == 200){
  106. if( !res.data.skip ){
  107. item.position = res.data.position;
  108. return;
  109. }
  110. Object.assign( item, res.data )
  111. mediaList[item.index] = item;
  112. if( item.isFinish){
  113. this.recalcXs()
  114. }
  115. this.setData( {item, mediaList} );
  116. }
  117. })
  118. },
  119. playMedia( e ){
  120. let index = e.currentTarget.dataset.index;
  121. let mediaList = this.data.mediaList
  122. let item = mediaList[index];
  123. item.index = index;
  124. item.prev_tick = item.position
  125. wx.pageScrollTo({
  126. scrollTop: 0,
  127. duration: 1000
  128. })
  129. if( item.url ){
  130. this.setData({onPlay: true, item})
  131. return;
  132. }
  133. app.formPost('study.GetMedia', {id:item.id}).then( res => {
  134. if( res.code == 200){
  135. if( !res.data ) return
  136. item.url = res.data
  137. mediaList[index] = item;
  138. this.setData({onPlay: true, item, mediaList})
  139. }
  140. });
  141. },
  142. doWxPay( data ){
  143. let that = this
  144. wx.requestPayment({
  145. timeStamp: data.timeStamp,
  146. nonceStr: data.nonceStr,
  147. package: data.package,
  148. signType: data.signType,
  149. paySign: data.paySign,
  150. success: function (event) {
  151. wx.showModal({
  152. title: '支付成功',
  153. showCancel: false,
  154. content: '本次共支付费用¥'+(data.fee/100)+'元'+ `\n\n试卷更新可能延迟,请耐心等待`,
  155. success(res) {
  156. that.indexLoad()
  157. }
  158. })
  159. }
  160. })
  161. },
  162. startWxpay(e){
  163. let groupId = +e.currentTarget.dataset.id
  164. let system = this.data.system
  165. app.formPost('User.PayExam', {groupId, system}).then(res => {
  166. if (res.code === 200) {
  167. if( res.data.payType=="free" ){
  168. this.indexLoad()
  169. }else if (res.data.payType=="qrcode"){
  170. this.setData({info: res.data, buyModel:true})
  171. }else{
  172. this.doWxPay( res.data )
  173. }
  174. }
  175. })
  176. }
  177. })