app.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // 获取用户信息
  29. this.login();
  30. },
  31. login: function(){
  32. wx.getSetting({
  33. success: res => {
  34. if (res.authSetting['scope.userInfo']) {
  35. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  36. wx.login({
  37. success: res => {
  38. if (res.code && res.code != "") {
  39. this.globalData.code = res.code
  40. wx.getUserInfo({
  41. success: res => {
  42. // 可以将 res 发送给后台解码出 unionId
  43. this.globalData.res = res;
  44. this.globalData.userInfo = res.userInfo
  45. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  46. // 所以此处加入 callback 以防止这种情况
  47. if (this.userInfoReadyCallback) {
  48. this.userInfoReadyCallback(res)
  49. }
  50. },
  51. fail: res => {
  52. this.warning("网络错误,请检查您的网络是否正常~");
  53. }
  54. });
  55. }else{
  56. this.warning("登录失败,请重新授权后登录~");
  57. }
  58. }
  59. });
  60. } else {
  61. //请先授权
  62. }
  63. }
  64. });
  65. },
  66. globalData: {
  67. code : null,
  68. userInfo: null,
  69. isReloadLists: false,
  70. isReloadIcon: false,
  71. info : null,
  72. res : null,
  73. background: "/images/theme/10001.jpg",
  74. networkType : "wifi",
  75. },
  76. toast : function(str) {
  77. wx.showToast({
  78. title: str,
  79. duration: appConfig.duration
  80. });
  81. },
  82. warning : function(str) {
  83. wx.showModal({
  84. title: "温馨提示",
  85. content : str || "",
  86. showCancel : false,
  87. })
  88. },
  89. getStackTrace : function () {
  90. var obj = {};
  91. Error.captureStackTrace(obj, this.getStackTrace);
  92. return obj.stack;
  93. },
  94. log : function() {
  95. /*
  96. if(appConfig.host.indexOf("http://localhost/") == -1) {
  97. return;
  98. }*/
  99. //console.log.apply(console, arguments)
  100. var stack = this.getStackTrace() || ""
  101. var matchResult = stack.split("at");
  102. var line = matchResult[5] || ""
  103. for (var i in arguments) {
  104. }
  105. if (typeof arguments[i] == 'object') {
  106. arguments[i] = JSON.stringify(arguments[i])
  107. }
  108. arguments[i] += " " + line.replace("Object.", "").replace("(", "").replace(")", "");
  109. console.log.apply(console, arguments)
  110. },
  111. })