家政小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

agentPage.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_FUNC = {
  16. _handleInput(e) {
  17. const {
  18. name
  19. } = e.target.dataset;
  20. this.setData({
  21. [`${name}`]: e.detail.value
  22. });
  23. },
  24. _isPhone(phone) {
  25. return /^1[345678]\d{9}$/.test(phone);
  26. }
  27. }
  28. const GLOBAL_LIFE_CIRCLE = {
  29. onReady() {
  30. // the `this` obj in func is pointing to the AgentPage
  31. },
  32. onLoad(options) {
  33. },
  34. onShow() {
  35. },
  36. onHide() {
  37. global.wx.hideToast('loading');
  38. },
  39. onUnload() {
  40. },
  41. onPullDownRefresh() {
  42. },
  43. onPageScroll() {
  44. },
  45. onReachBottom() {
  46. }
  47. }
  48. class AgentPage {
  49. /**
  50. * 构造函数
  51. */
  52. constructor(params) {
  53. Object.assign(this, {
  54. params
  55. });
  56. this._init();
  57. // 实例化Page
  58. Page(this.target);
  59. }
  60. /**
  61. * init data
  62. */
  63. _init() {
  64. this.target = {};
  65. // 初始化数据
  66. this._initDatas();
  67. // 初始化方法
  68. this._initMethods();
  69. // 初始化生命周期
  70. this._initLifeCircle();
  71. // add custom datas
  72. this._customDatas();
  73. }
  74. /**
  75. * 初始化数据
  76. */
  77. _initDatas() {
  78. this.target.data = this.params.data || {};
  79. }
  80. /**
  81. * 初始化方法
  82. */
  83. _initMethods() {
  84. const params = this.params;
  85. if (!params || typeof params !== 'object') return;
  86. // add custom life circle
  87. this._customLifeCircle();
  88. for (let key in params) {
  89. // 如果该属性不是对象自身拥有的函数
  90. if (!params.hasOwnProperty(key) || typeof params[key] !== 'function') continue;
  91. // 判断函数是否是生命周期
  92. if (typeof GLOBAL_LIFE_CIRCLE[key] === 'function') {
  93. // 把属于生命周期的函数暂存
  94. this.LIFE_CIRCLE_FUNC[key].push(params[key]);
  95. } else {
  96. // 其他函数透传
  97. this.target[key] = params[key];
  98. }
  99. }
  100. }
  101. /**
  102. * 加入定制生命周期
  103. */
  104. _customLifeCircle() {
  105. this.LIFE_CIRCLE_FUNC = [];
  106. for (let func in GLOBAL_LIFE_CIRCLE) {
  107. this.LIFE_CIRCLE_FUNC[func] = [GLOBAL_LIFE_CIRCLE[func]];
  108. }
  109. }
  110. /**
  111. * target加入生命周期函数
  112. */
  113. _initLifeCircle() {
  114. const _this = this;
  115. for (let circle in this.LIFE_CIRCLE_FUNC) {
  116. this.target[circle] = function (...args) {
  117. for (let fn of _this.LIFE_CIRCLE_FUNC[circle]) {
  118. fn.apply(this, args);
  119. }
  120. };
  121. }
  122. }
  123. /**
  124. * 加入定制的全局数据
  125. */
  126. _customDatas() {
  127. for (let item in GLOBAL_DATA) {
  128. this.target.data[item] = GLOBAL_DATA[item];
  129. }
  130. for (let func in GLOBAL_FUNC) {
  131. this.target[func] = GLOBAL_FUNC[func];
  132. }
  133. }
  134. }
  135. export default AgentPage;