持续集成分支

This commit is contained in:
aixianling
2024-10-31 14:34:57 +08:00
parent 6a833be062
commit 8c56cf808b
2165 changed files with 4116 additions and 8716 deletions

6045
library/common/crypto-js.js Normal file

File diff suppressed because it is too large Load Diff

68
library/common/dict.js Normal file
View File

@@ -0,0 +1,68 @@
/**
* 封装字典工具类
*/
const $dict = {
instance: null,
url: "/admin/dictionary/queryValsByCodeList",
init(config) {
this.instance = config?.instance
this.url = config?.url || this.url
},
dicts() {
let dicts = uni.getStorageSync('dicts') || null
typeof dicts == "string" ? (dicts = JSON.parse(dicts)) : dicts
return dicts || [];
},
load(...code) {
return this.instance && this.instance.post(this.url, null, {
withoutToken: true,
params: {
codeList: code.toString()
}
}).then((res) => {
if (res && res.data) {
let cacheDicts = {},
meta = {};
this.dicts().map((e) => (cacheDicts[e.key] = e));
res.data.map((e) => (meta[e.key] = e));
let dicts = {...cacheDicts, ...meta};
uni.setStorageSync('dicts', Object.values(dicts));
}
})
},
getDict(key) {
if (this.dicts().length) {
let dict = this.dicts().find((e) => e.key == key);
return dict ? dict.values : [];
} else return [];
},
getValue(key, label) {
if (this.dicts().length) {
let dict = this.dicts().find((e) => e.key == key);
if (dict) {
let item = dict.values.find((v) => v.dictName == label);
return item ? item.dictValue : label;
} else return label;
} else return label;
},
getLabel(key, value) {
if (this.dicts().length) {
let dict = this.dicts().find((e) => e.key == key);
if (dict) {
let item = dict.values.find((v) => v.dictValue == value);
return item ? item.dictName : value;
} else return value ? value : '';
} else return value ? value : '';
},
getColor(key, value) {
if (this.dicts().length) {
let dict = this.dicts().find((e) => e.key == key);
if (dict) {
let item = dict.values.find((v) => v.dictValue == value);
return item ? item.dictColor : value;
} else return value;
} else return value;
}
}
export default $dict

38
library/common/http.js Normal file
View File

@@ -0,0 +1,38 @@
import axios from 'axios'
const instance = axios.create({
timeout: 600000,
withCredentials: true,
})
const getToken = () => {
let vuex = uni.getStorageSync("vuex")
return !!vuex ? JSON.parse(vuex).user.token : null
}
const source = axios.CancelToken.source();
let throttleMap = {}
instance.interceptors.request.use(config => {
if (config.throttle) {// 节流处理
let timer = throttleMap[config.url]
if (!!timer) {
config.cancelToken = source.token
source.cancel("节流控制,取消请求:" + config.url)
}
throttleMap[config.url] = setTimeout(() => {
throttleMap[config.url] = null
}, config.throttle)
}
if (config.withoutToken) {
return config
} else if (getToken()) {
config.headers["Authorization"] = getToken()
} else {
config.cancelToken = source.token
source.cancel("用户未验证,取消请求:" + config.url)
}
return config
}, err => {
console.error(err)
return Promise.reject(err)
})
export default instance

465
library/common/modules.js Normal file
View File

