app.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const openIdUrl = require('./config').openIdUrl
  2. App({
  3. onLaunch: function () {
  4. console.log('App Launch')
  5. },
  6. onShow: function () {
  7. console.log('App Show')
  8. },
  9. onHide: function () {
  10. console.log('App Hide')
  11. },
  12. globalData: {
  13. hasLogin: false,
  14. openid: null
  15. },
  16. // lazy loading openid
  17. getUserOpenId: function(callback) {
  18. var self = this
  19. if (self.globalData.openid) {
  20. callback(null, self.globalData.openid)
  21. } else {
  22. wx.login({
  23. success: function(data) {
  24. wx.request({
  25. url: openIdUrl,
  26. data: {
  27. code: data.code
  28. },
  29. success: function(res) {
  30. console.log('拉取openid成功', res)
  31. self.globalData.openid = res.data.openid
  32. callback(null, self.globalData.openid)
  33. },
  34. fail: function(res) {
  35. console.log('拉取用户openid失败,将无法正常使用开放接口等服务', res)
  36. callback(res)
  37. }
  38. })
  39. },
  40. fail: function(err) {
  41. console.log('wx.login 接口调用失败,将无法正常使用开放接口等服务', err)
  42. callback(err)
  43. }
  44. })
  45. }
  46. }
  47. })