index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. "use strict";
  2. var common_vendor = require("../../../../common/vendor.js");
  3. var uni_modules_uviewPlus_libs_function_test = require("./test.js");
  4. var uni_modules_uviewPlus_libs_function_digit = require("./digit.js");
  5. var 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\u63D0\u793A\uFF1A${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("fillString must be String");
  180. }
  181. const str = this;
  182. if (str.length >= maxLength)
  183. return String(str);
  184. const fillLength = maxLength - str.length;
  185. let times = Math.ceil(fillLength / fillString.length);
  186. while (times >>= 1) {
  187. fillString += fillString;
  188. if (times === 1) {
  189. fillString += fillString;
  190. }
  191. }
  192. return fillString.slice(0, fillLength) + str;
  193. };
  194. }
  195. function timeFormat(dateTime = null, formatStr = "yyyy-mm-dd") {
  196. let date;
  197. if (!dateTime) {
  198. date = new Date();
  199. } else if (/^\d{10}$/.test(dateTime.toString().trim())) {
  200. date = new Date(dateTime * 1e3);
  201. } else if (typeof dateTime === "string" && /^\d+$/.test(dateTime.trim())) {
  202. date = new Date(Number(dateTime));
  203. } else {
  204. date = new Date(typeof dateTime === "string" ? dateTime.replace(/-/g, "/") : dateTime);
  205. }
  206. const timeSource = {
  207. "y": date.getFullYear().toString(),
  208. "m": (date.getMonth() + 1).toString().padStart(2, "0"),
  209. "d": date.getDate().toString().padStart(2, "0"),
  210. "h": date.getHours().toString().padStart(2, "0"),
  211. "M": date.getMinutes().toString().padStart(2, "0"),
  212. "s": date.getSeconds().toString().padStart(2, "0")
  213. };
  214. for (const key in timeSource) {
  215. const [ret] = new RegExp(`${key}+`).exec(formatStr) || [];
  216. if (ret) {
  217. const beginIndex = key === "y" && ret.length === 2 ? 2 : 0;
  218. formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex));
  219. }
  220. }
  221. return formatStr;
  222. }
  223. function timeFrom(timestamp = null, format = "yyyy-mm-dd") {
  224. if (timestamp == null)
  225. timestamp = Number(new Date());
  226. timestamp = parseInt(timestamp);
  227. if (timestamp.toString().length == 10)
  228. timestamp *= 1e3;
  229. let timer = new Date().getTime() - timestamp;
  230. timer = parseInt(timer / 1e3);
  231. let tips = "";
  232. switch (true) {
  233. case timer < 300:
  234. tips = "\u521A\u521A";
  235. break;
  236. case (timer >= 300 && timer < 3600):
  237. tips = `${parseInt(timer / 60)}\u5206\u949F\u524D`;
  238. break;
  239. case (timer >= 3600 && timer < 86400):
  240. tips = `${parseInt(timer / 3600)}\u5C0F\u65F6\u524D`;
  241. break;
  242. case (timer >= 86400 && timer < 2592e3):
  243. tips = `${parseInt(timer / 86400)}\u5929\u524D`;
  244. break;
  245. default:
  246. if (format === false) {
  247. if (timer >= 2592e3 && timer < 365 * 86400) {
  248. tips = `${parseInt(timer / (86400 * 30))}\u4E2A\u6708\u524D`;
  249. } else {
  250. tips = `${parseInt(timer / (86400 * 365))}\u5E74\u524D`;
  251. }
  252. } else {
  253. tips = timeFormat(timestamp, format);
  254. }
  255. }
  256. return tips;
  257. }
  258. function trim(str, pos = "both") {
  259. str = String(str);
  260. if (pos == "both") {
  261. return str.replace(/^\s+|\s+$/g, "");
  262. }
  263. if (pos == "left") {
  264. return str.replace(/^\s*/, "");
  265. }
  266. if (pos == "right") {
  267. return str.replace(/(\s*$)/g, "");
  268. }
  269. if (pos == "all") {
  270. return str.replace(/\s+/g, "");
  271. }
  272. return str;
  273. }
  274. function queryParams(data = {}, isPrefix = true, arrayFormat = "brackets") {
  275. const prefix = isPrefix ? "?" : "";
  276. const _result = [];
  277. if (["indices", "brackets", "repeat", "comma"].indexOf(arrayFormat) == -1)
  278. arrayFormat = "brackets";
  279. for (const key in data) {
  280. const value = data[key];
  281. if (["", void 0, null].indexOf(value) >= 0) {
  282. continue;
  283. }
  284. if (value.constructor === Array) {
  285. switch (arrayFormat) {
  286. case "indices":
  287. for (let i = 0; i < value.length; i++) {
  288. _result.push(`${key}[${i}]=${value[i]}`);
  289. }
  290. break;
  291. case "brackets":
  292. value.forEach((_value) => {
  293. _result.push(`${key}[]=${_value}`);
  294. });
  295. break;
  296. case "repeat":
  297. value.forEach((_value) => {
  298. _result.push(`${key}=${_value}`);
  299. });
  300. break;
  301. case "comma":
  302. let commaStr = "";
  303. value.forEach((_value) => {
  304. commaStr += (commaStr ? "," : "") + _value;
  305. });
  306. _result.push(`${key}=${commaStr}`);
  307. break;
  308. default:
  309. value.forEach((_value) => {
  310. _result.push(`${key}[]=${_value}`);
  311. });
  312. }
  313. } else {
  314. _result.push(`${key}=${value}`);
  315. }
  316. }
  317. return _result.length ? prefix + _result.join("&") : "";
  318. }
  319. function toast(title, duration = 2e3) {
  320. common_vendor.index.showToast({
  321. title: String(title),
  322. icon: "none",
  323. duration
  324. });
  325. }
  326. function type2icon(type = "success", fill = false) {
  327. if (["primary", "info", "error", "warning", "success"].indexOf(type) == -1)
  328. type = "success";
  329. let iconName = "";
  330. switch (type) {
  331. case "primary":
  332. iconName = "info-circle";
  333. break;
  334. case "info":
  335. iconName = "info-circle";
  336. break;
  337. case "error":
  338. iconName = "close-circle";
  339. break;
  340. case "warning":
  341. iconName = "error-circle";
  342. break;
  343. case "success":
  344. iconName = "checkmark-circle";
  345. break;
  346. default:
  347. iconName = "checkmark-circle";
  348. }
  349. if (fill)
  350. iconName += "-fill";
  351. return iconName;
  352. }
  353. function priceFormat(number, decimals = 0, decimalPoint = ".", thousandsSeparator = ",") {
  354. number = `${number}`.replace(/[^0-9+-Ee.]/g, "");
  355. const n = !isFinite(+number) ? 0 : +number;
  356. const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
  357. const sep = typeof thousandsSeparator === "undefined" ? "," : thousandsSeparator;
  358. const dec = typeof decimalPoint === "undefined" ? "." : decimalPoint;
  359. let s = "";
  360. s = (prec ? uni_modules_uviewPlus_libs_function_digit.round(n, prec) + "" : `${Math.round(n)}`).split(".");
  361. const re = /(-?\d+)(\d{3})/;
  362. while (re.test(s[0])) {
  363. s[0] = s[0].replace(re, `$1${sep}$2`);
  364. }
  365. if ((s[1] || "").length < prec) {
  366. s[1] = s[1] || "";
  367. s[1] += new Array(prec - s[1].length + 1).join("0");
  368. }
  369. return s.join(dec);
  370. }
  371. function getDuration(value, unit = true) {
  372. const valueNum = parseInt(value);
  373. if (unit) {
  374. if (/s$/.test(value))
  375. return value;
  376. return value > 30 ? `${value}ms` : `${value}s`;
  377. }
  378. if (/ms$/.test(value))
  379. return valueNum;
  380. if (/s$/.test(value))
  381. return valueNum > 30 ? valueNum : valueNum * 1e3;
  382. return valueNum;
  383. }
  384. function padZero(value) {
  385. return `00${value}`.slice(-2);
  386. }
  387. function formValidate(instance, event) {
  388. const formItem = $parent.call(instance, "u-form-item");
  389. const form = $parent.call(instance, "u-form");
  390. if (formItem && form) {
  391. form.validateField(formItem.prop, () => {
  392. }, event);
  393. }
  394. }
  395. function getProperty(obj, key) {
  396. if (typeof obj !== "object" || obj == null) {
  397. return "";
  398. }
  399. if (typeof key !== "string" || key === "") {
  400. return "";
  401. }
  402. if (key.indexOf(".") !== -1) {
  403. const keys = key.split(".");
  404. let firstObj = obj[keys[0]] || {};
  405. for (let i = 1; i < keys.length; i++) {
  406. if (firstObj) {
  407. firstObj = firstObj[keys[i]];
  408. }
  409. }
  410. return firstObj;
  411. }
  412. return obj[key];
  413. }
  414. function setProperty(obj, key, value) {
  415. if (typeof obj !== "object" || obj == null) {
  416. return;
  417. }
  418. const inFn = function(_obj, keys, v) {
  419. if (keys.length === 1) {
  420. _obj[keys[0]] = v;
  421. return;
  422. }
  423. while (keys.length > 1) {
  424. const k = keys[0];
  425. if (!_obj[k] || typeof _obj[k] !== "object") {
  426. _obj[k] = {};
  427. }
  428. keys.shift();
  429. inFn(_obj[k], keys, v);
  430. }
  431. };
  432. if (typeof key !== "string" || key === "")
  433. ;
  434. else if (key.indexOf(".") !== -1) {
  435. const keys = key.split(".");
  436. inFn(obj, keys, value);
  437. } else {
  438. obj[key] = value;
  439. }
  440. }
  441. function page() {
  442. const pages2 = getCurrentPages();
  443. return `/${pages2[pages2.length - 1].route || ""}`;
  444. }
  445. function pages() {
  446. const pages2 = getCurrentPages();
  447. return pages2;
  448. }
  449. var index = {
  450. range,
  451. getPx,
  452. sleep,
  453. os,
  454. sys,
  455. random,
  456. guid,
  457. $parent,
  458. addStyle,
  459. addUnit,
  460. deepClone,
  461. deepMerge,
  462. shallowMerge,
  463. error,
  464. randomArray,
  465. timeFormat,
  466. timeFrom,
  467. trim,
  468. queryParams,
  469. toast,
  470. type2icon,
  471. priceFormat,
  472. getDuration,
  473. padZero,
  474. formValidate,
  475. getProperty,
  476. setProperty,
  477. page,
  478. pages
  479. };
  480. exports.$parent = $parent;
  481. exports.addStyle = addStyle;
  482. exports.addUnit = addUnit;
  483. exports.deepMerge = deepMerge;
  484. exports.error = error;
  485. exports.formValidate = formValidate;
  486. exports.getPx = getPx;
  487. exports.guid = guid;
  488. exports.index = index;
  489. exports.os = os;
  490. exports.page = page;
  491. exports.priceFormat = priceFormat;
  492. exports.queryParams = queryParams;
  493. exports.random = random;
  494. exports.sleep = sleep;
  495. exports.sys = sys;
  496. exports.timeFormat = timeFormat;
  497. exports.toast = toast;