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.

101 lines
2.3 KiB

8 months ago
8 months ago
  1. function toArray(data) {
  2. if (!data) return data
  3. let arr = new Array()
  4. if (data instanceof Array){
  5. return data
  6. } else {
  7. return [data]
  8. }
  9. }
  10. function generateUUID() {
  11. const timestamp = new Date().getTime().toString(); // 获取当前时间戳
  12. const random = Math.random().toString().substr(2, 8); // 生成8位随机数
  13. const increment = Math.floor(Math.random() * 1000000000).toString().padStart(9, '0'); // 生成9位自增数
  14. return timestamp + random + increment;
  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. console.log("verificationAll:", data);
  35. if (!data){
  36. uni.showToast({
  37. title: '表单数据未填写',
  38. icon: "error"
  39. })
  40. return true
  41. }
  42. for (let key in data) {
  43. if (!data[key] || data[key] === "") {
  44. console.log(key);
  45. uni.showToast({
  46. title: '必填数据未填写' + key,
  47. icon: "error"
  48. })
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. function getLocation(fn){
  55. if(uni.getStorageSync('open_address') == 0){
  56. return {}
  57. }
  58. uni.getLocation({
  59. type: 'wgs84',
  60. geocode:true,//设置该参数为true可直接获取经纬度及城市信息
  61. success(orientation) {
  62. console.log('获取定位信息',orientation);
  63. let data = {
  64. latitude : orientation.latitude,//纬度
  65. longitude : orientation.longitude,//经度
  66. }
  67. fn && fn(data)
  68. },
  69. fail(err) {
  70. console.log("获取定位失败",err);
  71. uni.showToast({
  72. title: '获取地址失败,将导致部分功能不可用',
  73. icon:'none'
  74. });
  75. }
  76. });
  77. }
  78. //验证手机号是否合法
  79. function verificationPhone(phone){
  80. 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)){
  81. return false
  82. }
  83. return true
  84. }
  85. export default {
  86. toArray: toArray,
  87. generateUUID: generateUUID,
  88. verificationAll: verificationAll,
  89. generateRandomColor: generateRandomColor,
  90. generateLightRandomColor: generateLightRandomColor,
  91. getLocation,
  92. verificationPhone
  93. }