index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import store from '../store';
  2. // 日期格式化
  3. export function parseTime(time, pattern) {
  4. if (time != 'null') {
  5. if (arguments.length === 0 || !time) {
  6. return null;
  7. }
  8. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
  9. let date;
  10. if (typeof time === 'object') {
  11. date = time;
  12. } else {
  13. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  14. time = parseInt(time);
  15. } else if (typeof time === 'string') {
  16. time = time
  17. .replace(new RegExp(/-/gm), '/')
  18. .replace('T', ' ')
  19. .replace(new RegExp(/\.[\d]{3}/gm), '');
  20. }
  21. if (typeof time === 'number' && time.toString().length === 10) {
  22. time = time * 1000;
  23. }
  24. date = new Date(time);
  25. }
  26. const formatObj = {
  27. y: date.getFullYear(),
  28. m: date.getMonth() + 1,
  29. d: date.getDate(),
  30. h: date.getHours(),
  31. i: date.getMinutes(),
  32. s: date.getSeconds(),
  33. a: date.getDay(),
  34. };
  35. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  36. let value = formatObj[key];
  37. // Note: getDay() returns 0 on Sunday
  38. if (key === 'a') {
  39. return ['日', '一', '二', '三', '四', '五', '六'][value];
  40. }
  41. if (result.length > 0 && value < 10) {
  42. value = '0' + value;
  43. }
  44. return value || 0;
  45. });
  46. return time_str;
  47. } else {
  48. return '';
  49. }
  50. }
  51. // 千分号
  52. export function Micrometer(num) {
  53. if (num != null) {
  54. let numt = (num || 0).toString().split('.');
  55. if (numt[1] == undefined) {
  56. return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  57. } else {
  58. return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + '.' + numt[1];
  59. }
  60. } else {
  61. return 0.0;
  62. }
  63. }
  64. export function weeklyTimeDivision(time, type) {
  65. if (type == 0) {
  66. return time.split(' ')[0] + ' ';
  67. } else {
  68. return time.split(' ')[1];
  69. }
  70. }
  71. export function parseTimeParagraph(dayTime) {
  72. var currentDate = new Date(dayTime);
  73. var timesStamp = currentDate.getTime();
  74. var currenDay = currentDate.getDay();
  75. var dates = [];
  76. var tabTime = [];
  77. for (var i = 0; i < 14; i++) {
  78. var dataTime = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
  79. .toLocaleDateString()
  80. .replace(/\//g, '-');
  81. var dataTimeArr = dataTime.split('-');
  82. if (dataTimeArr[1] < 10) {
  83. dataTimeArr[1] = '0' + dataTimeArr[1];
  84. }
  85. if (dataTimeArr[2] < 10) {
  86. dataTimeArr[2] = '0' + dataTimeArr[2];
  87. }
  88. dates.push(
  89. dataTimeArr.join('-') +
  90. ' ' +
  91. ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][
  92. new Date(dataTime.toString().replace(/-/g, '/')).getDay()
  93. ]
  94. );
  95. }
  96. tabTime.push(dates[0]);
  97. tabTime.push(dates[4]);
  98. return dates;
  99. }
  100. export function weeklay(dataTime) {
  101. return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][new Date(dataTime).getDay()];
  102. }
  103. // 回显数据字典
  104. export function selectDictLabel(datas, value) {
  105. var actions = [];
  106. Object.keys(datas).some((key) => {
  107. if (datas[key].dictValue == '' + value) {
  108. actions.push(datas[key].dictLabel);
  109. return true;
  110. }
  111. });
  112. return actions.join('');
  113. }
  114. export function selectDictLabelu(datas, value) {
  115. var actions = [];
  116. Object.keys(datas).some((key) => {
  117. if (datas[key].dictValue == '' + value) {
  118. actions.push(datas[key].text);
  119. return true;
  120. }
  121. });
  122. return actions.join('');
  123. }
  124. // 回显数据字典(字符串数组)
  125. export function selectDictLabels(datas, value, separator) {
  126. var actions = [];
  127. var currentSeparator = undefined === separator ? ',' : separator;
  128. var temp = value.split(currentSeparator);
  129. Object.keys(value.split(currentSeparator)).some((val) => {
  130. Object.keys(datas).some((key) => {
  131. if (datas[key].dictValue == '' + temp[val]) {
  132. actions.push(datas[key].dictLabel + currentSeparator);
  133. }
  134. });
  135. });
  136. return actions.join('').substring(0, actions.join('').length - 1);
  137. }
  138. // 字符串格式化(%s )
  139. export function sprintf(str) {
  140. var args = arguments,
  141. flag = true,
  142. i = 1;
  143. str = str.replace(/%s/g, function () {
  144. var arg = args[i++];
  145. if (typeof arg === 'undefined') {
  146. flag = false;
  147. return '';
  148. }
  149. return arg;
  150. });
  151. return flag ? str : '';
  152. }
  153. // 转换字符串,undefined,null等转化为""
  154. export function praseStrEmpty(str) {
  155. if (!str || str == 'undefined' || str == 'null') {
  156. return '';
  157. }
  158. return str;
  159. }
  160. //
  161. export function twoPointSum(latA, lonA, latB, lonB) {
  162. var coordinate = 6371000;
  163. var PI = 3.14159265358979324;
  164. var x =
  165. Math.cos((latA * PI) / 180) *
  166. Math.cos((latB * PI) / 180) *
  167. Math.cos(((lonA - lonB) * PI) / 180);
  168. var y = Math.sin((latA * PI) / 180) * Math.sin((latB * PI) / 180);
  169. var s = x + y;
  170. if (s > 1) s = 1;
  171. if (s < -1) s = -1;
  172. var alphabet = Math.acos(s);
  173. var PointSum = alphabet * coordinate;
  174. return PointSum;
  175. }
  176. export function CJ02BD(gcjLat, gcjLon) {
  177. var x = gcjLon,
  178. y = gcjLat;
  179. var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
  180. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  181. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  182. var bdLon = z * Math.cos(theta) + 0.0065;
  183. var bdLat = z * Math.sin(theta) + 0.006;
  184. return {
  185. lat: bdLat,
  186. lon: bdLon,
  187. };
  188. }
  189. export function gcj02BD(gcjLat, gcjLon) {
  190. var x = gcjLon - 0.0065,
  191. y = gcjLat - 0.006;
  192. var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
  193. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  194. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  195. var bdLon = z * Math.cos(theta);
  196. var bdLat = z * Math.sin(theta);
  197. return { lat: bdLat, lon: bdLon };
  198. }
  199. /**
  200. *门店类型集合,返回对应的类型
  201. *@param {*String} dictValue //类型
  202. * */
  203. export function verifyStoreType(dictValue) {
  204. if (!dictValue) return null;
  205. let storeData = null;
  206. storeData = store.getters.storeType.find((val) => val.dictValue == dictValue);
  207. let remarkType = storeData ? JSON.parse(storeData.remark) : null;
  208. return remarkType;
  209. }