app.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. console.log(info, res)
  32. wx.request({
  33. url: api.loginUrl,
  34. method: 'POST',
  35. header: {
  36. 'content-type': 'json'
  37. },
  38. data: {
  39. code: res.code,
  40. iv: info.iv,
  41. encryptData: info.encryptData
  42. },
  43. success: info => {
  44. if (info.data.status == 'success') {
  45. wx.setStorageSync('pt_student_id', info.data.id);
  46. }
  47. }
  48. });
  49. }
  50. });
  51. }
  52. })