mixins.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * 如果你想删除本文件,请先确认它使用的范围,感谢合作~
  3. *
  4. * author: 375890534@qq.com
  5. */
  6. export default {
  7. methods: {
  8. //转义符换成普通字符
  9. escape2Html(str) {
  10. if (!str) return str;
  11. var arrEntities = {
  12. 'lt': '<',
  13. 'gt': '>',
  14. 'nbsp': ' ',
  15. 'amp': '&',
  16. 'quot': '"'
  17. };
  18. return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
  19. return arrEntities[t];
  20. });
  21. },
  22. //普通字符转换成转义符
  23. html2Escape(sHtml) {
  24. if (!sHtml) return sHtml;
  25. return sHtml.replace(/[<>&"]/g, function(c) {
  26. return {
  27. '<': '&lt;',
  28. '>': '&gt;',
  29. '&': '&amp;',
  30. '"': '&quot;'
  31. } [c];
  32. });
  33. },
  34. //setData polyfill 勿删!!! (用于转换后的uniapp的项目能直接使用this.setData()函数)
  35. setData: function(obj, callback) {
  36. let that = this;
  37. const handleData = (tepData, tepKey, afterKey) => {
  38. var tepData2 = tepData;
  39. tepKey = tepKey.split('.');
  40. tepKey.forEach(item => {
  41. if (tepData[item] === null || tepData[item] === undefined) {
  42. let reg = /^[0-9]+$/;
  43. tepData[item] = reg.test(afterKey) ? [] : {};
  44. tepData2 = tepData[item];
  45. } else {
  46. tepData2 = tepData[item];
  47. }
  48. });
  49. return tepData2;
  50. };
  51. const isFn = function(value) {
  52. return typeof value == 'function' || false;
  53. };
  54. Object.keys(obj).forEach(function(key) {
  55. let val = obj[key];
  56. key = key.replace(/\]/g, '').replace(/\[/g, '.');
  57. let front, after;
  58. let index_after = key.lastIndexOf('.');
  59. if (index_after != -1) {
  60. after = key.slice(index_after + 1);
  61. front = handleData(that, key.slice(0, index_after), after);
  62. } else {
  63. after = key;
  64. front = that;
  65. }
  66. if (front.$data && front.$data[after] === undefined) {
  67. Object.defineProperty(front, after, {
  68. get() {
  69. return front.$data[after];
  70. },
  71. set(newValue) {
  72. front.$data[after] = newValue;
  73. that.hasOwnProperty("$forceUpdate") && that.$forceUpdate();
  74. },
  75. enumerable: true,
  76. configurable: true
  77. });
  78. front[after] = val;
  79. } else {
  80. that.$set(front, after, val);
  81. }
  82. });
  83. // this.$forceUpdate();
  84. isFn(callback) && this.$nextTick(callback);
  85. }
  86. }
  87. }