index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import store from '../store';
  2. import { Toast } from 'vant';
  3. import { phoneCheck } from '@/api/index';
  4. // 日期格式化
  5. export function parseTime(time, pattern) {
  6. if (time != 'null') {
  7. if (arguments.length === 0 || !time) {
  8. return null;
  9. }
  10. const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
  11. let date;
  12. if (typeof time === 'object') {
  13. date = time;
  14. } else {
  15. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  16. time = parseInt(time);
  17. } else if (typeof time === 'string') {
  18. time = time
  19. .replace(new RegExp(/-/gm), '/')
  20. .replace('T', ' ')
  21. .replace(new RegExp(/\.[\d]{3}/gm), '');
  22. }
  23. if (typeof time === 'number' && time.toString().length === 10) {
  24. time = time * 1000;
  25. }
  26. date = new Date(time);
  27. }
  28. const formatObj = {
  29. y: date.getFullYear(),
  30. m: date.getMonth() + 1,
  31. d: date.getDate(),
  32. h: date.getHours(),
  33. i: date.getMinutes(),
  34. s: date.getSeconds(),
  35. a: date.getDay(),
  36. };
  37. const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
  38. let value = formatObj[key];
  39. // Note: getDay() returns 0 on Sunday
  40. if (key === 'a') {
  41. return ['日', '一', '二', '三', '四', '五', '六'][value];
  42. }
  43. if (result.length > 0 && value < 10) {
  44. value = '0' + value;
  45. }
  46. return value || 0;
  47. });
  48. return time_str;
  49. } else {
  50. return '';
  51. }
  52. }
  53. // 千分号
  54. // 中文日期格式(年-月-日)
  55. export function formatChineseDate(time) {
  56. // 统一处理日期格式
  57. const formatDate = (dateObj) => {
  58. const year = dateObj.getFullYear();
  59. const month = (dateObj.getMonth() + 1).toString().padStart(2, '0');
  60. const day = dateObj.getDate().toString().padStart(2, '0');
  61. return `${year}年${month}月${day}日`;
  62. };
  63. // 处理字符串格式(支持多种分隔符)
  64. if (typeof time === 'string') {
  65. // 清理日期字符串中的时间部分(如果有)
  66. const dateString = time.split(' ')[0];
  67. // 匹配 yyyy-mm-dd 或 yyyy/m/d 等格式
  68. if (/^\d{4}[-\/]\d{1,2}[-\/]\d{1,2}$/.test(dateString)) {
  69. const separator = dateString.includes('-') ? '-' : '/';
  70. const [year, month, day] = dateString.split(separator);
  71. return formatDate(new Date(year, month - 1, day));
  72. }
  73. }
  74. // 处理时间戳和Date对象
  75. try {
  76. const date = new Date(time);
  77. if (!isNaN(date)) {
  78. return formatDate(date);
  79. }
  80. } catch (e) {
  81. console.error('Invalid date format:', time);
  82. }
  83. return '无效日期格式';
  84. }
  85. export function Micrometer(num) {
  86. if (num != null) {
  87. let numt = (num || 0).toString().split('.');
  88. if (numt[1] == undefined) {
  89. return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  90. } else {
  91. return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + '.' + numt[1];
  92. }
  93. } else {
  94. return 0.0;
  95. }
  96. }
  97. export function weeklyTimeDivision(time, type) {
  98. if (type == 0) {
  99. return time.split(' ')[0] + ' ';
  100. } else {
  101. return time.split(' ')[1];
  102. }
  103. }
  104. export function parseTimeParagraph(dayTime) {
  105. var currentDate = new Date(dayTime);
  106. var timesStamp = currentDate.getTime();
  107. var currenDay = currentDate.getDay();
  108. var dates = [];
  109. var tabTime = [];
  110. for (var i = 0; i < 14; i++) {
  111. var dataTime = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
  112. .toLocaleDateString()
  113. .replace(/\//g, '-');
  114. var dataTimeArr = dataTime.split('-');
  115. if (dataTimeArr[1] < 10) {
  116. dataTimeArr[1] = '0' + dataTimeArr[1];
  117. }
  118. if (dataTimeArr[2] < 10) {
  119. dataTimeArr[2] = '0' + dataTimeArr[2];
  120. }
  121. dates.push(
  122. dataTimeArr.join('-') +
  123. ' ' +
  124. ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][
  125. new Date(dataTime.toString().replace(/-/g, '/')).getDay()
  126. ],
  127. );
  128. }
  129. tabTime.push(dates[0]);
  130. tabTime.push(dates[4]);
  131. return dates;
  132. }
  133. export function weeklay(dataTime) {
  134. return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][new Date(dataTime).getDay()];
  135. }
  136. // 回显数据字典
  137. export function selectDictLabel(datas, value) {
  138. var actions = [];
  139. Object.keys(datas).some((key) => {
  140. if (datas[key].dictValue == '' + value) {
  141. actions.push(datas[key].dictLabel);
  142. return true;
  143. }
  144. });
  145. return actions.join('');
  146. }
  147. export function selectDictLabelu(datas, value) {
  148. var actions = [];
  149. Object.keys(datas).some((key) => {
  150. if (datas[key].dictValue == '' + value) {
  151. actions.push(datas[key].text);
  152. return true;
  153. }
  154. });
  155. return actions.join('');
  156. }
  157. // 回显数据字典(字符串数组)
  158. export function selectDictLabels(datas, value, separator) {
  159. var actions = [];
  160. var currentSeparator = undefined === separator ? ',' : separator;
  161. var temp = value.split(currentSeparator);
  162. Object.keys(value.split(currentSeparator)).some((val) => {
  163. Object.keys(datas).some((key) => {
  164. if (datas[key].dictValue == '' + temp[val]) {
  165. actions.push(datas[key].dictLabel + currentSeparator);
  166. }
  167. });
  168. });
  169. return actions.join('').substring(0, actions.join('').length - 1);
  170. }
  171. // 字符串格式化(%s )
  172. export function sprintf(str) {
  173. var args = arguments,
  174. flag = true,
  175. i = 1;
  176. str = str.replace(/%s/g, function () {
  177. var arg = args[i++];
  178. if (typeof arg === 'undefined') {
  179. flag = false;
  180. return '';
  181. }
  182. return arg;
  183. });
  184. return flag ? str : '';
  185. }
  186. // 转换字符串,undefined,null等转化为""
  187. export function praseStrEmpty(str) {
  188. if (!str || str == 'undefined' || str == 'null') {
  189. return '';
  190. }
  191. return str;
  192. }
  193. //
  194. export function twoPointSum(latA, lonA, latB, lonB) {
  195. var coordinate = 6371000;
  196. var PI = 3.14159265358979324;
  197. var x =
  198. Math.cos((latA * PI) / 180) *
  199. Math.cos((latB * PI) / 180) *
  200. Math.cos(((lonA - lonB) * PI) / 180);
  201. var y = Math.sin((latA * PI) / 180) * Math.sin((latB * PI) / 180);
  202. var s = x + y;
  203. if (s > 1) s = 1;
  204. if (s < -1) s = -1;
  205. var alphabet = Math.acos(s);
  206. var PointSum = alphabet * coordinate;
  207. return PointSum;
  208. }
  209. export function CJ02BD(gcjLat, gcjLon) {
  210. var x = gcjLon,
  211. y = gcjLat;
  212. var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
  213. var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  214. var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  215. var bdLon = z * Math.cos(theta) + 0.0065;
  216. var bdLat = z * Math.sin(theta) + 0.006;
  217. return {
  218. lat: bdLat,
  219. lon: bdLon,
  220. };
  221. }
  222. export function gcj02BD(gcjLat, gcjLon) {
  223. var x = gcjLon - 0.0065,
  224. y = gcjLat - 0.006;
  225. var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
  226. var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  227. var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  228. var bdLon = z * Math.cos(theta);
  229. var bdLat = z * Math.sin(theta);
  230. return { lat: bdLat, lon: bdLon };
  231. }
  232. /**
  233. *门店类型集合,返回对应的类型
  234. *@param {*String} dictValue //类型
  235. * */
  236. export function verifyStoreType(dictValue) {
  237. if (!dictValue) return null;
  238. let storeData = null;
  239. storeData = store.getters.storeType.find((val) => val.dictValue == dictValue);
  240. let remarkType = storeData ? JSON.parse(storeData.remark) : null;
  241. return remarkType;
  242. }
  243. export function getMonthCommon() {
  244. let timeData = '';
  245. // 获取当前日期
  246. var currentDate = new Date();
  247. // 获取当前月份
  248. var currentMonth = currentDate.getMonth();
  249. var previousMonthDate1 = new Date();
  250. if (currentDate.getDate() == 1) {
  251. previousMonthDate1.setMonth(currentMonth - 1);
  252. }
  253. var previousMonth1 = previousMonthDate1.getMonth();
  254. var previousYear1 = previousMonthDate1.getFullYear();
  255. // 计算前三个月的年份和月份
  256. var previousMonthDate = new Date();
  257. if (currentDate.getDate() == 1) {
  258. previousMonthDate.setMonth(currentMonth - 3);
  259. } else {
  260. previousMonthDate.setMonth(currentMonth - 2);
  261. }
  262. var previousMonth = previousMonthDate.getMonth();
  263. var previousYear = previousMonthDate.getFullYear();
  264. //前三个月
  265. if (previousYear1 == previousYear) {
  266. var formattedPreviousMonth1 = previousYear1 + '-' + (previousMonth1 + 1);
  267. // 格式化年份和月份
  268. var formattedPreviousMonth = previousYear + '-' + (previousMonth + 1);
  269. timeData =
  270. formattedPreviousMonth.split('-')[1] + '-' + formattedPreviousMonth1.split('-')[1] + '月';
  271. } else {
  272. var formattedPreviousMonth1 = previousYear1 + '年' + (previousMonth1 + 1) + '月';
  273. // .toString().padStart(2, '0');
  274. // 格式化年份和月份
  275. var formattedPreviousMonth = previousYear + '年' + (previousMonth + 1) + '月';
  276. timeData = formattedPreviousMonth + '-' + formattedPreviousMonth1;
  277. }
  278. return timeData;
  279. }
  280. // 手机号校验
  281. export function validatePhone(telephone) {
  282. return new Promise((resolve, reject) => {
  283. if (telephone == '' || !telephone) {
  284. Toast('请输入手机号');
  285. reject();
  286. }
  287. var telrg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
  288. if (telephone.trim() == '') {
  289. reject();
  290. } else if (!telrg.test(telephone)) {
  291. Toast('手机号格式错误');
  292. reject();
  293. } else {
  294. phoneCheck({ phoneNumber: telephone }).then((res) => {
  295. if (res.code == 200) {
  296. resolve();
  297. } else {
  298. Toast(res.msg);
  299. reject();
  300. }
  301. });
  302. }
  303. });
  304. }
  305. // 分享视频链接
  306. export function shareVedioLinks() {
  307. window.location.href =
  308. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww5444eb205d75e730&redirect_uri=https%3A%2F%2Fknowledgewiki.nipponpaint.com.cn%2Fapi%2Fauth%2Fwx%2FshareOuth%3Fguid%3Dd4ac9b85658570389cfebe70e4505071%26shareId%3DSHARE21482&response_type=code&scope=snsapi_base&state=ZyKPSJuKA6-ZikabG6WSBqgGRsjsYK8j6P5I2bVdqOs&agentid=1000291#wechat_redirect';
  309. }
  310. export function convertToChinese(num) {
  311. if (num === 0) return '零';
  312. const digitMap = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
  313. const units = ['', '十', '百', '千'];
  314. const bigUnits = ['', '万', '亿'];
  315. let str = num.toString();
  316. let result = '';
  317. let len = str.length;
  318. for (let i = 0; i < len; i++) {
  319. let digit = parseInt(str[i]);
  320. let pos = len - 1 - i;
  321. if (digit !== 0) {
  322. result += digitMap[digit] + units[pos % 4];
  323. } else if (result && result[result.length - 1] !== '零') {
  324. result += '零';
  325. }
  326. if (pos % 4 === 0 && pos > 0) {
  327. result += bigUnits[Math.floor(pos / 4)];
  328. }
  329. }
  330. // 清理多余的零
  331. result = result.replace(/零+/g, '零').replace(/^零|零$/g, '');
  332. // 特殊处理:如果以"一十"开头,去掉"一"
  333. if (result.startsWith('一十')) {
  334. result = result.substring(1);
  335. }
  336. return result;
  337. }