app.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. console.log(info, res)
  33. wx.request({
  34. url: api.loginUrl,
  35. method: 'POST',
  36. data: {
  37. code: res.code,
  38. iv: info.iv,
  39. encryptedData: info.encryptedData
  40. },
  41. success: info => {
  42. console.log(info);
  43. if (info.data.status == 'success') {
  44. wx.setStorageSync('pt_student', info.data.data);
  45. getApp().globalData.ptStudent = info.data.data;
  46. wx.switchTab({
  47. url: '/pages/index/index',
  48. })
  49. }
  50. }
  51. });
  52. }
  53. });
  54. }
  55. })