家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

agentPage.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * 微信小程序Page对象代理构造函数
  3. * @author andypliang
  4. * @description 用于小程序项目页面中公共属性及公共函数的提取,并拥有公共的生命周期
  5. */
  6. const systemInfo = wx.getSystemInfoSync && wx.getSystemInfoSync() || {};
  7. const GLOBAL_DATA = {
  8. // 全局的图片路径 根据项目自己调整
  9. IMG_PATH: `https://cdn.domain.com/`,
  10. // 系统信息
  11. systemInfo,
  12. //是否iPhoneX标志,便于页面做适配
  13. iPhoneX: systemInfo.model && systemInfo.model.indexOf('iPhone X') >= 0
  14. };
  15. const GLOBAL_LIFE_CIRCLE = {
  16. onReady() {
  17. // the `this` obj in func is pointing to the AgentPage
  18. },
  19. onLoad(options) {
  20. },
  21. onShow() {
  22. },
  23. onHide() {
  24. global.wx.hideToast('loading');
  25. },
  26. onUnload() {
  27. },
  28. onPullDownRefresh() {
  29. },
  30. onPageScroll() {
  31. },
  32. onReachBottom() {
  33. }
  34. }
  35. class AgentPage {
  36. /**
  37. * 构造函数
  38. */
  39. constructor(params) {
  40. Object.assign(this, {
  41. params
  42. });
  43. this._init();
  44. // 实例化Page
  45. Page(this.target);
  46. }
  47. /**
  48. * init data
  49. */
  50. _init() {
  51. this.target = {};
  52. // 初始化数据
  53. this._initDatas();
  54. // 初始化方法
  55. this._initMethods();
  56. // 初始化生命周期
  57. this._initLifeCircle();
  58. // add custom datas
  59. this._customDatas();
  60. }
  61. /**
  62. * 初始化数据
  63. */
  64. _initDatas() {
  65. this.target.data = this.params.data || {};
  66. }
  67. /**
  68. * 初始化方法
  69. */
  70. _initMethods() {
  71. const params = this.params;
  72. if (!params || typeof params !== 'object') return;
  73. // add custom life circle
  74. this._customLifeCircle();
  75. for (let key in params) {
  76. // 如果该属性不是对象自身拥有的函数
  77. if (!params.hasOwnProperty(key) || typeof params[key] !== 'function') continue;
  78. // 判断函数是否是生命周期
  79. if (typeof GLOBAL_LIFE_CIRCLE[key] === 'function') {
  80. // 把属于生命周期的函数暂存
  81. this.LIFE_CIRCLE_FUNC[key].push(params[key]);
  82. } else {
  83. // 其他函数透传
  84. this.target[key] = params[key];
  85. }
  86. }
  87. }
  88. /**
  89. * 加入定制生命周期
  90. */
  91. _customLifeCircle() {
  92. this.LIFE_CIRCLE_FUNC = [];
  93. for (let func in GLOBAL_LIFE_CIRCLE) {
  94. this.LIFE_CIRCLE_FUNC[func] = [GLOBAL_LIFE_CIRCLE[func]];
  95. }
  96. }
  97. /**
  98. * target加入生命周期函数
  99. */
  100. _initLifeCircle() {
  101. const _this = this;
  102. for (let circle in this.LIFE_CIRCLE_FUNC) {
  103. this.target[circle] = function (...args) {
  104. for (let fn of _this.LIFE_CIRCLE_FUNC[circle]) {
  105. fn.apply(this, args);
  106. }
  107. };
  108. }
  109. }
  110. /**
  111. * 加入定制的全局数据
  112. */
  113. _customDatas() {
  114. for (let item in GLOBAL_DATA) {
  115. this.target.data[item] = GLOBAL_DATA[item];
  116. }
  117. }
  118. }
  119. export default AgentPage;