From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uni_modules/wu-ui-tools/libs/mixin/mixin.js | 188 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 188 insertions(+), 0 deletions(-) diff --git a/uni_modules/wu-ui-tools/libs/mixin/mixin.js b/uni_modules/wu-ui-tools/libs/mixin/mixin.js new file mode 100644 index 0000000..217f534 --- /dev/null +++ b/uni_modules/wu-ui-tools/libs/mixin/mixin.js @@ -0,0 +1,188 @@ +import * as index from '../function/index.js'; +import * as test from '../function/test.js'; + +export default { + // 瀹氫箟姣忎釜缁勪欢閮藉彲鑳介渶瑕佺敤鍒扮殑澶栭儴鏍峰紡浠ュ強绫诲悕 + props: { + // 姣忎釜缁勪欢閮芥湁鐨勭埗缁勪欢浼犻�掔殑鏍峰紡锛屽彲浠ヤ负瀛楃涓叉垨鑰呭璞″舰寮� + customStyle: { + type: [Object, String], + default: () => ({}) + }, + customClass: { + type: String, + default: '' + }, + // 璺宠浆鐨勯〉闈㈣矾寰� + url: { + type: String, + default: '' + }, + // 椤甸潰璺宠浆鐨勭被鍨� + linkType: { + type: String, + default: 'navigateTo' + } + }, + data() { + return {} + }, + onLoad() { + // getRect鎸傝浇鍒�$w涓婏紝鍥犱负杩欐柟娉曢渶瑕佷娇鐢╥n(this)锛屾墍浠ユ棤娉曟妸瀹冪嫭绔嬫垚涓�涓崟鐙殑鏂囦欢瀵煎嚭 + this.$w.getRect = this.$wuGetRect; + }, + created() { + // 缁勪欢褰撲腑锛屽彧鏈塩reated澹版槑鍛ㄦ湡锛屼负浜嗚兘鍦ㄧ粍浠朵娇鐢紝鏁呬篃鍦╟reated涓皢鏂规硶鎸傝浇鍒�$w + this.$w.getRect = this.$wuGetRect; + }, + computed: { + $w() { + return { + ...index, + test + } + }, + /** + * 鐢熸垚bem瑙勫垯绫诲悕 + * 鐢变簬寰俊灏忕▼搴忥紝H5锛宯vue涔嬮棿缁戝畾class鐨勫樊寮傦紝鏃犳硶閫氳繃:class="[bem()]"鐨勫舰寮忚繘琛屽悓鐢� + * 鏁呴噰鐢ㄥ涓嬫姌涓仛娉曪紝鏈�鍚庤繑鍥炵殑鏄暟缁勶紙涓�鑸钩鍙帮級鎴栧瓧绗︿覆锛堟敮浠樺疂鍜屽瓧鑺傝烦鍔ㄥ钩鍙帮級锛岀被浼糩'a', 'b', 'c']鎴�'a b c'鐨勫舰寮� + * @param {String} name 缁勪欢鍚嶇О + * @param {Array} fixed 涓�鐩翠細瀛樺湪鐨勭被鍚� + * @param {Array} change 浼氭牴鎹彉閲忓�间负true鎴栬�協alse鑰屽嚭鐜版垨鑰呴殣钘忕殑绫诲悕 + * @returns {Array|string} + */ + bem() { + return function(name, fixed, change) { + // 绫诲悕鍓嶇紑 + const prefix = `wu-${name}--` + const classes = {} + if (fixed) { + fixed.map((item) => { + // 杩欓噷鐨勭被鍚嶏紝浼氫竴鐩村瓨鍦� + classes[prefix + this[item]] = true + }) + } + if (change) { + change.map((item) => { + // 杩欓噷鐨勭被鍚嶏紝浼氭牴鎹畉his[item]鐨勫�间负true鎴栬�協alse锛岃�岃繘琛屾坊鍔犳垨鑰呯Щ闄ゆ煇涓�涓被 + this[item] ? (classes[prefix + item] = this[item]) : (delete classes[prefix + item]) + }) + } + return Object.keys(classes) + // 鏀粯瀹濓紝澶存潯灏忕▼搴忔棤娉曞姩鎬佺粦瀹氫竴涓暟缁勭被鍚嶏紝鍚﹀垯瑙f瀽鍑烘潵鐨勭粨鏋滀細甯︽湁","锛岃�屽鑷村け鏁� + // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK || MP-BAIDU + .join(' ') + // #endif + } + } + }, + methods: { + // 璺宠浆鏌愪竴涓〉闈� + openPage(urlKey = 'url') { + const url = this[urlKey] + if (url) { + // 鎵ц绫讳技uni.navigateTo鐨勬柟娉� + uni[this.linkType]({ + url + }) + } + }, + // 鏌ヨ鑺傜偣淇℃伅 + // 鐩墠姝ゆ柟娉曞湪鏀粯瀹濆皬绋嬪簭涓棤娉曡幏鍙栫粍浠惰窡鎺ョ偣鐨勫昂瀵革紝涓烘敮浠樺疂鐨刡ug(2020-07-21) + // 瑙e喅鍔炴硶涓哄湪缁勪欢鏍归儴鍐嶅涓�涓病鏈変换浣曚綔鐢ㄧ殑view鍏冪礌 + $wuGetRect(selector, all) { + return new Promise((resolve) => { + uni.createSelectorQuery() + .in(this)[all ? 'selectAll' : 'select'](selector) + .boundingClientRect((rect) => { + if (all && Array.isArray(rect) && rect.length) { + resolve(rect) + } + if (!all && rect) { + resolve(rect) + } + }) + .exec() + }) + }, + // 鏌ヨ鑺傜偣甯冨眬鏄惁鐩镐氦 + IntersectionObserver(_this, nodeName, callback) { + this.$nextTick(() => { + // #ifndef APP-NVUE || H5 + let intersectionObserver = uni.createIntersectionObserver(_this) + intersectionObserver.relativeToViewport({ + bottom: Number(this.lazyLoadRootMargin) + }).observe(nodeName, res => { + callback(res, ()=>intersectionObserver.disconnect()); + }) + // #endif + // #ifdef H5 + if (!window.__wu_observedComponents) window.__wu_observedComponents = new Map(); // 鐢ㄤ簬瀛樺偍鍏冪礌鍙婂叾瀵瑰簲鐨勫洖璋冨嚱鏁� + if (!window.__wu_IntersectionObserver) { + window.__wu_IntersectionObserver = new IntersectionObserver((entries) => { + entries.forEach(entry => { + const callback = window.__wu_observedComponents.get(entry.target); + if (callback) { + callback(entry, ()=>{ + window.__wu_IntersectionObserver.unobserve(entry.target); + window.__wu_observedComponents.delete(entry.target); + }); + } + }); + }, { + root: null, + rootMargin: Number(this.lazyLoadRootMargin) + 'px', + threshold: 0 + }); + } + window.__wu_observedComponents.set(_this.$el, callback); + window.__wu_IntersectionObserver.observe(_this.$el) + // #endif + }) + }, + getParentData(parentName = '') { + // 閬垮厤鍦╟reated涓幓瀹氫箟parent鍙橀噺 + if (!this.parent) this.parent = {} + // 杩欓噷鐨勬湰璐ㄥ師鐞嗘槸锛岄�氳繃鑾峰彇鐖剁粍浠跺疄渚�(涔熷嵆绫讳技wu-radio鐨勭埗缁勪欢wu-radio-group鐨則his) + // 灏嗙埗缁勪欢this涓搴旂殑鍙傛暟锛岃祴鍊肩粰鏈粍浠�(wu-radio鐨則his)鐨刾arentData瀵硅薄涓搴旂殑灞炴�� + // 涔嬫墍浠ラ渶瑕佽繖涔堝仛锛屾槸鍥犱负鎵�鏈夌涓紝澶存潯灏忕▼搴忎笉鏀寔閫氳繃this.parent.xxx鍘荤洃鍚埗缁勪欢鍙傛暟鐨勫彉鍖� + // 姝ゅ骞朵笉浼氳嚜鍔ㄦ洿鏂板瓙缁勪欢鐨勬暟鎹紝鑰屾槸渚濊禆鐖剁粍浠秝u-radio-group鍘荤洃鍚琩ata鐨勫彉鍖栵紝鎵嬪姩璋冪敤鏇存柊瀛愮粍浠剁殑鏂规硶鍘婚噸鏂拌幏鍙� + this.parent = this.$w.$parent.call(this, parentName) + if (this.parent.children) { + // 濡傛灉鐖剁粍浠剁殑children涓嶅瓨鍦ㄦ湰缁勪欢鐨勫疄渚嬶紝鎵嶅皢鏈疄渚嬫坊鍔犲埌鐖剁粍浠剁殑children涓� + this.parent.children.indexOf(this) === -1 && this.parent.children.push(this) + } + if (this.parent && this.parentData) { + // 鍘嗛亶parentData涓殑灞炴�э紝灏唒arent涓殑鍚屽悕灞炴�ц祴鍊肩粰parentData + Object.keys(this.parentData).map((key) => { + this.parentData[key] = this.parent[key] + }) + } + }, + // 闃绘浜嬩欢鍐掓场 + preventEvent(e) { + e && typeof(e.stopPropagation) === 'function' && e.stopPropagation() + }, + // 绌烘搷浣� + noop(e) { + this.preventEvent(e) + } + }, + onReachBottom() { + uni.$emit('wuOnReachBottom') + }, + beforeDestroy() { + // 鍒ゆ柇褰撳墠椤甸潰鏄惁瀛樺湪parent鍜宑hldren锛屼竴鑸湪checkbox鍜宑heckbox-group鐖跺瓙鑱斿姩鐨勫満鏅細鏈夋鎯呭喌 + // 缁勪欢閿�姣佹椂锛岀Щ闄ゅ瓙缁勪欢鍦ㄧ埗缁勪欢children鏁扮粍涓殑瀹炰緥锛岄噴鏀捐祫婧愶紝閬垮厤鏁版嵁娣蜂贡 + if (this.parent && test.array(this.parent.children)) { + // 缁勪欢閿�姣佹椂锛岀Щ闄ょ埗缁勪欢涓殑children鏁扮粍涓搴旂殑瀹炰緥 + const childrenList = this.parent.children + childrenList.map((child, index) => { + // 濡傛灉鐩哥瓑锛屽垯绉婚櫎 + if (child === this) { + childrenList.splice(index, 1) + } + }) + } + } +} \ No newline at end of file -- Gitblit v1.9.3