You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
7.5 KiB

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