mixins.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * @Author: zhang peng
  3. * @Date: 2021-08-03 10:57:51
  4. * @LastEditTime: 2022-05-04 21:24:16
  5. * @LastEditors: zhang peng
  6. * @Description:
  7. * @FilePath: /miniprogram-to-uniapp2/src/project/template/polyfill/mixins.js
  8. *
  9. * 如果你想删除本文件,请先确认它使用的范围,感谢合作~
  10. * 如有疑问,请直接联系: 375890534@qq.com
  11. */
  12. export default {
  13. methods: {
  14. /**
  15. * 转义符换成普通字符
  16. * @param {*} str
  17. * @returns
  18. */
  19. escape2Html (str) {
  20. if (!str) return str
  21. var arrEntities = {
  22. 'lt': '<',
  23. 'gt': '>',
  24. 'nbsp': ' ',
  25. 'amp': '&',
  26. 'quot': '"'
  27. }
  28. return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
  29. return arrEntities[t]
  30. })
  31. },
  32. /**
  33. * 普通字符转换成转义符
  34. * @param {*} sHtml
  35. * @returns
  36. */
  37. html2Escape (sHtml) {
  38. if (!sHtml) return sHtml
  39. return sHtml.replace(/[<>&"]/g, function (c) {
  40. return {
  41. '<': '&lt;',
  42. '>': '&gt;',
  43. '&': '&amp;',
  44. '"': '&quot;'
  45. }[c]
  46. })
  47. },
  48. /**
  49. * setData polyfill 勿删!!!
  50. * 用于转换后的uniapp的项目能直接使用this.setData()函数
  51. * @param {*} obj
  52. * @param {*} callback
  53. */
  54. setData: function (obj, callback) {
  55. let that = this
  56. const handleData = (tepData, tepKey, afterKey) => {
  57. var tepData2 = tepData
  58. tepKey = tepKey.split('.')
  59. tepKey.forEach(item => {
  60. if (tepData[item] === null || tepData[item] === undefined) {
  61. let reg = /^[0-9]+$/
  62. tepData[item] = reg.test(afterKey) ? [] : {}
  63. tepData2 = tepData[item]
  64. } else {
  65. tepData2 = tepData[item]
  66. }
  67. })
  68. return tepData2
  69. }
  70. const isFn = function (value) {
  71. return typeof value == 'function' || false
  72. }
  73. Object.keys(obj).forEach(function (key) {
  74. let val = obj[key]
  75. key = key.replace(/\]/g, '').replace(/\[/g, '.')
  76. let front, after
  77. let index_after = key.lastIndexOf('.')
  78. if (index_after != -1) {
  79. after = key.slice(index_after + 1)
  80. front = handleData(that, key.slice(0, index_after), after)
  81. } else {
  82. after = key
  83. front = that
  84. }
  85. if (front.$data && front.$data[after] === undefined) {
  86. Object.defineProperty(front, after, {
  87. get () {
  88. return front.$data[after]
  89. },
  90. set (newValue) {
  91. front.$data[after] = newValue
  92. that.hasOwnProperty("$forceUpdate") && that.$forceUpdate()
  93. },
  94. enumerable: true,
  95. configurable: true
  96. })
  97. front[after] = val
  98. } else {
  99. that.$set(front, after, val)
  100. }
  101. })
  102. // this.$forceUpdate();
  103. isFn(callback) && this.$nextTick(callback)
  104. },
  105. /**
  106. * 解析事件里的动态函数名,这种没有()的函数名,在uniapp不被执行
  107. * 比如:<view bindtap="{{openId==undefined?'denglu':'hy_to'}}">立即</view>
  108. * @param {*} exp
  109. */
  110. parseEventDynamicCode (e, exp) {
  111. if (typeof (this[exp]) === 'function') {
  112. this[exp](e)
  113. }
  114. },
  115. /**
  116. * 用于处理对props进行赋值的情况
  117. * //简单处理一下就行了
  118. *
  119. * @param {*} target
  120. * @returns
  121. */
  122. deepClone (target) {
  123. return JSON.parse(JSON.stringify(target))
  124. },
  125. /**
  126. * 用于处理dataset
  127. * 自定义组件的事件里,是获取不到e.currentTarget.dataset的
  128. * 因此收集data-参数,手动传进去
  129. *
  130. * @param {*} event
  131. * @param {*} dataSet
  132. * @returns
  133. */
  134. datasetHandle (event, dataSet = {}) {
  135. if (event && !event.currentTarget) {
  136. if (dataSet.tagId) {
  137. event.currentTarget = {
  138. id: dataSet.tagId
  139. }
  140. } else {
  141. event.currentTarget = {
  142. dataset: dataSet
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }