app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. //调用API从本地缓存中获取数据
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. // 登录
  9. // wx.login({
  10. // success: res => {
  11. // // 发送 res.code 到后台换取 openId, sessionKey, unionId
  12. // }
  13. // })
  14. // // 获取用户信息
  15. // wx.getSetting({
  16. // success: res => {
  17. // if (res.authSetting['scope.userInfo']zx) {
  18. // // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  19. // wx.getUserInfo({
  20. // success: res => {
  21. // // 可以将 res 发送给后台解码出 unionId
  22. // this.globalData.userInfo = res.userInfo
  23. // // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  24. // // 所以此处加入 callback 以防止这种情况
  25. // if (this.userInfoReadyCallback) {
  26. // this.userInfoReadyCallback(res)
  27. // }
  28. // }
  29. // })
  30. // }
  31. // }
  32. // })
  33. },
  34. getUserInfo: function (cb) {
  35. var that = this;
  36. if (this.globalData.userInfo) {
  37. typeof cb == "function" && cb(this.globalData.userInfo)
  38. } else {
  39. //调用登录接口
  40. wx.login({
  41. success: function (res) {
  42. if (res.code) {
  43. wx.request({
  44. //获取openid接口
  45. url: 'https://weixin.ai160.com',
  46. header: { 'content-type': 'application/json' },
  47. data:{
  48. js_code: res.code,
  49. grant_type: 'authorization_code'
  50. },
  51. method:'GET',
  52. success:function(res){
  53. console.log(res)
  54. }
  55. })
  56. //发起网络请求
  57. // wx.request({
  58. // url: 'https://test.com/onLogin',
  59. // data: {
  60. // code: res.code
  61. // }
  62. // })
  63. } else {
  64. console.log('登录失败!' + res.errMsg)
  65. }
  66. wx.getUserInfo({
  67. success: function (res) {
  68. console.log(res);
  69. that.globalData.userInfo = res.userInfo;
  70. typeof cb == "function" && cb(that.globalData.userInfo)
  71. }
  72. })
  73. }
  74. });
  75. }
  76. },
  77. globalData: {
  78. userInfo: null
  79. }
  80. })