艺易修小程序24.08.21
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.

152 lines
3.1 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
  1. function toArray(data) {
  2. if (!data) return []
  3. if (data instanceof Array){
  4. return data
  5. } else {
  6. return [data]
  7. }
  8. }
  9. function generateUUID() {
  10. return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  11. var r = Math.random() * 16 | 0,
  12. v = c == 'x' ? r : (r & 0x3 | 0x8);
  13. return v.toString(16);
  14. });
  15. }
  16. function generateRandomColor() {
  17. const letters = '0123456789ABCDEF';
  18. let color = '#';
  19. for (let i = 0; i < 6; i++) {
  20. color += letters[Math.floor(Math.random() * 16)];
  21. }
  22. return color;
  23. }
  24. function generateLightRandomColor() {
  25. const min = 150;
  26. const range = 105;
  27. const r = Math.floor(Math.random() * range + min);
  28. const g = Math.floor(Math.random() * range + min);
  29. const b = Math.floor(Math.random() * range + min);
  30. const color = 'rgb(' + r + ',' + g + ',' + b + ')';
  31. return color;
  32. }
  33. function verificationAll(data){
  34. if (!data){
  35. uni.showToast({
  36. title: '表单数据未填写',
  37. icon: "error"
  38. })
  39. return true
  40. }
  41. for (let key in data) {
  42. if (!data[key] || data[key] === "") {
  43. uni.showToast({
  44. title: '必填数据未填写' + key,
  45. icon: "error"
  46. })
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. //验证手机号是否合法
  53. function verificationPhone(phone){
  54. 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)){
  55. return false
  56. }
  57. return true
  58. }
  59. //获取url中参数的方法
  60. export function getHrefParams(name) {
  61. var url = window.location.href;
  62. try {
  63. var cs = url.split('?')[1]; //获取?之后的参数字符串
  64. var cs_arr = cs.split('&'); //参数字符串分割为数组
  65. for (var i = 0; i < cs_arr.length; i++) { //遍历数组,拿到json对象
  66. if (cs_arr[i].split('=')[0] == name) {
  67. return cs_arr[i].split('=')[1];
  68. }
  69. }
  70. return "";
  71. } catch {
  72. return "";
  73. }
  74. }
  75. //深度对比合并两个对象,相同属性b会覆盖a
  76. export function deepMergeObject(a, b){
  77. let data = JSON.parse(JSON.stringify(a))
  78. function mergeObject(obj1, obj2){
  79. for(let key in obj2){
  80. if(typeof obj1[key] == 'object'){
  81. obj1[key] = mergeObject(obj1[key], obj2[key])
  82. }else{
  83. obj1[key] = obj2[key]
  84. }
  85. }
  86. return obj1
  87. }
  88. return mergeObject(data, b)
  89. }
  90. function params(url){
  91. if(typeof url == 'object'){
  92. data.url = '/pages' + data.url
  93. return url
  94. }
  95. let data = {
  96. url
  97. }
  98. data.url = '/pages' + data.url
  99. return data
  100. }
  101. export function navigateTo(...args){
  102. uni.navigateTo(params(...args))
  103. }
  104. export function navigateBack(num = -1){
  105. uni.navigateBack(num)
  106. }
  107. //选择图片
  108. export function chooseImage(callBack){
  109. uni.chooseImage({
  110. count: 5, //默认9
  111. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  112. sourceType: ['album'], //从相册选择
  113. success: function(res) {
  114. callBack && callBack(res)
  115. }
  116. });
  117. }
  118. //图片预览
  119. export function previewImage(options){
  120. uni.previewImage(options);
  121. }
  122. export default {
  123. toArray,
  124. generateUUID,
  125. verificationAll,
  126. generateRandomColor,
  127. generateLightRandomColor,
  128. verificationPhone,
  129. getHrefParams,
  130. deepMergeObject,
  131. navigateTo,
  132. navigateBack,
  133. chooseImage,
  134. previewImage
  135. }