app.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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(res.code, info.iv, info.encryptedData)
  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. encryptedData: info.encryptedData
  42. },
  43. success: info => {
  44. console.log(info);
  45. if (info.data.status == 'success') {
  46. wx.setStorageSync('pt_student_id', info.data.id);
  47. }
  48. }
  49. });
  50. }
  51. });
  52. }
  53. })