app.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. wx.checkSession({
  10. success: function() {
  11. },
  12. fail: function() {
  13. wx.login({
  14. success: res => {
  15. wx.request({
  16. url: api.loginUrl,
  17. method: 'GET',
  18. data: {
  19. code: res.code
  20. },
  21. success: res => {
  22. if(res.data.status == 'success') {
  23. wx.setStorageSync('we_chat_user_id', res.data.id);
  24. }
  25. }
  26. })
  27. }
  28. })
  29. }
  30. })
  31. },
  32. getUserInfo: function(cb) {
  33. var that = this
  34. if (this.globalData.userInfo) {
  35. typeof cb == "function" && cb(this.globalData.userInfo)
  36. } else {
  37. //调用登录接口
  38. wx.getUserInfo({
  39. withCredentials: false,
  40. success: function(res) {
  41. that.globalData.userInfo = res.userInfo
  42. typeof cb == "function" && cb(that.globalData.userInfo)
  43. }
  44. })
  45. }
  46. },
  47. globalData: {
  48. userInfo: null,
  49. ptStudent: null
  50. },
  51. login: function(info) {
  52. wx.login({
  53. success: res => {
  54. console.log(info, res)
  55. wx.request({
  56. url: api.loginUrl,
  57. method: 'POST',
  58. data: {
  59. code: res.code,
  60. iv: info.iv,
  61. encryptedData: info.encryptedData
  62. },
  63. success: info => {
  64. console.log(info);
  65. if (info.data.status == 'success') {
  66. wx.setStorageSync('pt_student', info.data.data);
  67. getApp().globalData.ptStudent = info.data.data;
  68. wx.switchTab({
  69. url: '/pages/index/index',
  70. })
  71. }
  72. }
  73. });
  74. }
  75. });
  76. }
  77. })