mixin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import * as index from '../function/index.js';
  2. import * as test from '../function/test.js';
  3. export default {
  4. // 定义每个组件都可能需要用到的外部样式以及类名
  5. props: {
  6. // 每个组件都有的父组件传递的样式,可以为字符串或者对象形式
  7. customStyle: {
  8. type: [Object, String],
  9. default: () => ({})
  10. },
  11. customClass: {
  12. type: String,
  13. default: ''
  14. },
  15. // 跳转的页面路径
  16. url: {
  17. type: String,
  18. default: ''
  19. },
  20. // 页面跳转的类型
  21. linkType: {
  22. type: String,
  23. default: 'navigateTo'
  24. }
  25. },
  26. data() {
  27. return {}
  28. },
  29. onLoad() {
  30. // getRect挂载到$w上,因为这方法需要使用in(this),所以无法把它独立成一个单独的文件导出
  31. this.$w.getRect = this.$wuGetRect;
  32. },
  33. created() {
  34. // 组件当中,只有created声明周期,为了能在组件使用,故也在created中将方法挂载到$w
  35. this.$w.getRect = this.$wuGetRect;
  36. },
  37. computed: {
  38. $w() {
  39. return {
  40. ...index,
  41. test
  42. }
  43. },
  44. /**
  45. * 生成bem规则类名
  46. * 由于微信小程序,H5,nvue之间绑定class的差异,无法通过:class="[bem()]"的形式进行同用
  47. * 故采用如下折中做法,最后返回的是数组(一般平台)或字符串(支付宝和字节跳动平台),类似['a', 'b', 'c']或'a b c'的形式
  48. * @param {String} name 组件名称
  49. * @param {Array} fixed 一直会存在的类名
  50. * @param {Array} change 会根据变量值为true或者false而出现或者隐藏的类名
  51. * @returns {Array|string}
  52. */
  53. bem() {
  54. return function(name, fixed, change) {
  55. // 类名前缀
  56. const prefix = `wu-${name}--`
  57. const classes = {}
  58. if (fixed) {
  59. fixed.map((item) => {
  60. // 这里的类名,会一直存在
  61. classes[prefix + this[item]] = true
  62. })
  63. }
  64. if (change) {
  65. change.map((item) => {
  66. // 这里的类名,会根据this[item]的值为true或者false,而进行添加或者移除某一个类
  67. this[item] ? (classes[prefix + item] = this[item]) : (delete classes[prefix + item])
  68. })
  69. }
  70. return Object.keys(classes)
  71. // 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  72. // #ifdef MP-ALIPAY || MP-TOUTIAO || MP-LARK || MP-BAIDU
  73. .join(' ')
  74. // #endif
  75. }
  76. }
  77. },
  78. methods: {
  79. // 跳转某一个页面
  80. openPage(urlKey = 'url') {
  81. const url = this[urlKey]
  82. if (url) {
  83. // 执行类似uni.navigateTo的方法
  84. uni[this.linkType]({
  85. url
  86. })
  87. }
  88. },
  89. // 查询节点信息
  90. // 目前此方法在支付宝小程序中无法获取组件跟接点的尺寸,为支付宝的bug(2020-07-21)
  91. // 解决办法为在组件根部再套一个没有任何作用的view元素
  92. $wuGetRect(selector, all) {
  93. return new Promise((resolve) => {
  94. uni.createSelectorQuery()
  95. .in(this)[all ? 'selectAll' : 'select'](selector)
  96. .boundingClientRect((rect) => {
  97. if (all && Array.isArray(rect) && rect.length) {
  98. resolve(rect)
  99. }
  100. if (!all && rect) {
  101. resolve(rect)
  102. }
  103. })
  104. .exec()
  105. })
  106. },
  107. // 查询节点布局是否相交
  108. IntersectionObserver(_this, nodeName, callback) {
  109. this.$nextTick(() => {
  110. // #ifndef APP-NVUE || H5
  111. let intersectionObserver = uni.createIntersectionObserver(_this)
  112. intersectionObserver.relativeToViewport({
  113. bottom: Number(this.lazyLoadRootMargin)
  114. }).observe(nodeName, res => {
  115. callback(res, ()=>intersectionObserver.disconnect());
  116. })
  117. // #endif
  118. // #ifdef H5
  119. if (!window.__wu_observedComponents) window.__wu_observedComponents = new Map(); // 用于存储元素及其对应的回调函数
  120. if (!window.__wu_IntersectionObserver) {
  121. window.__wu_IntersectionObserver = new IntersectionObserver((entries) => {
  122. entries.forEach(entry => {
  123. const callback = window.__wu_observedComponents.get(entry.target);
  124. if (callback) {
  125. callback(entry, ()=>{
  126. window.__wu_IntersectionObserver.unobserve(entry.target);
  127. window.__wu_observedComponents.delete(entry.target);
  128. });
  129. }
  130. });
  131. }, {
  132. root: null,
  133. rootMargin: Number(this.lazyLoadRootMargin) + 'px',
  134. threshold: 0
  135. });
  136. }
  137. window.__wu_observedComponents.set(_this.$el, callback);
  138. window.__wu_IntersectionObserver.observe(_this.$el)
  139. // #endif
  140. })
  141. },
  142. getParentData(parentName = '') {
  143. // 避免在created中去定义parent变量
  144. if (!this.parent) this.parent = {}
  145. // 这里的本质原理是,通过获取父组件实例(也即类似wu-radio的父组件wu-radio-group的this)
  146. // 将父组件this中对应的参数,赋值给本组件(wu-radio的this)的parentData对象中对应的属性
  147. // 之所以需要这么做,是因为所有端中,头条小程序不支持通过this.parent.xxx去监听父组件参数的变化
  148. // 此处并不会自动更新子组件的数据,而是依赖父组件wu-radio-group去监听data的变化,手动调用更新子组件的方法去重新获取
  149. this.parent = this.$w.$parent.call(this, parentName)
  150. if (this.parent.children) {
  151. // 如果父组件的children不存在本组件的实例,才将本实例添加到父组件的children中
  152. this.parent.children.indexOf(this) === -1 && this.parent.children.push(this)
  153. }
  154. if (this.parent && this.parentData) {
  155. // 历遍parentData中的属性,将parent中的同名属性赋值给parentData
  156. Object.keys(this.parentData).map((key) => {
  157. this.parentData[key] = this.parent[key]
  158. })
  159. }
  160. },
  161. // 阻止事件冒泡
  162. preventEvent(e) {
  163. e && typeof(e.stopPropagation) === 'function' && e.stopPropagation()
  164. },
  165. // 空操作
  166. noop(e) {
  167. this.preventEvent(e)
  168. }
  169. },
  170. onReachBottom() {
  171. uni.$emit('wuOnReachBottom')
  172. },
  173. beforeDestroy() {
  174. // 判断当前页面是否存在parent和chldren,一般在checkbox和checkbox-group父子联动的场景会有此情况
  175. // 组件销毁时,移除子组件在父组件children数组中的实例,释放资源,避免数据混乱
  176. if (this.parent && test.array(this.parent.children)) {
  177. // 组件销毁时,移除父组件中的children数组中对应的实例
  178. const childrenList = this.parent.children
  179. childrenList.map((child, index) => {
  180. // 如果相等,则移除
  181. if (child === this) {
  182. childrenList.splice(index, 1)
  183. }
  184. })
  185. }
  186. }
  187. }