index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const uni_modules_uviewPlus_libs_function_test = require("./test.js");
  4. const uni_modules_uviewPlus_libs_function_digit = require("./digit.js");
  5. function range(min = 0, max = 0, value = 0) {
  6. return Math.max(min, Math.min(max, Number(value)));
  7. }
  8. function getPx(value, unit = false) {
  9. if (uni_modules_uviewPlus_libs_function_test.test.number(value)) {
  10. return unit ? `${value}px` : Number(value);
  11. }
  12. if (/(rpx|upx)$/.test(value)) {
  13. return unit ? `${common_vendor.index.upx2px(parseInt(value))}px` : Number(common_vendor.index.upx2px(parseInt(value)));
  14. }
  15. return unit ? `${parseInt(value)}px` : parseInt(value);
  16. }
  17. function sleep(value = 30) {
  18. return new Promise((resolve) => {
  19. setTimeout(() => {
  20. resolve();
  21. }, value);
  22. });
  23. }
  24. function os() {
  25. return common_vendor.index.getSystemInfoSync().platform.toLowerCase();
  26. }
  27. function sys() {
  28. return common_vendor.index.getSystemInfoSync();
  29. }
  30. function random(min, max) {
  31. if (min >= 0 && max > 0 && max >= min) {
  32. const gab = max - min + 1;
  33. return Math.floor(Math.random() * gab + min);
  34. }
  35. return 0;
  36. }
  37. function guid(len = 32, firstU = true, radix = null) {
  38. const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
  39. const uuid = [];
  40. radix = radix || chars.length;
  41. if (len) {
  42. for (let i = 0; i < len; i++)
  43. uuid[i] = chars[0 | Math.random() * radix];
  44. } else {
  45. let r;
  46. uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
  47. uuid[14] = "4";
  48. for (let i = 0; i < 36; i++) {
  49. if (!uuid[i]) {
  50. r = 0 | Math.random() * 16;
  51. uuid[i] = chars[i == 19 ? r & 3 | 8 : r];
  52. }
  53. }
  54. }
  55. if (firstU) {
  56. uuid.shift();
  57. return `u${uuid.join("")}`;
  58. }
  59. return uuid.join("");
  60. }
  61. function $parent(name = void 0) {
  62. let parent = this.$parent;
  63. while (parent) {
  64. if (parent.$options && parent.$options.name !== name) {
  65. parent = parent.$parent;
  66. } else {
  67. return parent;
  68. }
  69. }
  70. return false;
  71. }
  72. function addStyle(customStyle, target = "object") {
  73. if (uni_modules_uviewPlus_libs_function_test.test.empty(customStyle) || typeof customStyle === "object" && target === "object" || target === "string" && typeof customStyle === "string") {
  74. return customStyle;
  75. }
  76. if (target === "object") {
  77. customStyle = trim(customStyle);
  78. const styleArray = customStyle.split(";");
  79. const style = {};
  80. for (let i = 0; i < styleArray.length; i++) {
  81. if (styleArray[i]) {
  82. const item = styleArray[i].split(":");
  83. style[trim(item[0])] = trim(item[1]);
  84. }
  85. }
  86. return style;
  87. }
  88. let string = "";
  89. for (const i in customStyle) {
  90. const key = i.replace(/([A-Z])/g, "-$1").toLowerCase();
  91. string += `${key}:${customStyle[i]};`;
  92. }
  93. return trim(string);
  94. }
  95. function addUnit(value = "auto", unit = "") {
  96. if (!unit) {
  97. unit = common_vendor.index.$u.config.unit || "px";
  98. }
  99. value = String(value);
  100. return uni_modules_uviewPlus_libs_function_test.test.number(value) ? `${value}${unit}` : value;
  101. }
  102. function deepClone(obj) {
  103. if ([null, void 0, NaN, false].includes(obj))
  104. return obj;
  105. if (typeof obj !== "object" && typeof obj !== "function") {
  106. return obj;
  107. }
  108. const o = uni_modules_uviewPlus_libs_function_test.test.array(obj) ? [] : {};
  109. for (const i in obj) {
  110. if (obj.hasOwnProperty(i)) {
  111. o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
  112. }
  113. }
  114. return o;
  115. }
  116. function deepMerge(target = {}, source = {}) {
  117. target = deepClone(target);
  118. if (typeof target !== "object" || typeof source !== "object")
  119. return false;
  120. for (const prop in source) {
  121. if (!source.hasOwnProperty(prop))
  122. continue;
  123. if (prop in target) {
  124. if (typeof target[prop] !== "object") {
  125. target[prop] = source[prop];
  126. } else if (typeof source[prop] !== "object") {
  127. target[prop] = source[prop];
  128. } else if (target[prop].concat && source[prop].concat) {
  129. target[prop] = target[prop].concat(source[prop]);
  130. } else {
  131. target[prop] = deepMerge(target[prop], source[prop]);
  132. }
  133. } else {
  134. target[prop] = source[prop];
  135. }
  136. }
  137. return target;
  138. }
  139. function error(err) {
  140. {
  141. console.error(`uView提示:${err}`);
  142. }
  143. }
  144. function randomArray(array = []) {
  145. return array.sort(() => Math.random() - 0.5);
  146. }
  147. if (!String.prototype.padStart) {
  148. String.prototype.padStart = function(maxLength, fillString = " ") {
  149. if (Object.prototype.toString.call(fillString) !== "[object String]") {
  150. throw new TypeError(
  151. "fillString must be String"
  152. );
  153. }
  154. const str = this;
  155. if (str.length >= maxLength)
  156. return String(str);
  157. const fillLength = maxLength - str.length;
  158. let times = Math.ceil(fillLength / fillString.length);
  159. while (times >>= 1) {
  160. fillString += fillString;
  161. if (times === 1) {
  162. fillString += fillString;
  163. }
  164. }
  165. return fillString.slice(0, fillLength) + str;
  166. };
  167. }
  168. function timeFormat(dateTime = null, formatStr = "yyyy-mm-dd") {
  169. let date;
  170. if (!dateTime) {
  171. date = /* @__PURE__ */ new Date();
  172. } else if (/^\d{10}$/.test(dateTime.toString().trim())) {
  173. date = new Date(dateTime * 1e3);
  174. } else if (typeof dateTime === "string" && /^\d+$/.test(dateTime.trim())) {
  175. date = new Date(Number(dateTime));
  176. } else {
  177. date = new Date(
  178. typeof dateTime === "string" ? dateTime.replace(/-/g, "/") : dateTime
  179. );
  180. }
  181. const timeSource = {
  182. "y": date.getFullYear().toString(),
  183. // 年
  184. "m": (date.getMonth() + 1).toString().padStart(2, "0"),
  185. // 月
  186. "d": date.getDate().toString().padStart(2, "0"),
  187. // 日
  188. "h": date.getHours().toString().padStart(2, "0"),
  189. // 时
  190. "M": date.getMinutes().toString().padStart(2, "0"),
  191. // 分
  192. "s": date.getSeconds().toString().padStart(2, "0")
  193. // 秒
  194. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  195. };
  196. for (const key in timeSource) {
  197. const [ret] = new RegExp(`${key}+`).exec(formatStr) || [];
  198. if (ret) {
  199. const beginIndex = key === "y" && ret.length === 2 ? 2 : 0;
  200. formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex));
  201. }
  202. }
  203. return formatStr;
  204. }
  205. function timeFrom(timestamp = null, format = "yyyy-mm-dd") {
  206. if (timestamp == null)
  207. timestamp = Number(/* @__PURE__ */ new Date());
  208. timestamp = parseInt(timestamp);
  209. if (timestamp.toString().length == 10)
  210. timestamp *= 1e3;
  211. let timer = (/* @__PURE__ */ new Date()).getTime() - timestamp;
  212. timer = parseInt(timer / 1e3);
  213. let tips = "";
  214. switch (true) {
  215. case timer < 300:
  216. tips = "刚刚";
  217. break;
  218. case (timer >= 300 && timer < 3600):
  219. tips = `${parseInt(timer / 60)}分钟前`;
  220. break;
  221. case (timer >= 3600 && timer < 86400):
  222. tips = `${parseInt(timer / 3600)}小时前`;
  223. break;
  224. case (timer >= 86400 && timer < 2592e3):
  225. tips = `${parseInt(timer / 86400)}天前`;
  226. break;
  227. default:
  228. if (format === false) {
  229. if (timer >= 2592e3 && timer < 365 * 86400) {
  230. tips = `${parseInt(timer / (86400 * 30))}个月前`;
  231. } else {
  232. tips = `${parseInt(timer / (86400 * 365))}年前`;
  233. }
  234. } else {
  235. tips = timeFormat(timestamp, format);
  236. }
  237. }
  238. return tips;
  239. }
  240. function trim(str, pos = "both") {
  241. str = String(str);
  242. if (pos == "both") {
  243. return str.replace(/^\s+|\s+$/g, "");
  244. }
  245. if (pos == "left") {
  246. return str.replace(/^\s*/, "");
  247. }
  248. if (pos == "right") {
  249. return str.replace(/(\s*$)/g, "");
  250. }
  251. if (pos == "all") {
  252. return str.replace(/\s+/g, "");
  253. }
  254. return str;
  255. }
  256. function queryParams(data = {}, isPrefix = true, arrayFormat = "brackets") {
  257. const prefix = isPrefix ? "?" : "";
  258. const _result = [];
  259. if (["indices", "brackets", "repeat", "comma"].indexOf(arrayFormat) == -1)
  260. arrayFormat = "brackets";
  261. for (const key in data) {
  262. const value = data[key];
  263. if (["", void 0, null].indexOf(value) >= 0) {
  264. continue;
  265. }
  266. if (value.constructor === Array) {
  267. switch (arrayFormat) {
  268. case "indices":
  269. for (let i = 0; i < value.length; i++) {
  270. _result.push(`${key}[${i}]=${value[i]}`);
  271. }
  272. break;
  273. case "brackets":
  274. value.forEach((_value) => {
  275. _result.push(`${key}[]=${_value}`);
  276. });
  277. break;
  278. case "repeat":
  279. value.forEach((_value) => {
  280. _result.push(`${key}=${_value}`);
  281. });
  282. break;
  283. case "comma":
  284. let commaStr = "";
  285. value.forEach((_value) => {
  286. commaStr += (commaStr ? "," : "") + _value;
  287. });
  288. _result.push(`${key}=${commaStr}`);
  289. break;
  290. default:
  291. value.forEach((_value) => {
  292. _result.push(`${key}[]=${_value}`);
  293. });
  294. }
  295. } else {
  296. _result.push(`${key}=${value}`);
  297. }
  298. }
  299. return _result.length ? prefix + _result.join("&") : "";
  300. }
  301. function toast(title, duration = 2e3) {
  302. common_vendor.index.showToast({
  303. title: String(title),
  304. icon: "none",
  305. duration
  306. });
  307. }
  308. function type2icon(type = "success", fill = false) {
  309. if (["primary", "info", "error", "warning", "success"].indexOf(type) == -1)
  310. type = "success";
  311. let iconName = "";
  312. switch (type) {
  313. case "primary":
  314. iconName = "info-circle";
  315. break;
  316. case "info":
  317. iconName = "info-circle";
  318. break;
  319. case "error":
  320. iconName = "close-circle";
  321. break;
  322. case "warning":
  323. iconName = "error-circle";
  324. break;
  325. case "success":
  326. iconName = "checkmark-circle";
  327. break;
  328. default:
  329. iconName = "checkmark-circle";
  330. }
  331. if (fill)
  332. iconName += "-fill";
  333. return iconName;
  334. }
  335. function priceFormat(number, decimals = 0, decimalPoint = ".", thousandsSeparator = ",") {
  336. number = `${number}`.replace(/[^0-9+-Ee.]/g, "");
  337. const n = !isFinite(+number) ? 0 : +number;
  338. const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
  339. const sep = typeof thousandsSeparator === "undefined" ? "," : thousandsSeparator;
  340. const dec = typeof decimalPoint === "undefined" ? "." : decimalPoint;
  341. let s = "";
  342. s = (prec ? uni_modules_uviewPlus_libs_function_digit.round(n, prec) + "" : `${Math.round(n)}`).split(".");
  343. const re = /(-?\d+)(\d{3})/;
  344. while (re.test(s[0])) {
  345. s[0] = s[0].replace(re, `$1${sep}$2`);
  346. }
  347. if ((s[1] || "").length < prec) {
  348. s[1] = s[1] || "";
  349. s[1] += new Array(prec - s[1].length + 1).join("0");
  350. }
  351. return s.join(dec);
  352. }
  353. function getDuration(value, unit = true) {
  354. const valueNum = parseInt(value);
  355. if (unit) {
  356. if (/s$/.test(value))
  357. return value;
  358. return value > 30 ? `${value}ms` : `${value}s`;
  359. }
  360. if (/ms$/.test(value))
  361. return valueNum;
  362. if (/s$/.test(value))
  363. return valueNum > 30 ? valueNum : valueNum * 1e3;
  364. return valueNum;
  365. }
  366. function padZero(value) {
  367. return `00${value}`.slice(-2);
  368. }
  369. function formValidate(instance, event) {
  370. const formItem = common_vendor.index.$u.$parent.call(instance, "u-form-item");
  371. const form = common_vendor.index.$u.$parent.call(instance, "u-form");
  372. if (formItem && form) {
  373. form.validateField(formItem.prop, () => {
  374. }, event);
  375. }
  376. }
  377. function getProperty(obj, key) {
  378. if (!obj) {
  379. return;
  380. }
  381. if (typeof key !== "string" || key === "") {
  382. return "";
  383. }
  384. if (key.indexOf(".") !== -1) {
  385. const keys = key.split(".");
  386. let firstObj = obj[keys[0]] || {};
  387. for (let i = 1; i < keys.length; i++) {
  388. if (firstObj) {
  389. firstObj = firstObj[keys[i]];
  390. }
  391. }
  392. return firstObj;
  393. }
  394. return obj[key];
  395. }
  396. function setProperty(obj, key, value) {
  397. if (!obj) {
  398. return;
  399. }
  400. const inFn = function(_obj, keys, v) {
  401. if (keys.length === 1) {
  402. _obj[keys[0]] = v;
  403. return;
  404. }
  405. while (keys.length > 1) {
  406. const k = keys[0];
  407. if (!_obj[k] || typeof _obj[k] !== "object") {
  408. _obj[k] = {};
  409. }
  410. keys.shift();
  411. inFn(_obj[k], keys, v);
  412. }
  413. };
  414. if (typeof key !== "string" || key === "")
  415. ;
  416. else if (key.indexOf(".") !== -1) {
  417. const keys = key.split(".");
  418. inFn(obj, keys, value);
  419. } else {
  420. obj[key] = value;
  421. }
  422. }
  423. function page() {
  424. const pages2 = getCurrentPages();
  425. return `/${pages2[pages2.length - 1].route || ""}`;
  426. }
  427. function pages() {
  428. const pages2 = getCurrentPages();
  429. return pages2;
  430. }
  431. function setConfig({
  432. props = {},
  433. config = {},
  434. color = {},
  435. zIndex = {}
  436. }) {
  437. const {
  438. deepMerge: deepMerge2
  439. } = common_vendor.index.$u;
  440. common_vendor.index.$u.config = deepMerge2(common_vendor.index.$u.config, config);
  441. common_vendor.index.$u.props = deepMerge2(common_vendor.index.$u.props, props);
  442. common_vendor.index.$u.color = deepMerge2(common_vendor.index.$u.color, color);
  443. common_vendor.index.$u.zIndex = deepMerge2(common_vendor.index.$u.zIndex, zIndex);
  444. }
  445. const index = {
  446. range,
  447. getPx,
  448. sleep,
  449. os,
  450. sys,
  451. random,
  452. guid,
  453. $parent,
  454. addStyle,
  455. addUnit,
  456. deepClone,
  457. deepMerge,
  458. error,
  459. randomArray,
  460. timeFormat,
  461. timeFrom,
  462. trim,
  463. queryParams,
  464. toast,
  465. type2icon,
  466. priceFormat,
  467. getDuration,
  468. padZero,
  469. formValidate,
  470. getProperty,
  471. setProperty,
  472. page,
  473. pages,
  474. setConfig
  475. };
  476. exports.index = index;