index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div class="scroll-touch">
  3. <scroll-view
  4. :scroll-left="scrollleft"
  5. id="fixd"
  6. :class="['section-area','fix']"
  7. scroll-x="true"
  8. style="width: 100%;white-space:nowrap;background-color:#fff; border-bottom: 1px solid #ebebeb; box-sizing: border-box;"
  9. >
  10. <div class="nav">
  11. <div
  12. :id="'navitem' +index"
  13. @click="tabClick(index)"
  14. v-for="(item,index) in nav"
  15. :key="index"
  16. class="nav_item"
  17. :class="activeIndex == index? 'active':''"
  18. >{{item.name}}</div>
  19. </div>
  20. </scroll-view>
  21. <swiper
  22. :style="['height:100vh']"
  23. class="swiper"
  24. duration="200"
  25. :current="activeIndex"
  26. @change="swiperChange"
  27. >
  28. <div v-for="(swi,s) in nav" :key="s">
  29. <swiper-item>
  30. <div class="content scroll-touch" v-if="activeIndex == s">
  31. <!-- {{swi.name}} -->
  32. <div class="content-ad">
  33. <!-- banner广告 -->
  34. <ad unit-id="adunit-7496c51cbc82905d" ad-intervals="30"></ad>
  35. </div>
  36. <wsh-list-wrap :channelId="swi.channel_id" />
  37. </div>
  38. </swiper-item>
  39. </div>
  40. </swiper>
  41. <!-- <div class="clude-fixed" @click="cludeHandle" v-if="cludeShow">赚金币</div> -->
  42. </div>
  43. </template>
  44. <script>
  45. import {get} from '@/utils/request'
  46. import ListWrap from '@/components/list-wrap'
  47. import config from '@/utils/config'
  48. export default {
  49. components: {
  50. 'wsh-list-wrap': ListWrap
  51. },
  52. data () {
  53. return {
  54. nav: [],
  55. top: 0, // 导航栏初始到屏幕顶部高度
  56. activeIndex: 0, // 选项卡及swiper位置
  57. scrollleft: 0, // scroll-view 横向滚动条位置
  58. windowWidth: 0, // 窗口宽度
  59. swiperHeight: 2000, // 此处为swiper高度,
  60. cludeShow: true,
  61. scrollTimer: '',
  62. homeAd: null
  63. }
  64. },
  65. created () {
  66. this.login()
  67. },
  68. methods: {
  69. async getUserInfo () {
  70. await wx.request({
  71. method: 'post',
  72. url: config.userUrl,
  73. data: {
  74. appid: config.appId,
  75. mp_token: wx.getStorageSync('mpToken'),
  76. user_id: wx.getStorageSync('userInfo').user_id
  77. },
  78. success: res => {
  79. console.log(res)
  80. if (res.data.code === 0) {
  81. wx.setStorageSync('userInfo', res.data.data.user_info)
  82. this.userInfo = wx.getStorageSync('userInfo')
  83. } else {
  84. wx.clearStorage()
  85. }
  86. }
  87. })
  88. },
  89. login () {
  90. let user = wx.getStorageSync('userInfo')
  91. if (!user) {
  92. var that = this
  93. wx.login({
  94. success (res) {
  95. if (res.code) {
  96. console.log({
  97. 'res': res,
  98. 'config': config.appId
  99. })
  100. // 这里可以把code传给后台,后台用此获取openid及session_key
  101. that.loginHandle(res.code)
  102. }
  103. }
  104. })
  105. } else {
  106. this.getUserInfo()
  107. }
  108. },
  109. loginHandle (code) {
  110. // console.log(config.loginUrl)
  111. wx.request({
  112. method: 'post',
  113. url: config.loginUrl,
  114. data: {
  115. code: code,
  116. appid: config.appId
  117. },
  118. success: res => {
  119. // console.log(res)
  120. if (res.data.code === 0) {
  121. wx.setStorageSync('mpToken', res.data.data.mp_token)
  122. wx.setStorageSync('sessionKey', res.data.data.session_key)
  123. wx.setStorageSync('userInfo', res.data.data.user_info)
  124. this.userInfo = wx.getStorageSync('userInfo')
  125. } else {
  126. wx.clearStorage()
  127. }
  128. }
  129. })
  130. },
  131. cludeHandle () {
  132. wx.navigateTo({
  133. url: '/pages/banner/main'
  134. })
  135. },
  136. async getCategoryNav () {
  137. const params = {
  138. key_name: 'channels-huanlian'
  139. }
  140. let navData = await get('/v1/common/web-config', params)
  141. this.nav = navData.list
  142. },
  143. tabClick (e) {
  144. this.activeIndex = e
  145. },
  146. swiperChange (e) {
  147. this.activeIndex = e.mp.detail.current
  148. let that = this
  149. const query = wx.createSelectorQuery()
  150. query.select('#navitem' + this.activeIndex).boundingClientRect()
  151. query.exec(function (res) {
  152. if (res[0].right > that.windowWidth) {
  153. that.scrollleft = res[0].right
  154. } else if (res[0].left < 0) {
  155. that.scrollleft = res[0].left
  156. }
  157. })
  158. }
  159. },
  160. mounted () {
  161. this.getCategoryNav()
  162. },
  163. onLoad () {
  164. let that = this
  165. let res = wx.getSystemInfoSync()
  166. that.windowWidth = res.windowWidth
  167. const query = wx.createSelectorQuery()
  168. query.select('#fixd', '.nav').boundingClientRect()
  169. query.exec(function (res) {
  170. that.top = res[0].top
  171. })
  172. if (wx.createInterstitialAd) {
  173. this.homeAd = wx.createInterstitialAd({
  174. adUnitId: 'adunit-5ecedc2dc28ef414'
  175. })
  176. this.homeAd.onLoad(() => {
  177. console.log('插屏 广告加载成功')
  178. })
  179. this.homeAd.onError((err) => {
  180. console.log('onError event emit', err)
  181. })
  182. // this.homeAd.onClose(() => {})
  183. }
  184. if (this.homeAd) {
  185. const self = this
  186. setTimeout(() => {
  187. self.homeAd.show().catch((err) => {
  188. console.error('errer', err)
  189. })
  190. }, 10000)
  191. }
  192. },
  193. onPageScroll (e) {
  194. }
  195. }
  196. </script>
  197. <style lang="scss" scoped>
  198. // .section-area ::-webkit-scrollbar {
  199. // width: 0;
  200. // height: 0;
  201. // color: transparent;
  202. // }
  203. .scroll-touch {
  204. -webkit-overflow-scrolling: touch;
  205. }
  206. .nav {
  207. width: 100%;
  208. display: flex;
  209. height: 50px;
  210. background-color: #fff;
  211. box-sizing: border-box;
  212. .nav_item {
  213. flex: auto;
  214. text-align: center;
  215. line-height: 50px;
  216. padding: 0 10px;
  217. }
  218. .active {
  219. color: #f34b50;
  220. }
  221. }
  222. .content {
  223. background-color: #fff;
  224. overflow: hidden;
  225. height: 100%;
  226. overflow-y: scroll;
  227. .content-ad {
  228. width: 100%;
  229. }
  230. }
  231. .fix {
  232. position: fixed;
  233. top: 0;
  234. left: 0;
  235. width: 100%;
  236. z-index: 10;
  237. // animation: move 0.1s linear;
  238. }
  239. .swiper {
  240. position: relative;
  241. top: 50px;
  242. left: 0;
  243. }
  244. @keyframes move {
  245. from {
  246. opacity: 0.7;
  247. }
  248. to {
  249. opacity: 1;
  250. }
  251. }
  252. .clude-fixed {
  253. position: fixed;
  254. z-index: 999;
  255. bottom: 17%;
  256. right: 2%;
  257. width: 50px;
  258. height: 50px;
  259. border-radius: 50%;
  260. background: yellow;
  261. font-size: 13px;
  262. text-align: center;
  263. line-height: 50px;
  264. }
  265. </style>