app.js 2.5 KB

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