mixin.js 8.6 KB

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