index.nvue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <template>
  2. <view class="tabs">
  3. <search-bar @search="navToSearch" icon="iconkuaijiecaidan" @tab="tabClick" :placeholder="placeholder">
  4. </search-bar>
  5. <scroll-view ref="tabbar1" id="tab-bar" class="tab-bar" :scroll="false" :scroll-x="true" :show-scrollbar="false"
  6. :scroll-into-view="scrollInto">
  7. <view style="flex-direction: column;">
  8. <view style="flex-direction: row;">
  9. <view class="uni-tab-item" v-for="(tab,index) in tabList" :key="tab.id" :id="tab.id"
  10. :ref="'tabitem'+index" :data-id="index" :data-current="index" @click="ontabtap"
  11. :class="tabIndex == index? 'uni-tab-item-active' : ''">
  12. <text class="uni-tab-item-title"
  13. :class="tabIndex == index ? 'uni-tab-item-title-active' : ''">{{ tab.label == 'common.LatestNews' ? i18n.LatestNews: tab.label }}</text>
  14. </view>
  15. </view>
  16. <!-- <view class="scroll-view-indicator">
  17. <view ref="underline" class="scroll-view-underline" :class="isTap ? 'scroll-view-animation':''" :style="{left: indicatorLineLeft + 'px', width: indicatorLineWidth + 'px'}"></view>
  18. </view> -->
  19. </view>
  20. </scroll-view>
  21. <view class="tab-bar-line"></view>
  22. <swiper class="tab-box" ref="swiper1" :current="tabIndex" :duration="300" @change="onswiperchange"
  23. @transition="onswiperscroll" @animationfinish="animationfinish" @onAnimationEnd="animationfinish">
  24. <swiper-item class="swiper-item" v-for="(page, index) in tabList" :key="index">
  25. <newsPage class="page-item" :pageItem="page" ref="page"></newsPage>
  26. </swiper-item>
  27. </swiper>
  28. </view>
  29. </template>
  30. <script>
  31. import searchBar from './components/rf-search-bar';
  32. // #ifdef APP-PLUS
  33. const dom = weex.requireModule('dom');
  34. // #endif
  35. import newsPage from './news-page.nvue';
  36. // 缓存每页最多
  37. const MAX_CACHE_DATA = 100;
  38. // 缓存页签数量
  39. const MAX_CACHE_PAGE = 3;
  40. const TAB_PRELOAD_OFFSET = 1;
  41. export default {
  42. components: {
  43. newsPage,
  44. searchBar,
  45. },
  46. data() {
  47. return {
  48. placeholder: getApp().$i18n.locale == 'zh' ? '请输入关键字' : 'Please input keywords',
  49. searchTitle: '',
  50. tabList: [],
  51. pageList: [],
  52. tabIndex: 0,
  53. cacheTab: [],
  54. scrollInto: "",
  55. navigateFlag: false,
  56. indicatorLineLeft: 0,
  57. indicatorLineWidth: 0,
  58. isTap: false,
  59. currentIndex: '',
  60. }
  61. },
  62. onLoad() {
  63. this.getList();
  64. },
  65. onShow() {
  66. this.currentIndex = uni.getStorageSync('currentIndex')
  67. this.searchTitle = getApp().$i18n.locale == 'zh' ? '资讯' : 'Information';
  68. this.getIntegrate()
  69. },
  70. onReady() {
  71. this._lastTabIndex = 0;
  72. this.swiperWidth = 0;
  73. this.tabbarWidth = 0;
  74. this.tabListSize = {};
  75. this._touchTabIndex = 0;
  76. },
  77. computed: {
  78. i18n() {
  79. return getApp().$t('common');
  80. }
  81. },
  82. watch: {
  83. i18n() {
  84. this.placeholder = getApp().$i18n.locale == 'zh' ? '请输入关键字' : 'Please input keywords',
  85. this.getList();
  86. }
  87. },
  88. methods: {
  89. getIntegrate() {
  90. console.log(this.currentIndex, 11111111);
  91. if (this.currentIndex = 'point_read_information') {
  92. } else {
  93. console.log('未登录无法获取积分');
  94. }
  95. },
  96. async getList() { //获取头部导航
  97. const res = await getApp().$myRequest({
  98. url: '/sys/sysDicts',
  99. data: {
  100. type: getApp().$i18n.locale == 'en' ? "CMS_INFORMATION_TYPE_DICT_EN" :
  101. "CMS_INFORMATION_TYPE_DICT",
  102. }
  103. });
  104. if (res.data) {
  105. this.tabList = res.data[0] || [];
  106. let both = {
  107. label: 'common.LatestNews',
  108. value: '',
  109. id: 'tab1',
  110. };
  111. this.tabList.unshift(both);
  112. this.tabList.forEach((item, index) => {
  113. item.id = 'tab' + index;
  114. });
  115. this.$nextTick(() => {
  116. this.pageList = this.$refs.page;
  117. this.switchTab(this.tabIndex);
  118. this.selectorQuery();
  119. })
  120. }
  121. },
  122. navToSearch() {
  123. uni.navigateTo({
  124. url: '/pages/index/search/search'
  125. })
  126. },
  127. tabClick() {
  128. return;
  129. },
  130. ontabtap(e) {
  131. let index = e.target.dataset.current || e.currentTarget.dataset.current;
  132. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  133. this.isTap = true;
  134. var currentSize = this.tabListSize[index];
  135. this.updateIndicator(currentSize.left, currentSize.width);
  136. this._touchTabIndex = index;
  137. // #endif
  138. this.switchTab(index);
  139. },
  140. onswiperchange(e) {
  141. // 注意:百度小程序会触发2次
  142. // #ifndef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  143. let index = e.target.current || e.detail.current;
  144. this.switchTab(index);
  145. // #endif
  146. },
  147. onswiperscroll(e) {
  148. if (this.isTap) {
  149. return;
  150. }
  151. var offsetX = e.detail.dx;
  152. var preloadIndex = this._lastTabIndex;
  153. if (offsetX > TAB_PRELOAD_OFFSET) {
  154. preloadIndex++;
  155. } else if (offsetX < -TAB_PRELOAD_OFFSET) {
  156. preloadIndex--;
  157. }
  158. if (preloadIndex === this._lastTabIndex || preloadIndex < 0 || preloadIndex > this.pageList.length - 1) {
  159. return;
  160. }
  161. if (this.pageList[preloadIndex].dataList.length === 0) {
  162. this.loadTabData(preloadIndex);
  163. }
  164. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  165. var percentage = Math.abs(this.swiperWidth / offsetX);
  166. var currentSize = this.tabListSize[this._lastTabIndex];
  167. var preloadSize = this.tabListSize[preloadIndex];
  168. var lineL = currentSize.left + (preloadSize.left - currentSize.left) / percentage;
  169. var lineW = currentSize.width + (preloadSize.width - currentSize.width) / percentage;
  170. this.updateIndicator(lineL, lineW);
  171. // #endif
  172. },
  173. animationfinish(e) {
  174. // #ifdef APP-PLUS || H5 || MP-WEIXIN || MP-QQ
  175. let index = e.detail.current;
  176. if (this._touchTabIndex === index) {
  177. this.isTap = false;
  178. }
  179. this._lastTabIndex = index;
  180. this.switchTab(index);
  181. this.updateIndicator(this.tabListSize[index].left, this.tabListSize[index].width);
  182. // #endif
  183. },
  184. selectorQuery() {
  185. // #ifdef APP-NVUE
  186. dom.getComponentRect(this.$refs.tabbar1, res => {
  187. this.tabbarWidth = res.size.width;
  188. });
  189. dom.getComponentRect(this.$refs['swiper1'], res => {
  190. this.swiperWidth = res.size.width;
  191. });
  192. // 因 nvue 暂不支持 class 查询
  193. var queryTabSize = uni.createSelectorQuery().in(this);
  194. for (var i = 0; i < this.tabList.length; i++) {
  195. queryTabSize.select('#' + this.tabList[i].id).boundingClientRect();
  196. }
  197. queryTabSize.exec(rects => {
  198. rects.forEach((rect) => {
  199. this.tabListSize[rect.dataset.id] = rect;
  200. })
  201. this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex]
  202. .width);
  203. this.switchTab(this.tabIndex);
  204. });
  205. // #endif
  206. // #ifdef MP-WEIXIN || H5 || MP-QQ
  207. uni.createSelectorQuery().in(this).select('.tab-box').fields({
  208. dataset: true,
  209. size: true,
  210. }, (res) => {
  211. this.swiperWidth = res.width;
  212. }).exec();
  213. uni.createSelectorQuery().in(this).selectAll('.uni-tab-item').boundingClientRect((rects) => {
  214. rects.forEach((rect) => {
  215. this.tabListSize[rect.dataset.id] = rect;
  216. })
  217. this.updateIndicator(this.tabListSize[this.tabIndex].left, this.tabListSize[this.tabIndex]
  218. .width);
  219. }).exec();
  220. // #endif
  221. },
  222. getElementSize(dom, ref, id) {
  223. dom.getComponentRect(ref, res => {
  224. this.tabListSize[id] = res.size;
  225. });
  226. },
  227. updateIndicator(left, width) {
  228. this.indicatorLineLeft = left;
  229. this.indicatorLineWidth = width;
  230. },
  231. switchTab(index) {
  232. if (this.pageList[index].dataList.length === 0) {
  233. this.loadTabData(index);
  234. }
  235. if (this.tabIndex === index) {
  236. return;
  237. }
  238. // 缓存 tabId
  239. if (this.pageList[this.tabIndex].dataList.length > MAX_CACHE_DATA) {
  240. let isExist = this.cacheTab.indexOf(this.tabIndex);
  241. if (isExist < 0) {
  242. this.cacheTab.push(this.tabIndex);
  243. }
  244. }
  245. this.tabIndex = index;
  246. // #ifdef APP-NVUE
  247. this.scrollTabTo(index);
  248. // #endif
  249. // #ifndef APP-NVUE
  250. this.scrollInto = this.tabList[index].id;
  251. // #endif
  252. // 释放 tabId
  253. if (this.cacheTab.length > MAX_CACHE_PAGE) {
  254. let cacheIndex = this.cacheTab[0];
  255. this.clearTabData(cacheIndex);
  256. this.cacheTab.splice(0, 1);
  257. }
  258. },
  259. scrollTabTo(index) {
  260. const el = this.$refs['tabitem' + index][0];
  261. let offset = 0;
  262. // TODO fix ios offset
  263. if (index > 0) {
  264. offset = this.tabbarWidth / 2 - this.tabListSize[index].width / 2;
  265. if (this.tabListSize[index].right < this.tabbarWidth / 2) {
  266. offset = this.tabListSize[0].width;
  267. }
  268. }
  269. dom.scrollToElement(el, {
  270. offset: -offset
  271. });
  272. },
  273. loadTabData(index) {
  274. this.pageList[index].loadData(false, index);
  275. },
  276. clearTabData(index) {
  277. this.pageList[index].clear();
  278. }
  279. }
  280. }
  281. </script>
  282. <style scoped>
  283. /* #ifndef APP-PLUS */
  284. page {
  285. width: 100%;
  286. min-height: 100%;
  287. display: flex;
  288. background-color: #f1f6f5;
  289. }
  290. /* #endif */
  291. .tabs {
  292. flex: 1;
  293. flex-direction: column;
  294. overflow: hidden;
  295. /* #ifdef MP-ALIPAY || MP-BAIDU */
  296. height: 100vh;
  297. /* #endif */
  298. }
  299. .tab-bar {
  300. /* #ifdef APP-PLUS */
  301. width: 750upx;
  302. /* #endif */
  303. height: 42px;
  304. flex-direction: row;
  305. /* #ifndef APP-PLUS */
  306. white-space: nowrap;
  307. /* #endif */
  308. background-color: #FFFFFF;
  309. }
  310. /* #ifndef APP-NVUE */
  311. .tab-bar ::-webkit-scrollbar {
  312. display: none;
  313. width: 0 !important;
  314. height: 0 !important;
  315. -webkit-appearance: none;
  316. background: transparent;
  317. }
  318. /* #endif */
  319. .scroll-view-indicator {
  320. position: relative;
  321. height: 2px;
  322. background-color: transparent;
  323. }
  324. .scroll-view-underline {
  325. position: absolute;
  326. top: 0;
  327. bottom: 0;
  328. width: 0;
  329. background-color: #007AFF;
  330. }
  331. .scroll-view-animation {
  332. transition-duration: 0.2s;
  333. transition-property: left;
  334. }
  335. .tab-bar-line {
  336. height: 1px;
  337. background-color: #cccccc;
  338. }
  339. .tab-box {
  340. flex: 1;
  341. }
  342. .uni-tab-item {
  343. /* #ifndef APP-PLUS */
  344. display: inline-block;
  345. /* #endif */
  346. flex-wrap: nowrap;
  347. padding-left: 20px;
  348. padding-right: 20px;
  349. }
  350. .uni-tab-item-title {
  351. color: #333;
  352. font-size: 15px;
  353. height: 40px;
  354. line-height: 40px;
  355. flex-wrap: nowrap;
  356. /* #ifndef APP-PLUS */
  357. white-space: nowrap;
  358. /* #endif */
  359. }
  360. .uni-tab-item-title-active {
  361. color: #007AFF;
  362. }
  363. .uni-tab-item {}
  364. .uni-tab-item-active {
  365. border-bottom-width: 2px;
  366. border-bottom-style: solid;
  367. border-bottom-color: #007AFF;
  368. }
  369. .swiper-item {
  370. flex: 1;
  371. flex-direction: column;
  372. }
  373. </style>