app.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. var openid;
  5. var _this=this;
  6. wx.getStorage({
  7. key: 'openid',
  8. success: function(res) {
  9. openid=res.data
  10. _this.globalData.openid = openid
  11. },
  12. fail:res=>{
  13. // 登录
  14. wx.login({
  15. success: res => {
  16. console.log(res)
  17. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  18. wx.request({
  19. url: 'https://www.scxjc.club/api/wx/openid',
  20. method: 'GET',
  21. data: {
  22. code: res.code
  23. },
  24. success: res => {
  25. if (res.data.data.openid){
  26. _this.globalData.openid = res.data.data.openid
  27. wx.setStorage({
  28. key: 'openid',
  29. data: res.data.data.openid,
  30. })
  31. }
  32. }
  33. })
  34. }
  35. })
  36. }
  37. })
  38. // 获取用户信息
  39. wx.getSetting({
  40. success: res => {
  41. if (res.authSetting['scope.userInfo']) {
  42. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  43. wx.getUserInfo({
  44. success: res => {
  45. // 可以将 res 发送给后台解码出 unionId
  46. this.globalData.userInfo = res.userInfo
  47. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  48. // 所以此处加入 callback 以防止这种情况
  49. if (this.userInfoReadyCallback) {
  50. this.userInfoReadyCallback(res)
  51. }
  52. }
  53. })
  54. }
  55. }
  56. })
  57. // wx.request({
  58. // url:'https://www.scxjc.club/api/wx/sujectitem',
  59. // success: res => {
  60. // this.globalData.categoryArray = res.data.data
  61. // }
  62. // })
  63. },
  64. globalData: {
  65. userInfo: null,
  66. host:'https://www.scxjc.club',
  67. categoryArray:[],
  68. openid:''
  69. }
  70. })