mixin.js 7.4 KB

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