@@ -0,0 +1,465 @@
import http from "./http";
import Vue from "vue";
import CryptoJS from "./crypto-js";
import qs from 'query-string'
import dayjs from "./monent";
/**
* 用户登录信息和方法
*/
export const user = {
state: () => ({
token: "",
expiredTime: ""
}),
mutations: {
setToken(state, token) {
state.token = token
},
setExpired(state, time) {
state.expiredTime = time
},
setUser(state, user) {
for (const key in user) {
Vue.set(state, key, user[key])
}
},
initWaterMarker(state) {
const waterMarked = document.querySelector('#waterMarker')
if (!waterMarked && state?.name) {
const waterMarker = document.createElement('div')
waterMarker.id = 'waterMarker'
waterMarker.style.position = 'absolute'
waterMarker.style.display = 'flex'
waterMarker.style.flexWrap = 'wrap'
waterMarker.style.overflow = 'hidden'
waterMarker.style.justifyContent = 'space-between'
waterMarker.style.alignContent = 'flex-start'
waterMarker.style.zIndex = '2'
waterMarker.style.top = '0'
waterMarker.style.color = 'rgba(153,153,153,.2)'
waterMarker.style.width = '100%'
waterMarker.style.height = '100%'
waterMarker.style.pointerEvents = 'none'
for (let i = 0; i < 200; i++) {
const markerItem = document.createElement('div')
markerItem.style.fontSize = '14px'
markerItem.style.padding = '30px'
markerItem.style.height = '80px'
markerItem.style.transform = 'rotate(-20deg)'
markerItem.style.flexShrink = '0'
markerItem.innerText = state?.name + state?.phone?.slice(-4) || "未授权"
waterMarker.appendChild(markerItem)
}
document.querySelector('uni-page-body')?.appendChild(waterMarker)
}
// canvas 方案
// const HiDPICanvas = (w, h, ratio) => {
// const PIXEL_RATIO = () => {
// const c = document.createElement("canvas"),
// ctx = c.getContext("2d"),
// dpr = window.devicePixelRatio || 1,
// bsr = ctx['webkitBackingStorePixelRatio'] ||
// ctx['mozBackingStorePixelRatio'] ||
// ctx['msBackingStorePixelRatio'] ||
// ctx['oBackingStorePixelRatio'] ||
// ctx['backingStorePixelRatio'] || 1;
//
// return dpr / bsr;
// }
// if (!ratio) {
// ratio = PIXEL_RATIO();
// }
// const can = document.createElement("canvas");
// can.width = w * ratio;
// can.height = h * ratio;
// can.style.width = w + "px";
// can.style.height = h + "px";
// can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);
// return can
// }
// if (!waterMarked && state.user?.name) {
// const canvas = HiDPICanvas(100, 40)
// canvas.style.display = 'none';
// let ctx = canvas.getContext("2d")
// ctx.rotate(-20 * Math.PI / 180);
// ctx.fillStyle = 'rgba(153,153,153,.3)';
// ctx.textAlign = 'left';
// ctx.textBaseline = 'middle';
// ctx.font = "lighter 7px 'Microsoft YaHei UI',sans-serif"
// ctx.fillText(state.user?.name + state.user?.phone?.slice(-4) || "未授权", 0, 30);
// const waterMarker = document.createElement('div')
// if (waterMarker) {
// waterMarker.id = 'waterMarker'
// waterMarker.style.position = 'absolute'
// waterMarker.style.zIndex = '2'
// waterMarker.style.top = '0'
// waterMarker.style.width = '100%'
// waterMarker.style.height = '100%'
// waterMarker.style.pointerEvents = 'none'
// waterMarker.style.backgroundImage = "url(" + canvas.toDataURL("image/png") + ")"
// document.querySelector('uni-page-body').appendChild(waterMarker)
// }
// }
}
},
actions: {
getToken({commit, dispatch}, params) {
const encryptByDES = password => {
let isIos = uni.getSystemInfoSync().system.toUpperCase == 'ios'
let key = "thanks,villcloud"
let iv = CryptoJS.enc.Latin1.parse(key)
let encrypted = CryptoJS.AES.encrypt(password, iv, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
if (isIos) {
return encodeURIComponent(encrypted.toString());
} else {
return encrypted.toString();
}
}
let {module, code} = params, action = "/auth/oauth/token"
if (!!code) {
action = "/auth/wechatcp/token"
}
return http.post(action, params, {
withoutToken: true,
module,
params: {
...params, grant_type: 'password',
password: encryptByDES(params.password)
},
headers: {
Authorization: "Basic d2VjaGF0OndlY2hhdA=="
}
}).then(res => {
if (res?.access_token) {
const token = [res?.token_type, res?.access_token].join(" ").trim()
commit("setToken", token)
commit("setExpired", dayjs().add(res.expires_in - 600,'s').unix())
return token
} else return Promise.reject(res.msg)
})
},
getWechatToken({commit, dispatch}, params) {
const encryptByDES = password => {
let isIos = uni.getSystemInfoSync().system.toUpperCase == 'ios'
let key = "thanks,villcloud"
let iv = CryptoJS.enc.Latin1.parse(key)
let encrypted = CryptoJS.AES.encrypt(password, iv, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
if (isIos) {
return encodeURIComponent(encrypted.toString());
} else {
return encrypted.toString();
}
}
let {module} = params, action = "/auth/wechat-mp/token"
return http.post(action, params, {
withoutToken: true,
throttle: 500,
module,
params: {
...params, grant_type: 'password',
password: encryptByDES(params.password)
},
headers: {
Authorization: "Basic d3htcDp3eG1w"
}
}).then(res => {
if (res?.access_token) {
const token = [res?.token_type, res?.access_token].join(" ").trim()
commit("setToken", token)
return token
} else return Promise.reject(res.msg)
})
},
getAccount({dispatch, commit}, config) {
//获取企业微信后台账号信息
return http.post("/admin/user/detail-phone", null, config).then(res => {
if (res?.data) {
commit('setUser', res.data)
return Promise.all([dispatch('getGridInfo', config)])
}
})
},
getGridInfo({commit}, config) {
//获取登录着网格员信息
return http.post("/app/appgirdmemberinfo/checkLogOnUser", null, config).then(res => {
if (res?.data) {
let {
girdId,
girdMemberId,
girdName,
checkType: girdCheckType,
appGirdInfo: gridInfo,
isSign
} = res.data,
gridExtra = {isSign}
return commit("setUser", {girdId, girdMemberId, girdName, girdCheckType, gridInfo, gridExtra})
}
}).catch(() => 0)
}
}
}
/**
* 企微jssdk功能
*/
let timer = {}
/**
* 微信oauth2跳转链接
* https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&agentid=AGENTID#wechat_redirect
* @param params
*/
const oauth2Weixin = params => {
const redirect = qs.parseUrl(location.href)
delete redirect.query.code
delete redirect.query.state
return qs.stringifyUrl({
url: "https://open.weixin.qq.com/connect/oauth2/authorize",
query: {
response_type: 'code',
redirect_uri: qs.stringifyUrl(redirect),
...params
},
fragmentIdentifier: "wechat_redirect"
})
}
export const wxwork = {
state: () => ({
agentSignURL: "",
apiList: [],
config: {}
}),
mutations: {
setConfig(state, config) {
state.config = config
},
setAgentSignURL(state, url) {
state.agentSignURL = url
},
setApiList(state, list) {
state.apiList = list
},
},
actions: {
agentSign({state, commit}, params = {}) {
//授权jssdk在url上使用,并获取corpId
let url = window.location.href
if (state.agentSignURL == url && state.config.corpId) {
return Promise.resolve()
} else {
commit("setAgentSignURL", url)
commit("setApiList", [])
let action = "/app/wxcp/portal/agentSign"
if (!!params?.action) {
action = params.action
delete params.action
} else if (!!params?.suiteId) {
action = "/app/wxcptp/portal/agentSign"
}
return http.post(action, null, {
withoutToken: true,
params: {...params, url}
}).then(res => {
if (res?.data) {
let config = {
...params,
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
beta: true,// 必须这么写否则wx.invoke调用形式的jsapi会有问题
corpId: res.data.corpid, // 必填企业微信的corpid必须与当前登录的企业一致
agentId: res.data.agentid, // 必填企业微信的应用id e.g. 1000247
timestamp: Number(res.data.timestamp), // 必填,生成签名的时间戳
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
signature: res.data.signature,// 必填,签名,见 附录-JS-SDK使用权限签名算法
...res.data
}
commit("setConfig", config)
return config
}
}).catch(err => {
console.error(err)
})
}
},
getCode({state, dispatch}, params) {
const excute = (tryAgentSign = false) => {
if (!params?.appid) {
let {corpid: corpId, suiteId, agentid} = state.config, scope = "snsapi_privateinfo"
if (/AppForm/.test(location.pathname)) {
scope = "snsapi_base"
} else if (suiteId) {
corpId = suiteId
scope = "snsapi_privateinfo"
}
params = {appid: corpId, agentid, scope}
return new Promise((resolve, reject) => {
if (corpId && scope) {
const oauthURL = oauth2Weixin(params)
location.replace(oauthURL)
} else if (!tryAgentSign && corpId) {
dispatch("agentSign", {corpId, suiteId}).then(() => excute(true)).then(() => resolve())
} else reject("URL缺少必要参数(corpId)")
})
} else new Promise(() => {
const oauthURL = oauth2Weixin(params)
location.replace(oauthURL)
})
}
return excute()
},
injectJWeixin({state, commit, rootState}, apis) {
const inject = (jsApiList) => new Promise((resolve, reject) => {
jsApiList = jsApiList || []
if (timer.injectJWeixin) {//节流设置,500ms内的多次请求合并到一处
clearTimeout(timer.injectJWeixin)
jsApiList = [...new Set([...state.apiList, ...jsApiList])]
commit("setApiList", jsApiList)
}
timer.injectJWeixin = setTimeout(() => {
const sdk = wx?.agentConfig ? wx : jWeixin
console.log("agentConfig配置:")
console.log({...state.config, jsApiList})
sdk?.agentConfig({
...state.config, jsApiList,
success: res => resolve(res),
fail: err => {
console.error(err)
reject(err)
}
})
}, 500)
})
return inject([apis].flat().filter(Boolean))
},
injectSDK({dispatch, state}, apis) {
const {corpId, suiteId} = state.config
return dispatch('agentSign', {corpId, suiteId}).then(() => dispatch("injectJWeixin", [apis].flat()))
},
previewFile({dispatch}, op) {
if (window.navigator.userAgent.indexOf("Windows NT") > -1) {
uni.showToast({
title: "企业微信暂不支持PC端的预览文件!",
icon: 'none'
})
} else {
dispatch("injectSDK", "previewFile").then(() => {
setTimeout(() => {
let sdk = typeof wx?.invoke == 'function' ? wx : jWeixin
sdk?.invoke('previewFile', {...op}, res => {
console.log(res)
})
}, 500)
})
}
},
closeAgent({dispatch}) {
dispatch("injectSDK", "closeWindow").then(() => {
setTimeout(() => {
let sdk = typeof wx?.closeWindow == 'function' ? wx : jWeixin
sdk?.closeWindow()
}, 500)
})
},
selectEnterpriseContact({dispatch}, params) {
return new Promise((resolve, reject) => {
dispatch("injectSDK", "selectEnterpriseContact").then(() => {
setTimeout(() => {
let sdk = typeof wx?.invoke == 'function' ? wx : jWeixin
sdk?.invoke("selectEnterpriseContact", {
fromDepartmentId: -1,
mode: "multi",
type: ["user"],
...params
}, res => {
if (res.err_msg == "selectEnterpriseContact:ok") {
if (typeof res.result == 'string') {
res.result = JSON.parse(res.result)
}
resolve(res.result)
} else {
reject(res)
}
})
}, 500)
})
})
},
selectPrivilegedContact({dispatch}, params) {
return new Promise((resolve, reject) => {
dispatch("injectSDK", "selectPrivilegedContact").then(() => {
setTimeout(() => {
let sdk = typeof wx?.invoke == 'function' ? wx : jWeixin
sdk?.invoke("selectPrivilegedContact", {
fromDepartmentId: -1,
mode: "multi",
...params
}, res => {
if (res.err_msg == "selectPrivilegedContact:ok") {
if (typeof res.result == 'string') {
res.result = JSON.parse(res.result)
}
resolve(res.result)
} else {
reject(res)
}
})
}, 300)
}).catch(() => {
reject('error')
})
})
},
initOpenData({dispatch, commit, state}, params = {}) {
const loadSdk = (count = 0) => {
if (!!window?.WWOpenData) {
const canvas = params?.canvas
if (canvas) delete params.canvas
if (timer.initOpenData) {
clearTimeout(timer.initOpenData)
}
const init = () => canvas ? dispatch('initCanvas') : dispatch('bindElements')
timer.initOpenData = setTimeout(() => {
window?.WWOpenData?.checkSession({
success: () => init(),
fail: () => {
dispatch("injectSDK", "initWwOpenData").then(() => init())
}
})
}, 500)
} else if (count > 10) {
console.log("无法获取WWOpenData")
} else {
setTimeout(() => {
loadSdk(++count)
}, 200)
}
}
dispatch("injectSDK", "initWwOpenData").then(() => loadSdk())
},
bindElements() {
const nodes = document.querySelectorAll('.AiOpenData')
window.WWOpenData?.bindAll(nodes)
},
initCanvas() {
window.WWOpenData?.initCanvas()
},
transCanvas(store, items) {
return new Promise((resolve, reject) => {
window.WWOpenData?.prefetch({items}, (err, data) => {
err ? reject(err) : resolve(data)
})
})
},
shareToExternalChat({dispatch}, params) {
return new Promise(resolve => {
dispatch("injectSDK", "shareToExternalChat").then(() => window?.wx?.invoke("shareToExternalChat", {...params}, resolve) || console.error("jssdk未成功加载!"))
})
}
}
}

7
library/common/monent.js Normal file
View File

@@ -0,0 +1,7 @@
let relativeTime = require('dayjs/plugin/relativeTime');
require('dayjs/locale/zh-cn');
let dayjs_plugin_duration = require('dayjs/plugin/duration');
const dayjs = require("dayjs");
dayjs.extend(dayjs_plugin_duration);
dayjs.extend(relativeTime);
export default dayjs

View File

@@ -0,0 +1,68 @@
import http from "./http"
/**
* 观察者工具对象,用于前端接口监测
*/
class Observer {
constructor() {
this.initXHRObserver()
}
static saveLog(item = {}) {
const api = {
method: item.method,
path: item.url,
url: location.href,
nodeProcess: process.env.NODE_ENV,
status: item.response?.code || item.status,
code: item.response?.code,
error: item.response?.code != 0 ? item.response?.data : null,
device: navigator.userAgent
}
if (!/(sockjs-node|hot-update|monitorApi|frontjs|apiForward)/.test(api.path)) {
if (!!this.timer) {
clearTimeout(this.timer)
}
this.pending = [this.pending, api].flat().filter(Boolean)
this.timer = setTimeout(() => {
http.post("/admin/apiForward", this.pending, {
withoutToken: true,
params: {url: "http://dvcp.cunwuyun.cn/ns/node/monitorApi/addOrUpdate"}
}).then(res => {
if (res?.code == 0) {
this.pending = []
}
}).catch(() => this.cancelOB = true)
}, 5000)
}
}
initXHRObserver() {
const origin = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (...args) {
let send = this.send;
let _this = this
let post_data = []
this.send = function (...data) {
post_data = data;
return send.apply(_this, data)
}
this.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
// 请求后拦截
!this.cancelOB && Observer.saveLog({
url: args[1],
status: this.status,
method: args[0],
data: post_data,
response: JSON.parse(this.response || null)
})
}
}, false)
return origin.apply(this, args)
}
}
}
export default Observer

