common.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* eslint-disable */
  2. // @ts-nocheck
  3. const STORAGE_USER_KEY = 'STORAGE_USER_KEY'
  4. // const STORAGE_CARTLIST_KEY = 'STORAGE_CARTLIST_KEY'
  5. // const STORAGE_QUERYMYLIST_KEY = 'STORAGE_QUERYMYLIST_KEY'
  6. // import { Toast } from 'mint-ui';
  7. import axios from 'axios'
  8. const prefix = 'http://testapi.gu-dao.cn';
  9. // import authService from '@/api/authService.js'
  10. //定义一些常量
  11. var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
  12. var PI = 3.1415926535897932384626;
  13. var a = 6378245.0;
  14. var ee = 0.00669342162296594323;
  15. export default {
  16. // 获取
  17. getLocal(key = STORAGE_USER_KEY) {
  18. // console.log('get local operation')
  19. return JSON.parse(window.localStorage.getItem(key))
  20. },
  21. // 设置用
  22. /**
  23. * @param {any} res
  24. */
  25. setLocal(res, key = STORAGE_USER_KEY, isSaveOldData = false) {
  26. //第三个参数是true的话,会增加数据而不是重新设置,res必须是数组
  27. if (isSaveOldData) {
  28. if (this.getLocal(key)) {
  29. let oldData = this.getLocal(key);
  30. return window.localStorage.setItem(key, JSON.stringify(oldData.concat(res)))
  31. }
  32. }
  33. return window.localStorage.setItem(key, JSON.stringify(res))
  34. },
  35. /**
  36. * @param {any[]} obj
  37. */
  38. deepClone(obj) {
  39. var o;
  40. if (typeof obj == "object") {
  41. if (obj === null) {
  42. o = null;
  43. } else {
  44. if (obj instanceof Array) {
  45. o = [];
  46. for (var i = 0, len = obj.length; i < len; i++) {
  47. o.push(this.deepClone(obj[i]));
  48. }
  49. } else {
  50. o = {};
  51. for (var j in obj) {
  52. o[j] = this.deepClone(obj[j]);
  53. }
  54. }
  55. }
  56. } else {
  57. o = obj;
  58. }
  59. return o;
  60. },
  61. getGuid() {
  62. function S4() {
  63. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  64. }
  65. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  66. },
  67. isNotEmpty (value) {
  68. return !this.isEmpty(value)
  69. },
  70. /**
  71. * @param {string} value
  72. */
  73. isEmpty(value) {
  74. let result = false;
  75. if (value == null || value == undefined) {
  76. result = true;
  77. }
  78. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  79. result = true;
  80. }
  81. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  82. result = true;
  83. }
  84. return result;
  85. },
  86. /**
  87. * @param {any} time
  88. */
  89. formatTime(time) {
  90. // 格式:yyyy-MM-dd hh:mm:ss
  91. let date = new Date(Number(time));
  92. let Y = date.getFullYear() + '-';
  93. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  94. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  95. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  96. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  97. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  98. return Y + M + D + h + m + s;
  99. },
  100. /**
  101. * @param {any} time
  102. */
  103. formatDate(time) {
  104. // 格式:yyyy-MM-dd
  105. let date = new Date(Number(time));
  106. let Y = date.getFullYear() + '-';
  107. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  108. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  109. return Y + M + D;
  110. },
  111. /**
  112. * 字符串转数组
  113. * @param {*} value
  114. * @returns
  115. */
  116. stringToArray(value) {
  117. if (this.isEmpty(value)) return []
  118. if (typeof value !== 'string') return []
  119. if (value.indexOf(",") < 0) return [value]
  120. let array = value.split(",");
  121. return array
  122. },
  123. /**
  124. * 数组转字符串
  125. * @param {*} value
  126. * @returns
  127. */
  128. arrayToString(value) {
  129. if (typeof value == "object" && value instanceof Array) {
  130. if (value.length == 0) return ""
  131. let str = '';
  132. value.forEach(element => {
  133. str += element + ',';
  134. })
  135. return str.substr(0, str.length - 1);
  136. } else {
  137. return ""
  138. }
  139. },
  140. /**
  141. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  142. * 即谷歌、高德 转 百度
  143. * @param {number} lng
  144. * @param {number} lat
  145. * @returns {*[]}
  146. */
  147. gcj02tobd09(lng, lat) {
  148. var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
  149. var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
  150. var bd_lng = z * Math.cos(theta) + 0.0065;
  151. var bd_lat = z * Math.sin(theta) + 0.006;
  152. return [bd_lng, bd_lat]
  153. },
  154. /**
  155. * @param {string} value
  156. */
  157. isPhone(value) {
  158. var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  159. if (!myreg.test(value)) {
  160. return false;
  161. } else {
  162. return true;
  163. }
  164. },
  165. /**
  166. * @param {string} value
  167. */
  168. isIDCardNo(value) {
  169. var myreg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
  170. if (!myreg.test(value)) {
  171. return false;
  172. } else {
  173. return true;
  174. }
  175. }
  176. }