From d1448cb0ef10f358bb7bddb4e1ec268515e0b787 Mon Sep 17 00:00:00 2001 From: gaoluyang <2820782392@qq.com> Date: 星期二, 15 七月 2025 11:46:57 +0800 Subject: [PATCH] 项目初始化 --- uni_modules/uview-ui/libs/mixin/mixin.js | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 160 insertions(+), 0 deletions(-) diff --git a/uni_modules/uview-ui/libs/mixin/mixin.js b/uni_modules/uview-ui/libs/mixin/mixin.js new file mode 100644 index 0000000..cd8f773 --- /dev/null +++ b/uni_modules/uview-ui/libs/mixin/mixin.js @@ -0,0 +1,160 @@ +module.exports = { + // 瀹氫箟姣忎釜缁勪欢閮藉彲鑳介渶瑕佺敤鍒扮殑澶栭儴鏍峰紡浠ュ強绫诲悕 + props: { + // 姣忎釜缁勪欢閮芥湁鐨勭埗缁勪欢浼犻�掔殑鏍峰紡锛屽彲浠ヤ负瀛楃涓叉垨鑰呭璞″舰寮� + customStyle: { + type: [Object, String], + default: () => ({}) + }, + customClass: { + type: String, + default: '' + }, + // 璺宠浆鐨勯〉闈㈣矾寰� + url: { + type: String, + default: '' + }, + // 椤甸潰璺宠浆鐨勭被鍨� + linkType: { + type: String, + default: 'navigateTo' + } + }, + data() { + return {} + }, + onLoad() { + // getRect鎸傝浇鍒�$u涓婏紝鍥犱负杩欐柟娉曢渶瑕佷娇鐢╥n(this)锛屾墍浠ユ棤娉曟妸瀹冪嫭绔嬫垚涓�涓崟鐙殑鏂囦欢瀵煎嚭 + this.$u.getRect = this.$uGetRect + }, + created() { + // 缁勪欢褰撲腑锛屽彧鏈塩reated澹版槑鍛ㄦ湡锛屼负浜嗚兘鍦ㄧ粍浠朵娇鐢紝鏁呬篃鍦╟reated涓皢鏂规硶鎸傝浇鍒�$u + this.$u.getRect = this.$uGetRect + }, + computed: { + // 鍦�2.x鐗堟湰涓紝灏嗕細鎶�$u鎸傝浇鍒皍ni瀵硅薄涓嬶紝瀵艰嚧鍦ㄦā鏉夸腑鏃犳硶浣跨敤uni.$u.xxx褰㈠紡 + // 鎵�浠ヨ繖閲岄�氳繃computed璁$畻灞炴�у皢鍏堕檮鍔犲埌this.$u涓婏紝灏卞彲浠ュ湪妯℃澘鎴栬�卝s涓娇鐢╱ni.$u.xxx + // 鍙湪nvue鐜閫氳繃姝ゆ柟寮忓紩鍏ュ畬鏁寸殑$u锛屽叾浠栧钩鍙颁細鍑虹幇鎬ц兘闂锛岄潪nvue鍒欐寜闇�寮曞叆锛堜富瑕佸師鍥犳槸props杩囧ぇ锛� + $u() { + // #ifndef APP-NVUE + // 鍦ㄩ潪nvue绔紝绉婚櫎props锛宧ttp锛宮ixin绛夊璞★紝閬垮厤鍦ㄥ皬绋嬪簭setData鏃舵暟鎹繃澶у奖鍝嶆�ц兘 + return uni.$u.deepMerge(uni.$u, { + props: undefined, + http: undefined, + mixin: undefined + }) + // #endif + // #ifdef APP-NVUE + return uni.$u + // #endif + }, + /** + * 鐢熸垚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 = `u-${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 + .join(' ') + // #endif + } + } + }, + methods: { + // 璺宠浆鏌愪竴涓〉闈� + openPage(urlKey = 'url') { + const url = this[urlKey] + if (url) { + // 鎵ц绫讳技uni.navigateTo鐨勬柟娉� + uni[this.linkType]({ + url + }) + } + }, + // 鏌ヨ鑺傜偣淇℃伅 + // 鐩墠姝ゆ柟娉曞湪鏀粯瀹濆皬绋嬪簭涓棤娉曡幏鍙栫粍浠惰窡鎺ョ偣鐨勫昂瀵革紝涓烘敮浠樺疂鐨刡ug(2020-07-21) + // 瑙e喅鍔炴硶涓哄湪缁勪欢鏍归儴鍐嶅涓�涓病鏈変换浣曚綔鐢ㄧ殑view鍏冪礌 + $uGetRect(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() + }) + }, + getParentData(parentName = '') { + // 閬垮厤鍦╟reated涓幓瀹氫箟parent鍙橀噺 + if (!this.parent) this.parent = {} + // 杩欓噷鐨勬湰璐ㄥ師鐞嗘槸锛岄�氳繃鑾峰彇鐖剁粍浠跺疄渚�(涔熷嵆绫讳技u-radio鐨勭埗缁勪欢u-radio-group鐨則his) + // 灏嗙埗缁勪欢this涓搴旂殑鍙傛暟锛岃祴鍊肩粰鏈粍浠�(u-radio鐨則his)鐨刾arentData瀵硅薄涓搴旂殑灞炴�� + // 涔嬫墍浠ラ渶瑕佽繖涔堝仛锛屾槸鍥犱负鎵�鏈夌涓紝澶存潯灏忕▼搴忎笉鏀寔閫氳繃this.parent.xxx鍘荤洃鍚埗缁勪欢鍙傛暟鐨勫彉鍖� + // 姝ゅ骞朵笉浼氳嚜鍔ㄦ洿鏂板瓙缁勪欢鐨勬暟鎹紝鑰屾槸渚濊禆鐖剁粍浠秛-radio-group鍘荤洃鍚琩ata鐨勫彉鍖栵紝鎵嬪姩璋冪敤鏇存柊瀛愮粍浠剁殑鏂规硶鍘婚噸鏂拌幏鍙� + this.parent = uni.$u.$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('uOnReachBottom') + }, + beforeDestroy() { + // 鍒ゆ柇褰撳墠椤甸潰鏄惁瀛樺湪parent鍜宑hldren锛屼竴鑸湪checkbox鍜宑heckbox-group鐖跺瓙鑱斿姩鐨勫満鏅細鏈夋鎯呭喌 + // 缁勪欢閿�姣佹椂锛岀Щ闄ゅ瓙缁勪欢鍦ㄧ埗缁勪欢children鏁扮粍涓殑瀹炰緥锛岄噴鏀捐祫婧愶紝閬垮厤鏁版嵁娣蜂贡 + if (this.parent && uni.$u.test.array(this.parent.children)) { + // 缁勪欢閿�姣佹椂锛岀Щ闄ょ埗缁勪欢涓殑children鏁扮粍涓搴旂殑瀹炰緥 + const childrenList = this.parent.children + childrenList.map((child, index) => { + // 濡傛灉鐩哥瓑锛屽垯绉婚櫎 + if (child === this) { + childrenList.splice(index, 1) + } + }) + } + } +} -- Gitblit v1.9.3