app.js 3.3 KB

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