index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="main">
  3. <code-elf-guide v-if="guidePages && list.length > 0" :s="s" :list="list"></code-elf-guide>
  4. <view v-if="guidePages && list.length == 0" class="jump-over" @tap="launchFlag()">{{jumpover}}</view>
  5. </view>
  6. </template>
  7. <script>
  8. import codeElfGuide from '@/components/code-elf-guide/code-elf-guide.vue'
  9. let timer
  10. export default {
  11. components: {
  12. codeElfGuide
  13. },
  14. data() {
  15. return {
  16. guidePages: false,
  17. list: [],
  18. jumpover: '跳过',
  19. s: 6
  20. }
  21. },
  22. onLoad() {
  23. // this.loadExecution()
  24. // #ifdef APP-PLUS
  25. if (plus.os.name == 'iOS') {
  26. // 首先获取一次网络状态
  27. uni.getNetworkType({
  28. success: (res) => {
  29. console.log(res, 111)
  30. if (res.networkType != 'none') {
  31. this.getAd()
  32. }
  33. }
  34. });
  35. } else {
  36. this.getAd()
  37. }
  38. // #endif
  39. // #ifdef H5
  40. this.getAd()
  41. // #endif
  42. },
  43. onShow() {
  44. // 监听网络变化
  45. uni.onNetworkStatusChange((res) => {
  46. console.log(res, 222)
  47. if (res.isConnected) {
  48. this.getAd()
  49. }
  50. });
  51. },
  52. onHide() {
  53. uni.offNetworkStatusChange(() => {})
  54. },
  55. onUnload() {
  56. clearInterval(timer)
  57. },
  58. methods: {
  59. loadExecution: function() {
  60. /**
  61. * 获取本地存储中launchFlag的值
  62. * 若存在,说明不是首次启动,直接进入首页;
  63. * 若不存在,说明是首次启动,进入引导页;
  64. */
  65. try {
  66. // 获取本地存储中launchFlag标识
  67. const value = uni.getStorageSync('launchFlag');
  68. if (value) {
  69. this.guidePages = false
  70. // launchFlag=true直接跳转到首页
  71. uni.reLaunch({
  72. url: '/pages/index/news'
  73. });
  74. } else {
  75. // launchFlag!=true显示引导页
  76. this.guidePages = true
  77. }
  78. } catch (e) {
  79. // error
  80. uni.setStorage({
  81. key: 'launchFlag',
  82. data: true,
  83. success: function() {
  84. console.log('error时存储launchFlag');
  85. }
  86. });
  87. this.guidePages = true
  88. }
  89. return;
  90. uni.reLaunch({
  91. url: '/pages/index/news'
  92. });
  93. },
  94. getAd() {
  95. this.$http.get('/ad/ad/getShopAdByAdL/0/startAdvertising').then(res => {
  96. if (res.code == 200) {
  97. this.list = res.list
  98. if (res.list.length > 0) {
  99. this.loadExecution()
  100. timer = setInterval(() => {
  101. this.s--
  102. if (this.s == 0) {
  103. uni.setStorage({
  104. key: 'launchFlag',
  105. data: true,
  106. success: function() {
  107. console.log('error时存储launchFlag');
  108. }
  109. });
  110. uni.reLaunch({
  111. url: '/pages/index/news'
  112. });
  113. clearInterval(timer)
  114. }
  115. }, 1000)
  116. } else {
  117. uni.reLaunch({
  118. url: '/pages/index/news'
  119. });
  120. }
  121. }
  122. }).catch(err => {
  123. console.log(err, 222)
  124. this.guidePages = true
  125. this.getAd()
  126. })
  127. },
  128. launchFlag: function() {
  129. /**
  130. * 向本地存储中设置launchFlag的值,即启动标识;
  131. */
  132. uni.setStorage({
  133. key: 'launchFlag',
  134. data: true,
  135. });
  136. uni.reLaunch({
  137. url: '/pages/index/news'
  138. });
  139. },
  140. }
  141. }
  142. </script>
  143. <style>
  144. page,
  145. .main {
  146. width: 100%;
  147. height: 100%;
  148. }
  149. .jump-over {
  150. position: absolute;
  151. height: 60upx;
  152. line-height: 60upx;
  153. padding: 0 40upx;
  154. border-radius: 30upx;
  155. font-size: 32upx;
  156. color: #454343;
  157. border: 1px solid #454343;
  158. z-index: 999;
  159. }
  160. .jump-over {
  161. right: 45upx;
  162. top: 125upx;
  163. }
  164. </style>