z-paging-utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // [z-paging]工具类
  2. import zConfig from './z-paging-config'
  3. import zLocalConfig from '../config/index'
  4. const storageKey = 'Z-PAGING-REFRESHER-TIME-STORAGE-KEY'
  5. let config = null;
  6. /*
  7. 当z-paging未使用uni_modules管理时,控制台会有警告:WARNING: Module not found: Error: Can't resolve '@/uni_modules/z-paging'...
  8. 此时注释下方try中的代码即可
  9. */
  10. // #ifdef VUE2
  11. try {
  12. const contextKeys = require.context('@/uni_modules/z-paging', false, /\z-paging-config$/).keys();
  13. if (contextKeys.length) {
  14. const suffix = '.js';
  15. config = require('@/uni_modules/z-paging/z-paging-config' + suffix);
  16. }
  17. } catch (e) {}
  18. // #endif
  19. //获取默认配置信息
  20. function gc(key, defaultValue) {
  21. if (!config) {
  22. if (zLocalConfig && Object.keys(zLocalConfig).length) {
  23. config = zLocalConfig;
  24. } else {
  25. const temConfig = zConfig.getConfig();
  26. if (zConfig && temConfig) {
  27. config = temConfig;
  28. }
  29. }
  30. }
  31. if (!config) return defaultValue;
  32. const value = config[_toKebab(key)];
  33. return value === undefined ? defaultValue : value;
  34. }
  35. //获取最终的touch位置
  36. function getTouch(e) {
  37. let touch = null;
  38. if (e.touches && e.touches.length) {
  39. touch = e.touches[0];
  40. } else if (e.changedTouches && e.changedTouches.length) {
  41. touch = e.changedTouches[0];
  42. } else if (e.datail && e.datail != {}) {
  43. touch = e.datail;
  44. } else {
  45. return {
  46. touchX: 0,
  47. touchY: 0
  48. }
  49. }
  50. return {
  51. touchX: touch.clientX,
  52. touchY: touch.clientY
  53. };
  54. }
  55. //判断当前手势是否在z-paging内触发
  56. function getTouchFromZPaging(target) {
  57. if (target && target.tagName && target.tagName !== 'BODY' && target.tagName !== 'UNI-PAGE-BODY') {
  58. const classList = target.classList;
  59. if (classList && classList.contains('z-paging-content')) {
  60. return {
  61. isFromZp: true,
  62. isPageScroll: classList.contains('z-paging-content-page'),
  63. isReachedTop: classList.contains('z-paging-reached-top')
  64. };
  65. } else {
  66. return getTouchFromZPaging(target.parentNode);
  67. }
  68. } else {
  69. return {isFromZp: false};
  70. }
  71. }
  72. //获取z-paging所在的parent
  73. function getParent(parent) {
  74. if (!parent) return null;
  75. if (parent.$refs.paging) return parent;
  76. return getParent(parent.$parent);
  77. }
  78. //打印错误信息
  79. function consoleErr(err) {
  80. console.error(`[z-paging]${err}`);
  81. }
  82. //设置下拉刷新时间
  83. function setRefesrherTime(time, key) {
  84. const datas = getRefesrherTime() || {};
  85. datas[key] = time;
  86. uni.setStorageSync(storageKey, datas);
  87. }
  88. //获取下拉刷新时间
  89. function getRefesrherTime() {
  90. return uni.getStorageSync(storageKey);
  91. }
  92. //通过下拉刷新标识key获取下拉刷新时间
  93. function getRefesrherTimeByKey(key) {
  94. const datas = getRefesrherTime();
  95. return datas && datas[key] ? datas[key] : null;
  96. }
  97. //通过下拉刷新标识key获取下拉刷新时间(格式化之后)
  98. function getRefesrherFormatTimeByKey(key, textMap) {
  99. const time = getRefesrherTimeByKey(key);
  100. const timeText = time ? _timeFormat(time, textMap) : textMap.none;
  101. return `${textMap.title}${timeText}`;
  102. }
  103. //将文本的px或者rpx转为px的值
  104. function convertTextToPx(text) {
  105. const dataType = Object.prototype.toString.call(text);
  106. if (dataType === '[object Number]') return text;
  107. let isRpx = false;
  108. if (text.indexOf('rpx') !== -1 || text.indexOf('upx') !== -1) {
  109. text = text.replace('rpx', '').replace('upx', '');
  110. isRpx = true;
  111. } else if (text.indexOf('px') !== -1) {
  112. text = text.replace('px', '');
  113. }
  114. if (!isNaN(text)) {
  115. if (isRpx) return Number(uni.upx2px(text));
  116. return Number(text);
  117. }
  118. return 0;
  119. }
  120. //获取当前时间
  121. function getTime() {
  122. return (new Date()).getTime();
  123. }
  124. //获取z-paging实例id
  125. function getInstanceId() {
  126. const s = [];
  127. const hexDigits = "0123456789abcdef";
  128. for (let i = 0; i < 10; i++) {
  129. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  130. }
  131. return s.join('') + getTime();
  132. }
  133. //------------------ 私有方法 ------------------------
  134. //时间格式化
  135. function _timeFormat(time, textMap) {
  136. const date = new Date(time);
  137. const currentDate = new Date();
  138. const dateDay = new Date(time).setHours(0, 0, 0, 0);
  139. const currentDateDay = new Date().setHours(0, 0, 0, 0);
  140. const disTime = dateDay - currentDateDay;
  141. let dayStr = '';
  142. const timeStr = _dateTimeFormat(date);
  143. if (disTime === 0) {
  144. dayStr = textMap.today;
  145. } else if (disTime === -86400000) {
  146. dayStr = textMap.yesterday;
  147. } else {
  148. dayStr = _dateDayFormat(date, date.getFullYear() !== currentDate.getFullYear());
  149. }
  150. return `${dayStr} ${timeStr}`;
  151. }
  152. //date格式化为年月日
  153. function _dateDayFormat(date, showYear = true) {
  154. const year = date.getFullYear();
  155. const month = date.getMonth() + 1;
  156. const day = date.getDate();
  157. return showYear ? `${year}-${_fullZeroToTwo(month)}-${_fullZeroToTwo(day)}` : `${_fullZeroToTwo(month)}-${_fullZeroToTwo(day)}`;
  158. }
  159. //data格式化为时分
  160. function _dateTimeFormat(date) {
  161. const hour = date.getHours();
  162. const minute = date.getMinutes();
  163. return `${_fullZeroToTwo(hour)}:${_fullZeroToTwo(minute)}`;
  164. }
  165. //不满2位在前面填充0
  166. function _fullZeroToTwo(str) {
  167. str = str.toString();
  168. return str.length === 1 ? '0' + str : str;
  169. }
  170. //驼峰转短横线
  171. function _toKebab(value) {
  172. return value.replace(/([A-Z])/g, "-$1").toLowerCase();
  173. }
  174. export default {
  175. gc,
  176. setRefesrherTime,
  177. getRefesrherFormatTimeByKey,
  178. getTouch,
  179. getTouchFromZPaging,
  180. getParent,
  181. convertTextToPx,
  182. getTime,
  183. getInstanceId,
  184. consoleErr
  185. };