ad.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="page" :style="{height:windowHeight}">
  3. <view class="dis j-s a-c" style="padding: 0 10px;">
  4. <view class="maskTool">
  5. <text>广告</text>
  6. <text>丨</text>
  7. <text v-if="countdownTime>0">观看{{countdownTime}}秒后获得奖励</text>
  8. <text v-else>已获得奖励</text>
  9. </view>
  10. <view class="maskTool1 dis j-s a-c">
  11. <image :src="isMuted?'../../static/img/volume1.png':'../../static/img/volume2.png'" mode=""
  12. style="width: 15px;height: 15px;" @click="isMuteds"></image>
  13. <text>丨</text>
  14. <!-- #ifdef APP-PLUS -->
  15. <text @click="showAlert">关闭</text>
  16. <!-- #endif -->
  17. <!-- #ifdef H5 -->
  18. <text @click="showAlert1">关闭</text>
  19. <!-- #endif -->
  20. </view>
  21. </view>
  22. <view class="" :style="{width:'100%',height:'calc(100% - 45px)',position: 'relative'}">
  23. <video id="myVideo" :src="videoUrl" autoplay controls :style="{width:'100%',height:'100%'}"
  24. :controls="false" @play="startCountdown" :muted="muted" :enable-progress-gesture="false">
  25. </video>
  26. </view>
  27. <u-modal v-model="show" content="暂未获得奖励,是否继续观看视频" show-cancel-button confirm-text="继续" :show-title="false"
  28. :content-style="{fontWeight:'bold'}" :cancel-style="{fontWeight:'bold'}"
  29. :confirm-style="{fontWeight:'bold'}" cancel-text="放弃" @confirm="confirm" @cancel="cancel"></u-modal>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. countdownTime: 0,
  37. videoTimeLength: 0,
  38. windowHeight: "",
  39. isMuted: true,
  40. show: false,
  41. videoContext: "",
  42. muted: false,
  43. videoUrl: "",
  44. videoId: "",
  45. userId: "",
  46. }
  47. },
  48. onShow() {
  49. // 确保视频元素存在
  50. this.$nextTick(() => {
  51. this.videoContext = uni.createVideoContext('myVideo', this);
  52. });
  53. },
  54. async onLoad(params) {
  55. if (params) {
  56. this.userId = params.userId;
  57. }
  58. const res = uni.getSystemInfoSync();
  59. this.windowHeight = res.windowHeight + 'px';
  60. this.marketQuery();
  61. },
  62. methods: {
  63. confirm() {
  64. this.videoContext.play();
  65. // 继续倒计时
  66. this.startCountdown();
  67. },
  68. cancel() {
  69. this.marketCallback();
  70. uni.navigateBack(-1)
  71. },
  72. isMuteds() {
  73. this.isMuted = !this.isMuted;
  74. this.muted = !this.muted;
  75. },
  76. startCountdown() {
  77. if (this.intervalId) {
  78. clearInterval(this.intervalId);
  79. }
  80. this.intervalId = setInterval(() => {
  81. if (this.countdownTime > 0) {
  82. this.countdownTime--;
  83. } else {
  84. clearInterval(this.intervalId);
  85. }
  86. }, 1000);
  87. },
  88. showAlert1() {
  89. if (this.countdownTime > 0) {
  90. this.videoContext.pause();
  91. if (this.intervalId) {
  92. clearInterval(this.intervalId);
  93. }
  94. this.show = true;
  95. // 暂停倒计时
  96. } else {
  97. uni.navigateBack(-1)
  98. this.marketCallback();
  99. }
  100. },
  101. showAlert() {
  102. if (this.countdownTime > 0) {
  103. this.videoContext.pause();
  104. if (this.intervalId) {
  105. clearInterval(this.intervalId);
  106. }
  107. plus.nativeUI.confirm(
  108. "暂未获得奖励,是否继续观看视频。",
  109. (e) => {
  110. if (e.index === 0) {
  111. this.videoContext.play();
  112. // 继续倒计时
  113. this.startCountdown();
  114. } else if (e.index === 1) {
  115. uni.navigateBack(-1)
  116. }
  117. }, {
  118. buttons: ["继续", "放弃"]
  119. }
  120. );
  121. } else {
  122. uni.navigateBack(-1)
  123. }
  124. },
  125. marketQuery() {
  126. uni.request({
  127. url: 'http://192.168.0.106:8082/api/advertising/getCompany',
  128. method: "POST",
  129. data: {
  130. userId: this.userId
  131. },
  132. success: (res) => {
  133. this.videoUrl = 'http://192.168.0.106:8082/api/' + res.data.videoUrl;
  134. this.countdownTime = res.data.videoTimeLength;
  135. this.videoTimeLength = res.data.videoTimeLength;
  136. this.videoId = res.data.id;
  137. }
  138. })
  139. },
  140. marketCallback() {
  141. uni.request({
  142. url: 'http://192.168.0.106:8082/api/callback/advertisingCallback',
  143. method: "POST",
  144. data: {
  145. isCatOver: this.countdownTime > 0 ? '0' : '1',
  146. catVideoTime: this.videoTimeLength - this.countdownTime,
  147. videoId: this.videoId,
  148. userId: this.userId,
  149. },
  150. success: (res) => {
  151. }
  152. })
  153. },
  154. },
  155. beforeDestroy() {
  156. if (this.intervalId) {
  157. clearInterval(this.intervalId);
  158. }
  159. }
  160. }
  161. </script>
  162. <style>
  163. page {
  164. background-color: #000;
  165. }
  166. </style>
  167. <style lang="scss" scoped>
  168. .page {
  169. padding-top: 40px;
  170. background-color: #000;
  171. }
  172. .maskTool {
  173. border: 1px solid #999;
  174. height: auto;
  175. color: #fff;
  176. border-radius: 20px;
  177. padding: 5px 10px;
  178. }
  179. .maskTool1 {
  180. border: 1px solid #999;
  181. height: auto;
  182. color: #fff;
  183. border-radius: 20px;
  184. padding: 5px 10px;
  185. }
  186. </style>