log.js 5.0 KB

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