app.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //app.js
  2. const appConfig = require('config')
  3. App({
  4. onLaunch: function () {
  5. wx.getSystemInfo({
  6. success: e => {
  7. this.globalData.StatusBar = e.statusBarHeight;
  8. let custom = wx.getMenuButtonBoundingClientRect();
  9. this.globalData.Custom = custom;
  10. this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
  11. }
  12. })
  13. // 获取用户信息
  14. this.login();
  15. },
  16. login: function(){
  17. wx.getSetting({
  18. success: res => {
  19. if (res.authSetting['scope.userInfo']) {
  20. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  21. wx.login({
  22. success: res => {
  23. if (res.code && res.code != "") {
  24. this.globalData.code = res.code
  25. wx.getUserInfo({
  26. success: res => {
  27. // 可以将 res 发送给后台解码出 unionId
  28. this.globalData.res = res;
  29. this.globalData.userInfo = res.userInfo
  30. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  31. // 所以此处加入 callback 以防止这种情况
  32. if (this.userInfoReadyCallback) {
  33. this.userInfoReadyCallback(res)
  34. }
  35. },
  36. fail: res => {
  37. this.warning("网络错误,请检查您的网络是否正常~");
  38. }
  39. });
  40. }else{
  41. this.warning("登录失败,请重新授权后登录~");
  42. }
  43. }
  44. });
  45. } else {
  46. //请先授权
  47. }
  48. }
  49. });
  50. },
  51. globalData: {
  52. code : null,
  53. userInfo: null,
  54. isReloadLists: false,
  55. isReloadIcon: false,
  56. info : null,
  57. res : null,
  58. background: "/images/theme/10001.jpg",
  59. networkType : "wifi"
  60. },
  61. toast : function(str) {
  62. wx.showToast({
  63. title: str,
  64. duration: appConfig.duration
  65. });
  66. },
  67. warning : function(str) {
  68. wx.showModal({
  69. title: "温馨提示",
  70. content : str || "",
  71. showCancel : false,
  72. })
  73. },
  74. getStackTrace : function () {
  75. var obj = {};
  76. Error.captureStackTrace(obj, this.getStackTrace);
  77. return obj.stack;
  78. },
  79. log : function() {
  80. var stack = this.getStackTrace() || "";
  81. var matchResult = stack.split("at ");
  82. var line = matchResult[5] || ""
  83. for (var i in arguments) {
  84. }
  85. if (typeof arguments[i] == 'object') {
  86. arguments[i] = JSON.stringify(arguments[i])
  87. }
  88. arguments[i] += " " + line.replace("Object.", "").replace("(", "").replace(")", "");
  89. console.log.apply(console, arguments)
  90. },
  91. })