z-paging-utils.js 5.9 KB

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