app.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. },
  10. getUserInfo: function(cb) {
  11. var that = this
  12. if (this.globalData.userInfo) {
  13. typeof cb == "function" && cb(this.globalData.userInfo)
  14. } else {
  15. //调用登录接口
  16. wx.getUserInfo({
  17. withCredentials: false,
  18. success: function(res) {
  19. that.globalData.userInfo = res.userInfo
  20. typeof cb == "function" && cb(that.globalData.userInfo)
  21. }
  22. })
  23. }
  24. },
  25. globalData: {
  26. userInfo: null,
  27. ptStudent: null
  28. },
  29. login: function (info) {
  30. wx.login({
  31. success: res => {
  32. wx.request({
  33. url: api.loginUrl,
  34. method: 'POST',
  35. data: {
  36. code: res.code,
  37. iv: info.iv,
  38. encryptedData: info.encryptedData
  39. },
  40. success: info => {
  41. // console.log(info);
  42. if (info.data.status == 'success') {
  43. wx.setStorageSync('pt_student', info.data.data);
  44. getApp().globalData.ptStudent = info.data.data;
  45. wx.switchTab({
  46. url: '/pages/index/index',
  47. })
  48. }
  49. }
  50. });
  51. }
  52. });
  53. }
  54. })