app.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const utils = require('../../api/utils')
  2. const { warnLife } = utils
  3. const { setIfWatch } = require('./utils')
  4. const getUrl = function() {
  5. const pages = getCurrentPages()
  6. const url = pages[pages.length - 1].route
  7. const _arr = url.split('/')
  8. const _name = _arr[_arr.length - 1]
  9. my.setStorageSync({
  10. key: '_pageMsg',
  11. data: {
  12. pageName: _name,
  13. pagePath: url,
  14. },
  15. })
  16. }
  17. module.exports = {
  18. processTransformationApp(_opts, options) {
  19. _opts = Object.assign(_opts, options)
  20. _opts.onLaunch = function(res) {
  21. if (!my.canIUse('component2')) { console.warn('《建议用户开启 component2 模式,详情:IDE 中的 详情 > 项目配置 中,勾选 component2》') }
  22. my.removeStorageSync({
  23. key: 'logInfo',
  24. })
  25. my.removeStorageSync({
  26. key: '_pageMsg',
  27. })
  28. getUrl()
  29. let body = {}
  30. function pre(params = {}) {
  31. return utils.defineGetter(params, body.params, (obj, prop) => {
  32. warnLife(`onLaunch's return value is not support ${prop} attribute!`, `onLaunch/${prop}`)
  33. })
  34. }
  35. if (options.onLaunch) {
  36. body = {
  37. params: {
  38. scene: {
  39. type: 0,
  40. desc: 'missing',
  41. },
  42. shareTicket: {
  43. type: 0,
  44. desc: 'missing',
  45. },
  46. },
  47. }
  48. res = pre(res)
  49. if (typeof options.data === 'function') {
  50. options.data = options.data()
  51. }
  52. options.onLaunch.call(this, res)
  53. }
  54. if (options.onPageNotFound) {
  55. warnLife('There is no onPageNotFound life cycle', 'onPageNotFound')
  56. }
  57. if (options.onPageNotFound) {
  58. warnLife('There is no onPageNotFound life cycle', 'onPageNotFound')
  59. }
  60. }
  61. _opts.onShow = function(res) {
  62. setIfWatch(true)
  63. let body = {}
  64. function pre(params = {}) {
  65. return utils.defineGetter(params, body.params, (obj, prop) => {
  66. warnLife(`onShow's return value is not support ${prop} attribute!`, `onShow/${prop}`)
  67. })
  68. }
  69. if (options.onShow) {
  70. body = {
  71. params: {
  72. scene: {
  73. type: 0,
  74. desc: 'missing',
  75. },
  76. shareTicket: {
  77. type: 0,
  78. desc: 'missing',
  79. },
  80. },
  81. }
  82. res = pre(res)
  83. options.onShow.call(this, res)
  84. }
  85. }
  86. _opts.onHide = function() {
  87. setIfWatch(false)
  88. if (options.onHide) {
  89. warnLife('', 'app/onHide')
  90. options.onHide.call(this)
  91. }
  92. }
  93. if (options.onError) {
  94. _opts.onError = function(...args) {
  95. options.onError.apply(this, args)
  96. }
  97. }
  98. },
  99. }