1. import CONFIG from '../../config.js';
    2. import serve from 'commonSrc/common/js/serve';
    3. import {loginZhenai} from 'commonSrc/api/commonAPI';
    4. /**
    5. * 同步下载文件
    6. * Created by Joetse on 2021-03-16.
    7. *
    8. */
    9. export function syncDownloadFile (path) {
    10. const baseURL = CONFIG.API_PROTOCOL() + "//" + CONFIG.API_HOST_NAME();
    11. const url = '' + baseURL + path;
    12. window.open(url);
    13. }
    14. export function goAlbum (memberId) {
    15. const album = document.querySelector('#albumUrl');
    16. album ? album.parentNode.removeChild(album) : '';
    17. serve.httpRequestMsg({memberId}, loginZhenai, (res) => {
    18. const oHead = document.getElementsByTagName('HEAD').item(0);
    19. const oScript = document.createElement("script");
    20. oScript.type = "text/javascript";
    21. oScript.src = `https://album.zhenai.com/return-cks?token=${res.data.token}`;
    22. oScript.id = 'albumUrl';
    23. oHead.appendChild(oScript);
    24. window.open(`https://album.zhenai.com/u/${memberId}`);
    25. });
    26. }
    27. /**
    28. * 解析url参数
    29. * Created by yi on 2016-12-28.
    30. * @return Object {id:12334}
    31. */
    32. export function urlParse () {
    33. let url = window.location.search;
    34. let obj = {};
    35. let reg = /[?&][^?&]+=[^?&]]+/g;
    36. let arr = url.match(reg);
    37. // ['?id=123454','&a=b']
    38. if (arr) {
    39. arr.forEach((item) => {
    40. let tempArr = item.substring(1).split('=');
    41. let key = tempArr[0];
    42. let val = tempArr[1];
    43. obj[key] = val;
    44. });
    45. }
    46. return {id: 123123};
    47. }
    48. /** 数组排序
    49. *@param prop 属性字段
    50. *@returns 排序后的数组 {Array}
    51. */
    52. export function rank (prop) {
    53. return function (obj1, obj2) {
    54. var val1 = obj1[prop];
    55. var val2 = obj2[prop];
    56. if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
    57. val1 = Number(val1);
    58. val2 = Number(val2);
    59. }
    60. if (val1 < val2) { //降序排列
    61. return 1;
    62. } else if (val1 > val2) {
    63. return -1;
    64. } else {
    65. return 0;
    66. }
    67. };
    68. }
    69. /** 金额转千分位
    70. *@param num (123456)
    71. *@returns str (123,456.00)
    72. */
    73. export function moneyFormatStr (_num) {
    74. if (!!_num) {
    75. var _tmp = _num + ""; //1,234,567
    76. _tmp = _tmp.indexOf('.') > -1 ? _tmp : _tmp + '.00';
    77. var _decimal = _tmp.split('.')[1];
    78. _tmp = _tmp.split('.')[0];
    79. var _leg = _tmp.length;
    80. var start = _leg % 3;
    81. var stack = [];
    82. if (start > 0) {
    83. stack.push(_tmp.substr(0, start));
    84. }
    85. for (var s = start; s < _leg; s += 3) {
    86. stack.push(_tmp.substr(s, 3));
    87. }
    88. return stack.join(",") + '.' + _decimal;
    89. } else {
    90. return '0.00';
    91. }
    92. }
    93. /** 千分位转金额
    94. *@param str (123,456.00)
    95. *@returns num (123456.00)
    96. */
    97. export function moneyFormatNum (_num) {
    98. if (!!_num) {
    99. _num = _num + "";
    100. _num = _num.replace(/,/g, '');
    101. return parseFloat(_num).toFixed(2);
    102. } else {
    103. return '';
    104. }
    105. }
    106. export function moneyFloatToInt (num) {
    107. let reg = /^\d+(\.\d+)?$/;
    108. return reg.test(num);
    109. }
    110. /**
    111. * [getMonthDaysTimeRange 初始化30天的选择范围]
    112. * @return {[Array]} [时间范围数组]
    113. */
    114. export function getMonthDaysTimeRange () {
    115. let now = new Date();
    116. return [new Date(now.setHours(0, 0, 0, 0) - (30 * 24 * 60 * 60 * 1000)), new Date(now.setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1)];
    117. }
    118. /**
    119. * [getSevenDaysTimeRange 初始化七天的选择范围 20**-**-** 00:00:00 到 20**-**-** 23:59:59]
    120. * @return {[Array]} [时间范围数组]
    121. */
    122. export function getSevenDaysTimeRange () {
    123. let now = new Date();
    124. return [new Date(now.setHours(0, 0, 0, 0) - 604800000), new Date(now.setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1)];
    125. }
    126. /**
    127. * [getTodayTimeRange 初始化一天的选择范围 20**-**-** 00:00:00 到 20**-**-** 23:59:59]
    128. * @return {[Array]} [时间范围数组]
    129. */
    130. export function getTodayTimeRange () {
    131. let now = new Date();
    132. return [new Date(now.setHours(0, 0, 0, 0)), new Date(now.setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1)];
    133. }
    134. /**
    135. * [setSecondToEnd 时间选择框点击确定时结束时间格式化成 23:59:59]
    136. * @param {[type]} time [选择的时间]
    137. * @param {Function} cb [回调函数]
    138. */
    139. export function setSecondToEnd (time, cb) {
    140. if (time !== undefined && time !== '' && time !== null && (typeof time === 'string')) {
    141. let arr = time.split(' - '),
    142. startTime = arr[0],
    143. endTime = arr[1],
    144. endTimeArr = endTime.split(' '),
    145. endTimeDay = endTimeArr[0],
    146. endTimeSecond = endTimeArr[1];
    147. if (endTimeSecond === '00:00' || endTimeSecond === '00:00:00') {
    148. cb([new Date(startTime), new Date(endTimeDay + ' 23:59:59')]);
    149. }
    150. }
    151. if (time === '') {
    152. cb(getTodayTimeRange());
    153. }
    154. }
    155. /**
    156. * [_isEmptyObj 判断对象是否为空对象]
    157. * @param {[type]} obj [需要判断的对象]
    158. * @return {Boolean} [true就为空对象]
    159. */
    160. export function isEmptyObj (obj) {
    161. for (var name in obj) {
    162. if (obj.hasOwnProperty(name)) {
    163. return false; //返回false,不为空对象
    164. }
    165. }
    166. return true; //返回true,为空对象
    167. }
    168. /**
    169. * 去空格
    170. * @param {[type]} s [description]
    171. * @return {[type]} [description]
    172. */
    173. export function trim (s) {
    174. if (typeof(s) === 'number' ) {
    175. return s;
    176. } else {
    177. return s.replace(/(^\s*)|(\s*$)/g, "");
    178. }
    179. }
    180. /**
    181. * 成熟度显示统一处理
    182. * @param {[type]} caseClass [description]
    183. * @return {[type]} [description]
    184. */
    185. function showCaseClass (caseClass) {
    186. if (caseClass !== null) {
    187. let CaseClassStr = caseClass + '';
    188. if (typeof caseClass === 'number') {
    189. if (caseClass === 512) {
    190. return '-1类';
    191. } else if (caseClass === 513) { // 红娘的vip跟进页12类会员
    192. return '12类';
    193. } else if (CaseClassStr.indexOf('.') !== -1) {
    194. let LCaseClass = caseClass * 10;
    195. return (LCaseClass % 100) / 10 + '类';
    196. } else {
    197. return (caseClass % 100) + '类';
    198. }
    199. }
    200. } else {
    201. return '未分类';
    202. }
    203. }
    204. /**
    205. * [_getCurrentSystem 获取当前系统值,与currentSystem不冲突,currentSystem用于vue文件的调用]
    206. */
    207. export function getCurrentSystem () {
    208. let currentsystem = location.pathname.split('/')[1];
    209. // if (currentsystem === "szmatchmaker") {
    210. // currentsystem = "matchmaker";
    211. // }
    212. return currentsystem;
    213. }
    214. /**
    215. * [_filterName 将部门中的名称去除]
    216. * @param {[String]} name [需要过滤的字符串] "深圳邀约三军二区(罗东秀)三部(严天恩)"
    217. * @return {[String]} [过滤后的字符串] 深圳邀约三军二区
    218. */
    219. function filterOrganName (name) {
    220. return (typeof name === "string" ? name.replace(/\([^\)]*\)/g, "") : "");
    221. }
    222. function filterEmpty (value) {
    223. return value !== null && value !== undefined ? value : '';
    224. }
    225. /**
    226. * 生成唯一标识
    227. * uuid(8, 2)
    228. * uuid(8, 10)
    229. * uuid(8, 16)
    230. */
    231. function uuid (len, radix) {
    232. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    233. let uuid = [], i;
    234. radix = radix || chars.length;
    235. if (len) {
    236. // Compact form
    237. for (i = 0; i < len; i++) {
    238. uuid[i] = chars[0 | Math.random()*radix];
    239. }
    240. } else {
    241. // rfc4122, version 4 form
    242. let r;
    243. // rfc4122 requires these characters
    244. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    245. uuid[14] = '4';
    246. // Fill in random data. At i==19 set the high bits of clock sequence as
    247. // per rfc4122, sec. 4.1.5
    248. for (i = 0; i < 36; i++) {
    249. if (!uuid[i]) {
    250. r = 0 | Math.random() * 16;
    251. uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
    252. }
    253. }
    254. }
    255. return uuid.join('');
    256. }
    257. /**
    258. * 添加千分分隔符
    259. */
    260. export function comdify (n) {
    261. let re = /\d{1,3}(?=(\d{3})+$)/g;
    262. let n1 = n.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) {
    263. return s1.replace(re, "$&,") + s2;
    264. });
    265. return n1;
    266. }
    267. /**
    268. * 去除千分位中的 ‘,’
    269. * @param {Number} num
    270. */
    271. export function delcommafy (num) {//去除千分位中的‘,’
    272. num = num.toString();
    273. num = num.replace(/,/gi, '');
    274. return num;
    275. }
    276. /**
    277. * 获取数据范围的organ以及deptid值
    278. * @param {*} actionCode
    279. * @param {*} data
    280. */
    281. export function getAuthRangeValueAndDeptIds (actionCode = '', data = []) {
    282. let result = {rangeValue: 0, deptIds: ''};
    283. data.forEach(item => {
    284. if (item.actionCode === actionCode) {
    285. if(!item.types) {
    286. result = {rangeValue: '', deptIds: ''};
    287. }
    288. if (item.types && item.types.length > 0 && item.types[0].ranges[0]) {
    289. result.rangeValue = item.types[0].ranges[0].rangeValue;
    290. result.deptIds = item.types[0].ranges[0].deptIds;
    291. }
    292. }
    293. });
    294. return result;
    295. }
    296. /**
    297. * 获取数据范围的organ
    298. * @param {*} actionCode
    299. * @param {*} data
    300. */
    301. export function getAuthRangeValue (actionCode = '', data = []) {
    302. let rangeValue = 0;
    303. data.forEach(item => {
    304. if (item.actionCode === actionCode) {
    305. if (item.types.length > 0 && item.types[0].ranges[0]) {
    306. rangeValue = item.types[0].ranges[0].rangeValue;
    307. }
    308. }
    309. });
    310. return rangeValue;
    311. }
    312. /**
    313. * 获取同城门店数据范围的
    314. * @param {*} actionCode
    315. * @param {*} data
    316. */
    317. export function getSameCityShop (actionCode = '', data = []) {
    318. let deptIds = [];
    319. data.forEach(item => {
    320. if (item.actionCode === actionCode) {
    321. if (item.types.length > 0 && item.types[0].ranges[0]) {
    322. deptIds = item.types[0].ranges[0].deptIds;
    323. }
    324. }
    325. });
    326. if (deptIds && deptIds.length > 0) {
    327. deptIds.forEach((item, index) => {
    328. return deptIds[index] = Number(item)
    329. })
    330. }
    331. return deptIds;
    332. }
    333. /**
    334. * 获取同城门店数据范围的
    335. * @param {*} actionCode
    336. * @param {*} data
    337. */
    338. export function getApiHost () {
    339. let httpHost = CONFIG.API_PROTOCOL() + '//' + CONFIG.API_HOST_NAME();
    340. return httpHost;
    341. }
    342. const formatNumber = n => {
    343. n = n.toString()
    344. return n[1] ? n : '0' + n
    345. }
    346. export const formatDate = (date, symbol = '-') => {
    347. const year = date.getFullYear()
    348. const month = date.getMonth() + 1
    349. const day = date.getDate()
    350. return [year, month, day].map(formatNumber).join(symbol)
    351. }
    352. export const getDateDiff = (startDate, endDate) => {
    353. startDate = formatDate(new Date(startDate))
    354. endDate = formatDate(new Date(endDate))
    355. const startTime = new Date(Date.parse(startDate.replace(/-/g, '/'))).getTime()
    356. const endTime = new Date(Date.parse(endDate.replace(/-/g, '/'))).getTime()
    357. const dates = Math.floor(startTime - endTime) / (1000 * 60 * 60 * 24)
    358. return Math.abs(dates)
    359. }
    360. /**
    361. * 防抖
    362. * @param func
    363. * @param wait
    364. * @param immediate
    365. * @returns {function(...[*]=): *}
    366. */
    367. export function debounce (func, wait, immediate) {
    368. let timeout, args, context, timestamp, result
    369. const later = function () {
    370. // 据上一次触发时间间隔
    371. const last = +new Date() - timestamp
    372. // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    373. if (last < wait && last > 0) {
    374. timeout = setTimeout(later, wait - last)
    375. } else {
    376. timeout = null
    377. // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
    378. if (!immediate) {
    379. result = func.apply(context, args)
    380. if (!timeout) context = args = null
    381. }
    382. }
    383. }
    384. return function (...args) {
    385. context = this
    386. timestamp = +new Date()
    387. const callNow = immediate && !timeout
    388. // 如果延时不存在,重新设定延时
    389. if (!timeout) timeout = setTimeout(later, wait)
    390. if (callNow) {
    391. result = func.apply(context, args)
    392. context = args = null
    393. }
    394. return result
    395. }
    396. }
    397. // 节流
    398. export function throttle (func, delay) {
    399. let prev = Date.now()
    400. return function () {
    401. const context = this
    402. let args = arguments
    403. let now = Date.now()
    404. if (now - prev >= delay) {
    405. func.apply(context, args)
    406. prev = Date.now()
    407. }
    408. }
    409. }
    410. export function isAssetTypeAnVideo (filePath) {
    411. let index = filePath.lastIndexOf('.')
    412. let ext = filePath.substr(index + 1)
    413. return ['mp4', 'mov', 'm4v'].indexOf(ext.toLowerCase()) !== -1
    414. }
    415. /**
    416. * @param {blob} data
    417. * @description 使用 axios 从接口中返回 data 时,发起请求时需要配置 responseType: blob
    418. */
    419. export function downloadFileByBlob (data, {type = 'text', filename = 'untitle.txt'}) {
    420. const url = window.URL.createObjectURL(new Blob([data], {type}));
    421. const link = document.createElement('a');
    422. link.style.display = 'none';
    423. link.href = url;
    424. link.setAttribute('download', filename);
    425. document.body.appendChild(link);
    426. link.click();
    427. document.body.removeChild(link);
    428. }
    429. export default {
    430. urlParse: urlParse,
    431. rank: rank,
    432. moneyFormatStr: moneyFormatStr,
    433. moneyFormatNum: moneyFormatNum,
    434. moneyFloatToInt: moneyFloatToInt,
    435. getMonthDaysTimeRange: getMonthDaysTimeRange,
    436. getSevenDaysTimeRange: getSevenDaysTimeRange,
    437. getTodayTimeRange: getTodayTimeRange,
    438. setSecondToEnd: setSecondToEnd,
    439. isEmptyObj: isEmptyObj,
    440. trim: trim,
    441. getCurrentSystem: getCurrentSystem,
    442. showCaseClass: showCaseClass,
    443. filterOrganName: filterOrganName,
    444. filterEmpty: filterEmpty,
    445. uuid: uuid,
    446. comdify: comdify,
    447. delcommafy: delcommafy,
    448. getSameCityShop: getSameCityShop,
    449. getAuthRangeValueAndDeptIds,
    450. getAuthRangeValue: getAuthRangeValue,
    451. getApiHost: getApiHost,
    452. getDateDiff,
    453. formatDate,
    454. debounce,
    455. throttle,
    456. isAssetTypeAnVideo,
    457. downloadFileByBlob
    458. };