爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.5 KiB

  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. function toArray(data) {
  4. if (!data)
  5. return [];
  6. if (data instanceof Array) {
  7. return data;
  8. } else {
  9. return [data];
  10. }
  11. }
  12. function generateUUID() {
  13. return "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(/[xy]/g, function(c) {
  14. var r = Math.random() * 16 | 0, v = c == "x" ? r : r & 3 | 8;
  15. return v.toString(16);
  16. });
  17. }
  18. function generateRandomColor() {
  19. const letters = "0123456789ABCDEF";
  20. let color = "#";
  21. for (let i = 0; i < 6; i++) {
  22. color += letters[Math.floor(Math.random() * 16)];
  23. }
  24. return color;
  25. }
  26. function generateLightRandomColor() {
  27. const min = 150;
  28. const range = 105;
  29. const r = Math.floor(Math.random() * range + min);
  30. const g = Math.floor(Math.random() * range + min);
  31. const b = Math.floor(Math.random() * range + min);
  32. const color = "rgb(" + r + "," + g + "," + b + ")";
  33. return color;
  34. }
  35. function verificationAll(data, msg) {
  36. if (!msg) {
  37. common_vendor.index.__f__("log", "at utils/utils.js:63", msg);
  38. return false;
  39. }
  40. if (!data) {
  41. common_vendor.index.showToast({
  42. title: "表单数据未填写",
  43. icon: "none"
  44. });
  45. }
  46. for (let key in msg) {
  47. if (!data[key]) {
  48. common_vendor.index.showToast({
  49. title: msg[key],
  50. icon: "none"
  51. });
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. function verificationPhone(phone) {
  58. if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(phone)) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. function getHrefParams(name) {
  64. var url = window.location.href;
  65. try {
  66. var cs = url.split("?")[1];
  67. var cs_arr = cs.split("&");
  68. for (var i = 0; i < cs_arr.length; i++) {
  69. if (cs_arr[i].split("=")[0] == name) {
  70. return cs_arr[i].split("=")[1];
  71. }
  72. }
  73. return "";
  74. } catch {
  75. return "";
  76. }
  77. }
  78. function deepMergeObject(a, b) {
  79. let data = JSON.parse(JSON.stringify(a));
  80. function mergeObject(obj1, obj2) {
  81. for (let key in obj2) {
  82. if (typeof obj1[key] == "object") {
  83. obj1[key] = mergeObject(obj1[key], obj2[key]);
  84. } else {
  85. obj1[key] = obj2[key];
  86. }
  87. }
  88. return obj1;
  89. }
  90. return mergeObject(data, b);
  91. }
  92. function copyText(content) {
  93. common_vendor.index.setClipboardData({
  94. data: content,
  95. success: () => {
  96. common_vendor.index.showToast({
  97. title: "复制成功",
  98. icon: "none"
  99. });
  100. }
  101. });
  102. }
  103. function stringFormatHtml(str) {
  104. return str && str.replace(/\n/gi, "<br>").replace(/ /gi, " ");
  105. }
  106. function params(url) {
  107. if (typeof url == "object") {
  108. return url;
  109. }
  110. let data = {
  111. url
  112. };
  113. if (!data.url.includes("/pages")) {
  114. data.url = "/pages" + data.url;
  115. }
  116. return data;
  117. }
  118. function navigateTo(...args) {
  119. common_vendor.index.navigateTo(params(...args));
  120. }
  121. function navigateBack(num = -1) {
  122. common_vendor.index.navigateBack(num);
  123. }
  124. function redirectTo(...args) {
  125. common_vendor.index.redirectTo(params(...args));
  126. }
  127. const toLogin = /* @__PURE__ */ function() {
  128. let time = 0;
  129. return () => {
  130. if ((/* @__PURE__ */ new Date()).getTime() - time < 1e3) {
  131. return;
  132. }
  133. time = (/* @__PURE__ */ new Date()).getTime();
  134. common_vendor.index.navigateTo({
  135. url: "/pages/index/index"
  136. });
  137. };
  138. }();
  139. const util = {
  140. toArray,
  141. generateUUID,
  142. verificationAll,
  143. generateRandomColor,
  144. generateLightRandomColor,
  145. verificationPhone,
  146. getHrefParams,
  147. deepMergeObject,
  148. navigateTo,
  149. navigateBack,
  150. redirectTo,
  151. copyText,
  152. stringFormatHtml,
  153. toLogin
  154. };
  155. exports.util = util;
  156. //# sourceMappingURL=../../.sourcemap/mp-weixin/utils/utils.js.map