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.

89 lines
2.1 KiB

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. uni.getLocation({
  56. type: 'wgs84',
  57. geocode:true,//设置该参数为true可直接获取经纬度及城市信息
  58. success(orientation) {
  59. console.log('获取定位信息',orientation);
  60. let data = {
  61. latitude : orientation.latitude,//纬度
  62. longitude : orientation.longitude,//经度
  63. }
  64. fn && fn(data)
  65. },
  66. fail(err) {
  67. console.log("获取定位失败",err);
  68. uni.showToast({
  69. title: '获取地址失败,将导致部分功能不可用',
  70. icon:'none'
  71. });
  72. }
  73. });
  74. }
  75. export default {
  76. toArray: toArray,
  77. generateUUID: generateUUID,
  78. verificationAll: verificationAll,
  79. generateRandomColor: generateRandomColor,
  80. generateLightRandomColor: generateLightRandomColor,
  81. getLocation,
  82. }