建材商城系统20241014
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.

267 lines
5.5 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. /**
  2. * 将数据转换为数组格式
  3. * @param {any} data - 需要转换的数据
  4. * @returns {Array} 转换后的数组
  5. */
  6. function toArray(data) {
  7. if (!data) return []
  8. if (data instanceof Array){
  9. return data
  10. } else {
  11. return [data]
  12. }
  13. }
  14. /**
  15. * 生成UUID
  16. * @returns {string} 生成的UUID字符串
  17. */
  18. function generateUUID() {
  19. return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  20. var r = Math.random() * 16 | 0,
  21. v = c == 'x' ? r : (r & 0x3 | 0x8);
  22. return v.toString(16);
  23. });
  24. }
  25. /**
  26. * 生成随机颜色
  27. * @returns {string} 生成的十六进制颜色值
  28. */
  29. function generateRandomColor() {
  30. const letters = '0123456789ABCDEF';
  31. let color = '#';
  32. for (let i = 0; i < 6; i++) {
  33. color += letters[Math.floor(Math.random() * 16)];
  34. }
  35. return color;
  36. }
  37. /**
  38. * 生成浅色系的随机颜色
  39. * @returns {string} 生成的RGB格式颜色值
  40. */
  41. function generateLightRandomColor() {
  42. const min = 150;
  43. const range = 105;
  44. const r = Math.floor(Math.random() * range + min);
  45. const g = Math.floor(Math.random() * range + min);
  46. const b = Math.floor(Math.random() * range + min);
  47. const color = 'rgb(' + r + ',' + g + ',' + b + ')';
  48. return color;
  49. }
  50. /**
  51. * 表单数据验证
  52. * @param {Object} data - 需要验证的表单数据
  53. * @param {Object} msg - 验证失败时的提示信息
  54. * @returns {boolean} 验证结果true表示验证失败false表示验证通过
  55. */
  56. function verificationAll(data, msg){
  57. if (!msg){
  58. console.log(msg);
  59. return false
  60. }
  61. if (!data){
  62. uni.showToast({
  63. title: '表单数据未填写',
  64. icon: "none"
  65. })
  66. }
  67. for (let key in msg) {
  68. if (!data[key]) {
  69. uni.showToast({
  70. title: msg[key],
  71. icon: "none"
  72. })
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. /**
  79. * 验证手机号是否合法
  80. * @param {string} phone - 需要验证的手机号
  81. * @returns {boolean} 验证结果true表示合法false表示不合法
  82. */
  83. function verificationPhone(phone){
  84. 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)){
  85. return false
  86. }
  87. return true
  88. }
  89. //获取url中参数的方法
  90. /**
  91. * 获取URL中指定参数的值
  92. * @param {string} name - 参数名称
  93. * @returns {string} 参数值如果不存在则返回空字符串
  94. */
  95. export function getHrefParams(name) {
  96. var url = window.location.href;
  97. try {
  98. var cs = url.split('?')[1]; //获取?之后的参数字符串
  99. var cs_arr = cs.split('&'); //参数字符串分割为数组
  100. for (var i = 0; i < cs_arr.length; i++) { //遍历数组,拿到json对象
  101. if (cs_arr[i].split('=')[0] == name) {
  102. return cs_arr[i].split('=')[1];
  103. }
  104. }
  105. return "";
  106. } catch {
  107. return "";
  108. }
  109. }
  110. /**
  111. * 深度合并两个对象相同属性b会覆盖a
  112. * @param {Object} a - 目标对象
  113. * @param {Object} b - 源对象
  114. * @returns {Object} 合并后的新对象
  115. */
  116. export function deepMergeObject(a, b){
  117. let data = JSON.parse(JSON.stringify(a))
  118. function mergeObject(obj1, obj2){
  119. for(let key in obj2){
  120. if(typeof obj1[key] == 'object'){
  121. obj1[key] = mergeObject(obj1[key], obj2[key])
  122. }else{
  123. obj1[key] = obj2[key]
  124. }
  125. }
  126. return obj1
  127. }
  128. return mergeObject(data, b)
  129. }
  130. /**
  131. * 复制文本到剪贴板
  132. * @param {string} content - 要复制的内容
  133. */
  134. export function copyText(content) {
  135. uni.setClipboardData({
  136. data: content,
  137. success: () => {
  138. uni.showToast({
  139. title: '复制成功',
  140. icon: 'none'
  141. })
  142. }
  143. })
  144. }
  145. /**
  146. * 将字符串中的文本格式化为HTML
  147. * @param {string} str - 需要格式化的字符串
  148. * @returns {string} 格式化后的HTML字符串
  149. */
  150. export function stringFormatHtml(str){
  151. return str && str.replace(/\n/gi, '<br>')
  152. .replace(/ /gi, ' ')
  153. }
  154. /**
  155. * 处理页面导航参数
  156. * @param {string|Object} url - 页面路径或导航参数对象
  157. * @returns {Object} 处理后的导航参数对象
  158. */
  159. function params(url){
  160. if(typeof url == 'object'){
  161. return url
  162. }
  163. let data = {
  164. url
  165. }
  166. if(!data.url.includes('/pages')){
  167. data.url = '/pages' + data.url
  168. }
  169. return data
  170. }
  171. /**
  172. * 页面导航方法
  173. * @param {...any} args - 导航参数
  174. */
  175. export function navigateTo(...args){
  176. uni.navigateTo(params(...args))
  177. }
  178. /**
  179. * 返回上一页
  180. * @param {number} num - 返回的页面数默认为-1
  181. */
  182. export function navigateBack(num = -1){
  183. if(getCurrentPages().length == 1){
  184. uni.reLaunch({
  185. url: '/pages/index/index'
  186. })
  187. return
  188. }
  189. uni.navigateBack(num)
  190. }
  191. /**
  192. * 重定向到指定页面
  193. * @param {...any} args - 导航参数
  194. */
  195. export function redirectTo(...args){
  196. uni.redirectTo(params(...args))
  197. }
  198. /**
  199. * 登录跳转函数防止短时间内多次调用
  200. * @returns {Function} 节流处理后的登录跳转函数
  201. */
  202. export const toLogin = function(){
  203. let time = 0
  204. return () => {
  205. if(new Date().getTime() - time < 1000){
  206. return
  207. }
  208. time = new Date().getTime()
  209. uni.navigateTo({
  210. url: '/pages_order/auth/wxLogin'
  211. })
  212. }
  213. }()
  214. // 将对象转换为URL参数
  215. function objectToUrlParams(obj) {
  216. if (!obj || typeof obj !== 'object') {
  217. return '';
  218. }
  219. return Object.keys(obj)
  220. .filter(key => obj[key] !== undefined && obj[key] !== null)
  221. .map(key => {
  222. const value = typeof obj[key] === 'object'
  223. ? JSON.stringify(obj[key])
  224. : obj[key];
  225. return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  226. })
  227. .join('&');
  228. }
  229. export default {
  230. toArray,
  231. generateUUID,
  232. verificationAll,
  233. generateRandomColor,
  234. generateLightRandomColor,
  235. verificationPhone,
  236. getHrefParams,
  237. deepMergeObject,
  238. navigateTo,
  239. navigateBack,
  240. redirectTo,
  241. copyText,
  242. stringFormatHtml,
  243. toLogin,
  244. objectToUrlParams,
  245. }