57
library/common/tree.js Normal file
View File

@@ -0,0 +1,57 @@
class Tree {
constructor(list = [], config) {
this.config = {
key: 'id', parent: 'parentId', children: 'children',
...config
}
this.list = list
if (Array.isArray(list)) this.tree = this.arr2tree(list)
}
arr2tree(list) {
const {key, parent, children} = this.config
const result = []
this.map = {}
const ids = list?.map(e => `#${e[key]}#`)?.toString()
for (const e of list) {
const id = e[key], pid = e[parent]
this.map[id] = {...e, [children]: [this.map[id]?.[children]].flat().filter(Boolean)}
const treeItem = this.map[id]
if (!!pid && ids.indexOf(`#${pid}#`) > -1) {
if (!this.map[pid]) {
this.map[pid] = {
children: []
}
}
this.map[pid].children.push(treeItem)
} else result.push(treeItem)
}
const removeNullChildren = node => {
if (node[children] && node[children].length > 0) {
node[children].map(c => removeNullChildren(c))
} else delete node[children]
}
result.forEach(removeNullChildren)
return result
}
root(id) {
return this.map[id]
}
find(id) {
return this.map[id]
}
every(cb) {
const iterate = list => {
list?.map(e => {
cb(e)
iterate(e.children)
})
}
iterate(this.tree)
}
}
export default Tree

