z-paging-main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // [z-paging]核心js
  2. import zStatic from './z-paging-static'
  3. import c from './z-paging-constant'
  4. import u from './z-paging-utils'
  5. import zPagingRefresh from '../components/z-paging-refresh'
  6. import zPagingLoadMore from '../components/z-paging-load-more'
  7. import zPagingEmptyView from '../../z-paging-empty-view/z-paging-empty-view'
  8. // modules
  9. import commonLayoutModule from './modules/common-layout'
  10. import dataHandleModule from './modules/data-handle'
  11. import i18nModule from './modules/i18n'
  12. import nvueModule from './modules/nvue'
  13. import emptyModule from './modules/empty'
  14. import refresherModule from './modules/refresher'
  15. import loadMoreModule from './modules/load-more'
  16. import loadingModule from './modules/loading'
  17. import chatRecordModerModule from './modules/chat-record-mode'
  18. import scrollerModule from './modules/scroller'
  19. import backToTopModule from './modules/back-to-top'
  20. import virtualListModule from './modules/virtual-list'
  21. import Enum from './z-paging-enum'
  22. const systemInfo = u.getSystemInfoSync();
  23. export default {
  24. name: "z-paging",
  25. components: {
  26. zPagingRefresh,
  27. zPagingLoadMore,
  28. zPagingEmptyView
  29. },
  30. mixins: [
  31. commonLayoutModule,
  32. dataHandleModule,
  33. i18nModule,
  34. nvueModule,
  35. emptyModule,
  36. refresherModule,
  37. loadMoreModule,
  38. loadingModule,
  39. chatRecordModerModule,
  40. scrollerModule,
  41. backToTopModule,
  42. virtualListModule
  43. ],
  44. data() {
  45. return {
  46. // --------------静态资源---------------
  47. base64BackToTop: zStatic.base64BackToTop,
  48. // -------------全局数据相关--------------
  49. // 当前加载类型
  50. loadingType: Enum.LoadingType.Refresher,
  51. requestTimeStamp: 0,
  52. wxsPropType: '',
  53. renderPropScrollTop: -1,
  54. checkScrolledToBottomTimeOut: null,
  55. cacheTopHeight: -1,
  56. statusBarHeight: systemInfo.statusBarHeight,
  57. scrollViewHeight: 0,
  58. pagingOrgTop: -1,
  59. // --------------状态&判断---------------
  60. insideOfPaging: -1,
  61. isLoadFailed: false,
  62. isIos: systemInfo.platform === 'ios',
  63. disabledBounce: false,
  64. fromCompleteEmit: false,
  65. disabledCompleteEmit: false,
  66. pageLaunched: false,
  67. active: false,
  68. // ---------------wxs相关---------------
  69. wxsIsScrollTopInTopRange: true,
  70. wxsScrollTop: 0,
  71. wxsPageScrollTop: 0,
  72. wxsOnPullingDown: false,
  73. };
  74. },
  75. props: {
  76. // 调用complete后延迟处理的时间,单位为毫秒,默认0毫秒,优先级高于minDelay
  77. delay: {
  78. type: [Number, String],
  79. default: u.gc('delay', 0),
  80. },
  81. // 触发@query后最小延迟处理的时间,单位为毫秒,默认0毫秒,优先级低于delay(假设设置为300毫秒,若分页请求时间小于300毫秒,则在调用complete后延迟[300毫秒-请求时长];若请求时长大于300毫秒,则不延迟),当show-refresher-when-reload为true或reload(true)时,其最小值为400
  82. minDelay: {
  83. type: [Number, String],
  84. default: u.gc('minDelay', 0),
  85. },
  86. // 设置z-paging的style,部分平台(如微信小程序)无法直接修改组件的style,可使用此属性代替
  87. pagingStyle: {
  88. type: Object,
  89. default: u.gc('pagingStyle', {}),
  90. },
  91. // 设置z-paging的class,优先级低于pagingStyle和height、width、maxWidth、bgColor
  92. pagingClass: {
  93. type: [String, Array, Object],
  94. default: u.gc('pagingClass', ''),
  95. },
  96. // z-paging的高度,优先级低于pagingStyle中设置的height;传字符串,如100px、100rpx、100%
  97. height: {
  98. type: String,
  99. default: u.gc('height', '')
  100. },
  101. // z-paging的宽度,优先级低于pagingStyle中设置的width;传字符串,如100px、100rpx、100%
  102. width: {
  103. type: String,
  104. default: u.gc('width', '')
  105. },
  106. // z-paging的最大宽度,优先级低于pagingStyle中设置的max-width;传字符串,如100px、100rpx、100%。默认为空,也就是铺满窗口宽度,若设置了特定值则会自动添加margin: 0 auto
  107. maxWidth: {
  108. type: String,
  109. default: u.gc('maxWidth', '')
  110. },
  111. // z-paging的背景色,优先级低于pagingStyle中设置的background。传字符串,如"#ffffff"
  112. bgColor: {
  113. type: String,
  114. default: u.gc('bgColor', '')
  115. },
  116. // 设置z-paging的容器(插槽的父view)的style
  117. pagingContentStyle: {
  118. type: Object,
  119. default: u.gc('pagingContentStyle', {}),
  120. },
  121. // z-paging是否自动高度,若自动高度则会自动铺满屏幕
  122. autoHeight: {
  123. type: Boolean,
  124. default: u.gc('autoHeight', false)
  125. },
  126. // z-paging是否自动高度时,附加的高度,注意添加单位px或rpx,若需要减少高度,则传负数
  127. autoHeightAddition: {
  128. type: [Number, String],
  129. default: u.gc('autoHeightAddition', '0px')
  130. },
  131. // loading(下拉刷新、上拉加载更多)的主题样式,支持black,white,默认black
  132. defaultThemeStyle: {
  133. type: String,
  134. default: u.gc('defaultThemeStyle', 'black')
  135. },
  136. // z-paging是否使用fixed布局,若使用fixed布局,则z-paging的父view无需固定高度,z-paging高度默认为100%,默认为是(当使用内置scroll-view滚动时有效)
  137. fixed: {
  138. type: Boolean,
  139. default: u.gc('fixed', true)
  140. },
  141. // 是否开启底部安全区域适配
  142. safeAreaInsetBottom: {
  143. type: Boolean,
  144. default: u.gc('safeAreaInsetBottom', false)
  145. },
  146. // 开启底部安全区域适配后,是否使用placeholder形式实现,默认为否。为否时滚动区域会自动避开底部安全区域,也就是所有滚动内容都不会挡住底部安全区域,若设置为是,则滚动时滚动内容会挡住底部安全区域,但是当滚动到底部时才会避开底部安全区域
  147. useSafeAreaPlaceholder: {
  148. type: Boolean,
  149. default: u.gc('useSafeAreaPlaceholder', false)
  150. },
  151. // z-paging bottom的背景色,默认透明,传字符串,如"#ffffff"
  152. bottomBgColor: {
  153. type: String,
  154. default: u.gc('bottomBgColor', '')
  155. },
  156. // slot="top"的view的z-index,默认为99,仅使用页面滚动时有效
  157. topZIndex: {
  158. type: Number,
  159. default: u.gc('topZIndex', 99)
  160. },
  161. // z-paging内容容器父view的z-index,默认为1
  162. superContentZIndex: {
  163. type: Number,
  164. default: u.gc('superContentZIndex', 1)
  165. },
  166. // z-paging内容容器部分的z-index,默认为1
  167. contentZIndex: {
  168. type: Number,
  169. default: u.gc('contentZIndex', 1)
  170. },
  171. // z-paging二楼的z-index,默认为100
  172. f2ZIndex: {
  173. type: Number,
  174. default: u.gc('f2ZIndex', 100)
  175. },
  176. // 使用页面滚动时,是否在不满屏时自动填充满屏幕,默认为是
  177. autoFullHeight: {
  178. type: Boolean,
  179. default: u.gc('autoFullHeight', true)
  180. },
  181. // 是否监听列表触摸方向改变,默认为否
  182. watchTouchDirectionChange: {
  183. type: Boolean,
  184. default: u.gc('watchTouchDirectionChange', false)
  185. },
  186. // 是否监听列表滚动方向改变,默认为否
  187. watchScrollDirectionChange: {
  188. type: Boolean,
  189. default: u.gc('watchScrollDirectionChange', false)
  190. },
  191. // 是否只使用基础布局,设置为true后将关闭mounted自动请求数据、关闭下拉刷新和滚动到底部加载更多,强制隐藏空数据图。默认为否
  192. layoutOnly: {
  193. type: Boolean,
  194. default: u.gc('layoutOnly', false)
  195. },
  196. // z-paging中布局的单位,默认为rpx
  197. unit: {
  198. type: String,
  199. default: u.gc('unit', 'rpx')
  200. }
  201. },
  202. created() {
  203. // 组件创建时,检测是否开始加载状态
  204. if (this.createdReload && !this.isOnly && this.auto) {
  205. this._startLoading();
  206. this.$nextTick(this._preReload);
  207. }
  208. },
  209. mounted() {
  210. this.active = true;
  211. this.wxsPropType = u.getTime().toString();
  212. this.renderJsIgnore;
  213. if (!this.createdReload && !this.isOnly && this.auto) {
  214. // 开始预加载
  215. u.delay(() => this.$nextTick(this._preReload), 0);
  216. }
  217. // 如果开启了列表缓存,在初始化的时候通过缓存数据填充列表数据
  218. this.finalUseCache && this._setListByLocalCache();
  219. let delay = 0;
  220. // #ifdef H5 || MP
  221. delay = c.delayTime;
  222. // #endif
  223. this.$nextTick(() => {
  224. // 初始化systemInfo
  225. this.systemInfo = u.getSystemInfoSync();
  226. // 初始化z-paging高度
  227. !this.usePageScroll && this.autoHeight && this._setAutoHeight();
  228. this.loaded = true;
  229. u.delay(() => {
  230. // 更新fixed模式下z-paging的布局,主要是更新windowTop、windowBottom
  231. this.updateFixedLayout();
  232. // 更新缓存中z-paging整个内容容器高度
  233. this._updateCachedSuperContentHeight();
  234. // 更新z-paging中scroll-view高度
  235. this._updateScrollViewHeight();
  236. });
  237. })
  238. // 初始化页面滚动模式下slot="top"、slot="bottom"高度
  239. this.updatePageScrollTopHeight();
  240. this.updatePageScrollBottomHeight();
  241. // 初始化slot="left"、slot="right"宽度
  242. this.updateLeftAndRightWidth();
  243. if (this.finalRefresherEnabled && this.useCustomRefresher) {
  244. this.$nextTick(() => {
  245. this.isTouchmoving = true;
  246. })
  247. }
  248. if (!this.layoutOnly) {
  249. // 监听uni.$emit中全局emit的complete error等事件
  250. this._onEmit();
  251. }
  252. // #ifdef APP-NVUE
  253. if (!this.isIos && !this.useChatRecordMode) {
  254. this.nLoadingMoreFixedHeight = true;
  255. }
  256. // 在nvue中更新nvue下拉刷新view容器的宽度,而不是写死默认的750rpx,需要考虑列表宽度不是铺满屏幕的情况
  257. this._nUpdateRefresherWidth();
  258. // #endif
  259. // #ifndef APP-PLUS
  260. this.$nextTick(() => {
  261. // 非app平台中,在通过获取css设置的底部安全区域占位view高度设置bottom距离后,更新页面滚动底部高度
  262. setTimeout(() => {
  263. this._getCssSafeAreaInsetBottom(() => this.safeAreaInsetBottom && this.updatePageScrollBottomHeight());
  264. }, delay)
  265. })
  266. // #endif
  267. },
  268. destroyed() {
  269. this._handleUnmounted();
  270. },
  271. // #ifdef VUE3
  272. unmounted() {
  273. this._handleUnmounted();
  274. },
  275. // #endif
  276. watch: {
  277. defaultThemeStyle: {
  278. handler(newVal) {
  279. if (newVal.length) {
  280. this.finalRefresherDefaultStyle = newVal;
  281. }
  282. },
  283. immediate: true
  284. },
  285. autoHeight(newVal) {
  286. this.loaded && !this.usePageScroll && this._setAutoHeight(newVal);
  287. },
  288. autoHeightAddition(newVal) {
  289. this.loaded && !this.usePageScroll && this.autoHeight && this._setAutoHeight(newVal);
  290. },
  291. },
  292. computed: {
  293. // 当前z-paging的内置样式
  294. finalPagingStyle() {
  295. const pagingStyle = { ...this.pagingStyle };
  296. if (!this.systemInfo) return pagingStyle;
  297. const { windowTop, windowBottom } = this;
  298. if (!this.usePageScroll && this.fixed) {
  299. if (windowTop && !pagingStyle.top) {
  300. pagingStyle.top = windowTop + 'px';
  301. }
  302. if (windowBottom && !pagingStyle.bottom) {
  303. pagingStyle.bottom = windowBottom + 'px';
  304. }
  305. }
  306. if (this.bgColor.length && !pagingStyle['background']) {
  307. pagingStyle['background'] = this.bgColor;
  308. }
  309. if (this.height.length && !pagingStyle['height']) {
  310. pagingStyle['height'] = this.height;
  311. }
  312. if (this.width.length && !pagingStyle['width']) {
  313. pagingStyle['width'] = this.width;
  314. }
  315. if (this.maxWidth.length && !pagingStyle['max-width']) {
  316. pagingStyle['max-width'] = this.maxWidth;
  317. pagingStyle['margin'] = '0 auto';
  318. }
  319. return pagingStyle;
  320. },
  321. // 当前z-paging内容的样式
  322. finalPagingContentStyle() {
  323. if (this.contentZIndex != 1) {
  324. this.pagingContentStyle['z-index'] = this.contentZIndex;
  325. this.pagingContentStyle['position'] = 'relative';
  326. }
  327. return this.pagingContentStyle;
  328. },
  329. // 最终的当前开启安全区域适配后,是否使用placeholder形式实现。如果slot=bottom存在,则应当交由固定在底部的view处理,因此需排除此情况
  330. finalUseSafeAreaPlaceholder() {
  331. return this.useSafeAreaPlaceholder && !this.zSlots.bottom;
  332. },
  333. renderJsIgnore() {
  334. if ((this.usePageScroll && this.useChatRecordMode) || (!this.refresherEnabled && this.scrollable) || !this.useCustomRefresher) {
  335. this.$nextTick(() => {
  336. this.renderPropScrollTop = 10;
  337. })
  338. }
  339. return 0;
  340. },
  341. windowHeight() {
  342. if (!this.systemInfo) return 0;
  343. return this.systemInfo.windowHeight || 0;
  344. },
  345. windowBottom() {
  346. if (!this.systemInfo) return 0;
  347. return this.systemInfo.windowBottom || 0;
  348. },
  349. // 是否是ios+h5
  350. isIosAndH5() {
  351. // #ifndef H5
  352. return false;
  353. // #endif
  354. return this.isIos;
  355. },
  356. // 是否是只使用基础布局或者只使用下拉刷新
  357. isOnly() {
  358. return this.layoutOnly || this.refresherOnly;
  359. },
  360. },
  361. methods: {
  362. // 当前版本号
  363. getVersion() {
  364. return `z-paging v${c.version}`;
  365. },
  366. // 设置nvue List的specialEffects
  367. setSpecialEffects(args) {
  368. this.setListSpecialEffects(args);
  369. },
  370. // 与setSpecialEffects等效,兼容旧版本
  371. setListSpecialEffects(args) {
  372. this.nFixFreezing = args && Object.keys(args).length;
  373. if (this.isIos) {
  374. this.privateRefresherEnabled = 0;
  375. }
  376. !this.usePageScroll && this.$refs['zp-n-list'].setSpecialEffects(args);
  377. },
  378. // #ifdef APP-VUE
  379. // 当app长时间进入后台后进入前台,因系统内存管理导致app重新加载时,进行一些适配处理
  380. _handlePageLaunch() {
  381. // 首次触发不进行处理,只有进入后台后打开app重新加载时才处理
  382. if (this.pageLaunched) {
  383. // 解决在vue3+ios中,app ReLaunch时顶部下拉刷新展示位置向下偏移的问题
  384. // #ifdef VUE3
  385. this.refresherThresholdUpdateTag = 1;
  386. this.$nextTick(() => {
  387. this.refresherThresholdUpdateTag = 0;
  388. })
  389. // #endif
  390. // 解决使用虚拟列表时,app ReLaunch时白屏问题
  391. this._checkVirtualListScroll();
  392. }
  393. this.pageLaunched = true;
  394. },
  395. // #endif
  396. // 使手机发生较短时间的振动(15ms)
  397. _doVibrateShort() {
  398. // #ifndef H5
  399. // #ifdef APP-PLUS
  400. if (this.isIos) {
  401. const UISelectionFeedbackGenerator = plus.ios.importClass('UISelectionFeedbackGenerator');
  402. const feedbackGenerator = new UISelectionFeedbackGenerator();
  403. feedbackGenerator.init();
  404. setTimeout(() => {
  405. feedbackGenerator.selectionChanged();
  406. }, 0)
  407. } else {
  408. plus.device.vibrate(15);
  409. }
  410. // #endif
  411. // #ifndef APP-PLUS
  412. uni.vibrateShort();
  413. // #endif
  414. // #endif
  415. },
  416. // 设置z-paging高度
  417. async _setAutoHeight(shouldFullHeight = true, scrollViewNode = null) {
  418. const heightKey = 'min-height';
  419. try {
  420. if (shouldFullHeight) {
  421. // 如果需要铺满全屏,则计算当前全屏可是区域的高度
  422. let finalScrollViewNode = scrollViewNode || await this._getNodeClientRect('.zp-scroll-view');
  423. let finalScrollBottomNode = await this._getNodeClientRect('.zp-page-bottom');
  424. if (finalScrollViewNode) {
  425. const scrollViewTop = finalScrollViewNode[0].top;
  426. let scrollViewHeight = this.windowHeight - scrollViewTop;
  427. scrollViewHeight -= finalScrollBottomNode ? finalScrollBottomNode[0].height : 0;
  428. const additionHeight = u.convertToPx(this.autoHeightAddition);
  429. // 在支付宝小程序中,添加!important会导致min-height失效,因此在支付宝小程序中需要去掉
  430. let importantSuffix = ' !important';
  431. // #ifdef MP-ALIPAY
  432. importantSuffix = '';
  433. // #endif
  434. const finalHeight = scrollViewHeight + additionHeight - (this.insideMore ? 1 : 0) + 'px' + importantSuffix;
  435. this.$set(this.scrollViewStyle, heightKey, finalHeight);
  436. this.$set(this.scrollViewInStyle, heightKey, finalHeight);
  437. }
  438. } else {
  439. this.$delete(this.scrollViewStyle, heightKey);
  440. this.$delete(this.scrollViewInStyle, heightKey);
  441. }
  442. } catch (e) {}
  443. },
  444. // 更新scroll-view高度
  445. async _updateScrollViewHeight() {
  446. const scrollViewNode = await this._getNodeClientRect('.zp-scroll-view');
  447. if (scrollViewNode) {
  448. const scrollViewNodeHeight = scrollViewNode[0].height;
  449. this.scrollViewHeight = scrollViewNodeHeight;
  450. this.pagingOrgTop = scrollViewNode[0].top;
  451. // 设置scroll-view内容器的最小高度等于scroll-view的高度(为了解决在快手小程序中内容较少时scroll-view内容器高度无法铺满scroll-view的问题)
  452. // #ifdef MP-KUAISHOU
  453. this.$set(this.scrollViewInStyle, 'min-height', scrollViewNodeHeight + 'px');
  454. // #endif
  455. }
  456. },
  457. // 组件销毁后续处理
  458. _handleUnmounted() {
  459. this.active = false;
  460. if (!this.layoutOnly) {
  461. this._offEmit();
  462. }
  463. // 取消监听键盘高度变化事件(H5、百度小程序、抖音小程序、飞书小程序、QQ小程序、快手小程序不支持)
  464. // #ifndef H5 || MP-BAIDU || MP-TOUTIAO || MP-QQ || MP-KUAISHOU
  465. this.useChatRecordMode && uni.offKeyboardHeightChange(this._handleKeyboardHeightChange);
  466. // #endif
  467. },
  468. // 触发更新是否超出页面状态
  469. _updateInsideOfPaging() {
  470. this.insideMore && this.insideOfPaging === true && setTimeout(this.doLoadMore, 200)
  471. },
  472. // 清除timeout
  473. _cleanTimeout(timeout) {
  474. if (timeout) {
  475. clearTimeout(timeout);
  476. timeout = null;
  477. }
  478. return timeout;
  479. },
  480. // 添加全局emit监听
  481. _onEmit() {
  482. uni.$on(c.errorUpdateKey, (errorMsg) => {
  483. if (this.loading) {
  484. if (!!errorMsg) {
  485. this.customerEmptyViewErrorText = errorMsg;
  486. }
  487. this.complete(false).catch(() => {});
  488. }
  489. })
  490. uni.$on(c.completeUpdateKey, (data) => {
  491. setTimeout(() => {
  492. if (this.loading) {
  493. if (!this.disabledCompleteEmit) {
  494. const type = data.type || 'normal';
  495. const list = data.list || data;
  496. const rule = data.rule;
  497. this.fromCompleteEmit = true;
  498. switch (type){
  499. case 'normal':
  500. this.complete(list);
  501. break;
  502. case 'total':
  503. this.completeByTotal(list, rule);
  504. break;
  505. case 'nomore':
  506. this.completeByNoMore(list, rule);
  507. break;
  508. case 'key':
  509. this.completeByKey(list, rule);
  510. break;
  511. default:
  512. break;
  513. }
  514. } else {
  515. this.disabledCompleteEmit = false;
  516. }
  517. }
  518. }, 1);
  519. })
  520. },
  521. // 销毁全局emit和listener监听
  522. _offEmit(){
  523. uni.$off(c.errorUpdateKey);
  524. uni.$off(c.completeUpdateKey);
  525. },
  526. },
  527. };