import { save } from "../../resources/api/cntnStats"; /** * 공통 처리 플러그인 */ function prefixZero(number, length) { var zero = ''; number = number.toString(); if (number.length < length) { for (let i = 0; i < length - number.length; i++) { zero += '0'; } } return zero + number; } import Vue from "vue"; import axios from "axios"; import {cmmnFindByCdSystem} from "../../resources/api/codeManage"; import COMMON_UTIL from '../../resources/js/commonUtil'; import useStore from "vuex"; import VueCookies from "vue-cookies"; export default { install(Vue) { // 공통메세지 let messageList = []; // 공통코드 등록 Vue.config.globalProperties.$initCmmnMessage = async function() { try { const params = { cdId: 'messageGroup'}; const res = await cmmnFindByCdSystem(params); if (res.status == 200) { messageList = res.data.data.code.childCdList; } } catch (error) { alert(error.response.data.message); } }, // 메세지 가져오기 Vue.config.globalProperties.$getCmmnMessage = function(code, text) { if(messageList.length == 0){ return ""; } let message = []; let result = ''; // 메세지 코드 구분 확인 if(code.substr(0, 3) === 'msg'){ message = messageList[0].childCdList; }else if(code.substr(0, 3) === 'cnf'){ message = messageList[1].childCdList; }else if(code.substr(0, 3) === 'chk'){ message = messageList[2].childCdList; }else if(code.substr(0, 3) === 'err'){ message = messageList[3].childCdList; }else{ return('등록되지 않은 메세지 입니다.'); } for(let i = 0 ; i < message.length; i++){ if(message[i].cdId == code){ result = message[i].cdVl; break; } } if(result == ''){ return('등록되지 않은 메세지 입니다.'); } if(text != null){ //result = result.replace('{0}', text); return text + result; }else{ //result = result.replace('{0}', ''); return result; } }, //시간 구하기 Vue.config.globalProperties.$getFullTime = function (hour, minute, seconds) { var date = new Date(); var h = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); if (this.$isEmpty(hour) == false) { h += hour; } if (this.$isEmpty(minute) == false) { m += minute; } if (this.$isEmpty(seconds) == false) { s += seconds; } return prefixZero(h, 2) + ":" + prefixZero(m, 2) + ":" + prefixZero(s, 2); } // 공통코드 호출 Vue.config.globalProperties.$getCommonCode = async function (GroupCode) { const promise = new Promise((resolve, reject) => { axios({ url: '/cmmn/code/findByCdSystem.json', method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8' }, data: JSON.stringify({ 'cdId': GroupCode }) }).then(function (response) { resolve(response.data.data.code.childCdList) }).catch(function (error) { resolve('cancle') }); }); return promise.then( (data) => { return data; } ).catch(function (err) { console.log(err) return []; }); } // 공통코드 값 매핑 Vue.config.globalProperties.$getCommonCodeValue = function (cmmnCode, code) { for(let i = 0 ; i < cmmnCode.length; i++){ if(cmmnCode[i].cdId == code){ return cmmnCode[i].cdNm; } } return code; } // 빈값체크 Vue.config.globalProperties.$isEmpty = function (data) { if (data === undefined || data === null || data === "" || data.length === 0 || (data.constructor == Object && Object.keys(data).length === 0)) { if ((typeof data) === "number") { return false } else { return true; } } else { return false; } } /** * 일시에서 시,분,초 자르기 */ Vue.config.globalProperties.$yyyymmdd = function (data) { if (data === null || data === "") { return "-"; } else { let date = data.substr(0, 10); return date; } } /** * 전화번호 출력 시 '-'을 추가하여 출력 */ Vue.config.globalProperties.$HyphenMinus = function (phoneNumber) { if (!phoneNumber) return phoneNumber; phoneNumber = phoneNumber.replace(/[^0-9]/g, ""); let tmp = ""; if (phoneNumber.length < 4) { return phoneNumber; } else if (phoneNumber.length < 7) { tmp += phoneNumber.substr(0, 3); tmp += "-"; tmp += phoneNumber.substr(3); return tmp; } else if (phoneNumber.length == 8) { tmp += phoneNumber.substr(0, 4); tmp += "-"; tmp += phoneNumber.substr(4); return tmp; } else if (phoneNumber.length < 10) { if (phoneNumber.substr(0, 2) == "02") { //02-123-5678 tmp += phoneNumber.substr(0, 2); tmp += "-"; tmp += phoneNumber.substr(2, 3); tmp += "-"; tmp += phoneNumber.substr(5); return tmp; } } else if (phoneNumber.length < 11) { if (phoneNumber.substr(0, 2) == "02") { //02-1234-5678 tmp += phoneNumber.substr(0, 2); tmp += "-"; tmp += phoneNumber.substr(2, 4); tmp += "-"; tmp += phoneNumber.substr(6); return tmp; } else { //010-123-4567 tmp += phoneNumber.substr(0, 3); tmp += "-"; tmp += phoneNumber.substr(3, 3); tmp += "-"; tmp += phoneNumber.substr(6); return tmp; } } else { //010-1234-5678 tmp += phoneNumber.substr(0, 3); tmp += "-"; tmp += phoneNumber.substr(3, 4); tmp += "-"; tmp += phoneNumber.substr(7); return tmp; } } /** * 사업자등록번호 xxx-xx-xxxx 양식으로 출력 */ Vue.config.globalProperties.$hyphenBrno = function (brno) { if (!brno) return brno; brno = brno.replace(/[^0-9]/g, ""); if (brno.length !== 10) return brno; let formattedBrno = ""; formattedBrno += brno.substr(0, 3); formattedBrno += "-"; formattedBrno += brno.substr(3, 2); formattedBrno += "-"; formattedBrno += brno.substr(5, 5); return formattedBrno; } /** * 오늘 년-월-일 구하기 */ Vue.config.globalProperties.$today = function () { let date = new Date(); let today = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().substring(0, 10); return today; } /** * 일주일전 년-월-일 구하기 */ Vue.config.globalProperties.$oneWeekAgo = function () { let date = new Date(); // 현재 날짜에서 7일을 빼줍니다. let oneWeekAgoDate = new Date(date.getTime() - (6 * 24 * 60 * 60 * 1000) - (date.getTimezoneOffset() * 60000)).toISOString().substring(0, 10); return oneWeekAgoDate; } /** * N달전 년-월-일 구하기 */ Vue.config.globalProperties.$nMonthAgo = function (today, nMonth) { let date = new Date(today); let oneMonthAgo = new Date(date.setMonth(date.getMonth() - nMonth) - (date.getTimezoneOffset() * 60000)).toISOString().substring(0, 10); return oneMonthAgo; }, /** * N년전 년-월-일 구하기 */ Vue.config.globalProperties.$nYearAgo = function (today, nYear) { let date = new Date(today); let nYearAgo = new Date(date.setFullYear(date.getFullYear() - nYear) - (date.getTimezoneOffset() * 60000)).toISOString().substring(0, 4); return nYearAgo; }, /* 유효성 검사 */ // 아이디 정규식(5~20자의 영문 소문자, 숫자와 특수기호(_),(-)만 사용) Vue.config.globalProperties.$idCheck = function (data) { let validateId = /^(?=.*[a-z])[a-z0-9_-]{5,20}$/; if (validateId.test(data) === true) return true; return false; } // 비밀번호 정규식(6~20자의 영문 대문자, 소문자, 숫자, 특수문자를 사용) Vue.config.globalProperties.$pwCheck = function (data) { let validatePw = /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{6,20}$/; if (validatePw.test(data) === true) return true; return false; } // 이메일 형식 검사 Vue.config.globalProperties.$email = function (email) { const emailPattern = /^[A-Za-z0-9_\\.\\-]+@[A-Za-z0-9\\-]+\.[A-Za-z0-9\\-\\.]+$/; try { return emailPattern.test(email); } catch (e) { return false; } } //사용자명 정규식 Vue.config.globalProperties.$checkName = function (data) { let validateEmail = /^[가-힣a-zA-Z]{1,20}$/; if (validateEmail.test(data) === true) return true; return false; } // IPv4 정규식 Vue.config.globalProperties.$ipv4 = function (ip) { let validateIPv4 = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/; if (validateIPv4.test(ip) === true) return true; return false; } // 3글자 마다 콤마 찍기 (돈) Vue.config.globalProperties.$comma = function (text) { try { return text.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } catch (e) { if (text === undefined || text === null || text === "" || text.length === 0) { return "-"; } else { return text; } } } // 사업자번호 정규식(10자리) Vue.config.globalProperties.$businessNumber = function (data) { let validateBusinessNumber = /^\d{10}$/; if (validateBusinessNumber.test(data) === true) return true; return false; } // 이메일 마스킹 처리 Vue.config.globalProperties.$email_format_WithMasking = function (mail) { if (mail != null && mail != '' && mail.length > 3) { return mail.replace(/(?<=.{3})./gi, '*'); }else{ return mail; } } // 전화번호 마스킹 처리 Vue.config.globalProperties.$number_format_WithMasking = function (number) { if (number != null && number != '') { let maskNumber = ''; let phone = COMMON_UTIL.getMask(number); let phoneParts = phone.split('-'); if (phoneParts.length === 3) { let middlePartMasked = phoneParts[1].replace(/[0-9]/g, '*'); maskNumber = phoneParts[0] + '-' + middlePartMasked + '-' + phoneParts[2]; } else { return phone; } return maskNumber; } else { return ""; } } // 첨부파일 다운로드 로직 Vue.config.globalProperties.$getImageDown = async function (fileId) { try { const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); return url; } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } // 첨부파일 다운로드 로직 Vue.config.globalProperties.$imageDown = async function (file , srcId) { try { const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: file.fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); document.getElementById(srcId).src= url; } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } // 첨부파일 다운로드 로직(파일아이디, 파일명 ) Vue.config.globalProperties.$imageDownByid = async function (fileId, srcId) { try { const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); document.getElementById(srcId).src= url; } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } //첨부파일 다운로드 로직 Vue.config.globalProperties.$downloadFile = async function (file,auth) { if(auth.fileDwnldAuthrtYn != 'Y'){ alert('파일다운로드 권한이 없습니다.'); return false; } try { this.$store.commit('setLoading', true); const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: file.fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', file.fileNm+'.'+file.extnNm); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); if(!this.$route.path.startsWith('/kdm')) { let menuId = this.$store.state.currentMenu let bbsMngId = this.$route.meta.typeId; let bbsId= null; const roles = this.$store.state.roles.map(auth => auth.authority); if(bbsMngId === 'CONTS_0000000063') { bbsMngId = 'BBS_MNG_0000000004'; } if(this.$route.query.pageId) { bbsId = this.$route.query.pageId; } let data = { 'typeId' : 'FILE', 'menuId' : menuId, 'fileMngId': file.fileMngId, 'fileId': file.fileId ,'bbsMngId' : bbsMngId, 'bbsId' : bbsId, 'mbrAuthList' : roles } await save(data) } } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); alert(this.$getCmmnMessage("err024")) }finally{ this.$store.commit('setLoading', false); } } //첨부파일 다운로드 로직 Vue.config.globalProperties.$downloadFileById = async function (fileid, fileMngID, fileNm,bbsId,auth) { if(auth.fileDwnldAuthrtYn != 'Y'){ alert('파일다운로드 권한이 없습니다.'); return false; } try { this.$store.commit('setLoading', true); const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: fileid, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', fileNm); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); if(!this.$route.path.startsWith('/kdm')) { let menuId = this.$store.state.currentMenu // 공통관리파일 let bbsMngId = 'BBS_MNG_0000000007' const roles = this.$store.state.roles.map(auth => auth.authority); let data = { 'typeId' : 'FILE', 'menuId' : menuId, 'fileMngId': fileMngID , 'fileId': fileid ,'bbsMngId' : bbsMngId, 'bbsId' : bbsId, 'mbrAuthList' : roles } await save(data) } } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); alert(this.$getCmmnMessage("err024")) }finally{ this.$store.commit('setLoading', false); } } // PDF 뷰어오픈 Vue.config.globalProperties.$openPdfViewr= async function (file, object, modal) { try { const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: file.fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); object = url; modal = true; } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } // PDF 뷰어오픈 Vue.config.globalProperties.$openPdfViewrWindwow = function (fileid, pageAuth) { if(pageAuth.fileDwnldAuthrtYn != 'Y'){ alert('파일확인 권한이 없습니다.'); return false; } try { this.$store.commit('setPdfId', fileid); window.open( "/cmmn/pdfView.page", '_blank' ); } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } //첨부파일 다운로드 로직(권한체크 제외) Vue.config.globalProperties.$downloadFileByIdNoAuthCheck = async function (fileid, fileMngID, fileNm,bbsId) { try { this.$store.commit('setLoading', true); const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: fileid, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', fileNm); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); if(!this.$route.path.startsWith('/kdm')) { let menuId = this.$store.state.currentMenu // 공통관리파일 let bbsMngId = 'BBS_MNG_0000000007' const roles = this.$store.state.roles.map(auth => auth.authority); let data = { 'typeId' : 'FILE', 'menuId' : menuId, 'fileMngId': fileMngID , 'fileId': fileid ,'bbsMngId' : bbsMngId, 'bbsId' : bbsId, 'mbrAuthList' : roles } await save(data) } } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); alert(this.$getCmmnMessage("err024")) }finally{ this.$store.commit('setLoading', false); } } //첨부파일 다운로드(권한검사 X) - 회원가입 Vue.config.globalProperties.$downloadFileNoAuthCheck = async function (file) { try { this.$store.commit('setLoading', true); const response = await axios({ url: '/file/fileDownload.json', // URL 경로 확인 method: 'post', headers: { 'Content-Type': 'application/json; charset=UTF-8', Authorization: this.$store.state.authorization, }, data: file.fileId, responseType: 'blob', }); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', file.fileNm+'.'+file.extnNm); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); alert(this.$getCmmnMessage("err024")) }finally{ this.$store.commit('setLoading', false); } } // PDF 뷰어오픈 Vue.config.globalProperties.$openPdfViewrWindwowNoAuthCheck = function (fileid) { if(pageAuth.fileDwnldAuthrtYn != 'Y'){ alert('파일확인 권한이 없습니다.'); return false; } try { this.$store.commit('setPdfId', fileid); window.open( "/cmmn/pdfView.page", '_blank' ); } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } // PDF 뷰어오픈 Vue.config.globalProperties.$reportViwer = function () { try { window.open( "/cmmn/ReportViewer.page", '_blank' ); } catch (error) { // alert('파일 다운로드 중 오류가 발생했습니다.'); } } // 메뉴 상단 베너 정보 Vue.config.globalProperties.$getMenuInfo = function () { return this.$store.state.menuInfo; } // PageNavigation 정보 Vue.config.globalProperties.$getPageNaviInfo = function () { return this.$store.state.pageNaviInfo; } // PageNavigation 정보 Vue.config.globalProperties.$setLoading = function (bool) { this.$store.commit('setLoading', bool); } // 최상단 이동 Vue.config.globalProperties.$scrollToTop = function (bool) { window.scrollTo({ top: 0, behavior: 'smooth' // 부드럽게 스크롤 }); } // 최상단 이동 Vue.config.globalProperties.$replaceImagePath = function (val) { if(val != null){ return val.replace(/\\/gi, "/").replace(/c\:/gi,""); }else{ return val; } } //unix -> date 변환 Vue.config.globalProperties.$formatUnixToDate = function (val) { const date = new Date(val); const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } //이름 마스킹 처리 Vue.config.globalProperties.$name_format_WithMasking = function (name) { if (name != null && name != '') { if (name.length === 2) { return name.charAt(0) + '*'; } else if (name.length === 3) { return name.charAt(0) + '*' + name.charAt(2); } else if (name.length >= 4) { return name.charAt(0) + '*'.repeat(name.length - 2) + name.charAt(name.length - 1); } } return name; } //토큰 저장 Vue.config.globalProperties.$setToken = function (setAuthorization, setRefresh) { this.$store.commit("setAuthorization", setAuthorization); //this.$store.commit("setRefresh", setRefresh.refresh); // this.$cookies.set('refresh',res.headers.refresh) /** jwt토큰 복호화 **/ const base64String = setAuthorization.split('.')[1]; const base64 = base64String.replace(/-/g, '+').replace(/_/g, '/'); const mbr = JSON.parse(decodeURIComponent(window.atob(base64).split('').map(function (c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join(''))); this.$store.commit("setMbrId", mbr.mbrId); this.$store.commit("setMbrNm", mbr.mbrEncptFlnm); this.$store.commit("setAuthrtNm", mbr.authrtNm); this.$store.commit("setAuthrtTypeLvl", mbr.authrtTypeLvl); this.$store.commit('setRoles', mbr.roles); return mbr.roles; } } }