app.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. },
  28. login: function (info) {
  29. wx.login({
  30. success: res => {
  31. wx.request({
  32. url: api.loginUrl,
  33. method: 'POST',
  34. data: {
  35. code: res.code,
  36. iv: info.iv,
  37. encryptedData: info.encryptedData
  38. },
  39. success: info => {
  40. console.log(info);
  41. if (info.data.status == 'success') {
  42. wx.setStorageSync('pt_student_id', info.data.id);
  43. }
  44. }
  45. });
  46. }
  47. });
  48. }
  49. })