ad.vue 4.7 KB

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