app.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //app.js
  2. var api = require('utils/api.js');
  3. App({
  4. onLaunch: function() {
  5. //调用API从本地缓存中获取数据
  6. var logs = wx.getStorageSync('logs') || []
  7. logs.unshift(Date.now())
  8. wx.setStorageSync('logs', logs)
  9. var that = this;
  10. wx.checkSession({
  11. success: function() {
  12. var we_chat_user_id = wx.getStorageSync('we_chat_user_id')
  13. if(!we_chat_user_id) {
  14. that.login()
  15. }
  16. },
  17. fail: function() {
  18. that.login()
  19. }
  20. })
  21. },
  22. getUserInfo: function(cb) {
  23. var that = this
  24. if (this.globalData.userInfo) {
  25. typeof cb == "function" && cb(this.globalData.userInfo)
  26. } else {
  27. //调用登录接口
  28. wx.getUserInfo({
  29. withCredentials: false,
  30. success: function(res) {
  31. that.globalData.userInfo = res.userInfo
  32. typeof cb == "function" && cb(that.globalData.userInfo)
  33. }
  34. })
  35. }
  36. },
  37. globalData: {
  38. userInfo: null,
  39. ptStudent: null
  40. },
  41. login: function(info) {
  42. wx.login({
  43. success: res => {
  44. wx.request({
  45. url: api.loginUrl,
  46. method: 'GET',
  47. data: {
  48. code: res.code
  49. },
  50. success: res => {
  51. if (res.data.status == 'success') {
  52. wx.setStorageSync('we_chat_user_id', res.data.id);
  53. }
  54. }
  55. })
  56. }
  57. })
  58. }
  59. })