index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 getPx(value, unit = false) {
  7. if (uni_modules_uviewPlus_libs_function_test.number(value)) {
  8. return unit ? `${value}px` : Number(value);
  9. }
  10. if (/(rpx|upx)$/.test(value)) {
  11. return unit ? `${common_vendor.index.rpx2px(parseInt(value))}px` : Number(common_vendor.index.rpx2px(parseInt(value)));
  12. }
  13. return unit ? `${parseInt(value)}px` : parseInt(value);
  14. }
  15. function sleep(value = 30) {
  16. return new Promise((resolve) => {
  17. setTimeout(() => {
  18. resolve();
  19. }, value);
  20. });
  21. }
  22. function os() {
  23. const windowInfo = common_vendor.index.getWindowInfo() || {};
  24. const platform = windowInfo.platform || "";
  25. return String(platform).toLowerCase();
  26. }
  27. function sys() {
  28. return common_vendor.index.getWindowInfo();
  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.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. if (typeof customStyle === "object") {
  90. customStyle.forEach((val, i) => {
  91. const key = i.replace(/([A-Z])/g, "-$1").toLowerCase();
  92. string += `${key}:${val};`;
  93. });
  94. }
  95. return trim(string);
  96. }
  97. function addUnit(value = "auto", unit = "") {
  98. if (!unit) {
  99. unit = uni_modules_uviewPlus_libs_config_config.config.unit;
  100. }
  101. value = String(value);
  102. return uni_modules_uviewPlus_libs_function_test.number(value) ? `${value}${unit}` : value;
  103. }
  104. function deepClone(obj) {
  105. if ([null, void 0, NaN, false].includes(obj))
  106. return obj;
  107. if (typeof obj !== "object" && typeof obj !== "function") {
  108. return obj;
  109. }
  110. const o = uni_modules_uviewPlus_libs_function_test.array(obj) ? [] : {};
  111. for (const i in obj) {
  112. if (obj.hasOwnProperty(i)) {
  113. o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
  114. }
  115. }
  116. return o;
  117. }
  118. function deepMerge(targetOrigin = {}, source = {}) {
  119. let target = deepClone(targetOrigin);
  120. if (typeof target !== "object" || typeof source !== "object")
  121. return false;
  122. for (const prop in source) {
  123. if (!source.hasOwnProperty(prop))
  124. continue;
  125. if (prop in target) {
  126. if (source[prop] == null) {
  127. target[prop] = source[prop];
  128. } else if (typeof target[prop] !== "object") {
  129. target[prop] = source[prop];
  130. } else if (typeof source[prop] !== "object") {
  131. target[prop] = source[prop];
  132. } else if (target[prop].concat && source[prop].concat) {
  133. target[prop] = target[prop].concat(source[prop]);
  134. } else {
  135. target[prop] = deepMerge(target[prop], source[prop]);
  136. }
  137. } else {
  138. target[prop] = source[prop];
  139. }
  140. }
  141. return target;
  142. }
  143. function error(err) {
  144. {
  145. console.error(`uView提示:${err}`);
  146. }
  147. }
  148. if (!String.prototype.padStart) {
  149. String.prototype.padStart = function(maxLength, fillString = " ") {
  150. if (Object.prototype.toString.call(fillString) !== "[object String]") {
  151. throw new TypeError(
  152. "fillString must be String"
  153. );
  154. }
  155. const str = this;
  156. if (str.length >= maxLength)
  157. return String(str);
  158. const fillLength = maxLength - str.length;
  159. let times = Math.ceil(fillLength / fillString.length);
  160. while (times >>= 1) {
  161. fillString += fillString;
  162. if (times === 1) {
  163. fillString += fillString;
  164. }
  165. }
  166. return fillString.slice(0, fillLength) + str;
  167. };
  168. }
  169. function timeFormat(dateTime = null, formatStr = "yyyy-mm-dd") {
  170. let date;
  171. if (!dateTime) {
  172. date = /* @__PURE__ */ new Date();
  173. } else if (/^\d{10}$/.test(dateTime.toString().trim())) {
  174. date = new Date(dateTime * 1e3);
  175. } else if (typeof dateTime === "string" && /^\d+$/.test(dateTime.trim())) {
  176. date = new Date(Number(dateTime));
  177. } else {
  178. date = new Date(
  179. typeof dateTime === "string" ? dateTime.replace(/-/g, "/") : dateTime
  180. );
  181. }
  182. const timeSource = {
  183. "y": date.getFullYear().toString(),
  184. // 年
  185. "m": (date.getMonth() + 1).toString().padStart(2, "0"),
  186. // 月
  187. "d": date.getDate().toString().padStart(2, "0"),
  188. // 日
  189. "h": date.getHours().toString().padStart(2, "0"),
  190. // 时
  191. "M": date.getMinutes().toString().padStart(2, "0"),
  192. // 分
  193. "s": date.getSeconds().toString().padStart(2, "0")
  194. // 秒
  195. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  196. };
  197. for (const key in timeSource) {
  198. const [ret] = new RegExp(`${key}+`).exec(formatStr) || [];
  199. if (ret) {
  200. const beginIndex = key === "y" && ret.length === 2 ? 2 : 0;
  201. formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex));
  202. }
  203. }
  204. return formatStr;
  205. }
  206. function trim(str, pos = "both") {
  207. str = String(str);
  208. if (pos == "both") {
  209. return str.replace(/^\s+|\s+$/g, "");
  210. }
  211. if (pos == "left") {
  212. return str.replace(/^\s*/, "");
  213. }
  214. if (pos == "right") {
  215. return str.replace(/(\s*$)/g, "");
  216. }
  217. if (pos == "all") {
  218. return str.replace(/\s+/g, "");
  219. }
  220. return str;
  221. }
  222. function queryParams(data = {}, isPrefix = true, arrayFormat = "brackets") {
  223. const prefix = isPrefix ? "?" : "";
  224. const _result = [];
  225. if (["indices", "brackets", "repeat", "comma"].indexOf(arrayFormat) == -1)
  226. arrayFormat = "brackets";
  227. for (const key in data) {
  228. const value = data[key];
  229. if (["", void 0, null].indexOf(value) >= 0) {
  230. continue;
  231. }
  232. if (value.constructor === Array) {
  233. switch (arrayFormat) {
  234. case "indices":
  235. for (let i = 0; i < value.length; i++) {
  236. _result.push(`${key}[${i}]=${value[i]}`);
  237. }
  238. break;
  239. case "brackets":
  240. value.forEach((_value) => {
  241. _result.push(`${key}[]=${_value}`);
  242. });
  243. break;
  244. case "repeat":
  245. value.forEach((_value) => {
  246. _result.push(`${key}=${_value}`);
  247. });
  248. break;
  249. case "comma":
  250. let commaStr = "";
  251. value.forEach((_value) => {
  252. commaStr += (commaStr ? "," : "") + _value;
  253. });
  254. _result.push(`${key}=${commaStr}`);
  255. break;
  256. default:
  257. value.forEach((_value) => {
  258. _result.push(`${key}[]=${_value}`);
  259. });
  260. }
  261. } else {
  262. _result.push(`${key}=${value}`);
  263. }
  264. }
  265. return _result.length ? prefix + _result.join("&") : "";
  266. }
  267. function toast(title, duration = 2e3) {
  268. common_vendor.index.showToast({
  269. title: String(title),
  270. icon: "none",
  271. duration
  272. });
  273. }
  274. function priceFormat(number, decimals = 0, decimalPoint = ".", thousandsSeparator = ",") {
  275. number = `${number}`.replace(/[^0-9+-Ee.]/g, "");
  276. const n = !isFinite(+number) ? 0 : +number;
  277. const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
  278. const sep = typeof thousandsSeparator === "undefined" ? "," : thousandsSeparator;
  279. const dec = typeof decimalPoint === "undefined" ? "." : decimalPoint;
  280. let s = "";
  281. s = (prec ? uni_modules_uviewPlus_libs_function_digit.round(n, prec) + "" : `${Math.round(n)}`).split(".");
  282. const re = /(-?\d+)(\d{3})/;
  283. while (re.test(s[0])) {
  284. s[0] = s[0].replace(re, `$1${sep}$2`);
  285. }
  286. if ((s[1] || "").length < prec) {
  287. s[1] = s[1] || "";
  288. s[1] += new Array(prec - s[1].length + 1).join("0");
  289. }
  290. return s.join(dec);
  291. }
  292. function formValidate(instance, event) {
  293. const formItem = $parent.call(instance, "u-form-item");
  294. const form = $parent.call(instance, "u-form");
  295. if (formItem && form) {
  296. form.validateField(formItem.prop, () => {
  297. }, event);
  298. }
  299. }
  300. function page() {
  301. const pages2 = getCurrentPages();
  302. return `/${pages2[pages2.length - 1].route || ""}`;
  303. }
  304. exports.$parent = $parent;
  305. exports.addStyle = addStyle;
  306. exports.addUnit = addUnit;
  307. exports.deepMerge = deepMerge;
  308. exports.error = error;
  309. exports.formValidate = formValidate;
  310. exports.getPx = getPx;
  311. exports.guid = guid;
  312. exports.os = os;
  313. exports.page = page;
  314. exports.priceFormat = priceFormat;
  315. exports.queryParams = queryParams;
  316. exports.random = random;
  317. exports.sleep = sleep;
  318. exports.sys = sys;
  319. exports.timeFormat = timeFormat;
  320. exports.toast = toast;