common.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /**
  68. * @param {string} value
  69. */
  70. isEmpty(value) {
  71. let result = false;
  72. if (value == null || value == undefined) {
  73. result = true;
  74. }
  75. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  76. result = true;
  77. }
  78. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  79. result = true;
  80. }
  81. return result;
  82. },
  83. /**
  84. * @param {any} time
  85. */
  86. formatTime(time) {
  87. // 格式:yyyy-MM-dd hh:mm:ss
  88. let date = new Date(Number(time));
  89. let Y = date.getFullYear() + '-';
  90. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  91. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  92. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  93. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  94. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  95. return Y + M + D + h + m + s;
  96. },
  97. /**
  98. * @param {any} time
  99. */
  100. formatDate(time) {
  101. // 格式:yyyy-MM-dd
  102. let date = new Date(Number(time));
  103. let Y = date.getFullYear() + '-';
  104. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  105. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  106. return Y + M + D;
  107. },
  108. /**
  109. * 字符串转数组
  110. * @param {*} value
  111. * @returns
  112. */
  113. stringToArray(value) {
  114. if (this.isEmpty(value)) return []
  115. if (typeof value !== 'string') return []
  116. if (value.indexOf(",") < 0) return [value]
  117. let array = value.split(",");
  118. return array
  119. },
  120. /**
  121. * 数组转字符串
  122. * @param {*} value
  123. * @returns
  124. */
  125. arrayToString(value) {
  126. if (typeof value == "object" && value instanceof Array) {
  127. if (value.length == 0) return ""
  128. let str = '';
  129. value.forEach(element => {
  130. str += element + ',';
  131. })
  132. return str.substr(0, str.length - 1);
  133. } else {
  134. return ""
  135. }
  136. },
  137. /**
  138. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  139. * 即谷歌、高德 转 百度
  140. * @param {number} lng
  141. * @param {number} lat
  142. * @returns {*[]}
  143. */
  144. gcj02tobd09(lng, lat) {
  145. var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
  146. var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
  147. var bd_lng = z * Math.cos(theta) + 0.0065;
  148. var bd_lat = z * Math.sin(theta) + 0.006;
  149. return [bd_lng, bd_lat]
  150. },
  151. /**
  152. * @param {string} value
  153. */
  154. isPhone(value) {
  155. var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  156. if (!myreg.test(value)) {
  157. return false;
  158. } else {
  159. return true;
  160. }
  161. },
  162. /**
  163. * 身份证号验证
  164. * @param {string} value
  165. */
  166. isIDCardNo(value) {
  167. 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)$/;
  168. if (!myreg.test(value)) {
  169. return false;
  170. } else {
  171. return true;
  172. }
  173. },
  174. /**
  175. * 从身份证号中获取出生日期、性别、年龄
  176. * @param idCard
  177. * @returns {{sex: string, birth: null, age: number}}
  178. */
  179. decomposeIdCard(idCard) {
  180. let sex = null
  181. let birth = null
  182. let myDate = new Date()
  183. let month = myDate.getMonth() + 1
  184. let day = myDate.getDate()
  185. let age = 0
  186. if (idCard.length === 18) {
  187. age = myDate.getFullYear() - idCard.substring(6, 10) - 1
  188. sex = idCard.substring(16, 17)
  189. birth = idCard.substring(6, 10) + '-' + idCard.substring(10, 12) + '-' + idCard.substring(12, 14)
  190. // eslint-disable-next-line no-mixed-operators
  191. if (idCard.substring(10, 12) < month || idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day) age++
  192. }
  193. if (idCard.length === 15) {
  194. age = myDate.getFullYear() - idCard.substring(6, 8) - 1901
  195. sex = idCard.substring(13, 14)
  196. birth = '19' + idCard.substring(6, 8) + '-' + idCard.substring(8, 10) + '-' + idCard.substring(10, 12)
  197. // eslint-disable-next-line no-mixed-operators
  198. if (idCard.substring(8, 10) < month || idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day) age++
  199. }
  200. if (sex % 2 === 0) {
  201. sex = '2' // 性别代码 1代表男,2代表女,暂时不涉及其他类型性别
  202. } else {
  203. sex = '1'
  204. }
  205. return {age, sex, birth}
  206. }
  207. }