首次提交

This commit is contained in:
2026-08-20 18:29:49 +08:00
commit 76827aedfe
77 changed files with 24937 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
/**
* 用于处理对props进行赋值的情况
* //简单处理一下就行了
*
* @param {*} target
* @returns
*/
export function clone(target) {
return JSON.parse(JSON.stringify(target))
}
+21
View File
@@ -0,0 +1,21 @@
/**
* 用于处理dataset
* 自定义组件的事件里,是获取不到e.currentTarget.dataset的
* 因此收集data-参数,手动传进去
*
* @param {*} event
* @param {*} dataSet
*/
export function handleDataset(event, dataSet = {}) {
if (event && !event.currentTarget) {
if (dataSet.tagId) {
event.currentTarget = {
id: dataSet.tagId
}
} else {
event.currentTarget = {
dataset: dataSet
}
}
}
}
+36
View File
@@ -0,0 +1,36 @@
/**
* 转义符换成普通字符
* @param {*} str
* @returns
*/
export function escape2Html(str) {
if (!str) return str
var arrEntities = {
'lt': '<',
'gt': '>',
'nbsp': ' ',
'amp': '&',
'quot': '"'
}
return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
return arrEntities[t]
})
}
/**
* 普通字符转换成转义符
* @param {*} sHtml
* @returns
*/
export function html2Escape(sHtml) {
if (!sHtml) return sHtml
return sHtml.replace(/[<>&"]/g, function(c) {
return {
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
'"': '&quot;'
} [c]
})
}
+10
View File
@@ -0,0 +1,10 @@
/**
* 解析事件里的动态函数名,这种没有()的函数名,在uniapp不被执行
* 比如:<view bindtap="{{openId==undefined?'denglu':'hy_to'}}">立即</view>
* @param {*} exp
*/
export function parseEventDynamicCode(e, exp) {
if (typeof(this[exp]) === 'function') {
this[exp](e)
}
}
@@ -0,0 +1,19 @@
/**
* 接管getTabBar函数,默认uni-app是没有这个函数的
* 适用于使用custom-tab-bar自定义导航栏的小程序项目
* 需注意:
* 1.custom-tab-bar下面仍是小程序文件
* 2.pages.json里面需使用条件编译区分好小程序和非小程序的tabBar配置
*/
export function getTabBar() {
return {
setData(obj) {
if (typeof this.$mp?.page?.getTabBar === 'function' &&
this.$mp?.page?.getTabBar()) {
this.$mp.page.getTabBar().setData(obj)
} else {
console.log("当前平台不支持getTabBar(),已稍作处理,详细请参见相关文档。")
}
}
}
}
+8
View File
@@ -0,0 +1,8 @@
export * from './clone'
export * from './dataset'
export * from './escape'
export * from './event'
export * from './getTabBar'
export * from './relation'
export * from './selectComponent'
export * from './setData'
+10
View File
@@ -0,0 +1,10 @@
/**
* 组件间关系
* 注意:须与p-f-unicom配合使用!!!
* @param {*} name
* @returns
*/
export function getRelationNodes(name) {
if(!this.$unicom) throw "this.getRelationNodes()需与p-f-unicom配合使用!"
return this.$unicom('@' + name)
}
@@ -0,0 +1,196 @@
const createTraverse = () => {
let stop = false;
return function traverse(root, callback) {
if (!stop && typeof callback === 'function') {
let children = root.$children;
for (let index = 0; !stop && index < children.length; index++) {
let element = children[index];
stop = callback(element) === true;
traverse(element, callback);
}
}
};
};
/**
* 安全的JSON.stringify
* @param {Object} node
*/
function safeStringify(node) {
var cache = [];
var str = JSON.stringify(node, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// 移除
return;
}
// 收集所有的值
cache.push(value);
}
return value;
});
cache = null; // 清空变量,便于垃圾回收机制回收
return str
}
const match = (node, selector) => {
var vnode = node._vnode;
//好家伙,在微信小程序里,node里面根本找不到class,因此这种方式没法搞了
//关键之处!
// console.log("attrs", (vnode.context.$vnode.data));
vnode = vnode?.context?.$vnode ?? ""
//console.log(vnode.data) --> [Object] {"staticClass":"bar","attrs":{"_i":0}} at selectComponent.js:72
if (!vnode || !vnode.data) {
return false
}
let attrs = vnode.data.attrs || {};
let staticClass = vnode.data.staticClass || '';
const id = attrs.id || '';
if (selector[0] === '#') {
return selector.substr(1) === id;
} else {
staticClass = staticClass.trim().split(' ');
selector = selector.substr(1).split('.');
return selector.reduce((a, c) => a && staticClass.includes(c), true);
}
};
const selectorBuilder = (selector) => {
selector = selector.replace(/>>>/g, '>');
selector = selector.split('>').map(s => {
return s.trim().split(' ').join(`').descendant('`);
}).join(`').child('`);
// 替换掉new Function方式,因为小程序不支持new Function和eval
//return new Function('Selector', 'node', 'all', `return new Selector(node, all).descendant('` + selector + `')`);
return function(Selector, node, all) {
return new Selector(node, all).descendant(selector)
}
};
class Selector {
constructor(node, all = false) {
this.nodes = [node];
this.all = all;
}
child(selector) {
let matches = [];
if (this.all) {
this.nodes.forEach(node => {
matches.push(...node.$children.filter(node => match(node, selector)));
});
} else {
if (this.nodes.length > 0) {
let node = this.nodes[0].$children.find(node => match(node, selector));
matches = node ? [node] : [];
}
}
this.nodes = matches;
return this;
}
descendant(selector) {
let matches = [];
this.nodes.forEach(root => {
createTraverse()(root, (node) => {
if (match(node, selector)) {
matches.push(node);
return !this.all;
}
});
});
this.nodes = matches;
return this;
}
}
////////////////////////////////////////////selectComponent//////////////////////////////////////////////////
/**
* 其他平台,如APP
* @param {Object} selector
*/
function selectComponentOther(selector) {
const selectors = selector.split(',').map(s => s.trim());
if (!selectors[0]) {
return null;
}
const querySelector = selectorBuilder(selectors[0]);
return querySelector(Selector, this, false, selector).nodes[0];
}
/**
* 还是用这个微信小程序的实现吧
* @param {Object} selector
*/
var selectComponentWeiXin2 = function(selector) {
console.log(".$scope",this.$scope.selectComponent(selector))
return this.$scope.selectComponent(selector)?.data || undefined
}
/**
* selectComponent
* @param {Object} args
*/
export function selectComponent(args) {
// console.log(".$scope",this.$scope)
// #ifdef MP
//H5和小程序能正常使用这个函数
//重写selectComponent函数,因为默认会多一层$vm
return selectComponentWeiXin2.call(this, args)
// #endif
// #ifndef MP
// 因App的结构略有差异,此函数无法正常使用
// function(e){return function e(t,n){if(n(t.$vnode||t._vnode))return t;for(var r=t.$children,i=0;i<r.length;i++){var o=e(r[i],n);if(o)return o}}(this,ov(e))}
// return selectComponentOther(args)
return selectComponentOther.call(this, args)
// #endif
}
////////////////////////////////////////////selectAllComponents//////////////////////////////////////////////////
/**
* 其他平台,如APP
* @param {Object} selector
*/
function selectAllComponentsOther(selector) {
const selectors = selector.split(',').map(s => s.trim());
let selected = [];
selectors.forEach(selector => {
const querySelector = selectorBuilder(selector);
selected = selected.concat(querySelector(Selector, this, true, selector).nodes);
});
return selected;
}
/**
* 还是用这个微信小程序的实现吧
* @param {Object} selector
*/
var selectAllComponentsWeiXin2 = function(selector) {
var list = this.$scope.selectAllComponents(selector) || []
list = list.map(item => item.data)
return list
}
/**
* selectAllComponents
* @param {Object} args
*/
export function selectAllComponents(args) {
// #ifdef MP
//H5和小程序能正常使用这个函数
//重写selectComponent函数,因为默认会多一层$vm
return selectAllComponentsWeiXin2.call(this, args)
// #endif
// #ifndef MP
// 因App的结构略有差异,此函数无法正常使用
return selectAllComponentsOther.call(this, args)
// #endif
}
+90
View File
@@ -0,0 +1,90 @@
import _set from '../utils/_set'
import debounce from '../utils/debounce'
/**
* 老setData polyfill
* 用于转换后的uniapp的项目能直接使用this.setData()函数
* @param {*} obj
* @param {*} callback
*/
function oldSetData (obj, callback) {
let that = this
const handleData = (tepData, tepKey, afterKey) => {
var tepData2 = tepData
tepKey = tepKey.split('.')
tepKey.forEach(item => {
if (tepData[item] === null || tepData[item] === undefined) {
let reg = /^[0-9]+$/
tepData[item] = reg.test(afterKey) ? [] : {}
tepData2 = tepData[item]
} else {
tepData2 = tepData[item]
}
})
return tepData2
}
const isFn = function (value) {
return typeof value == 'function' || false
}
Object.keys(obj).forEach(function (key) {
let val = obj[key]
key = key.replace(/\]/g, '').replace(/\[/g, '.')
let front, after
let index_after = key.lastIndexOf('.')
if (index_after != -1) {
after = key.slice(index_after + 1)
front = handleData(that, key.slice(0, index_after), after)
} else {
after = key
front = that
}
if (front.$data && front.$data[after] === undefined) {
Object.defineProperty(front, after, {
get () {
return front.$data[after]
},
set (newValue) {
front.$data[after] = newValue
that.hasOwnProperty("$forceUpdate") && that.$forceUpdate()
},
enumerable: true,
configurable: true
})
front[after] = val
} else {
that.$set(front, after, val)
}
})
// this.$forceUpdate();
isFn(callback) && this.$nextTick(callback)
}
/**
* 变量名正则
*/
const variableNameReg = /^([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*$/
/**
* 2022-10-31 重写setData
* 2023-05-08 增加微信“简易双向绑定”支持
* 用于转换后的uniapp的项目能直接使用this.setData()函数
* @param {Object} obj
* @param {Object} callback
*/
export function setData (obj, callback = null) {
Object.keys(obj).forEach((key) => {
_set(this, key, obj[key])
//处理微信“简易双向绑定”
if (variableNameReg.test(key) && key.endsWith("Clone")) {
let propName = key.replace(/Clone$/, "")
if (this.$options && this.$options.propsData[propName]) {
this.$emit(`update:${propName}`, obj[key])
}
}
})
this.$forceUpdate();
if (typeof callback == 'function') this.$nextTick(callback)
}