log.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. const config = require('./config.js')
  2. const env = config.env === 'production' ? 'prod' : 'dev' // prod, 生产环境不输出
  3. const oldUrl = []
  4. let flag = false
  5. module.exports = {
  6. info() {},
  7. // 生命周期报错警告记录函数
  8. /**
  9. * msg 为发出的警告信息
  10. * lifeName 为发出警告的生命周期名字
  11. * **/
  12. warnLife(msg, lifeName) {
  13. if (env === 'prod') { return false }
  14. let _flag = true
  15. const rs = my.getStorageSync({ key: '_pageMsg' })
  16. if (lifeName === 'app/onHide') {
  17. flag = true
  18. return
  19. }
  20. if (flag) {
  21. for (let i = 0; i < oldUrl.length; i++) {
  22. if (oldUrl[i] === rs.data.pagePath) {
  23. _flag = false
  24. break
  25. }
  26. }
  27. }
  28. if (!_flag) {
  29. return
  30. } else {
  31. oldUrl.push(rs.data.pagePath)
  32. }
  33. let logInfo = {
  34. appName: '',
  35. appVersion: '',
  36. pages: [],
  37. }
  38. const page = {
  39. pageName: '',
  40. path: '',
  41. open: false,
  42. logs: [],
  43. }
  44. const log = {
  45. type: '',
  46. errorType: '',
  47. name: '',
  48. message: '',
  49. custom: '',
  50. }
  51. let p = false
  52. let l = false
  53. let a = -1
  54. const res = my.getStorageSync({ key: '__antmove_loginfo' })
  55. if (res.data !== null) {
  56. logInfo = res.data
  57. }
  58. page.pageName = rs.data.pageName
  59. page.path = rs.data.pagePath
  60. log.type = 'life'
  61. log.name = lifeName
  62. log.message = msg
  63. if (res.data !== null) {
  64. for (let i = 0; i < res.data.pages.length; i++) {
  65. if (rs.data.pagePath === res.data.pages[i].path) {
  66. p = true
  67. a = i
  68. for (let j = 0; j < res.data.pages[i].logs.length; j++) {
  69. if (lifeName === res.data.pages[i].logs[j].name) {
  70. l = true
  71. if (l) { break }
  72. }
  73. }
  74. }
  75. }
  76. if (p && !l) {
  77. logInfo.pages[a].logs.push(log)
  78. }
  79. if (!p && !l) {
  80. page.logs.push(log)
  81. logInfo.pages.push(page)
  82. }
  83. } else {
  84. page.logs.push(log)
  85. logInfo.pages.push(page)
  86. }
  87. my.setStorageSync({
  88. key: '__antmove_loginfo',
  89. data: logInfo,
  90. })
  91. console.warn(msg)
  92. },
  93. /**
  94. * warn 为api报警的就函数
  95. * _desc = {
  96. * apiName: 报警的api的名字
  97. * errorType: 报警的api警告等级
  98. * type: 警告描述
  99. * }
  100. * errorType等级分为:
  101. * 0 - missing - 不支持该属性
  102. * 1 - diff - 命名及格式不同
  103. * 3 - diffType - 类型不同
  104. * 4 - defaultValue - 默认值不同
  105. * 5 - wrapComponent - 使用自定义组件代替
  106. * 6 - diff tagName
  107. * 7 - equal - 完全支持
  108. */
  109. warn(
  110. msg,
  111. _desc = {
  112. apiName: '',
  113. errorType: '',
  114. type: '',
  115. },
  116. ) {
  117. if (env === 'prod') { return false }
  118. let logInfo = {
  119. appName: '',
  120. appVersion: '',
  121. pages: [],
  122. }
  123. const page = {
  124. pageName: '',
  125. path: '',
  126. open: '',
  127. logs: [],
  128. }
  129. const log = {
  130. type: '',
  131. errorType: '',
  132. name: '',
  133. message: '',
  134. custom: '',
  135. }
  136. const res = my.getStorageSync({ key: '__antmove_loginfo' })
  137. if (res.data !== null) {
  138. logInfo = res.data
  139. }
  140. const rs = my.getStorageSync({ key: '_pageMsg' })
  141. if (!rs || !rs.data) { return }
  142. page.pageName = rs.data.pageName
  143. page.path = rs.data.pagePath
  144. log.type = _desc.type
  145. log.name = _desc.apiName
  146. log.message = msg
  147. log.errorType = _desc.errorType
  148. if (!_desc.errorType && _desc.errorType !== 0) {
  149. log.custom = 'No missing attribute'
  150. } else if (_desc.errorType === 0) {
  151. log.custom = '不支持该属性'
  152. } else if (_desc.errorType === 1) {
  153. log.custom = '命名及格式不同'
  154. } else if (_desc.errorType === 2) {
  155. log.custom = '类型不同'
  156. } else if (_desc.errorType === 3) {
  157. log.custom = '默认值不同'
  158. }
  159. let p = false
  160. let l = false
  161. let a = -1
  162. if (res.data !== null) {
  163. for (let i = 0; i < res.data.pages.length; i++) {
  164. if (rs.data.pagePath === res.data.pages[i].path) {
  165. p = true
  166. a = i
  167. for (let j = 0; j < res.data.pages[i].logs.length; j++) {
  168. if (_desc.apiName === res.data.pages[i].logs[j].name) {
  169. l = true
  170. if (l) { break }
  171. }
  172. }
  173. }
  174. }
  175. if (p && !l) {
  176. logInfo.pages[a].logs.push(log)
  177. }
  178. if (!p && !l) {
  179. page.logs.push(log)
  180. logInfo.pages.push(page)
  181. }
  182. } else {
  183. page.logs.push(log)
  184. logInfo.pages.push(page)
  185. }
  186. my.setStorageSync({
  187. key: '__antmove_loginfo',
  188. data: logInfo,
  189. })
  190. console.warn(msg)
  191. },
  192. error() {},
  193. }