index.js 14 KB

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