340
library/common/util.js Normal file
View File

@@ -0,0 +1,340 @@
import dict from "./dict"
import dayjs from './monent'
import qs from 'query-string'
import reg from "./regular";
import tree from "./tree";
const confirm = (content, title, config) => {
let ops = {content}
if (typeof title == 'object') {
ops = {...ops, ...title}
} else ops = {...ops, title: title || "提示"}
return new Promise((resolve, reject) => {
uni.showModal({
...ops, ...config, success: res => {
if (res?.confirm) {
resolve()
} else if (res?.cancel) {
reject()
}
}
})
})
}
/**
* 获取年龄
* @param code
*/
const calcAge = (code) => {
let birthday
if (typeof code == 'string' && code.length == 18) {
birthday = dayjs(code.substring(6, 14), 'YYYYMMDD')
} else if (typeof code == 'object') {
birthday = code
}
return Math.ceil(dayjs().year() - dayjs(birthday).year())
}
/**
* 身份证工具包
*/
const idCardNoUtil = {
/*省,直辖市代码表*/
provinceAndCitys: {
11: "北京",
12: "天津",
13: "河北",
14: "山西",
15: "内蒙古",
21: "辽宁",
22: "吉林",
23: "黑龙江",
31: "上海",
32: "江苏",
33: "浙江",
34: "安徽",
35: "福建",
36: "江西",
37: "山东",
41: "河南",
42: "湖北",
43: "湖南",
44: "广东",
45: "广西",
46: "海南",
50: "重庆",
51: "四川",
52: "贵州",
53: "云南",
54: "西藏",
61: "陕西",
62: "甘肃",
63: "青海",
64: "宁夏",
65: "新疆",
71: "台湾",
81: "香港",
82: "澳门",
91: "国外"
},
/*每位加权因子*/
powers: ["7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"],
/*第18位校检码*/
parityBit: ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"],
/*性别*/
genders: {male: "男", female: "女"},
/*校验地址码*/
checkAddressCode: function (addressCode) {
const check = /^[1-9]\d{5}$/.test(addressCode)
if (!check) return false
return !!idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(0, 2))]
},
/*校验日期码*/
checkBirthDayCode: function (birDayCode) {
const check = /^[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))$/.test(birDayCode)
if (!check) return false
const yyyy = parseInt(birDayCode.substring(0, 4), 10)
const mm = parseInt(birDayCode.substring(4, 6), 10)
const dd = parseInt(birDayCode.substring(6), 10)
const xdata = new Date(yyyy, mm - 1, dd)
if (xdata > new Date()) {
return false //生日不能大于当前日期
} else return (xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd)
},
/*计算校检码*/
getParityBit: function (idCardNo) {
const id17 = idCardNo.substring(0, 17)
/*加权 */
let power = 0
for (let i = 0; i < 17; i++) {
power += parseInt(id17.charAt(i), 10) * parseInt(idCardNoUtil.powers[i])
}
/*取模*/
const mod = power % 11
return idCardNoUtil.parityBit[mod]
},
/*验证校检码*/
checkParityBit: function (idCardNo) {
const parityBit = idCardNo.charAt(17).toUpperCase()
return idCardNoUtil.getParityBit(idCardNo) == parityBit
},
/*校验15位或18位的身份证号码*/
checkIdCardNo: function (idCardNo) {
//15位和18位身份证号码的基本校验
const check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo)
if (!check) return false
//判断长度为15位或18位
if (idCardNo.length == 15) {
return idCardNoUtil.check15IdCardNo(idCardNo)
} else if (idCardNo.length == 18) {
return idCardNoUtil.check18IdCardNo(idCardNo)
} else {
return false
}
},
//校验15位的身份证号码
check15IdCardNo: function (idCardNo) {
//15位身份证号码的基本校验
let check = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}$/.test(idCardNo)
if (!check) return false
//校验地址码
const addressCode = idCardNo.substring(0, 6)
check = idCardNoUtil.checkAddressCode(addressCode)
if (!check) return false
const birDayCode = '19' + idCardNo.substring(6, 12)
//校验日期码
return idCardNoUtil.checkBirthDayCode(birDayCode)
},
//校验18位的身份证号码
check18IdCardNo: function (idCardNo) {
//18位身份证号码的基本格式校验
let check = /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/.test(idCardNo)
if (!check) return false
//校验地址码
const addressCode = idCardNo.substring(0, 6)
check = idCardNoUtil.checkAddressCode(addressCode)
if (!check) return false
//校验日期码
const birDayCode = idCardNo.substring(6, 14)
check = idCardNoUtil.checkBirthDayCode(birDayCode)
if (!check) return false
//验证校检码
return idCardNoUtil.checkParityBit(idCardNo)
},
formateDateCN: function (day) {
const yyyy = day.substring(0, 4)
const mm = day.substring(4, 6)
const dd = day.substring(6)
return yyyy + '-' + mm + '-' + dd
},
//获取信息
getIdCardInfo: function (idCardNo) {
let aday
let idCardInfo = {
gender: "", //性别
birthday: "", // 出生日期(yyyy-mm-dd)
sex: ""//系统性别码
}
if (idCardNo.length == 15) {
aday = '19' + idCardNo.substring(6, 12)
idCardInfo.birthday = idCardNoUtil.formateDateCN(aday)
if (parseInt(idCardNo.charAt(14)) % 2 == 0) {
idCardInfo.gender = idCardNoUtil.genders.female
} else {
idCardInfo.gender = idCardNoUtil.genders.male
}
} else if (idCardNo.length == 18) {
aday = idCardNo.substring(6, 14)
idCardInfo.birthday = idCardNoUtil.formateDateCN(aday)
if (parseInt(idCardNo.charAt(16)) % 2 == 0) {
idCardInfo.gender = idCardNoUtil.genders.female
} else {
idCardInfo.gender = idCardNoUtil.genders.male
}
idCardInfo.sex = "" + Number(idCardNo.substring(16, 17)) % 2
}
return idCardInfo
},
/*18位转15位*/
getId15: function (idCardNo) {
if (idCardNo.length == 15) {
return idCardNo
} else if (idCardNo.length == 18) {
return idCardNo.substring(0, 6) + idCardNo.substring(8, 17)
} else {
return null
}
},
/*15位转18位*/
getId18: function (idCardNo) {
if (idCardNo.length == 15) {
const id17 = idCardNo.substring(0, 6) + '19' + idCardNo.substring(6)
const parityBit = idCardNoUtil.getParityBit(id17)
return id17 + parityBit
} else if (idCardNo.length == 18) {
return idCardNo
} else {
return null
}
},
hideId(code) {
return code && code.replace(/^(\d{10})\d{4}(.{4}$)/g, `$1${Array(5).join('*')}$2`) || "-"
}
}
/**
* 封装权限判断方法
*/
export const permissions = flag => {
let buttons = []
if (localStorage.getItem('vuex')) {
const vuex = JSON.parse(localStorage.getItem('vuex'))
buttons = vuex.user.buttons
}
if (buttons && buttons.length > 0) {
return buttons.some(b => b.id == flag || b.permission == flag)
} else return false
}
export const copy = any => {
if (any) return JSON.parse(JSON.stringify(any))
else return any
}
export default {
dict,
confirm,
calcAge,
injectLib: (url, cb = () => 0) => {
const scriptList = document.body.querySelectorAll('script')
if (Object.values(scriptList || {}).findIndex(e => e.src == url) == -1) {
const script = document.createElement('script')
script.type = 'text/javascript'
script.src = url
script.addEventListener('load', () => cb())
document.body.appendChild(script)
} else cb()
},
dateFormat: (time, format) => {
return dayjs(time).format(format || 'YYYY-MM-DD').replace("Invalid Date", "")
},
formatName: (name) => {
return Array.from(name)?.slice(-2)?.toString() || ""
},
loading: title => {
uni.showLoading({
title: title ? title : '加载中',
mask: true
})
},
hideLoading: () => {
uni.hideLoading()
},
colorUtils: {
Hex2RGBA(color, alpha = 1) {
let hex = 0
if (color.charAt(0) == "#") {
if (color.length == 4) {
//检测诸如#FFF简写格式
color = "#" + color.charAt(1).repeat(2) +
color.charAt(2).repeat(2) +
color.charAt(3).repeat(2)
}
hex = parseInt(color.slice(1), 16)
}
let r = hex >> 16 & 0xFF
let g = hex >> 8 & 0xFF
let b = hex & 0xFF
return `rgba(${r},${g},${b},${alpha})`
},
RGBtoHex(r, g, b) {
let hex = r << 16 | g << 8 | b
return "#" + hex.toString(16)
}
},
dayjs,
idCardNoUtil,
qs,
permissions,
copy,
reg,
arr2tree(list, config = {}) {
const {key = 'id', parent = 'parentId', children = 'children'} = config
const result = []
const itemMap = {}
const ids = list?.map(e => `#${e[key]}#`)?.toString()
for (const e of list) {
const id = e[key], pid = e[parent]
itemMap[id] = {...e, [children]: [itemMap[id]?.[children]].flat().filter(Boolean)}
const treeItem = itemMap[id]
if (!!pid && ids.indexOf(`#${pid}#`) > -1) {
if (!itemMap[pid]) {
itemMap[pid] = {
children: []
}
}
itemMap[pid].children.push(treeItem)
} else result.push(treeItem)
}
const removeNullChildren = node => {
if (node[children] && node[children].length > 0) {
node[children].map(c => removeNullChildren(c))
} else delete node[children]
}
result.forEach(removeNullChildren)
return result
},
tree
}