common.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { Message } from 'element-ui'
  2. /* eslint-disable */
  3. // @ts-nocheck
  4. const STORAGE_USER_KEY = 'STORAGE_USER_KEY'
  5. // const STORAGE_CARTLIST_KEY = 'STORAGE_CARTLIST_KEY'
  6. // const STORAGE_QUERYMYLIST_KEY = 'STORAGE_QUERYMYLIST_KEY'
  7. // import { Toast } from 'mint-ui';
  8. import axios from 'axios'
  9. const prefix = 'http://testapi.gu-dao.cn';
  10. // import authService from '@/api/authService.js'
  11. //定义一些常量
  12. var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
  13. var PI = 3.1415926535897932384626;
  14. var a = 6378245.0;
  15. var ee = 0.00669342162296594323;
  16. export default {
  17. // 获取
  18. getLocal(key = STORAGE_USER_KEY) {
  19. // console.log('get local operation')
  20. return JSON.parse(window.localStorage.getItem(key))
  21. },
  22. // 设置用
  23. /**
  24. * @param {any} res
  25. */
  26. setLocal(res, key = STORAGE_USER_KEY, isSaveOldData = false) {
  27. //第三个参数是true的话,会增加数据而不是重新设置,res必须是数组
  28. if (isSaveOldData) {
  29. if (this.getLocal(key)) {
  30. let oldData = this.getLocal(key);
  31. return window.localStorage.setItem(key, JSON.stringify(oldData.concat(res)))
  32. }
  33. }
  34. return window.localStorage.setItem(key, JSON.stringify(res))
  35. },
  36. /**
  37. * @param {any[]} obj
  38. */
  39. deepClone(obj) {
  40. var o;
  41. if (typeof obj == "object") {
  42. if (obj === null) {
  43. o = null;
  44. } else {
  45. if (obj instanceof Array) {
  46. o = [];
  47. for (var i = 0, len = obj.length; i < len; i++) {
  48. o.push(this.deepClone(obj[i]));
  49. }
  50. } else {
  51. o = {};
  52. for (var j in obj) {
  53. o[j] = this.deepClone(obj[j]);
  54. }
  55. }
  56. }
  57. } else {
  58. o = obj;
  59. }
  60. return o;
  61. },
  62. getGuid() {
  63. function S4() {
  64. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  65. }
  66. return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  67. },
  68. isNotEmpty (value) {
  69. return !this.isEmpty(value)
  70. },
  71. /**
  72. * @param {string} value
  73. */
  74. isEmpty(value) {
  75. let result = false;
  76. if (value == null || value == undefined) {
  77. result = true;
  78. }
  79. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  80. result = true;
  81. }
  82. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  83. result = true;
  84. }
  85. return result;
  86. },
  87. /**
  88. * @param {any} time
  89. */
  90. formatTime(time) {
  91. // 格式:yyyy-MM-dd hh:mm:ss
  92. let date = new Date(Number(time));
  93. let Y = date.getFullYear() + '-';
  94. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  95. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  96. let h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';
  97. let m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';
  98. let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  99. return Y + M + D + h + m + s;
  100. },
  101. /**
  102. * @param {any} time
  103. */
  104. formatDate(time) {
  105. // 格式:yyyy-MM-dd
  106. let date = new Date(Number(time));
  107. let Y = date.getFullYear() + '-';
  108. let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  109. let D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
  110. return Y + M + D;
  111. },
  112. /**
  113. * 字符串转数组
  114. * @param {*} value
  115. * @returns
  116. */
  117. stringToArray(value) {
  118. if (this.isEmpty(value)) return []
  119. if (typeof value !== 'string') return []
  120. if (value.indexOf(",") < 0) return [value]
  121. let array = value.split(",");
  122. return array
  123. },
  124. /**
  125. * 数组转字符串
  126. * @param {*} value
  127. * @returns
  128. */
  129. arrayToString(value) {
  130. if (typeof value == "object" && value instanceof Array) {
  131. if (value.length == 0) return ""
  132. let str = '';
  133. value.forEach(element => {
  134. str += element + ',';
  135. })
  136. return str.substr(0, str.length - 1);
  137. } else {
  138. return ""
  139. }
  140. },
  141. /**
  142. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  143. * 即谷歌、高德 转 百度
  144. * @param {number} lng
  145. * @param {number} lat
  146. * @returns {*[]}
  147. */
  148. gcj02tobd09(lng, lat) {
  149. var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
  150. var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
  151. var bd_lng = z * Math.cos(theta) + 0.0065;
  152. var bd_lat = z * Math.sin(theta) + 0.006;
  153. return [bd_lng, bd_lat]
  154. },
  155. /**
  156. * @param {string} value
  157. */
  158. isPhone(value) {
  159. var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
  160. if (!myreg.test(value)) {
  161. return false;
  162. } else {
  163. return true;
  164. }
  165. },
  166. /**
  167. * 身份证号验证
  168. * @param {string} value
  169. */
  170. isIDCardNo(value) {
  171. 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)$/;
  172. if (!myreg.test(value)) {
  173. return false;
  174. } else {
  175. return true;
  176. }
  177. },
  178. /**
  179. * 从身份证号中获取出生日期、性别、年龄
  180. * @param idCard
  181. * @returns {{sex: string, birth: null, age: number}}
  182. */
  183. decomposeIdCard(idCard) {
  184. let sex = null
  185. let birth = null
  186. let myDate = new Date()
  187. let month = myDate.getMonth() + 1
  188. let day = myDate.getDate()
  189. let age = 0
  190. if (idCard.length === 18) {
  191. age = myDate.getFullYear() - idCard.substring(6, 10) - 1
  192. sex = idCard.substring(16, 17)
  193. birth = idCard.substring(6, 10) + '-' + idCard.substring(10, 12) + '-' + idCard.substring(12, 14)
  194. // eslint-disable-next-line no-mixed-operators
  195. if (idCard.substring(10, 12) < month || idCard.substring(10, 12) === month && idCard.substring(12, 14) <= day) age++
  196. }
  197. if (idCard.length === 15) {
  198. age = myDate.getFullYear() - idCard.substring(6, 8) - 1901
  199. sex = idCard.substring(13, 14)
  200. birth = '19' + idCard.substring(6, 8) + '-' + idCard.substring(8, 10) + '-' + idCard.substring(10, 12)
  201. // eslint-disable-next-line no-mixed-operators
  202. if (idCard.substring(8, 10) < month || idCard.substring(8, 10) === month && idCard.substring(10, 12) <= day) age++
  203. }
  204. if (sex % 2 === 0) {
  205. sex = '2' // 性别代码 1代表男,2代表女,暂时不涉及其他类型性别
  206. } else {
  207. sex = '1'
  208. }
  209. return {age, sex, birth}
  210. },
  211. /**
  212. * 判断一个文件的类型是否是图片
  213. * 目前用于给'el-upload'图片上传的':before-upload'属性使用,不是图片的话会上传失败并发出提示
  214. * @param file
  215. * @returns {boolean}
  216. */
  217. isPic (file) {
  218. let isJPG = true
  219. if (this.isEmpty(file.type)) {
  220. isJPG = false
  221. Message.error('上传图片只能是 JPG/PNG/GIF/BMP 格式')
  222. return isJPG
  223. }
  224. if (file.type !== 'image/jpeg' && file.type !== 'image/jpg' && file.type !== 'image/png' && file.type !== 'image/gif' && file.type !== 'image/bmp') {
  225. isJPG = false
  226. Message.error('上传图片只能是 JPG/PNG/GIF/BMP 格式')
  227. return isJPG
  228. }
  229. return isJPG
  230. }
  231. }