diypage.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // nova-travel/page/page-view/page-view.js
  2. var Parse = getApp().Parse;
  3. var company = getApp().globalData.company
  4. var Nova = getApp().Nova;
  5. const getTabs = require("../../utils/getTabs")
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. pageid: null,
  12. blocks: null,
  13. options: null,
  14. titleColor: {
  15. type: String,
  16. value: '#ffffff'
  17. },
  18. compcb: null,
  19. pulldownTime: { // 生命周期 pulldown:父组件在当前组件页面刷新是触发
  20. type: Number,
  21. value: null,
  22. observer: function (current, old) { // 属性值变化时执行
  23. console.log("pulldown diypage", current, old)
  24. if (current != old) {
  25. this.initPageBlocks().then(result => {
  26. wx.stopPullDownRefresh();
  27. });
  28. }
  29. }
  30. }
  31. // {
  32. // type: Object,
  33. // value: null,
  34. // observer:"update"
  35. // }
  36. },
  37. pageLifetimes: {
  38. // onPullDownRefresh: function(){
  39. // console.log("pulldown diypage",this.data)
  40. // setTimeout(() => {
  41. // wx.stopPullDownRefresh();
  42. // }, 2000);
  43. // }
  44. },
  45. lifetimes: {
  46. detached: function () {
  47. // 在组件实例被从页面节点树移除时执行
  48. },
  49. },
  50. attached: async function () {
  51. // 在组件实例进入页面节点树时执行
  52. console.log("attached diypage", this.data)
  53. this.data.options && this.setData({
  54. pageid: this.data.options.id,
  55. })
  56. this.initPageBlocks()
  57. this.getActiveColor()
  58. },
  59. /**
  60. * 组件的初始数据
  61. */
  62. data: {
  63. activeColor: null,
  64. background: "#ffffff"
  65. },
  66. /**
  67. * 组件的方法列表
  68. */
  69. methods: {
  70. onPullDownRefresh() {
  71. console.log("onpulldown diypage")
  72. // wx.startPullDownRefresh();
  73. this.initPageBlocks().then(result => {
  74. wx.stopPullDownRefresh();
  75. });
  76. },
  77. // 初始化页面区块内容
  78. async initPageBlocks() {
  79. let { pageid } = this.data
  80. console.log(pageid);
  81. if (pageid) {
  82. console.log("initPageBlocks")
  83. let query = new Parse.Query("DiyPage");
  84. let tab = await query.get(pageid);
  85. if (tab && tab.id) {
  86. let blocks = tab.get("blocks");
  87. // 把 cloud.file.futurestack.cn 的图片地址换为 file-cloud.fmode.cn(https 白名单已配)
  88. if (blocks && blocks.length) {
  89. blocks = JSON.parse(JSON.stringify(blocks));
  90. this._fixImageDomain(blocks);
  91. }
  92. this.setData({
  93. titleName: tab.get("titleBar") && tab.get("titleBar").title,
  94. titleType: tab.get("titleBar") && tab.get("titleBar").type,
  95. blocks: blocks,
  96. background: tab.get("titleBar") && tab.get("titleBar").backgroundColor
  97. })
  98. }
  99. console.log(this.data)
  100. return true;
  101. } else {
  102. return false
  103. }
  104. },
  105. // 递归替换 blocks 中所有 cloud.file.futurestack.cn → file-cloud.fmode.cn
  106. _fixImageDomain: function (obj) {
  107. if (!obj || typeof obj !== 'object') return;
  108. if (Array.isArray(obj)) {
  109. obj.forEach(item => this._fixImageDomain(item));
  110. } else {
  111. for (var key in obj) {
  112. if (typeof obj[key] === 'string' && obj[key].indexOf('cloud.file.futurestack.cn') !== -1) {
  113. obj[key] = obj[key].replace(/http:\/\/cloud\.file\.futurestack\.cn/g, 'https://file-cloud.fmode.cn');
  114. } else if (typeof obj[key] === 'object') {
  115. this._fixImageDomain(obj[key]);
  116. }
  117. }
  118. }
  119. },
  120. async getActiveColor() {
  121. let tabbarOption = await getTabs.getDiyTabs()
  122. let activeColor = tabbarOption.activeColor && tabbarOption.activeColor
  123. this.setData({
  124. activeColor
  125. })
  126. },
  127. // 获取父组件传递的数据
  128. update(newValue) {
  129. this.blocks = newValue
  130. console.log(this.blocks)
  131. },
  132. },
  133. pageLifetimes: {
  134. ready: function (ev) {
  135. },
  136. show: function (ev) {
  137. console.log("pageLifetimes", ev)
  138. }
  139. }
  140. })