app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //app.js
  2. App({
  3. onLaunch: function() {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. wx.getUserInfo({
  9. success: res => {
  10. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  11. // 没授权的状态下调用getUserInfo会弹框
  12. this.globalData.userInfo = res.userInfo
  13. this.globalData.rawData = res.rawData
  14. this.globalData.signature = res.signature
  15. console.log("获取用户信息" + res.rawData)
  16. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  17. // 所以此处加入 callback 以防止这种情况
  18. if (this.userInfoReadyCallback) {
  19. console.log("app.userInfoReadyCallback" + res.iv)
  20. this.userInfoReadyCallback(res)
  21. }
  22. }
  23. })
  24. // 检查sessionKey
  25. wx.checkSession({
  26. fail() {
  27. // session_key 已经失效,需要重新执行登录流程
  28. wx.login({
  29. success: res => {
  30. var userInfo = wx.getStorageSync("userInfo")
  31. if (userInfo == null && wx.setStorageSync("token") == null){
  32. //注册
  33. console.log("注册")
  34. wx.setStorage("userInfo", this.globalData.userInfo)
  35. } else if (userInfo != this.globalData.userInfo) {
  36. //更新 userInfo 和 token
  37. console.log("更新 userInfo 和 token")
  38. wx.setStorage("userInfo", this.globalData.userInfo)
  39. } else {
  40. //更新token
  41. console.log("更新token")
  42. }
  43. }
  44. })
  45. }
  46. })
  47. // 获取用户信息
  48. // wx.getSetting({
  49. // success: res => {
  50. // if (res.authSetting['scope.userInfo']) {
  51. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  52. // // 没授权的状态下调用getUserInfo会弹框
  53. // wx.getUserInfo({
  54. // success: res => {
  55. // console.log("app.getUserInfo" + res.iv)
  56. // // 可以将 res 发送给后台解码出 unionId
  57. // this.globalData.userInfo = res.userInfo
  58. // this.globalData.rawData = res.rawData
  59. // this.globalData.signature = res.signature
  60. // this.globalData.encryptedData = res.encryptedData
  61. // this.globalData.iv = res.iv
  62. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  63. // // 所以此处加入 callback 以防止这种情况
  64. // if (this.userInfoReadyCallback) {
  65. // console.log("app.userInfoReadyCallback" + res.iv)
  66. // this.userInfoReadyCallback(res)
  67. // }
  68. // }
  69. // })
  70. // }
  71. // }
  72. // })
  73. },
  74. register: function () {
  75. //注册新用户
  76. wx.request({
  77. url: this.globalData.host + "/register",
  78. data: {
  79. jsCode: res.code,
  80. rawData: this.globalData.rawData,
  81. signature: this.globalData.signature
  82. },
  83. header: {
  84. 'content-type': 'application/json' // 默认值
  85. },
  86. dataType: "json",
  87. method: "POST",
  88. success(res) {
  89. console.log(res.data)
  90. }
  91. })
  92. },
  93. globalData: {
  94. userInfo: null,
  95. rawData: '',
  96. signature: '',
  97. host: "https://77ee0a14.ngrok.io"
  98. }
  99. })
  100. /*
  101. 已登录的情况
  102. app.getUserInfosCddGUbsg0MB3oB7tvlmfA==
  103. app.userInfoReadyCallbacksCddGUbsg0MB3oB7tvlmfA==
  104. page.userInfoReadyCallbacksCddGUbsg0MB3oB7tvlmfA==
  105. */
  106. /*
  107. 未登录
  108. 先点,点击登录授权
  109. page.bindGetUserInfoBObXMSFjVRixIoyncg2kyA==
  110. */